Permalink
Browse files

Fixed string.ToColor with invalid string input

  • Loading branch information...
1 parent 3fac4ef commit 6f4cd72522fb3c2d6f5e6dada19c6e0afafb50c9 @robotboy655 robotboy655 committed Aug 23, 2016
Showing with 92 additions and 104 deletions.
  1. +92 −104 garrysmod/lua/includes/extensions/string.lua
@@ -1,16 +1,17 @@
+
local string = string
local math = math
--[[---------------------------------------------------------
- Name: string.ToTable( string )
+ Name: string.ToTable( string )
-----------------------------------------------------------]]
function string.ToTable ( str )
local tbl = {}
-
+
for i = 1, string.len( str ) do
tbl[i] = string.sub( str, i, i )
end
-
+
return tbl
end
@@ -79,23 +80,20 @@ local string_sub = string.sub
local string_find = string.find
local string_len = string.len
function string.Explode(separator, str, withpattern)
- if (separator == "") then return totable( str ) end
-
- if withpattern == nil then
- withpattern = false
- end
+ if ( separator == "" ) then return totable( str ) end
+ if ( withpattern == nil ) then withpattern = false end
local ret = {}
local current_pos = 1
- for i = 1, string_len(str) do
- local start_pos, end_pos = string_find(str, separator, current_pos, not withpattern)
- if not start_pos then break end
- ret[i] = string_sub(str, current_pos, start_pos - 1)
+ for i = 1, string_len( str ) do
+ local start_pos, end_pos = string_find( str, separator, current_pos, !withpattern )
+ if ( !start_pos ) then break end
+ ret[ i ] = string_sub( str, current_pos, start_pos - 1 )
current_pos = end_pos + 1
end
- ret[#ret + 1] = string_sub(str, current_pos)
+ ret[ #ret + 1 ] = string_sub( str, current_pos )
return ret
end
@@ -105,86 +103,86 @@ function string.Split( str, delimiter )
end
--[[---------------------------------------------------------
- Name: Implode(seperator ,Table)
- Desc: Takes a table and turns it into a string
- Usage: string.Implode( " ", {"This", "Is", "A", "Table"})
+ Name: Implode( seperator, Table)
+ Desc: Takes a table and turns it into a string
+ Usage: string.Implode( " ", { "This", "Is", "A", "Table" } )
-----------------------------------------------------------]]
-function string.Implode(seperator,Table) return
- table.concat(Table,seperator)
+function string.Implode( seperator, Table ) return
+ table.concat( Table, seperator )
end
--[[---------------------------------------------------------
- Name: GetExtensionFromFilename(path)
- Desc: Returns extension from path
- Usage: string.GetExtensionFromFilename("garrysmod/lua/modules/string.lua")
+ Name: GetExtensionFromFilename( path )
+ Desc: Returns extension from path
+ Usage: string.GetExtensionFromFilename("garrysmod/lua/modules/string.lua")
-----------------------------------------------------------]]
function string.GetExtensionFromFilename( path )
return path:match( "%.([^%.]+)$" )
end
--[[---------------------------------------------------------
- Name: StripExtension( path )
+ Name: StripExtension( path )
-----------------------------------------------------------]]
function string.StripExtension( path )
local i = path:match( ".+()%.%w+$" )
- if ( i ) then return path:sub(1, i-1) end
+ if ( i ) then return path:sub( 1, i - 1 ) end
return path
end
--[[---------------------------------------------------------
- Name: GetPathFromFilename(path)
- Desc: Returns path from filepath
- Usage: string.GetPathFromFilename("garrysmod/lua/modules/string.lua")
+ Name: GetPathFromFilename( path )
+ Desc: Returns path from filepath
+ Usage: string.GetPathFromFilename("garrysmod/lua/modules/string.lua")
-----------------------------------------------------------]]
-function string.GetPathFromFilename(path)
+function string.GetPathFromFilename( path )
return path:match( "^(.*[/\\])[^/\\]-$" ) or ""
end
+
--[[---------------------------------------------------------
- Name: GetFileFromFilename(path)
- Desc: Returns file with extension from path
- Usage: string.GetFileFromFilename("garrysmod/lua/modules/string.lua")
+ Name: GetFileFromFilename( path )
+ Desc: Returns file with extension from path
+ Usage: string.GetFileFromFilename("garrysmod/lua/modules/string.lua")
-----------------------------------------------------------]]
-function string.GetFileFromFilename(path)
+function string.GetFileFromFilename( path )
return path:match( "[\\/]([^/\\]+)$" ) or ""
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: string.FormattedTime( 123.456, "%02i:%02i:%02i") ==> "02:03:45"
- string.FormattedTime( 123.456, "%02i:%02i") ==> "02:03"
- string.FormattedTime( 123.456, "%2i:%02i") ==> " 2:03"
- string.FormattedTime( 123.456 ) ==> {h = 0, m = 2, s = 3, ms = 45}
+ 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: string.FormattedTime( 123.456, "%02i:%02i:%02i") ==> "02:03:45"
+ string.FormattedTime( 123.456, "%02i:%02i") ==> "02:03"
+ string.FormattedTime( 123.456, "%2i:%02i") ==> " 2:03"
+ string.FormattedTime( 123.456 ) ==> { h = 0, m = 2, s = 3, ms = 45 }
-------------------------------------------------------------------]]
-function string.FormattedTime( seconds, Format )
- if not seconds then seconds = 0 end
- local hours = math.floor(seconds / 3600)
- local minutes = math.floor((seconds / 60) % 60)
+function string.FormattedTime( seconds, format )
+ if ( not seconds ) then seconds = 0 end
+ local hours = math.floor( seconds / 3600 )
+ local minutes = math.floor( ( seconds / 60 ) % 60 )
local millisecs = ( seconds - math.floor( seconds ) ) * 100
- seconds = math.floor(seconds % 60)
-
- if Format then
- return string.format( Format, minutes, seconds, millisecs )
+ seconds = math.floor( seconds % 60 )
+
+ if ( format ) then
+ return string.format( format, minutes, seconds, millisecs )
else
- return { h=hours, m=minutes, s=seconds, ms=millisecs }
+ return { h = hours, m = minutes, s = seconds, ms = millisecs }
end
end
--[[---------------------------------------------------------
- Name: Old time functions
+ Name: Old time functions
-----------------------------------------------------------]]
-
-function string.ToMinutesSecondsMilliseconds( TimeInSeconds ) return string.FormattedTime( TimeInSeconds, "%02i:%02i:%02i") end
-function string.ToMinutesSeconds( TimeInSeconds ) return string.FormattedTime( TimeInSeconds, "%02i:%02i") end
+function string.ToMinutesSecondsMilliseconds( TimeInSeconds ) return string.FormattedTime( TimeInSeconds, "%02i:%02i:%02i" ) end
+function string.ToMinutesSeconds( TimeInSeconds ) return string.FormattedTime( TimeInSeconds, "%02i:%02i" ) end
local function pluralizeString(str, quantity)
- return str .. ((quantity ~= 1) and "s" or "")
+ return str .. ( ( quantity ~= 1 ) and "s" or "" )
end
function string.NiceTime( seconds )
@@ -193,93 +191,83 @@ function string.NiceTime( seconds )
if ( seconds < 60 ) then
local t = math.floor( seconds )
- return t .. pluralizeString(" second", t);
+ return t .. pluralizeString( " second", t )
end
if ( seconds < 60 * 60 ) then
local t = math.floor( seconds / 60 )
- return t .. pluralizeString(" minute", t);
+ return t .. pluralizeString( " minute", t )
end
if ( seconds < 60 * 60 * 24 ) then
local t = math.floor( seconds / (60 * 60) )
- return t .. pluralizeString(" hour", t);
+ return t .. pluralizeString( " hour", t )
end
if ( seconds < 60 * 60 * 24 * 7 ) then
- local t = math.floor( seconds / (60 * 60 * 24) )
- return t .. pluralizeString(" day", t);
+ local t = math.floor( seconds / ( 60 * 60 * 24 ) )
+ return t .. pluralizeString( " day", t )
end
-
+
if ( seconds < 60 * 60 * 24 * 7 * 52 ) then
- local t = math.floor( seconds / (60 * 60 * 24 * 7) )
- return t .. pluralizeString(" week", t);
+ local t = math.floor( seconds / ( 60 * 60 * 24 * 7 ) )
+ return t .. pluralizeString( " week", t )
end
- local t = math.floor( seconds / (60 * 60 * 24 * 7 * 52) )
- return t .. pluralizeString(" year", t);
+ local t = math.floor( seconds / ( 60 * 60 * 24 * 7 * 52 ) )
+ return t .. pluralizeString( " year", t )
end
-function string.Left(str, num)
- return string.sub(str, 1, num)
-end
-
-function string.Right(str, num)
- return string.sub(str, -num)
-end
-
+function string.Left( str, num ) return string.sub( str, 1, num ) end
+function string.Right( str, num ) return string.sub( str, -num ) end
function string.Replace( str, tofind, toreplace )
- local tbl = string.Explode(tofind, str)
-
- if tbl[1] then
- return table.concat(tbl, toreplace)
- end
-
+ local tbl = string.Explode( tofind, str )
+ if ( tbl[ 1 ] ) then return table.concat( tbl, toreplace ) end
return str
end
--[[---------------------------------------------------------
- Name: Trim(s)
- Desc: Removes leading and trailing spaces from a string.
- Optionally pass char to trim that character from the ends instead of space
+ Name: Trim( s )
+ Desc: Removes leading and trailing spaces from a string.
+ Optionally pass char to trim that character from the ends instead of space
-----------------------------------------------------------]]
function string.Trim( s, char )
- if char then char = char:PatternSafe() else char = "%s" end
+ if ( char ) then char = char:PatternSafe() else char = "%s" end
return string.match( s, "^" .. char .. "*(.-)" .. char .. "*$" ) or s
end
--[[---------------------------------------------------------
- Name: TrimRight(s)
- Desc: Removes trailing spaces from a string.
- Optionally pass char to trim that character from the ends instead of space
+ Name: TrimRight( s )
+ Desc: Removes trailing spaces from a string.
+ Optionally pass char to trim that character from the ends instead of space
-----------------------------------------------------------]]
function string.TrimRight( s, char )
- if char then char = char:PatternSafe() else char = "%s" end
+ if ( char ) then char = char:PatternSafe() else char = "%s" end
return string.match( s, "^(.-)" .. char .. "*$" ) or s
end
--[[---------------------------------------------------------
- Name: TrimLeft(s)
- Desc: Removes leading spaces from a string.
- Optionally pass char to trim that character from the ends instead of space
+ Name: TrimLeft( s )
+ Desc: Removes leading spaces from a string.
+ Optionally pass char to trim that character from the ends instead of space
-----------------------------------------------------------]]
function string.TrimLeft( s, char )
- if char then char = char:PatternSafe() else char = "%s" end
+ if ( char ) then char = char:PatternSafe() else char = "%s" end
return string.match( s, "^" .. char .. "*(.+)$" ) or s
end
function string.NiceSize( size )
-
+
size = tonumber( size )
if ( size <= 0 ) then return "0" end
if ( size < 1024 ) then return size .. " Bytes" end
if ( size < 1024 * 1024 ) then return math.Round( size / 1024, 2 ) .. " KB" end
- if ( size < 1024 * 1024 * 1024 ) then return math.Round( size / (1024*1024), 2 ) .. " MB" end
-
- return math.Round( size / (1024*1024*1024), 2 ) .. " GB"
+ if ( size < 1024 * 1024 * 1024 ) then return math.Round( size / ( 1024 * 1024 ), 2 ) .. " MB" end
+
+ return math.Round( size / ( 1024 * 1024 * 1024 ), 2 ) .. " GB"
end
@@ -290,7 +278,7 @@ function string.SetChar( s, k, v )
local start = s:sub( 0, k-1 )
local send = s:sub( k+1 )
-
+
return start .. v .. send
end
@@ -322,26 +310,26 @@ end
function string.EndsWith( String, End )
- return End == '' or string.sub( String, -string.len( End ) ) == End
+ return End == "" or string.sub( String, -string.len( End ) ) == End
end
function string.FromColor( color )
- return Format( "%i %i %i %i", color.r, color.g, color.b, color.a );
+ return Format( "%i %i %i %i", color.r, color.g, color.b, color.a )
end
function string.ToColor( str )
local col = Color( 255, 255, 255, 255 )
- col.r, col.g, col.b, col.a = str:match("(%d+) (%d+) (%d+) (%d+)")
+ local r, g, b, a = str:match( "(%d+) (%d+) (%d+) (%d+)" )
- col.r = tonumber( col.r )
- col.g = tonumber( col.g )
- col.b = tonumber( col.b )
- col.a = tonumber( col.a )
+ col.r = tonumber( r ) or 255
+ col.g = tonumber( g ) or 255
+ col.b = tonumber( b ) or 255
+ col.a = tonumber( a ) or 255
return col
@@ -351,8 +339,8 @@ function string.Comma( number )
local number, k = tostring( number ), nil
- while true do
- number, k = string.gsub( number, "^(-?%d+)(%d%d%d)", '%1,%2')
+ while true do
+ number, k = string.gsub( number, "^(-?%d+)(%d%d%d)", "%1,%2" )
if ( k == 0 ) then break end
end

0 comments on commit 6f4cd72

Please sign in to comment.