Download pdf - Lua 5.3 参考手册

Transcript
  • lua

    www.lua.org

  • \0

    luaconf.h LUA_32BITS

  • 0/0

    a.name a["name"]

    i ja[i] a[j]

    1.0 == 1

    a[2.0] = true 22

    type

    var_ENV.var

  • _ENV_ENV

    _ENV_ENV

    _ENV

    _ENV

    _G_G

    _ENVload

    load loadfile

    lua

    errorpcall xpcall

    xpcall lua_pcall

  • __add

    "add"

    getmetatable

    setmetatable

    ____add

    obj

    rawget(getmetatable(obj) or {}, "__" .. event_name)

  • +__add

    -*

    /%^-

    //&

    |~~

    >

    ..

    #

    ==

    // == ~= = < > = ( ) { } [ ] :: ; : , . .. ...

    \a \b \f \n\r \t \v \\

    \" \'\z

  • \0\xXX

    \ddd

    \u{XXX}

    [[ [=[]====]

    a1

    a = 'alo\n123"' a = "alo\n123\"" a = '\97lo\10\04923"' a = [[alo 123"]] a = [==[ alo 123"]==]

    e E 0x0X

    p P

  • 3 345 0xff 0xBEBADA

    3.0 3.1416 314.16e-2 0.31416E1 34e1 0x0.1E 0xA23p-4 0X1.921FB54442D18P+1

    -- --

    var ::= Name

    var ::= prefixexp [ exp ]

    t[i] gettable_event(t,i)gettable_event

    var.Name var["Name"]

  • var ::= prefixexp . Name

    x _ENV.x_ENV

    block ::= {stat}

    stat ::= ;

    a = b + c (print or io.write)('done')

    a = b + c(print or io.write)('done') a = b + c; (print or io.write)('done')

    ;(print or io.write)('done')

    stat ::= do block end

  • chunk ::= block

    _ENV _ENV

    luacstring.dump

    load

    stat ::= varlist = explistvarlist ::= var {, var}explist ::= exp {, exp}

    i = 3 i, a[i] = i+1, 20

    a[3] a[4] a[i] i

  • x, y = y, x

    x y

    x, y, z = y, z, x

    x y z

    t[i]= val settable_event(t,i,val)

    settable_event

    x = val _ENV.x = val

    stat ::= while exp do block endstat ::= repeat block until expstat ::= if exp then block {elseif exp then block} [else block]

    stat ::= goto Namestat ::= labellabel ::= :: Name ::

  • stat ::= break

    stat ::= return [explist] [;]

    doreturn end

    stat ::= for Name = exp , exp [, exp] do block end

    for v = e1, e2, e3 do block end

    do local var, limit, step = tonumber(e1), tonumber(e2), tonumber(e3 if not (var and limit and step) then error() end var = var - step while true do var = var + step if (step >= 0 and var > limit) or (step < 0 and var < limit) then break

  • end local v = var block end end

    var limit step

    v

    stat ::= for namelist in explist do block endnamelist ::= Name {, Name}

    for var_1, , var_n in explist do block end

    do local f, s, var = explist while true do local var_1, , var_n = f(s, var) if var_1 == nil then break end var = var_1 block end end

    explist

    f s var

  • var_i

    stat ::= functioncall

    stat ::= local namelist [= explist]

    exp ::= prefixexpexp ::= nil | false | trueexp ::= Numeralexp ::= LiteralStringexp ::= functiondefexp ::= tableconstructorexp ::= ...exp ::= exp binop expexp ::= unop expprefixexp ::= var | functioncall | ( exp )

  • ...

    f() -- 0 g(f(), x) -- f() g(x, f()) -- g x f() a,b,c = f(), x -- f() 1 c nil a,b = ... -- a -- b -- a b nil a,b,c = x, f() -- f() 2 a,b,c = f() -- f() 3 return f() -- f() return ... -- parameters return x,y,f() -- x, y, f() {f()} -- f() {...} -- {f(), nil} -- f()

    (f(x,y,z)) f(f(x,y,z)) f

    f

    +

  • -*///%^-

    /pow

    //

    &|~>>

  • formatstring.format

    ==~==

    ==

  • "0"==0t[0] t["0"]

    ~= ==

    a > b b < a a >= b b 10 10 or error() --> 10 nil or "a" --> "a" nil and 10 --> nil false and error() --> false false and nil --> false false or nil --> nil 10 and 20 --> 20

    -->

  • ..

    __concat

    #

    __len

    __len t

    {10, 20, nil, 40}

    4 3

    or and < > = ~= == | ~ & > .. + - * / // % unary operators (not # - ~) ^

    ..^

  • tableconstructor ::= { [fieldlist] }fieldlist ::= field {fieldsep field} [fieldsep]field ::= [ exp ] = exp | Name = exp | expfieldsep ::= , | ;

    [exp1] = exp2 exp1exp2 name = exp ["name"] = exp

    exp [i] = exp i

    a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 }

    do local t = {} t[f(1)] = g t[1] = "x" -- 1st exp t[2] = "y" -- 2nd exp t.x = 1 -- t["x"] = 1 t[3] = f(x) -- 3rd exp t[30] = 23 t[4] = 45 -- 4th exp a = t end

    exp

    functioncall ::= prefixexp args

  • functioncall ::= prefixexp : Name args

    v:name(args)v.name(v,args) v

    args ::= ( [explist] )args ::= tableconstructorargs ::= LiteralString

    f{fields}f({fields})f'string' f"string" f[[string]]

    f('string')

    return functioncall

    return (f(x)) -- return 2 * f(x) return x, f(x) -- f(x); return -- return x or f(x) --

    functiondef ::= function funcbodyfuncbody ::= ( [parlist] ) block end

  • stat ::= function funcname funcbodystat ::= local function Name funcbodyfuncname ::= Name {. Name} [: Name]

    function f () body end

    f = function () body end

    function t.a.b.c.f () body end

    t.a.b.c.f = function () body end

    local function f () body end

    local f; f = function () body end

    local f = function () body end

    f

    parlist ::= namelist [, ...] | ...

    ...

  • function f(a, b) end function g(a, b, ...) end function r() return 1,2,3 end

    CALL PARAMETERS f(3) a=3, b=nil f(3, 4) a=3, b=4 f(3, 4, 5) a=3, b=4 f(r(), 10) a=1, b=10 f(r()) a=1, b=2 g(3) a=3, b=nil, ... --> (nothing) g(3, 4) a=3, b=4, ... --> (nothing) g(3, 4, 5, 8) a=3, b=4, ... --> 5 8 g(5, r()) a=5, b=1, ... --> 2 3

    self

    function t.a.b.c:f (params) body end

    t.a.b.c.f = function (self, params) body end

  • x = 10 -- do -- local x = x -- 'x', 10 print(x) --> 10 x = x+1 do -- local x = x+1 -- 'x' print(x) --> 12 end print(x) --> 11 end print(x) --> 10

    local x = x xx

    a = {} local x = 20 for i=1,10 do local y = 0 a[i] = function () y=y+1; return x+y end end

    y x

    lua.h

  • LUA_USE_APICHECK

    lua_CFunction

    lua_checkstack

    LUA_MINSTACKLUA_MINSTACK

    lua_call

    lua_checkstack

  • 1 abs(index) top

    LUA_TNONE

    lua_pushcclosure

    lua_upvalueindexlua_upvalueindex(1)

    lua_upvalueindex(n)

  • LUA_REGISTRYINDEX

    luaL_ref

    lua.h

    LUA_RIDX_MAINTHREAD

    LUA_RIDX_GLOBALS

    longjmp

    LUAI_THROW

    setjmp

    lua_atpanic abort

    lua_error

  • longjmpfoo

    longjmpfoo

    lua_yieldk lua_callk lua_pcallkk

    lua_yieldk lua_callk lua_pcallk

    int original_function (lua_State *L) { ... /* code 1 */ status = lua_pcall(L, n, m, h); /* calls Lua */ ... /* code 2 */ }

    lua_pcall

    int k (lua_State *L, int status, lua_KContext ctx) { ... /* code 2 */ } int original_function (lua_State *L) { ... /* code 1 */ return k(L, lua_pcall(L, n, m, h), ctx); }

  • klua_KFunction lua_pcall

    lua_pcallk

    lua_pcall lua_pcallk

    int original_function (lua_State *L) { ... /* code 1 */ return k(L, lua_pcallk(L, n, m, h, ctx2, k), ctx1); }

    lua_pcallklua_callk

    lua_pcallk ctxlua_pcallk

    lua_pcallkLUA_YIELD LUA_OK

    lua_yieldk lua_callkLUA_YIELD

    lua_callkLUA_OK lua_yieldk

    lua_yieldk

    lua_callk

    op

    x|y

  • x y ?

    x -e v

    lua_absindex

    int lua_absindex (lua_State *L, int idx);

    idx

    lua_Alloc

    typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);

    realloc udlua_newstate ptr

    osizensize

    ptr NULL osize ptr

    ptr NULL osizeosize

    LUA_TSTRING LUA_TTABLE LUA_TFUNCTIONLUA_TUSERDATA LUA_TTHREAD osize

    nsize free NULL

    nsize reallocNULL osize >= nsize

    luaL_newstate

  • static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { (void)ud; (void)osize; /* not used */ if (nsize == 0) { free(ptr); return NULL; } else return realloc(ptr, nsize); }

    free(NULL) realloc(NULL,size)malloc(size) realloc

    lua_arith

    void lua_arith (lua_State *L, int op);

    op

    LUA_OPADD +LUA_OPSUB -LUA_OPMUL *LUA_OPDIV /LUA_OPIDIV //LUA_OPMOD %LUA_OPPOW ^LUA_OPUNM -LUA_OPBNOT ~LUA_OPBAND &LUA_OPBOR |LUA_OPBXOR ~LUA_OPSHL >

    lua_atpanic

  • lua_CFunction lua_atpanic (lua_State *L, lua_CFunction panicf);

    lua_call

    void lua_call (lua_State *L, int nargs, int nresults);

    lua_call nargs

    nresults nresultsLUA_MULTRET

    longjmp

    a = f("how", t.x, 14)

    lua_getglobal(L, "f"); /* function to be called */ lua_pushliteral(L, "how"); /* 1st argument */ lua_getglobal(L, "t"); /* table to be indexed */ lua_getfield(L, -1, "x"); /* push result of t.x (2nd arg) */ lua_remove(L, -2); /* remove 't' from the stack */ lua_pushinteger(L, 14); /* 3rd argument */ lua_call(L, 3, 1); /* call 'f' with 3 arguments and 1 result */ lua_setglobal(L, "a"); /* set global 'a' */

    lua_callk

    void lua_callk (lua_State *L, int nargs, int nresults,

  • lua_KContext ctx, lua_KFunction k);

    lua_call

    lua_CFunction

    typedef int (*lua_CFunction) (lua_State *L);

    lua_gettop(L)lua_gettop(L)

    static int foo (lua_State *L) { int n = lua_gettop(L); /* */ lua_Number sum = 0.0; int i; for (i = 1; i

  • lua_close

    void lua_close (lua_State *L);

    lua_compare

    int lua_compare (lua_State *L, int index1, int index2, int op);

    index1 op index2

    op

    LUA_OPEQ ==LUA_OPLT lua_getinfo

    f

    lua_Debug ar; lua_getglobal(L, "f"); /* 'f' */ lua_getinfo(L, ">S", &ar); printf("%d\n", ar.linedefined);

  • what ar

    n name namewhatS source short_src linedefined lastlinedefined

    whatl currentlinet istailcallu nups nparams isvarargfL

    f

    what

    lua_getlocal

    const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n);

    arlua_getstack lua_Hook

    ndebug.getlocal

    lua_getlocal

    ar NULL

    NULL

    lua_getstack

    int lua_getstack (lua_State *L, int level, lua_Debug *ar);

    lua_Debug

  • lua_getstack

    lua_getupvalue

    const char *lua_getupvalue (lua_State *L, int funcindex, int n);

    lua_getupvaluen funcindex

    NULL""

    lua_Hook

    typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);

    ar event

    LUA_HOOKCALL LUA_HOOKRETLUA_HOOKTAILCALL LUA_HOOKLINE LUA_HOOKCOUNT

    currentline arlua_getinfo

    event LUA_HOOKCALLLUA_HOOKTAILCALL

    k lua_yieldklua_pcallk lua_callk

    lua_yieldnresults

  • lua_sethook

    void lua_sethook (lua_State *L, lua_Hook f, int mask, int count);

    f maskLUA_MASKCALL LUA_MASKRET LUA_MASKLINE

    LUA_MASKCOUNT countLUA_MASKCOUNT

    count

    mask

    lua_setlocal

    const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n);

    ar n lua_getlocallua_getlocal lua_setlocal

    NULL

    lua_setupvalue

    const char *lua_setupvalue (lua_State *L, int funcindex, int n);

    funcindex n lua_getupvaluelua_getupvalue

    NULL

  • lua_upvalueid

    void *lua_upvalueid (lua_State *L, int funcindex, int n);

    funcindex nfuncindex n lua_getupvalue

    lua_getupvalue n

    lua_upvaluejoin

    void lua_upvaluejoin (lua_State *L, int funcindex1, int n1, int funcindex2, int n2);

    funcindex1 n1funcindex2 n2

    lauxlib.hluaL_

    bad argument #1

    luaL_check*

  • luaL_addchar

    void luaL_addchar (luaL_Buffer *B, char c);

    B luaL_Buffer c

    luaL_addlstring

    void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);

    B luaL_Buffer l s

    luaL_addsize

    void luaL_addsize (luaL_Buffer *B, size_t n);

    B luaL_BufferluaL_prepbuffer n

    luaL_addstring

    void luaL_addstring (luaL_Buffer *B, const char *s);

    B luaL_Buffer s

    luaL_addvalue

    void luaL_addvalue (luaL_Buffer *B);

    B luaL_Buffer

    luaL_argcheck

    void luaL_argcheck (lua_State *L, int cond, int arg,

  • const char *extramsg);

    condluaL_argerror

    luaL_argerror

    int luaL_argerror (lua_State *L, int arg, const char *extramsg);

    argextramsg

    bad argument #arg to 'funcname' (extramsg)

    luaL_Buffer

    typedef struct luaL_Buffer luaL_Buffer;

    luaL_Buffer bluaL_buffinit(L, &b)

    luaL_add*luaL_pushresult(&b)

    luaL_Buffer bluaL_buffinitsize(L, &b, sz) sz

    luaL_pushresultsize(&b, sz) sz

    luaL_addvalue luaL_pushresult

  • luaL_buffinit

    void luaL_buffinit (lua_State *L, luaL_Buffer *B);

    BluaL_Buffer

    luaL_buffinitsize

    char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz);

    luaL_buffinit luaL_prepbuffsize

    luaL_callmeta

    int luaL_callmeta (lua_State *L, int obj, const char *e);

    obj e

    luaL_checkany

    void luaL_checkany (lua_State *L, int arg);

    arg

    luaL_checkinteger

    lua_Integer luaL_checkinteger (lua_State *L, int arg);

    arglua_Integer

    luaL_checklstring

    const char *luaL_checklstring (lua_State *L, int arg, size_t *l);

    arg lNULL *l

    lua_tolstring

  • luaL_checknumber

    lua_Number luaL_checknumber (lua_State *L, int arg);

    arg

    luaL_checkoption

    int luaL_checkoption (lua_State *L, int arg, const char *def, const char *const lst[]);

    arg lst

    def NULL def arg

    luaL_checkstack

    void luaL_checkstack (lua_State *L, int sz, const char *msg);

    top + szmsg NULL

    luaL_checkstring

    const char *luaL_checkstring (lua_State *L, int arg);

    arg

    lua_tolstring

    luaL_checktype

    void luaL_checktype (lua_State *L, int arg, int t);

    arg t lua_type t

  • luaL_checkudata

    void *luaL_checkudata (lua_State *L, int arg, const char *tname);

    arg tnameluaL_newmetatablelua_touserdata

    luaL_checkversion

    void luaL_checkversion (lua_State *L);

    luaL_dofile

    int luaL_dofile (lua_State *L, const char *filename);

    (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0))

    luaL_dostring

    int luaL_dostring (lua_State *L, const char *str);

    (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0))

    luaL_error

    int luaL_error (lua_State *L, const char *fmt, ...);

    fmtlua_pushfstring

    returnluaL_error(args)

  • luaL_execresult

    int luaL_execresult (lua_State *L, int stat);

    os.executeio.close

    luaL_fileresult

    int luaL_fileresult (lua_State *L, int stat, const char *fname);

    io.open os.rename file:seek

    luaL_getmetafield

    int luaL_getmetafield (lua_State *L, int obj, const char *e);

    obj eLUA_TNIL

    luaL_getmetatable

    int luaL_getmetatable (lua_State *L, const char *tname);

    tname luaL_newmetatabletname

    luaL_getsubtable

    int luaL_getsubtable (lua_State *L, int idx, const char *fname);

    t[fname] t idx

    luaL_gsub

    const char *luaL_gsub (lua_State *L, const char *s, const char *p, const char *r);

    s p r

  • luaL_len

    lua_Integer luaL_len (lua_State *L, int index);

    #

    luaL_loadbuffer

    int luaL_loadbuffer (lua_State *L, const char *buff, size_t sz, const char *name);

    luaL_loadbufferx mode NULL

    luaL_loadbufferx

    int luaL_loadbufferx (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode);

    lua_loadbuff sz

    lua_load namemode lua_load

    luaL_loadfile

    int luaL_loadfile (lua_State *L, const char *filename);

    luaL_loadfilex mode NULL

    luaL_loadfilex

    int luaL_loadfilex (lua_State *L, const char *filename, const char *mode);

    lua_loadfilename filename NULL

    #

  • mode lua_load

    lua_loadLUA_ERRFILE

    lua_load

    luaL_loadstring

    int luaL_loadstring (lua_State *L, const char *s);

    lua_loads

    lua_load

    lua_load

    luaL_newlib

    void luaL_newlib (lua_State *L, const luaL_Reg l[]);

    l

    (luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))

    l

    luaL_newlibtable

    void luaL_newlibtable (lua_State *L, const luaL_Reg l[]);

    lluaL_setfuncs luaL_newlib

    l

    luaL_newmetatable

    int luaL_newmetatable (lua_State *L, const char *tname);

    tname__name = tname [tname]

  • = new table __name

    tname

    luaL_newstate

    lua_State *luaL_newstate (void);

    realloclua_newstate

    NULL

    luaL_openlibs

    void luaL_openlibs (lua_State *L);

    luaL_optinteger

    lua_Integer luaL_optinteger (lua_State *L, int arg, lua_Integer d);

    argd

    luaL_optlstring

    const char *luaL_optlstring (lua_State *L, int arg, const char *d, size_t *l);

    argd

    l NULL *l

    luaL_optnumber

    lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number d);

  • argd

    luaL_optstring

    const char *luaL_optstring (lua_State *L, int arg, const char *d);

    argd

    luaL_prepbuffer

    char *luaL_prepbuffer (luaL_Buffer *B);

    luaL_prepbuffsize LUAL_BUFFERSIZE

    luaL_prepbuffsize

    char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz);

    sz BluaL_Buffer

    luaL_addsize

    luaL_pushresult

    void luaL_pushresult (luaL_Buffer *B);

    B

    luaL_pushresultsize

    void luaL_pushresultsize (luaL_Buffer *B, size_t sz);

    luaL_addsize luaL_pushresult

    luaL_ref

    int luaL_ref (lua_State *L, int t);

    t

    t

  • luaL_reflua_rawgeti(L, t, r) r luaL_unref

    luaL_ref LUA_REFNILLUA_NOREF luaL_ref

    luaL_Reg

    typedef struct luaL_Reg { const char *name; lua_CFunction func;} luaL_Reg;

    luaL_setfuncs name funcluaL_Reg name func NULL

    luaL_requiref

    void luaL_requiref (lua_State *L, const char *modname, lua_CFunction openf, int glb);

    modname package.loaded openfmodname

    package.loaded[modname] require

    glb modname

    luaL_setfuncs

    void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup);

    l luaL_Reg

    nup nup

    luaL_setmetatable

    void luaL_setmetatable (lua_State *L, const char *tname);

  • tname luaL_newmetatable

    luaL_Stream

    typedef struct luaL_Stream { FILE *f; lua_CFunction closef;} luaL_Stream;

    LUA_FILEHANDLELUA_FILEHANDLE

    luaL_newmetatable

    luaL_Streamf NULL

    closef

    NULL

    luaL_testudata

    void *luaL_testudata (lua_State *L, int arg, const char *tname);

    luaL_checkudata NULL

    luaL_tolstring

    const char *luaL_tolstring (lua_State *L, int idx, size_t *len);

    len NULL*len

    "__tostring" luaL_tolstring

    luaL_traceback

    void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, int level);

  • L1 msg NULLlevel

    luaL_typename

    const char *luaL_typename (lua_State *L, int index);

    luaL_unref

    void luaL_unref (lua_State *L, int t, int ref);

    t ref luaL_refref

    ref LUA_NOREF LUA_REFNIL luaL_unref

    luaL_where

    void luaL_where (lua_State *L, int lvl);

    lvl

    chunkname:currentline:

    type getmetatable

    table.sort

  • luaL_openlibs

    luaL_requiref luaopen_baseluaopen_package luaopen_coroutineluaopen_string luaopen_utf8luaopen_table luaopen_mathluaopen_io luaopen_osluaopen_debug lualib.h

    assert (v [, message])

    v errormessage

    assertion failed!

    collectgarbage ([opt [, arg]])

    opt

    collectstop

  • restartcount

    step arg

    setpause arg

    setstepmul arg

    isrunning

    dofile ([filename])

    dofile stdindofile dofile

    error (message [, level])

    message error

    errorlevel

    errorerror

    _G

    getmetatable (object)

    object"__metatable"

  • ipairs (t)

    t

    for i,v in ipairs(t) do body end

    1,t[1] 2,t[2]

    load (chunk [, chunkname [, mode [, env]]])

    chunk chunkload chunk

    env

    _ENVstring.dump

    chunknamechunk chunk

    =(load)

    modeb t

    bt bt

    loadfile ([filename [, mode [, env]]])

    load filename

    next (table [, index])

  • nextnext

    nextnext(t)

    next

    pairs (t)

    t __pairs t

    next t

    for k,v in pairs(t) do body end

    t

    next

    pcall (f [, arg1, ])

    f fpcall

    pcallpcall

    print ()

    stdout tostringprint

    string.format io.write

    rawequal (v1, v2)

  • v1 v2

    rawget (table, index)

    table[index] tableindex

    rawlen (v)

    v v

    rawset (table, index, value)

    table[index] value tableindex value

    table

    select (index, )

    index indexindex

    "#" select

    setmetatable (table, metatable)

    metatable"__metatable"

    table

    tonumber (e [, base])

    base tonumber

    tonumber

  • base e

    A B Ze

    tostring (v)

    string.format

    v "__tostring" tostring v

    type (v)

    nilnumber string boolean

    table function thread userdata

    _VERSION

    Lua 5.3

    xpcall (f, msgh [, arg1, ])

    pcall msgh

    coroutine

    coroutine.create (f)

    f f"thread"

    coroutine.isyieldable ()

  • coroutine.resume (co [, val1, ])

    coval1

    resume val1

    resume yield

    resume

    coroutine.running ()

    coroutine.status (co)

    costatus "running" yield

    "suspended""normal"

    "dead"

    coroutine.wrap (f)

    f f

    resume resume

    coroutine.yield ()

    yield resume

    require package

  • require (modname)

    package.loadedmodname requirepackage.loaded[modname]

    require package.searchersrequire

    package.searchers

    require package.preload[modname]require

    package.pathpackage.cpathpackage.searchers

    require modname

    requirepackage.loaded[modname]

    package.loaded[modname] requirerequire

    package.loaded[modname]

    require

    package.config

    \/

    ;?

    !luaopen_

    -

    package.cpath

  • require

    package.pathpackage.cpath LUA_CPATH_5_3LUA_CPATH luaconf.h

    package.loaded

    require modnamepackage.loaded[modname] require

    require

    package.loadlib (libname, funcname)

    libname

    funcname *funcname

    funcname lua_CFunctionlua_CFunction

    requirelibname

    funcname

    dlfcn

    package.path

    require

    LUA_PATH_5_3 LUA_PATHluaconf.h

    ;;

    package.preload

    require

  • require

    package.searchers

    require

    requirerequire

    package.preload

    package.pathpackage.searchpath

    package.cpathpackage.searchpath

    "./?.so;./?.dll;/usr/local/?/init.so"

    foo ./foo.so ./foo.dll/usr/local/foo/init.so

    luaopen_

    a.b.c-v2.1luaopen_a_b_c

    a.b.c a

    luaopen_a_b_c

    package.searchpath

  • package.searchpath (name, path [, sep [, rep]])

    path name

    namesep rep

    "./?.lua;./?.lc;/usr/local/?/init.lua"

    foo.a ./foo/a.lua ./foo/a.lc/usr/local/foo/a/init.lua

    string__index

    string.byte(s,i) s:byte(i)

    string.byte (s [, i [, j]])

    s[i] s[i+1] s[j] ij i string.sub

    string.char ()

  • string.dump (function [, strip])

    loadstrip

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

    s patternfind s

    initplain

    patternplain init

    string.format (formatstring, )

    sprintf* h L l n p q q

    string.format('%q', 'a string with "quotes" and \n new line')

    "a string with \"quotes\" and \ new line"

    A a E e f G gc d i o u X x qs s

  • tostring

    string.gmatch (s, pattern)

    patterns pattern

    pattern

    s

    s = "hello world from Lua" for w in string.gmatch(s, "%a+") do print(w) end

    key=value

    t = {} s = "from=world, to=Lua" for k, v in string.gmatch(s, "(%w+)=(%w+)") do t[k] = v end

    ^

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

    s n n patternrepl repl

    gsub gsub

    repl %repl %d

    %0 %% %

    repl

    repl

  • x = string.gsub("hello world", "(%w+)", "%1 %1") --> x="hello hello world world" x = string.gsub("hello world", "%w+", "%0 %0", 1) --> x="hello hello world" x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1") --> x="world hello Lua from" x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv) --> x="home = /home/roberto, user = roberto" x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s) return load(s)() end) --> x="4+5 = 9" local t = {name="lua", version="5.3"} x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t) --> x="lua-5.3.tar.gz"

    string.len (s)

    """a\000bc\000"

    string.lower (s)

    string.match (s, pattern [, init])

    s patternmatch pattern

    patterninit

    string.pack (fmt, v1, v2, )

  • v1 v2fmt

    string.packsize (fmt)

    string.packs z

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

    n s sepsep n

    string.reverse (s)

    s

    string.sub (s, i [, j])

    s i j i jj

    string.sub(s,1,j) s j string.sub(s,-i) i

    i ji j

    string.unpack (fmt, s [, pos])

    fmt s string.packpos s

    s

    string.upper (s)

    string.find string.gmatch string.gsub string.match

  • ^$()%.[]*+-?

    .%a%c%d%g%l%p%s%u%w%x%x

    %

    [set] -%

    [%w_] [_%w][0-7] [0-7%l%-]

    -

    [%a-z] [a-%%]

    [^set]

    %a %c%S

    [a-z] %l

  • *+

    - *

    ?

    %n

    %bxy

    %b()%f[set]

    \0

    ^$

    ^ $

    "(a*(.)%w(%s*))" "a*(.)%w(%s*)"

    .%s*

    ()"()aa()" "flaaap"

  • string.pack string.packsize string.unpack

    =![n] nb charB charh shortH shortl longL longj lua_IntegerJ lua_UnsignedT size_ti[n] n intI[n] n intf floatd doublen lua_Numbercn nzs[n] nsize_txXop op

    [n] xX! string.pack

    string.unpack

    !n sn in In nstring.pack

    string.unpack

    !1=

  • cz s

    string.pack string.unpack

    utf8

    utf8.char ()

    utf8.charpattern

    [\0-\x7F\xC2-\xF4][\x80-\xBF]*

    utf8.codes (s)

    for p, c in utf8.codes(s) do body end

    s p c

    utf8.codepoint (s [, i [, j]])

    s i ji j i

    utf8.len (s [, i [, j]])

  • s i ji j

    utf8.offset (s, n [, i])

    s n in i n i#s + 1 utf8.offset(s, -n)

    n

    n s i

    s

    table

    __len

    table.concat (list [, sep [, i [, j]]])

    list[i]..sep..list[i+1] sep..list[j] sep ij #list i j

    table.insert (list, [pos,] value)

    list pos value list[pos],list[pos+1], , list[#list] pos #list+1table.insert(t,x) x t

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

    a1 a2a2[t], = a1[f],,a1[e] a2 a1

    f

  • table.pack ()

    n

    table.remove (list [, pos])

    list pos pos#list list[pos+1], list[pos+2],

    , list[#list] list[#list] pos #list + 1#list list[pos]

    pos #list table.remove(l) l

    table.sort (list [, comp])

    list[1] list[#list]comp

    notcomp(list[i+1],list[i])comp