You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
4.8 KiB

  1. -- 处理UTF8中文,显示长度一个中文等于两个英文,编码长度一个中文等于2,3,4个英文,以英文单位计算长度
  2. local function _analysisUTF8(str)
  3. local width = 0
  4. local i = 1
  5. local UTF8Map = {}
  6. while (i <= #str) do
  7. local curByte = string.byte(str, i)
  8. local byteCount = 1
  9. if curByte >= 240 then
  10. byteCount = 4
  11. elseif curByte > 225 then
  12. byteCount = 3
  13. elseif curByte > 192 then
  14. byteCount = 2
  15. end
  16. local char = string.sub(str, i, i+byteCount-1)
  17. if byteCount == 4 then
  18. char = "□"
  19. end
  20. table.insert(UTF8Map, char) --看看这个字是什么
  21. i = i + byteCount -- 重置下一字节的索引
  22. if byteCount == 1 then
  23. width = width + 1 -- 字符的个数(长度)
  24. else
  25. width = width + 2
  26. end
  27. end
  28. return width, UTF8Map
  29. end
  30. local function filter_spec_chars(s)
  31. local ss = {}
  32. local k = 1
  33. while true do
  34. if k > #s then break end
  35. local c = string.byte(s,k)
  36. if not c then break end
  37. if c<192 then
  38. --if (c>=48 and c<=57) or (c>= 65 and c<=90) or (c>=97 and c<=122) then
  39. table.insert(ss, string.char(c))
  40. --end
  41. k = k + 1
  42. elseif c<224 then
  43. k = k + 2
  44. table.insert(ss, string.char(63))
  45. elseif c<240 then
  46. if c>=228 and c<=233 then
  47. local c1 = string.byte(s,k+1)
  48. local c2 = string.byte(s,k+2)
  49. if c1 and c2 then
  50. local a1,a2,a3,a4 = 128,191,128,191
  51. if c == 228 then a1 = 184
  52. elseif c == 233 then a2,a4 = 190,c1 ~= 190 and 191 or 165
  53. end
  54. if c1>=a1 and c1<=a2 and c2>=a3 and c2<=a4 then
  55. table.insert(ss, string.char(c,c1,c2))
  56. end
  57. end
  58. end
  59. k = k + 3
  60. elseif c<248 then
  61. k = k + 4
  62. table.insert(ss, string.char(63))
  63. elseif c<252 then
  64. k = k + 5
  65. table.insert(ss, string.char(63))
  66. elseif c<254 then
  67. k = k + 6
  68. table.insert(ss, string.char(63))
  69. end
  70. end
  71. --return 4,table.concat(ss)
  72. return 4,ss
  73. end
  74. -- 获取当前字符展示宽度
  75. function string.getUTF8Width(str)
  76. local width, _ = _analysisUTF8(str)
  77. return width
  78. end
  79. -- 按照关键词把string转化为table
  80. function string.toTableByString(str, strKey)
  81. local strTable = {}
  82. str = tostring(str)
  83. function test(str)
  84. if str == "" then return end
  85. local str1, str2 = "", ""
  86. local strLen = string.len(str)
  87. local begNum, endNum = string.find(str, strKey)
  88. if type(begNum) ~= "number" or type(endNum) ~= "number" then
  89. str1 = str
  90. else
  91. if type(begNum) == "number" and begNum > 1 then
  92. str1 = string.sub(str, 1, begNum - 1)
  93. end
  94. if type(endNum) == "number" and endNum < strLen then
  95. str2 = string.sub(str, endNum + 1, strLen)
  96. end
  97. end
  98. table.insert(strTable, str1)
  99. test(str2)
  100. end
  101. test(str)
  102. return strTable
  103. end
  104. function string.getShortName(str,size)
  105. local width, UTF8Map = _analysisUTF8(str)
  106. local num,shortName = 0,""
  107. local max = #UTF8Map--size < max and
  108. if type(UTF8Map) == "table" and next(UTF8Map) then
  109. for _,v in ipairs(UTF8Map) do
  110. if num < size then
  111. if v then
  112. shortName = shortName .. v
  113. num = num + 1
  114. end
  115. end
  116. end
  117. if max > size then
  118. shortName = shortName.."..."
  119. end
  120. return shortName
  121. else
  122. for _,v in ipairs(UTF8Map) do
  123. if num < size then
  124. if v then
  125. shortName = shortName .. v
  126. num = num + 1
  127. end
  128. end
  129. end
  130. return shortName
  131. end
  132. end
  133. --另一种方式过滤
  134. function string.getShortName2(str,size)
  135. local width, UTF8Map = filter_spec_chars(str)
  136. local num,shortName = 0,""
  137. local max = #UTF8Map
  138. if size < max and type(UTF8Map) == "table" and next(UTF8Map) then
  139. for _,v in ipairs(UTF8Map) do
  140. if num < size then
  141. if v then
  142. shortName = shortName .. v
  143. num = num + 1
  144. end
  145. end
  146. end
  147. if max > size then
  148. shortName = shortName.."..."
  149. end
  150. return shortName
  151. else
  152. for _,v in ipairs(UTF8Map) do
  153. if num < size then
  154. if v then
  155. shortName = shortName .. v
  156. num = num + 1
  157. end
  158. end
  159. end
  160. return shortName
  161. end
  162. end
  163. --[[
  164. 逆序截取字符串
  165. ]]
  166. function string.subStringReverse(str, k)
  167. local ts = string.reverse(str)
  168. local _,i = string.find(ts, k)
  169. local m = string.len(ts) - i + 1
  170. return string.sub(str, 1, m) --返回字符串str字符k之前的部分
  171. end