Lua 标准库
Lua - 标准库
Section titled “Lua - 标准库”Lua 包含一组提供基本功能的标准库。这些库是用 C 语言实现的,并且在任何 Lua 环境中都随时可用。它们提供的服务范围从基本操作和字符串处理,到文件 I/O(输入/输出)和调试(debugging)。
标准库被组织成模块([table] 表)。有些函数是全局可访问的,而另一些则通过这些模块表来访问(例如,string.format,math.sin)。
- 基本库(Basic Library): 核心函数、错误处理、类型检查、协程(coroutine)操作。
- 包库(Package Library): 用于加载模块(
require)。 - 字符串操作库(String Manipulation Library): 用于处理字符串,包括模式匹配。
- 表操作库(Table Manipulation Library): 用于处理表的函数。
- 数学库(Mathematical Library): 标准数学函数。
- UTF-8 库(UTF-8 Library)(Lua 5.3+): 对 UTF-8 编码字符串的基础支持。
- 输入/输出(I/O)库(Input and Output (I/O) Library): 用于文件操作和标准输入/输出。
- 操作系统设施库(Operating System Facilities Library): 用于与操作系统交互的函数(例如,时间、文件系统操作)。
- 调试库(Debug Library): 用于调试(debugging)和内省(introspection)。
基本库(全局函数和 coroutine)
Section titled “基本库(全局函数和 coroutine)”此库提供了基本的 Lua 函数。其中许多是全局可访问的。
| 序号 | 库 / 方法与用途 |
|---|---|
| 1. | 核心函数与类型系统: assert、error、pcall、xpcall、type、tostring、tonumber、print。(在错误处理、数据类型等章节中介绍) |
| 2. | 元表(Metatables): getmetatable、setmetatable、rawequal、rawget、rawset。(在元表章节中介绍) |
| 3. | 迭代: pairs、ipairs、next。(在迭代器、循环章节中介绍) |
| 4. | 模块加载(已弃用(deprecated)的加载方式): load、loadfile、loadstring。这些函数将 Lua 代码从字符串/文件/函数编译成一个函数。dofile 执行一个文件。(现代模块加载使用包库中的 require 函数)。 |
| 5. | 环境: _G(全局环境表)。_VERSION(字符串,例如 "Lua 5.4")。注意:自 Lua 5.2 起,getfenv 和 setfenv 已被弃用;请使用 _ENV upvalue 进行环境操作。示例:print(_VERSION) — 显示当前 Lua 版本。 |
| 6. | 协程库: coroutine.create、coroutine.resume、coroutine.yield、coroutine.status、coroutine.wrap、coroutine.running、coroutine.isyieldable(Lua 5.2+)。(在协程章节中介绍) |
| 7. | 垃圾回收(Garbage Collection): collectgarbage。(在垃圾回收章节中介绍) |
| 8. | 可变参数(Variable Arguments): select(index, ...)。返回 index 之后的参数,或者如果 index 是 "#",则返回可变参数(varargs)的总数。table.pack(...)(Lua 5.2+)通常更推荐用于将可变参数收集到表中。 |
包库(package)
Section titled “包库(package)”管理 Lua 中的模块。主要函数是 require(modname),它加载并运行一个模块,并缓存其结果。package 表包含路径信息(package.path、package.cpath)、已加载模块(package.loaded)等。(在模块章节中介绍)
字符串操作库(string)
Section titled “字符串操作库(string)”提供字符串操作函数,例如 string.len、string.sub、string.find、string.gsub、string.format、string.upper、string.lower、string.rep、string.char、string.byte、string.pack、string.unpack(Lua 5.3+)。(在字符串章节中介绍)
表操作库(table)
Section titled “表操作库(table)”用于处理表(Lua 的主要数据结构)的函数,包括 table.insert、table.remove、table.concat、table.sort、table.pack(Lua 5.2+)、table.unpack(Lua 5.2+,以前是全局的 unpack)、table.move(Lua 5.3+)。(在表、数组章节中介绍)
数学库(math)
Section titled “数学库(math)”包含标准数学函数,如 math.sin、math.cos、math.sqrt、math.log、math.exp、math.abs、math.pi、math.huge、math.random、math.randomseed、math.floor、math.ceil,以及数值类型函数,如 math.type(Lua 5.3+)、math.tointeger(Lua 5.3+)。(如果可用,将在专门的数学库章节中详细介绍,或参考 Lua 官方文档)。
UTF-8 库(utf8)(Lua 5.3+)
Section titled “UTF-8 库(utf8)(Lua 5.3+)”提供对 UTF-8 编码字符串的基础支持。函数包括 utf8.char、utf8.codes(迭代器)、utf8.codepoint、utf8.len、utf8.offset。此库有助于正确处理多字节字符。
输入/输出库(io)
Section titled “输入/输出库(io)”用于文件操作和标准输入/输出流。函数包括 io.open、io.close、io.read、io.write、io.lines、io.input、io.output、io.tmpfile、file:read、file:write、file:lines、file:seek、file:close。(在文件 I/O 章节中介绍)
操作系统设施库(os)
Section titled “操作系统设施库(os)”提供与操作系统交互的函数,例如 os.clock(CPU 时间)、os.date(格式化日期/时间)、os.time(当前时间)、os.difftime、os.execute(运行 shell 命令)、os.exit、os.getenv、os.remove、os.rename、os.setlocale、os.tmpname。(如果可用,将在专门的操作系统设施章节中详细介绍,或参考 Lua 官方文档)。
调试库(debug)
Section titled “调试库(debug)”提供内省和调试功能。函数包括 debug.getinfo、debug.getlocal、debug.getupvalue、debug.sethook、debug.traceback。(在调试章节中介绍)
注意: 有关任何特定函数或库的详细信息,您所使用的 Lua 版本的官方 Lua 参考手册是最权威的资源。