Lua5.3 manual standard library Supplements

To recap cloud wind Gangster Lua 5.3 Reference Manual in the standard library content

6.0 standalone Lua

arg

lua will all command line parameters passed into a global table in arg. Name of the script in the local index 0, the first argument after the script name is followed at index 1, and so on. On the negative index parameter in any previous script name (ie the name of the interpreter and the options).

6.1 basis functions

next(table,[,index])

In particular, you can use next (t) to determine whether a table is empty.
In order traversal of the index is not defined, even if the index number is true. (If you want to traverse the table in numerical order, in digital form can be used for.)

select(index,...)

If the index is a number, then return to the parameters of the portion after the index th; negative number would index from back to front (-1 means the last parameter). Otherwise, index must be the string "#", then select returns the number of parameters.

6.3 module

pack.config

A description of some string configuration information to compile the package manager to prepare. This string consists of a series of lines:

The first line is divided directory strings. For Windows default '' for other systems it is '/'.
The second line is a path delimiter. The default value is ';'.
The third line is the template for marking the replacement string point. The default is '?'.
The fourth row is the string to be replaced in the execution of the program into the Windows directory path. The default is '!'.
The fifth row is a sign, all text after the marker will be ignored when building luaopen_ function name. The default is '-'.

使用:在不同系统上返回目录分割符`string.sub(pack.config,1,1)`

6.4 String Processing

string.find (s, pattern [, init [, plain]])

Plain optional parameter is true, the closed pattern matching mechanism. At this function does only direct action "find substring", and there is no pattern characters are treated as magic characters. Note that if given the plain, it is necessary to write init.

If the capture mode is defined, several values ​​will be captured returned after two indexes.

string.gsub (s, pattern, repl [, n])

If repl is a string, this string as a replacement. % Is an escape character: all forms of% by repl string d represents the d-th substrings captured, d may be 1-9. 0% indicates the whole matched strings. It represents a single string %%%.

If repl is tables, each time the match will be the first with a catch as the key to check this table.

If repl is a function, at the time of occurrence of each match will call this function. All captured substring turn as an argument.

string.len (s)

Receiving a string and returns its length. Empty string "" is at 0. Embedded zero is also included in the statistics, the length of "a \ 000bc \ 000" is 5

string.rep (s, n [, sep])

S n Returns string string sep as the delimiter string together. The default is an empty string sep (i.e. no delimiter). If n is not a positive number returns an empty string.

6.4.1 Match mode

Lua matching pattern directly by conventional string described. It is used for pattern matching functions string.find, string.gmatch, string.gsub, string.match. This section expresses the syntax and meaning of these strings (that is, what it can to match).

Character classes:

Character class is used to represent a set of characters. The following compositions can be used in character class:

  • x: (.? where x can not be a magic character ^ $ ()% [] * + - one in) represents the character x itself.
  • : (A point) can represent any character.
  • % A: represents any letters. (Alphabet)
  • % C: represents any control characters. (Control character)
  • % D: indicates any number. (Decimal)
  • % G: denotes any printable character except whitespace.
  • % L: represents all lowercase letters. (Lowercase)
  • % P: represents all punctuation. (Punctuation)
  • % S: represents all whitespace characters. (Space)
  • % U: represents all uppercase letters. (Uppercase)
  • % W: represents all letters and numbers.
  • % X: represents all hexadecimal notation.
  • % Z: represents the null character ( '\ 000')
  • % X: (x here is any non-alphabetic or numeric characters) represents the character x. This is the standard method for magic characters escaped. All non-alphabetic or numeric characters (including all punctuation, characters also include non-magic) can use a pre '%' in the pattern string represented itself.
  • [Set]: represents the union of all characters in this set. It may be '-' is connected, at both ends of the character written in ascending order to represent a wide range of characters. % X form mentioned above may be used in a set element represented therein. Other characters appear in the set represents themselves. For example, [% w_] (or [_% w]) represents all alphanumeric underlined), [0-7] represents the octal numbers, [0-7% l% -] represents the octal number plus lowercase letters and '-' character.

    CROSS range behavior classes and undefined. Thus, like [% az] or [a - %%] Such string pattern does not make sense.

  • Category (% a,% c, etc.) of all individual letters, uppercase letters if which are represented by the corresponding complement. For example,% S represents all non-space characters.

    How to define the letters, spaces, or other characters in the group depends on the current locale. Special attention: [az] may not be equivalent to% l.

Entry mode:

Mode entry can be

  • Single character class matches the category of any single character;
  • With a single character class '*' matches zero or more of a character class. This entry always match the longest possible string;
  • Class with a single character '+', one or more of the match such characters. This entry always match the longest possible string;
  • Class with a single character '-', the class matches zero or more characters. And '*' are different, the entry is always matching string as short as possible;
  • A single character class with a '?', Will match zero or a character class. Whenever possible, it will be a match;
  • % N, n herein may be from 1 to 9; a is equal to the number n matching entry capture (described later) of the substring.
  • % Bxy, where x and y are two distinct characters; the entry matches x y start end, and wherein x and y are balanced string. Means that, if the string is read from left to right, each of a read on the x + 1, y on a read-1, y that is the final end of the first count to the y 0. For example, entries% b () can be matched to the bracket equilibrium expression.
  • % F [set], refers to the border pattern; this will be matched to an entry in a blank within a character string before the set, and the previous character position does not belong to this set. Set set of meanings as described above. Calculation of the empty string start and end points of matched characters where there as it '\ 0' the same.

    Meaning that the boundary mode transition is never detected in the collection in the collection to indicate the matching range.

For example the following matches :

string.gsub ("THE (QUICK) brOWN FOx JUMPS", "%f[%a]%u+%f[%A]", print)

THE
QUICK
JUMPS

mode:

Mode refers to a mode entry sequence. In the top mode plus symbol '^' anchor do match from the beginning of the string. In the final surface model plus symbol '$' matching process will be anchored to the end of the string. If the '^' and '$' appears in the other position, they were no special meaning, only represent themselves.

capture:

Mode from a mode with a small child inside the parentheses; sub-modes are referred capture. When the matching is successful, the matching substring to capture the string is saved for future use. Catch in the order of their opening parenthesis numbered. For example, for mode "(A (.)% W (% S ))", the string matching "A (.)% W (% S )" stored in the first portion of the capture (and thus number 1); the matched characters are to capture the portion 2, to match the "% s *" is 3. "."

As a special case, the empty capture () to capture the current position of the string (which is a number). For example, if the pattern "() aa ()" is applied to the string "flaaap" on, will produce two capture: 3 and 5.

6.5 UTF-8 support

utf8.charpattern

Mode for an exact match to a sequence of UTF-8 byte (a string, not function) "[\ 0- \ x7F \ xC2- \ xF4] [\ x80- \ xBF] *" (see §6.4. 1). It is assumed that the processing object is a valid UTF-8 string.

6.6 Table processing

table.move (a1, f, e, t [,a2])

The table element is moved from table a1 a2. This function does times equivalent to multiple assignment this latter operation equivalent: a2 [t], ··· = a1 [f], ···, a1 [e]. The default value is a2 a1. Source and target range may overlap interval. F index must be a positive number.
a1-- original table
F - from index
E - End index
T - to index
a2 - target tables
move is to copy the value of a1 to a2 inside, not remove the original value in a1.

table.pack (···)

All parameters with the return key 2, and so filled the new table, and "n" parameter is defined as the total number of the field. Note that this list is not necessarily a return sequence.

table.pack(nil,2,3,4) -- 返回的就不是序列
table.sort (list [, comp])

Sorting algorithm is not stable; i.e., when the two elements of the order, which may change the relative position after the sort.

6.7 Math Library

math.modf (x)

Returns the integer part and a fractional part of x. The second result will be a float.

math.type (x)

If x is an integer, returns "integer", if it is floating-point, return to "float", if x is not a number, returns nil.

6.8 O library

I / O library provides two different styles of document processing interface. The first style using implicit file handle; it provides a default setting input file and the output file default operation, all input and output operations for the default file. The second style using explicit file handle.

When a hidden file handles, all the operations provided by the table io. When using explicit file handle, io.open returns a file handle, and all of the operation method by the file handle is provided.

Io table also provides three and C are the same meanings as the predefined file handles: io.stdin, io.stdout, and io.stderr. I / O library never closes them.

Unless otherwise stated, I / O function on error returns nil (second return value is an error message, the return value of the third system-related error code). Nil returns a different value if successful. On non-POSIX systems, remove the error message according to the error code process may not be thread-safe, because it uses the global variable C errno.

io.open (filename [, mode])

This function opens a specified file mode string pattern. Returns the new file handle. When an error, returns nil plus an error message.

mode string can be any of the following values:

"r": read mode (default);
"W": Write mode;
"A": append mode;
"R & lt +": update mode, before the data all of which are reserved;
"W +": all the previous update mode, data delete;
"a +": append update mode, all previous data are preserved, allowing only the end of the file is written to do.
mode can add a string 'b' at the end, which will open the file in binary manner on some systems.

io.popen (prog [, mode])

This function and system-related, not all platforms are available.

When turned on by a separate process, prog, file handle is returned that can be used to read data from the program (if the mode is "r", which is the default) or write is input to the program (if mode is "w" )

With this script can execute some commands and returns the result

io.tmpfile ()

If successful, it returns a handle to a temporary file. The file is opened in update mode, it will be automatically deleted at the end of the program.

io.type (obj)

Checks that obj is a valid file handle. If it is an obj file handle open, returns the string "file". If obj is a closed file handle, returns the string "closed file". If obj is not a file handle, returns nil.

file:read (···)

Read file file, in the format specified decide what to read. For each format, the function returns a character string corresponding to the read or digital. If it can not read data in the format corresponding to nil is returned. (For this last case, the function does not read a subsequent format.) When the call format does not pass, it will default format, read the next row (see description below).

Formats are

  • "N": a digital read, according to the conversion grammar Lua, and may return an integer or floating point. (Digital front or rear can have a space, and a symbol) as long as the legal digital configuration, the format string is always read as long as possible; read out if not form a valid prefix number (such as an empty string, " 0x "or" 3.4e- "), to suspend function runs, returns nil.
  • "A": start reading the entire file from its current location. If the end of the file, returns an empty string.
  • "L": read a line and ignore the tag end of the line. When the end of the file, returns nil This is the default format.
  • "L": read a line and keep the line end tag (if any), when the end of the file, returns nil.
  • number: reading a character string does not exceed the number of this number of bytes. When the end of the file, returns nil. If the number is zero, it reads nothing and returns an empty string. When the end of the file, returns nil.
    Format "l" and "L" for text files only.
file:seek ([whence [, offset]])

Set and get calculated based on the position at the beginning of the file. Provided the position specified by the offset and string whence whence decision point. Point can be:

  • "Set": base value is 0 (beginning of the file);
  • "Cur": base point for the current location;
  • "end": base point for the end of the file;
    when seek success, returns the final calculated from the beginning of the file from the file. When the seek fails, it returns nil plus an error description string.

The default value for whence is "cur", offset defaults to 0. Therefore, calling File: the Seek () can return to the current location of the file, it does not change; calls File: the Seek ( "the SET") position to the beginning of the file (and returns 0); call to File: the Seek ( "End") will position is set to the end of the file and returns the file size.

file:setvbuf (mode [, size])

A buffer mode output file. There are three modes:

  • "No": no buffering; output operations effective immediately.
  • "Full": full buffering; only when the cache is full or if you explicitly call for file flush (see io.flush) really do output operations.
  • "line": line buffer; an output buffer prior to each change to the line, for some special files (e.g. terminal apparatus) prior to any input buffer.
    For the latter two cases, size specifies the buffer size in units of bytes. By default there will be an appropriate size.

Operating system libraries

os.getenv (varname)

Return process environment variable varname value if the variable is not defined, returns nil.

Guess you like

Origin www.cnblogs.com/Fallever/p/11110584.html