utils.lua 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. --- 常用工具类接口
  2. -- @module utils
  3. -- @author 小强,稀饭放姜
  4. -- @license MIT
  5. -- @copyright openLuat.com
  6. -- @release 2017.10.19
  7. module(..., package.seeall)
  8. --- 字符转16进制,如"123abc"转为"313233616263"
  9. -- @param str 输入字符串
  10. -- @return hexstring 16进制组成的串
  11. -- @return len 输入的字符串长度
  12. -- @usage
  13. -- string.tohex("\1\2\3") -> "010203" 3
  14. -- string.tohex("123abc") -> "313233616263" 6
  15. function string.tohex(str)
  16. return str:gsub('.', function(c)
  17. return string.format("%02X", string.byte(c))
  18. end)
  19. end
  20. --- 16进制转字符,如"313233616263"转为"123abc", 函数里加入了过滤分隔符,可以过滤掉大部分分隔符(可参见正则表达式中\s和\p的范围)。
  21. -- @string hex,16进制组成的串
  22. -- @return charstring,字符组成的串
  23. -- @return len,输出字符串的长度
  24. -- @usage
  25. -- string.fromhex("010203") -> "\1\2\3"
  26. -- string.fromhex("313233616263:) -> "123abc"
  27. function string.fromhex(hex)
  28. --滤掉分隔符
  29. local hex = hex:gsub("[%s%p]", ""):upper()
  30. return hex:gsub("%x%x", function(c)
  31. return string.char(tonumber(c, 16))
  32. end)
  33. end
  34. --- 返回utf8编码字符串的长度
  35. -- @string str,utf8编码的字符串,支持中文
  36. -- @return number,返回字符串长度
  37. -- @usage local cnt = string.utf8len("中国"),str = 2
  38. function string.utf8len(str)
  39. local len = #str
  40. local left = len
  41. local cnt = 0
  42. local arr = {0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc}
  43. while left ~= 0 do
  44. local tmp = string.byte(str, -left)
  45. local i = #arr
  46. while arr[i] do
  47. if tmp >= arr[i] then
  48. left = left - i
  49. break
  50. end
  51. i = i - 1
  52. end
  53. cnt = cnt + 1
  54. end
  55. return cnt
  56. end
  57. -- 将一个字符转为urlencode编码
  58. local function urlencodeChar(c)
  59. return "%" .. string.format("%02X", string.byte(c))
  60. end
  61. --- 返回字符串的urlencode编码
  62. -- @string str,要转换编码的字符串
  63. -- @return str,urlencode编码的字符串
  64. -- @usage string.urlencode("####133")
  65. function string.urlencode(str)
  66. return string.gsub(string.gsub(string.gsub(tostring(str), "\n", "\r\n"), "([^%w%.%- ])", urlencodeChar), " ", "+")
  67. end
  68. --- 返回数字的千位符号格式
  69. -- @number num,数字
  70. -- @return string,千位符号的数字字符串
  71. -- @usage loca s = string.formatNumberThousands(1000) ,s = "1,000"
  72. function string.formatNumberThousands(num)
  73. local k, formatted
  74. formatted = tostring(tonumber(num))
  75. while true do
  76. formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
  77. if k == 0 then break end
  78. end
  79. return formatted
  80. end
  81. --- 判断文件是否存在
  82. -- @string path,文件全名例如:"/ldata/call.mp3"
  83. -- @return boole,存在为true,不存在为false
  84. -- @usage local ex = io.exists("/ldata/call.mp3")
  85. function io.exists(path)
  86. local file = io.open(path, "r")
  87. if file then
  88. io.close(file)
  89. return true
  90. end
  91. return false
  92. end
  93. --- 读取文件并返回文件的内容
  94. -- @string path,文件全名例如:"/ldata/call.txt"
  95. -- @return string,文件的内容,文件不存在返回nil
  96. -- @usage local c = io.readfile("/ldata/call.txt")
  97. function io.readfile(path)
  98. local file = io.open(path, "r")
  99. if file then
  100. local content = file:read("*a")
  101. io.close(file)
  102. return content
  103. end
  104. return nil
  105. end
  106. --- 写入文件指定的内容,默认为覆盖二进制模式
  107. -- @string path,文件全名例如:"/ldata/call.txt"
  108. -- @string content,文件内容
  109. -- @string mode,文件写入模式默认"w+b"
  110. -- @return string,文件的内容
  111. -- @usage local c = io.writefile("/ldata/call.txt","test")
  112. function io.writefile(path, content, mode)
  113. local mode = mode or "w+b"
  114. local file = io.open(path, mode)
  115. if file then
  116. if file:write(content) == nil then return false end
  117. io.close(file)
  118. return true
  119. else
  120. return false
  121. end
  122. end
  123. --- 将文件路径分解为table信息
  124. -- @string path,文件路径全名例如:"/ldata/call.txt"
  125. -- @return table,{dirname="/ldata/",filename="call.txt",basename="call",extname=".txt"}
  126. -- @usage loca p = io.pathinfo("/ldata/call.txt")
  127. function io.pathinfo(path)
  128. local pos = string.len(path)
  129. local extpos = pos + 1
  130. while pos > 0 do
  131. local b = string.byte(path, pos)
  132. if b == 46 then -- 46 = char "."
  133. extpos = pos
  134. elseif b == 47 then -- 47 = char "/"
  135. break
  136. end
  137. pos = pos - 1
  138. end
  139. local dirname = string.sub(path, 1, pos)
  140. local filename = string.sub(path, pos + 1)
  141. extpos = extpos - pos
  142. local basename = string.sub(filename, 1, extpos - 1)
  143. local extname = string.sub(filename, extpos)
  144. return {
  145. dirname = dirname,
  146. filename = filename,
  147. basename = basename,
  148. extname = extname
  149. }
  150. end
  151. --- 返回文件大小
  152. -- @string path,文件路径全名例如:"/ldata/call.txt","test"
  153. -- @return number ,文件大小
  154. -- @usage locan cnt = io.filesize("/ldata/call.txt")
  155. function io.filesize(path)
  156. local size = 0
  157. local file = io.open(path, "r")
  158. if file then
  159. local current = file:seek()
  160. size = file:seek("end")
  161. file:seek("set", current)
  162. io.close(file)
  163. end
  164. return size
  165. end