Module utils

模块功能:常用工具类接口

Info:

  • Copyright: openLuat
  • Release: 2019.01.05
  • License: MIT
  • Author: openLuat

Functions

string.toHex (str[, separator=""]) 将Lua字符串转成HEX字符串,如"123abc"转为"313233616263"
string.fromHex (hex) 将HEX字符串转成Lua字符串,如"313233616263"转为"123abc", 函数里加入了过滤分隔符,可以过滤掉大部分分隔符(可参见正则表达式中\s和\p的范围)。
string.utf8Len (str) 返回utf8编码字符串的长度
string.utf8ToTable (str) 返回utf8编码字符串的单个utf8字符的table
string.rawurlEncode (str) 返回字符串的 RFC3986 编码
string.urlEncode (str) 返回字符串的urlEncode编码
table.gsort (t, f) 返回一个迭代器函数,每次调用函数都会返回hash表的排序后的键值对
table.rconcat (l) table.concat的增强版,支持嵌套字符串数组
string.formatNumberThousands (num) 返回数字的千位符号格式
string.split (str, delimiter) 按照指定分隔符分割字符串
io.exists (path) 判断文件是否存在
io.readFile (path) 读取文件并返回文件的内容
io.writeFile (path, content, mode) 写入文件指定的内容,默认为覆盖二进制模式
io.pathInfo (path) 将文件路径分解为table信息
io.fileSize (path) 返回文件大小
io.readStream (path, offset, len) 返回指定位置读取的字符串


Functions

string.toHex (str[, separator=""])
将Lua字符串转成HEX字符串,如"123abc"转为"313233616263"

Parameters:

  • str string
     输入字符串
  • separator string [此参数可选,默认值为: ""]
     输出的16进制字符串分隔符

Returns:

  1. hexstring 16进制组成的串
  2. len 输入的字符串长度

Usage:

  • string.toHex("\1\2\3") -> "010203" 3
    string.toHex("123abc") -> "313233616263" 6
    string.toHex("123abc"," ") -> "31 32 33 61 62 63 " 6
string.fromHex (hex)
将HEX字符串转成Lua字符串,如"313233616263"转为"123abc", 函数里加入了过滤分隔符,可以过滤掉大部分分隔符(可参见正则表达式中\s和\p的范围)。

Parameters:

  • hex string
    16进制组成的串

Returns:

  1. charstring,字符组成的串
  2. len,输出字符串的长度

Usage:

  • string.fromHex("010203")       ->  "\1\2\3"
    string.fromHex("313233616263:) ->  "123abc"
string.utf8Len (str)
返回utf8编码字符串的长度

Parameters:

  • str string
    utf8编码的字符串,支持中文

Returns:

  • number,返回字符串长度

Usage:

  • local cnt = string.utf8Len("中国a"),cnt == 3
string.utf8ToTable (str)
返回utf8编码字符串的单个utf8字符的table

Parameters:

  • str string
    utf8编码的字符串,支持中文

Returns:

  • table,utf8字符串的table

Usage:

  • local t = string.utf8ToTable("中国2018")
string.rawurlEncode (str)
返回字符串的 RFC3986 编码

Parameters:

  • str string
    要转换编码的字符串,支持UTF8编码中文

Returns:

  • str, RFC3986 编码的字符串

Usage:

  • local str = string.rawurlEncode("####133") ,str == "%23%23%23%23133"
  • local str = string.rawurlEncode("中国2018") , str == "%e4%b8%ad%e5%9b%bd2018"
string.urlEncode (str)
返回字符串的urlEncode编码

Parameters:

  • str string
    要转换编码的字符串,支持UTF8编码中文

Returns:

  • str,urlEncode编码的字符串

Usage:

  • local str = string.urlEncode("####133") ,str == "%23%23%23%23133"
  • local str = string.urlEncode("中国2018") , str == "%e4%b8%ad%e5%9b%bd2018"
table.gsort (t, f)
返回一个迭代器函数,每次调用函数都会返回hash表的排序后的键值对

Parameters:

  • t table
     要排序的hash表
  • f
     自定义排序函数

Returns:

  • function.

Usage:

  • test = {a=1,f=9,d=2,c=8,b=5}
  • for name,line in pairsByKeys(test) do print(name,line) end
table.rconcat (l)
table.concat的增强版,支持嵌套字符串数组

Parameters:

  • l table
    嵌套字符串数组

Returns:

  • string

Usage:

  • print(table.rconcat({"a",{" nice "}," and ", {{" long "},{" list "}}}))
string.formatNumberThousands (num)
返回数字的千位符号格式

Parameters:

  • num number
    数字

Returns:

  • string,千位符号的数字字符串

Usage:

  • loca s = string.formatNumberThousands(1000) ,s = "1,000"
string.split (str, delimiter)
按照指定分隔符分割字符串

Parameters:

Returns:

  • 分割后的字符串列表

Usage:

  • "123,456,789":split(',') -> {'123','456','789'}
io.exists (path)
判断文件是否存在

Parameters:

  • path string
    文件全名例如:"/ldata/call.mp3"

Returns:

  • boole,存在为true,不存在为false

Usage:

  • local ex = io.exists("/ldata/call.mp3")
io.readFile (path)
读取文件并返回文件的内容

Parameters:

  • path string
    文件全名例如:"/ldata/call.txt"

Returns:

  • string,文件的内容,文件不存在返回nil

Usage:

  • local c = io.readFile("/ldata/call.txt")
io.writeFile (path, content, mode)
写入文件指定的内容,默认为覆盖二进制模式

Parameters:

  • path string
    文件全名例如:"/ldata/call.txt"
  • content string
    文件内容
  • mode string
    文件写入模式默认"w+b"

Returns:

  • string,文件的内容

Usage:

  • local c = io.writeFile("/ldata/call.txt","test")
io.pathInfo (path)
将文件路径分解为table信息

Parameters:

  • path string
    文件路径全名例如:"/ldata/call.txt"

Returns:

  • table,{dirname="/ldata/",filename="call.txt",basename="call",extname=".txt"}

Usage:

  • loca p = io.pathInfo("/ldata/call.txt")
io.fileSize (path)
返回文件大小

Parameters:

  • path string
    文件路径全名例如:"/ldata/call.txt","test"

Returns:

  • number ,文件大小

Usage:

  • locan cnt = io.fileSize("/ldata/call.txt")
io.readStream (path, offset, len)
返回指定位置读取的字符串

Parameters:

  • path string
    文件路径全名例如:"/ldata/call.txt"
  • offset number
    要读取的指定位置
  • len number
    要读取的字节数

Returns:

  • string,返回要读取的数据,读取失败返回nil
generated by LDoc 1.4.6 Last updated 2020-04-11 23:40:56