
local string	= string
local table	= table
local pairs	= pairs
local math	= math
local SERVER	= SERVER
local print	= print
local PrintTable = PrintTable
local coroutine = coroutine

module("stringx")



/*---------------------------------------------------------
   Name: explode(seperator ,string)
   Desc: Takes a string and turns it into a table
   Usage: stringx.explode( " ", "Seperate this string")
---------------------------------------------------------*/
function Explode(seperator ,str)
	local tble={}
	ll=0
	while true do
		l=string.find(str,seperator,ll+1,true)
		if l~=nil then
			table.insert(tble, string.sub(str,ll,l-1)) 
			ll=l+1
		else
			table.insert(tble, string.sub(str,ll))
			break
		end
	end
	return tble
end

/*---------------------------------------------------------
   Name: implode(seperator ,Table)
   Desc: Takes a table and turns it into a string
   Usage: stringx.implode( " ", {"This", "Is", "A", "Table"})
---------------------------------------------------------*/
function Implode(seperator,Table) return table.concat(Table,seperator) end


/*---------------------------------------------------------
   Name: GetExtensionFromFilename(path)
   Desc: Returns extension from path
   Usage: stringx.GetExtensionFromFilename("garrysmod/lua/modules/string.lua")
---------------------------------------------------------*/
function GetExtensionFromFilename(path)
	local ExplTable = explode("" ,path)
	for i = table.getn(ExplTable), 1, -1 do
		if ExplTable[i] == "." then return string.sub(path, i+1)end
		if ExplTable[i] == "/" or ExplTable[i] == "\\" then return "" end
	end
	return ""
end
/*---------------------------------------------------------
   Name: GetPathFromFilename(path)
   Desc: Returns path from filepath
   Usage: stringx.GetPathFromFilename("garrysmod/lua/modules/string.lua")
---------------------------------------------------------*/
function GetPathFromFilename(path)
	local ExplTable = explode("" ,path)
	for i = table.getn(ExplTable), 1, -1 do
		if ExplTable[i] == "/" or ExplTable[i] == "\\" then return string.sub(path, 1, i) end
	end
	return ""
end
/*---------------------------------------------------------
   Name: GetFileFromFilename(path)
   Desc: Returns file with extension from path
   Usage: stringx.GetFileFromFilename("garrysmod/lua/modules/string.lua")
---------------------------------------------------------*/
function GetFileFromFilename(path)
	local ExplTable = explode("" ,path)
	for i = table.getn(ExplTable), 1, -1 do
		if ExplTable[i] == "/" or ExplTable[i] == "\\" then return string.sub(path, i) end
	end
	return ""
end

/*-----------------------------------------------------------------
   Name: FormattedTime( TimeInSeconds, Format )
   Desc: Given a time in seconds, returns formatted time
         If 'Format' is not specified the function returns a table 
         conatining values for hours, mins, secs, ms

   Examples: stringx.FormattedTime( 123.456, "%02i:%02i:%02i")  ==> "02:03:45"
             stringx.FormattedTime( 123.456, "%02i:%02i")       ==> "02:03"
             stringx.FormattedTime( 123.456, "%2i:%02i")        ==> " 2:03"
             stringx.FormattedTime( 123.456 )        		==> {h = 0, m = 2, s = 3, ms = 45}
-----------------------------------------------------------------*/

function FormattedTime( TimeInSeconds, Format )
	if not TimeInSeconds then TimeInSeconds = 0 end

	local i = math.floor( TimeInSeconds )
	local h,m,s,ms	=	( i/3600 ),
				( i/60 )-( math.floor( i/3600 )*3600 ),
				TimeInSeconds-( math.floor( i/60 )*60 ),
				( TimeInSeconds-i )*100

	if Format then
		return string.format( Format, m, s, ms )
	else
		return { h=h, m=m, s=s, ms=ms }
	end
end

/*---------------------------------------------------------
   Name: Old time functions
---------------------------------------------------------*/

function ToMinutesSecondsMilliseconds( TimeInSeconds )	return FormattedTime( TimeInSeconds, "%02i:%02i:%02i")	end
function ToMinutesSeconds( TimeInSeconds )		return FormattedTime( TimeInSeconds, "%02i:%02i")	end






/*---------------------------------------------------------
   Name: ???
---------------------------------------------------------*/

function Left(str, num)
	return string.sub(str, 1, num)
end

function Right(str, num)
	return string.sub(str, -num)
end



function Replace(str, tofind, toreplace)
	local start = 1
	while (true) do
		local pos = string.find(str, tofind, start, true)
	
		if (pos == nil) then
			break
		end
		
		local left = string.sub(str, 1, pos-1)
		local right = string.sub(str, pos + #tofind)
		
		str = left .. toreplace .. right
		start = pos + #toreplace
	end
	return str
end

/*---------------------------------------------------------
   Name: Trim(s)
   Desc: Removes leading and trailing spaces from a string.
---------------------------------------------------------*/
function Trim(s)
	return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end

