Libraries
String
This string library provides generic functions for string manipulation, such as, finding and extracting substrings, and pattern matching. The patterns are described in the subsequent sections.
When indexing a string, the first character is at position 1. indexes can be negative and are interpreted as indexing backwards, from the end of the string. Therefore, the last character is at position -1.
string.find(s, pattern [, init [, plain]])
This function looks for the first match of the pattern in the string s. If it finds a match, then find returns the indexes of s where this occurrence starts and ends. Otherwise, it returns nil.
A third, optional numerical argument init specifies where to start the search. Its default value is 1 and may be negative. A value of true as a fourth optional argument plain turns off the pattern matching facilities, so the function does a plain "find substring" operation, with no characters in pattern being considered "magic".
You must specify init if plain is specified. If the pattern has captures, then in a successful match the captured values are also returned, after the two indexes.
Example
local x, y = string.find('hello world', 'world')
local a, b, c, d = string.find('my street number is 54a', '(%d+)(%a+)')
string.match(s, pattern [, init])
Looks for the first match of pattern in the string s. If it finds one, then match returns the captures from the pattern. Otherwise it returns nil. If pattern specifies no captures, then the whole match is returned. A third optional numerical argument init specifies where to start the search. Its default value is 1 and may be negative.
Example
string.gmatch(s, pattern)
Returns an iterator function, in each call it returns the next captures from pattern over string s. If pattern specifies no captures, then the whole match is produced in each call.
The example collects all pairs key=value from the given string into a dictionary table.
local t = {}
local s = 'from=world, to=moon'
for k, v in string.gmatch(s, '(%w+)=(%w+)') do
t[k] = v
end
string.sub(s, i [, j])
Returns the substring of s that starts at i and continues until j. i and j may be negative. If j is absent, then it is assumed to be equal to -1 (which is the same as the string length). In particular, the call string.sub(s,1,j) returns a prefix of s with length j, and string.sub(s, -i) returns a suffix of s with length i.
Example
string.gsub(s, pattern, repl [, n])
Returns a copy of s in which all occurrences of the pattern have been replaced by a replacement string specified by repl. It also returns a second value containing the total number of substitutions made. The optional last parameter n limits the maximum number of substitutions to occur. For instance, when n is 1 only the first occurrence of pattern is replaced.
The string repl is used for replacement. The character % works as an escape character: Any sequence in repl of the form %n, with n between 1 and 9, stands for the value of the n-th captured substring (see the following example). The sequence %0 stands for the whole match. The sequence %% stands for a single %.
Example
local x = string.gsub('hello world', '(%w+)', '%1 %1')
local x = string.gsub('hello world', '%w+', '%0 %0', 1)
local x = string.gsub('hello world from Lua', '(%w+)%s*(%w+)', '%2 %1')
string.len(s)
Receives a string and returns its length. The empty string '' has length 0.
Example
string.rep(s, n)
Returns a string that is the concatenation of n copies of the string s.
Example
string.reverse(s)
Returns a string that is the string s reversed.
Example
string.lower(s)
Receives a string and returns a copy of this string with all uppercase letters changed to lowercase. All other characters are left unchanged.
Example
string.upper(s)
Receives a string and returns a copy of this string with all lowercase letters changed to uppercase. All other characters are left unchanged.
Example
string.format(formatstring, e1, e2, ...)
Returns a formatted version of all arguments following the formatstring. The format string follows the same rules as the printf() family of standard C functions and must be a string. The only differences are that the options/modifiers *, l, L, n, p, and h are not supported and there is an extra option q. The q option formats a string in a form suitable to be safely read back by the Lua interpreter. The options c, d, E, e, f, g, G, i, o, u, x, and X all expect a number as argument, whereas q and s expect a string.
Options:
- %d, %i: Signed integer
- %u: Unsigned integer
- %f, %g, %G: Floating point
- %e, %E: Scientific notation
- %o: Octal integer
- %x, %X: Hexadecimal integer
- %c: Character
- %s: String
- %q: Safe string
Example
Pattern
Patterns are similar to regular expressions. A character class is used to represent a set of characters.
The following combinations are allowed in describing a character class:
Pattern | Description |
---|---|
x |
Represents the character x itself (if x is not one of the magic characters ^$()%.[]*+-?) |
. |
(a dot) represents all characters |
%a |
Represents all letters |
%c |
Represents all control characters |
%d |
Represents all digits |
%l |
Represents all lowercase letters |
%p |
Represents all punctuation characters |
%s |
Represents all space characters |
%u |
Represents all uppercase letters |
%w |
Represents all alphanumeric characters |
%x |
Represents all hexadecimal digits |
%z |
Represents the character with representation 0 |
%x |
Represents the character x (where x is any non-alphanumeric character). This is the standard way to escape the magic characters. Any punctuation character (even the non magic) can be preceded by a `%´ when used to represent itself in a pattern. |
[set] |
Represents the class which is the union of all characters in set. A range of characters may be specified by separating the end characters of the range with a `-´. All classes %x described above may also be used as components in set. All other characters in set represent themselves. The interaction between ranges and classes is not defined. Therefore, patterns like [%a-z] or [a-%%] have no meaning. Example of character sets:
|
[^set] |
Represents the complement of set, where set is interpreted as above. |
For all classes represented by single letters (%a, %c, and so on), the corresponding uppercase letter represents the complement of the class. For instance, %S represents all non-space characters.
A pattern item may be:
- a single character class, which matches any single character in the class.
- a single character class followed by `*´, which matches 0 or more repetitions of characters in the class. These repetition items will always match the longest possible sequence.
- a single character class followed by `+´, which matches 1 or more repetitions of characters in the class. These repetition items will always match the longest possible sequence.
- a single character class followed by `-´, which also matches 0 or more repetitions of characters in the class. Unlike `*´, these repetition items will always match the shortest possible sequence.
- a single character class followed by `?´, which matches 0 or 1 occurrence of a character in the class.
- %n, for n between 1 and 9; such item matches a substring equal to the n-th captured string (see below).
- %bxy, where x and y are two distinct characters; such item matches strings that start with x, end with y, and where the x and y are balanced. This means that, if one reads the string from left to right, counting +1 for an x and -1 for a y, the ending y is the first y where the count reaches 0. For instance, the item %b() matches expressions with balanced parentheses.
A pattern is a sequence of pattern items. A `^´ at the beginning of a pattern anchors the match at the beginning of the subject string. A `$´ at the end of a pattern anchors the match at the end of the subject string. At other positions, `^´ and `$´ have no special meaning and represent themselves.
A pattern may contain sub-patterns enclosed in parentheses; they describe captures. When a match succeeds, the substrings of the subject string that match captures are stored (captured) for future use. Captures are numbered according to their left parentheses. For instance, in the pattern "(a*(.)%w(%s*))", the part of the string matching "a*(.)%w(%s*)" is stored as the first capture (and therefore has number 1); the character matching "." is captured with number 2, and the part matching "%s*" has number 3.
As a special case, the empty capture () captures the current string position (a number). For instance, if we apply the pattern "()aa()" on the string "flaaap", there will be two captures: 3 and 5.
Unicode
If you want to process unicode characters, you can use the library unicode.utf8 which contains the similar functions like the string library, but with Unicode support. There is another library called unicode.ascii that has the same functionality like the library string.
XML Parsing
The library lxp contains several features to process XML data. The official reference to the functions are available at LuaExpat.
Here are the supported methods:
Methods | Description |
---|---|
lxp.new(callbacks, [separator]) |
The parser is created by a call to the function lxp.new, which returns the created parser or raises a Lua error. It receives the callbacks table and optionally the parser separator character used in the namespace expanded element names. |
close() |
Closes the parser, freeing all memory used by it. A call to parser:close() without a previous call to parser:parse() could result in an error. |
getbase() |
Returns the base for resolving relative URIs. |
getcallbacks() |
Returns the callbacks table. |
parse(s) |
Parse some more of the document. The string s contains part (or perhaps all) of the document. When called without arguments the document is closed (but the parser still has to be closed). The function returns a non nil value when the parser has been successful, and when the parser finds an error it returns five results: nil, msg, line, col, and pos, which are the error message, the line number, column number and absolute position of the error in the XML document. |
pos() |
Returns three results: the current parsing line, column, and absolute position. |
setbase(base) |
Sets the base to be used for resolving relative URIs in system identifiers. |
setencoding(encoding) |
Set the encoding to be used by the parser. There are four built-in encodings, passed as strings: "US-ASCII", "UTF-8", "UTF-16", and "ISO-8859-1". |
stop() |
Abort the parser and prevent it from parsing any further through the data it was last passed. Use to halt parsing the document when an error is discovered inside a callback, for example. The parser object cannot accept more data after this call. |
SQL Parsing
The self developed library sqlparsing contains many functions to process SQL statements. For more information about the functions and their usage, see SQL Preprocessor.
Internet Access
Through the library socket, you can open HTTP, ftp, and smtp connections. More information is available at http://w3.impa.br/~diego/software/luasocket/.
Math Library
The math library is an interface to the standard C math library and provides the following functions and values:
Library | Description |
---|---|
math.abs(x) |
Absolute value of x |
math.acos(x) |
Arc cosine of x (in radians) |
math.asin(x) |
Arc sine of x (in radians) |
math.atan(x) |
Arc tangent of x (in radians) |
math.atan2(y,x) |
Arc tangent of y/x (in radians), but uses the signs of both operands to find the quadrant of the result |
math.ceil(x) |
Smallest integer larger than or equal to x |
math.cos(x) |
Cosine of x (assumed to be in radians) |
math.cosh(x) |
Hyperbolic cosine of x |
math.deg(x) |
Angle x (given in radians) in degrees |
math.exp(x) |
Return natural exponential function of x |
math.floor(x) |
Largest integer smaller than or equal to x |
math.fmod(x,y) |
Modulo |
math.frexp(x) |
Returns m and n so that x=m2^n, n is an integer and the absolute value of m is in the range [0.5;1) (or zero if x is zero) |
math.huge |
Value HUGE_VAL which is larger than any other numerical value |
math.ldexp(m,n) |
Returns m2^n (n should be an integer) |
math.log(x) |
Natural logarithm of x |
math.log10(x) |
Base-10 logarithm of x |
math.max(x, ...) |
Maximum of values |
math.min(x, ...) |
Minimum of values |
math.modf(x) |
Returns two numbers, the integral part of x and the fractional part of x |
math.pi |
Value of π |
math.pow(x,y) |
Returns value x^y |
math.rad(x) |
Angle x (given in degrees) in radians |
math.random([m,[n]]) |
Interface to random generator function rand from ANSI C (no guarantees can be given for statistical properties). When called without arguments, returns a pseudorandom real number in the range [0;1). If integer m is specified, then the range is [1;m]. If called with m and n, then the range is [m;n]. |
math.randomseed(x) |
Sets x as seed for random generator |
math.sin(x) |
Sine of x (assumed to be in radians) |
math.sinh(x) |
Hyperbolic sine of x |
math.sqrt(x) |
Square root of x |
math.tan(x) |
Tangent of x |
math.tanh(x) |
Hyperbolic tangent of x |
Table Library
This library provides generic functions for table manipulation. Most functions in the table library assume that the table represents an array or a list. For these functions, the length of a table means the result of the length operator (#table). The functions and their descriptions are as follows:
- table.insert(table, [pos,] value): Inserts element value at position pos in table, shifting up other elements if necessary. The default value for pos is n+1, where n is the length of the table, so that a call table.insert(t,x) inserts x at the end of table t.
- table.remove(table [, pos]): Removes from table the element at position pos, shifting down other elements if necessary. Returns the value of the removed element. The default value for pos is n, where n is the length of the table, so that a call table.remove(t) removes the last element of table t.
- table.concat(table [, sep [, i [, j]]]): Returns table[i]..sep..table[i+1] ... sep..table[j]. The default value for sep is the empty string, the default for i is 1, and the default for j is the length of the table. If i is greater than j, returns the empty string.
- table.sort(table [, comp]): Sorts table elements in a given order, in-place, from table[1] to table[n], where n is the length of the table. If comp is given, then it must be a function that receives two table elements, and returns true when the first is less than the second (so that not comp(a[i+1],a[i]) will be true after the sort). If comp is not given, then the standard Lua operator < is used instead. The sort algorithm is not stable; that is, elements considered equal by the given order may have their relative positions changed by the sort.
System Tables
The following system tables show the existing database scripts:
- EXA_USER_SCRIPTS
- EXA_ALL_SCRIPTS
- EXA_DBA_SCRIPTS
For more information, see Metadata System Tables.