Table specific utilities are in the file Table Utilities.
Utilities | Table specific utilities are in the file Table Utilities. |
otlib | |
String Utilities | |
Explode | Split a string by a string. |
Trim | Trims leading and tailing whitespace from a string. |
LTrim | Exactly like Trim except it only trims the left side. |
RTrim | Exactly like Trim except it only trims the right side. |
Escape | Makes a string safe for pattern usage, like in string.gsub(). |
StripComments | Strips comments from a string. |
ParseArgs | This is similar to Explode with ( str, “%s+” ) except that it will not split up words within quotation marks. |
EditDistance | Finds the edit distance between two strings or tables. |
SplitCommentHeader | Splits a comment header in a string. |
Numeric Utilities | |
Round | Rounds a number to a given decimal place. |
Other Utilities | Things that don’t fit in any other category. |
ToBool | Converts a boolean, nil, string, or number to a boolean value. |
StoredExpression | Creates an object that will store an expression. |
DataEqualsAnyOf | Checks to see if an argument equals any of the other arguments passed in. |
String Utilities | |
Explode | Split a string by a string. |
Trim | Trims leading and tailing whitespace from a string. |
LTrim | Exactly like Trim except it only trims the left side. |
RTrim | Exactly like Trim except it only trims the right side. |
Escape | Makes a string safe for pattern usage, like in string.gsub(). |
StripComments | Strips comments from a string. |
ParseArgs | This is similar to Explode with ( str, “%s+” ) except that it will not split up words within quotation marks. |
EditDistance | Finds the edit distance between two strings or tables. |
SplitCommentHeader | Splits a comment header in a string. |
Numeric Utilities | |
Round | Rounds a number to a given decimal place. |
Other Utilities | Things that don’t fit in any other category. |
ToBool | Converts a boolean, nil, string, or number to a boolean value. |
StoredExpression | Creates an object that will store an expression. |
DataEqualsAnyOf | Checks to see if an argument equals any of the other arguments passed in. |
function Explode( str, separator, plain, limit )
Split a string by a string.
str | The input string to explode. |
separator | An optional string to specify what to split on. Defaults to %s+. |
plain | An optional boolean that turns off pattern matching facilities if true. This should make it faster and allows you to specify strings that would otherwise need to be escaped. Defaults to false. |
limit | An optional number that if set, the returned table will contain a maximum of limit elements with the last element containing the rest of str. Defaults to no limit. |
A table containing the exploded str.
Explode( "p1 p2 p3" )
returns...
{ "p1", "p2", "p3" }
v1.00 | Initial. |
function Trim( str )
Trims leading and tailing whitespace from a string.
str | The string to trim. |
The stripped string.
v1.00 | Initial. |
function LTrim( str )
Exactly like Trim except it only trims the left side. Taken from http://lua-users.org/wiki/CommonFunctions
v1.00 | Initial. |
function RTrim( str )
Exactly like Trim except it only trims the right side. Taken from http://lua-users.org/wiki/CommonFunctions
v1.00 | Initial. |
function StripComments( str, line_comment )
Strips comments from a string.
str | The input string to strip from. |
line_comment | The string of the comment to remove from str. Removes whenever it finds this text until the end of the line. |
A string of str with the comments removed.
StripComments( "Line 1 # My comment\n#Line with only a comment\nLine 2", "#" )
returns...
"Line 1 \n\nLine 2"
v1.00 | Initial. |
function ParseArgs( args )
This is similar to Explode with ( str, “%s+” ) except that it will not split up words within quotation marks.
args | The input string to split from. |
1 | A table containing the individual arguments. |
2 | A boolean stating whether or not mismatched quotes were found. |
ParseArgs( 'This is a "Cool sentence to" make "split up"' )
returns...
{ "This", "is", "a", "Cool sentence to", "make", "split up" }
v1.00 | Initial. |
function EditDistance( s, t, lim )
Finds the edit distance between two strings or tables. Edit distance is the minimum number of edits needed to transform one string or table into the other.
s | A string or table. |
t | Another string or table to compare against s. |
lim | An optional number to limit the function to a maximum edit distance. If specified and the function detects that the edit distance is going to be larger than limit, limit is returned immediately. |
A number specifying the minimum edits it takes to transform s into t or vice versa. Will not return a higher number than lim, if specified.
EditDistance( "Tuesday", "Teusday" ) -- One transposition. EditDistance( "kitten", "sitting" ) -- Two substitutions and a deletion.
returns...
1 3
v1.00 | Initial. |
function SplitCommentHeader( str, comment_prefix )
Splits a comment header in a string. A comment header is defined as a block in a string where every non-blank line starts with a certain prefix, until a non-blank line is reached that doesn’t start with the prefix.
str | The string to split the comment from. |
comment_prefix | The optional string comment prefix. Defaults to _”;”_. |
1 | The comment header. |
2 | Everything after the comment header. |
SplitCommentHeader( ";Comment 1\n;Comment 2\nData 1\nData 2" )
returns...
";Comment 1\n;Comment 2", "Data 1\nData2"
v1.00 | Initial. |
function Round( num, places )
Rounds a number to a given decimal place.
num | The number to round. |
places | The optional number of places to round to. 0 rounds to the nearest whole number, 1 rounds to the nearest tenth, 2 rounds to the nearest thousandth, etc. Negative numbers round into the non-fractional places; -1 rounds to the nearest tens, -2 rounds to the nearest hundreds, etc. Defaults to 0. |
The rounded number.
v1.00 | Initial. |
function ToBool( value )
Converts a boolean, nil, string, or number to a boolean value.
value | The boolean, nil, string, or number to convert. |
The converted boolean value.
v1.00 | Initial. |
function StoredExpression()
Creates an object that will store an expression.
A table that can be called and accepts any number of arguments, then stores and returns the arguments as given. You can access the arguments in the table by index, or retrieve the total number of aruments from field ‘n’.
A function ‘unpack’ is also defined for the returned table which returns each argument just like regular unpack does.
ex = StoredExpression() my_list = { "milk", "bread", "cookies", "eggs" } if ex( HasValue( my_list, "cookies" ) ) then print( "cookies are item #" .. ex[ 2 ] .. " on my list" ) end print( "there were " .. ex.n .. " variables passed back from HasValue" )
prints...
cookies are item #3 on my list there were 2 variables passed back from HasValue
v1.00 | Initial. |
function DataEqualsAnyOf( data, ... )
Checks to see if an argument equals any of the other arguments passed in.
data | The data to test equality against. |
... | All the other arguments, which get tested against data in successive order until a match is found or until we run out of arguments. |
A boolean stating whether or not any of the other arguments equaled the data argument.
This is a conveinence function so that instead of writing this...
if a == b or a == c or a == d then ... end
You can write this...
if DataEqualsAnyOf( a, b, c, d ) then ... end
v1.00 | Initial. |
Split a string by a string.
function Explode( str, separator, plain, limit )
Trims leading and tailing whitespace from a string.
function Trim( str )
Exactly like Trim except it only trims the left side.
function LTrim( str )
Exactly like Trim except it only trims the right side.
function RTrim( str )
Makes a string safe for pattern usage, like in string.gsub().
function Escape( str )
Strips comments from a string.
function StripComments( str, line_comment )
This is similar to Explode with ( str, “%s+” ) except that it will not split up words within quotation marks.
function ParseArgs( args )
Finds the edit distance between two strings or tables.
function EditDistance( s, t, lim )
Splits a comment header in a string.
function SplitCommentHeader( str, comment_prefix )
Rounds a number to a given decimal place.
function Round( num, places )
Converts a boolean, nil, string, or number to a boolean value.
function ToBool( value )
Creates an object that will store an expression.
function StoredExpression()
Checks to see if an argument equals any of the other arguments passed in.
function DataEqualsAnyOf( data, ... )