Some utility functions. Unlike the functions in util.lua, this file only holds non-HL2 specific functions.
Miscellaneous | Some utility functions. |
Functions | |
explode | Split a string by a separator. |
stripComments | Strips comments from a string |
makePatternSafe | Makes a string safe for pattern usage, like in string.gsub(). |
stripQuotes | Trims leading and tailing quotes from a string |
unescapeBackslash | Converts ‘\\’ to ‘\’ |
splitPort | |
splitArgs | This is similar to string.Explode( “ “, str ) except that it will also obey quotation marks. |
parseKeyValues | Parses a keyvalue formatted string into a table. |
makeKeyValues | Makes a key values string from a table. |
toBool | Converts a bool, nil, string, or number to a bool |
findVar | Given a string, find a var starting from the global namespace. |
setVar | Given a string, find and set a var starting from the global namespace. |
throwBadArg | Throws an error similar to the lua “bad argument #x to <fn_name> (<type> expected, got <type>). |
checkArg | Checks to see if an arg matches what is expected, if not, calls throwBadArg(). |
isValidSteamID | Checks to see if a given string is a valid steamid. |
isValidIP | Checks to see if a given string is a valid IPv4 address. |
removeCommentHeader | Removes a comment header. |
stringTimeToMinutes | Converts a string containing time information to minutes. |
secondsToStringTime | Converts a number of seconds to a string describing the time span. |
Inheritance | |
Functions | |
inheritsFrom | Creates a psudeo-inheritance for lua. |
Tables | |
root_class | This is a local table that holds our functions that we want all classes to have. |
Functions | |
root_class: | This is a utility function used by the metatable __call to resolve Class( ... |
root_class: | This is used to create new ‘class instances’. |
function ULib.explode( separator, str, limit )
Split a string by a separator.
separator | The separator string. |
str | A string. |
limit | (Optional) Max number of elements in the table |
A table of str split by separator, nil and error message otherwise.
v2.10 | Initial (dragged over from a GM9 archive though) |
function ULib.stripComments( str, comment, blockcommentbeg, blockcommentend )
Strips comments from a string
str | The string to stip comments from |
comment | The comment string. If it’s found, whatever comes after it on that line is ignored. ( IE: “//” ) |
blockcommentbeg | (Optional) The block comment begin string. ( IE: “/<star>” ) |
blockcommentend | (Optional, must be specified if above parameter is) The block comment end string. ( IE: “<star>/” ) |
The string with the comments stripped, nil and error otherwise.
v2.02 | Fixed block comments in more complicated files. |
function ULib.splitArgs( args, start_token, end_token )
This is similar to string.Explode( “ “, str ) except that it will also obey quotation marks.
args | The string to split from |
start_token | The string character to start a string with. |
end_token | The string character to end a string with. |
A table containing the individual arguments and a boolean stating whether or not mismatched quotes were found.
ULib.splitArgs( "This is a \"Cool sentence to\" make \"split up\"" )
returns...
{ "This", "is", "a", "Cool sentence to", "make", "split up" }
v2.10 | Can now handle tabs and trims strings before returning. |
v2.30 | Rewrite. Can now properly handle escaped quotes. New param, ignore_mismatch. |
v2.40 | Rewrite. Much more stable and predictable now. Removed ignore_mismatch param. As far as I can tell, it now matches the source engine’s split arg behavior exactly. Also accepts tokens to consider a string. |
function ULib.parseKeyValues( str, convert )
Parses a keyvalue formatted string into a table.
str | The string to parse. |
convert | (Optional, defaults to false) Setting this to true will convert garry’s keyvalues to a better form. This has two effects. First, it will remove the “Out”{} wrapper. Second, it will convert any keys that equate to a number to a number. |
The table, nil and error otherwise. If you find you’re missing information from the table, the file format might be incorrect.
test { "howdy" "bbq" foo { "bar" "false" } }
v2.10 | Initial (but tastefully stolen from a GM9 version) |
v2.30 | Rewrite. Much more robust and properly unescapes backslashes now. |
v2.40 | Properly handles escaped quotes now. |
function ULib.makeKeyValues( t, tab, completed )
Makes a key values string from a table.
t | The table to make the keyvalues from. This can only contain tables, numbers, and strings. |
tab | Only for internal use, this helps make inline tables look better. |
completed | A list of table values that have already been parsed, this is only for internal use to make sure we don’t hit an infinite loop. |
The string, nil and error otherwise.
If you use numbers as keys in the table, just the values will be used.
{ test = { howdy = "bbq", foo = { bar = "false" } } }
test { "howdy" "bbq" foo { "bar" "false" } }
v2.10 | Initial (but tastefully stolen from a GM9 version) |
v2.40 | Increased performance for insanely high table counts. |
function ULib.findVar( varLocation, rootTable )
Given a string, find a var starting from the global namespace. This will correctly parse tables. IE, “ULib.serialize”.
varLocation | The string location of the variable you wish to find or set (E.G., “ULib.myTable.MyVariable”). |
rootTable | The optional root table to search. Defaults to _G, the global environment. |
Two values as follows...
Status | A boolean indicating whether or not it was found. |
Value | The value of the variable. |
v2.40 | Removed dependency on gmod functions. |
v2.60 | Now returns two values to indicate success and value. Added second parameter for root table and added better handling for nil values. |
function ULib.setVar( varLocation, varValue, rootTable )
Given a string, find and set a var starting from the global namespace. This will correctly parse tables. IE, “ULib.serialize”.
varLocation | The string location of the variable you wish to find or set (E.G., “ULib.myTable.MyVariable”). |
varValue | The value to set it to. |
rootTable | The optional root table to search. Defaults to _G, the global environment. |
Two values as follows...
Status | A boolean indicating whether or not it was found and set. |
Value | The PREVIOUS value of the variable. |
v2.60 | Initial. |
function ULib.throwBadArg( argnum, fnName, expected, data, throwLevel )
Throws an error similar to the lua “bad argument #x to <fn_name> (<type> expected, got <type>).
argnum | (Optional) The argument number that was bad. |
fnName | (Optional) The name of the function being called. |
expected | (Optional) The string of the type you expected. |
data | (Optional) The actual data you got. |
throwLevel | (Optional, defaults to 3) How many levels up to throw the error. |
Never returns, throws an error
v2.40 | Initial. |
function ULib.checkArg( argnum, fnName, expected, data, throwLevel )
Checks to see if an arg matches what is expected, if not, calls throwBadArg().
argnum | (Optional) The argument number you’re. |
fnName | (Optional) The name of the function being called. |
expected | The string of the type you expect or a table of types you expect. |
data | The actual data you got. |
throwLevel | (Optional, defaults to 4) How many levels up to throw the error. |
Never returns if the data is bad, throws an error. Otherwise returns nil.
v2.40 | Initial. |
function ULib.stringTimeToMinutes( str )
Converts a string containing time information to minutes.
str | The time string. Defaults to minutes, “h” is for hours, “d” is for days, “w” is for weeks. |
The number of minutes represented by the string or nil if it’s unable to parse the string.
v2.41 | Initial |
v2.43 | Added year parameter |
v2.60 | Renamed function from “stringTimeToSeconds” to “stringTimeToMinutes”, because I am dumb |
function ULib.secondsToStringTime( secs )
Converts a number of seconds to a string describing the time span. Note that it rounds up to the minute level (ten seconds will be one minute).
secs | The number of seconds. |
A string representing the length of the span.
v2.60 | Initial |
Functions | |
inheritsFrom | Creates a psudeo-inheritance for lua. |
Tables | |
root_class | This is a local table that holds our functions that we want all classes to have. |
Functions | |
root_class: | This is a utility function used by the metatable __call to resolve Class( ... |
root_class: | This is used to create new ‘class instances’. |
function inheritsFrom( base_class )
Creates a psudeo-inheritance for lua. It will search for variables that do not exist in derived ‘classes’ in the parent ‘classes’, among other things explained below.
baseClass | The class to derive from. This value must either be nil or a class created using inheritsFrom(). |
The table of the derived class.
v2.40 | Initial. |
b = inherits_from( nil ) function b:instantiate( ... ) print( "base", unpack( arg ) ) end d = inherits_from( b ) function d:instantiate( ... ) print( "derived", unpack( arg ) ) end b1 = b( "should be base" ) d1 = d( "should be derived" ) print( "d1 is d?", d1:isa( d ), "is b?", d1:isa( b ) ) print( "b1 is d?", b1:isa( d ), "is b?", b1:isa( b ) )
base should be base derived should be derived d1 is d? true is b? true b1 is d? false is b? true
Split a string by a separator.
function ULib.explode( separator, str, limit )
Strips comments from a string
function ULib.stripComments( str, comment, blockcommentbeg, blockcommentend )
Makes a string safe for pattern usage, like in string.gsub().
function ULib.makePatternSafe( str )
Trims leading and tailing quotes from a string
function ULib.stripQuotes( s )
Converts ‘\\’ to ‘\’
function ULib.unescapeBackslash( s )
function ULib.splitPort( ipAndPort )
This is similar to string.Explode( “ “, str ) except that it will also obey quotation marks.
function ULib.splitArgs( args, start_token, end_token )
Parses a keyvalue formatted string into a table.
function ULib.parseKeyValues( str, convert )
Makes a key values string from a table.
function ULib.makeKeyValues( t, tab, completed )
Converts a bool, nil, string, or number to a bool
function ULib.toBool( x )
Given a string, find a var starting from the global namespace.
function ULib.findVar( varLocation, rootTable )
Given a string, find and set a var starting from the global namespace.
function ULib.setVar( varLocation, varValue, rootTable )
Throws an error similar to the lua “bad argument #x to fn_name (type expected, got type).
function ULib.throwBadArg( argnum, fnName, expected, data, throwLevel )
Checks to see if an arg matches what is expected, if not, calls throwBadArg().
function ULib.checkArg( argnum, fnName, expected, data, throwLevel )
Checks to see if a given string is a valid steamid.
function ULib.isValidSteamID( steamid )
Checks to see if a given string is a valid IPv4 address.
function ULib.isValidIP( ip )
Removes a comment header.
function ULib.removeCommentHeader( data, comment_char )
Converts a string containing time information to minutes.
function ULib.stringTimeToMinutes( str )
Converts a number of seconds to a string describing the time span.
function ULib.secondsToStringTime( secs )
Creates a psudeo-inheritance for lua.
function inheritsFrom( base_class )
This is used to create new ‘class instances’.
function root_class:create( ... )