Module corky.utils
Miscellaneous utility functions.
In addition to the functions listed below this module will set up table.unpack() if it does not exist, and table.pack() if it does not exist.
The string functions below will be added to the global string table.
Output buffering will be disabled for STDERR
.
Info:
Functions
string.trim (str) | Remove all spaces at the beginning and at the end of a string. |
string.split_csv (str) | Split a string into comma-separated values (CSV). |
string.startswith (str, start) | Check if a string A starts with a string B. |
warn (format, ...) | Print a warning message to STDERR. |
percent (value, min, max) | Get a percentage value relative to an interval. |
Functions
- string.trim (str) line 77
-
Remove all spaces at the beginning and at the end of a string.
Characters removed are
[ \t\n\v\f\r]
. The returned string will be empty if the original does not contain any other characters.See http://lua-users.org/wiki/StringTrim.
Parameters:
- str string The string to trim.
Returns:
- string The trimmed string.
- string.split_csv (str) line 111
-
Split a string into comma-separated values (CSV).
Quotes will be handled correctly. To include a quote inside a quoted field escape it with another quote (i.e.
"
becomes""
inside a quoted field). Quotes are only needed if a field contains one or more commas. Spaces around commas are allowed and will be stripped, all spaces inside quotes will be preserved. Empty fields are allowed.This function uses the LPeg module and is based on the CSV example found at http://www.inf.puc-rio.br/~roberto/lpeg/.
Parameters:
- str string The string to split.
Returns:
-
table
Array containing the individual fields (as strings). Fields may be empty. If there is any error parsing
the string,
nil
will be returned.
- string.startswith (str, start) line 128
-
Check if a string A starts with a string B.
See http://lua-users.org/wiki/StringRecipes.
Parameters:
Returns:
-
bool
Returns
true
if the string specified by the first parameter starts with the string specified as the second parameter, elsefalse
.
-
bool
Returns
- warn (format, ...) line 144
-
Print a warning message to STDERR.
The warning message printed is a formatted version of the function's variable number of arguments following the description given in its first argument (which must be a string), like string.format(). The prefix
"Corky: "
will be added to the message, as will a newline character at the end.Parameters:
- format string The format to use.
- ... Substitutions for the format string.
See also:
- percent (value, min, max) line 161
-
Get a percentage value relative to an interval.
If the value is less than or equal to the minimum value the return value will be
0
. If the value is greater than or equal to the maximum value the return value will be100
. In every other case the return value will be the percentage value in the interval.Parameters:
- value number The original value.
- min number The minimum value of the interval.
- max number The maximum value of the interval.
Returns:
-
number
The percentage value relative to the interval, a float between
0
and100
(inclusive).