在go中使用lua示例, 基于gopher-lua!
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

118 行
2.6 KiB

4 年前
  1. local pattern = '[%z\1-\127\194-\244][\128-\191]*'
  2. -- helper function
  3. local posrelat =
  4. function (pos, len)
  5. if pos < 0 then
  6. pos = len + pos + 1
  7. end
  8. return pos
  9. end
  10. utf8 = {}
  11. -- maps f over s's utf8 characters f can accept args: (visual_index, utf8_character, byte_index)
  12. utf8.map =
  13. function (s, f, no_subs)
  14. local i = 0
  15. if no_subs then
  16. for b, e in s:gmatch('()' .. pattern .. '()') do
  17. i = i + 1
  18. local c = e - b
  19. f(i, c, b)
  20. end
  21. else
  22. for b, c in s:gmatch('()(' .. pattern .. ')') do
  23. i = i + 1
  24. f(i, c, b)
  25. end
  26. end
  27. end
  28. -- generator for the above -- to iterate over all utf8 chars
  29. utf8.chars =
  30. function (s, no_subs)
  31. return coroutine.wrap(function () return utf8.map(s, coroutine.yield, no_subs) end)
  32. end
  33. -- returns the number of characters in a UTF-8 string
  34. utf8.len =
  35. function (s)
  36. -- count the number of non-continuing bytes
  37. return select(2, s:gsub('[^\128-\193]', ''))
  38. end
  39. utf8.GetMaxLenString =
  40. function (s, maxlen, symbol)
  41. -- s:待裁剪字符串,maxlen:最大长度,symbol:裁剪后补全符号
  42. symbol = symbol or ""
  43. local len = utf8.len(s)
  44. local dstString = s
  45. -- 超长,裁剪,加symbol
  46. if len > maxlen then
  47. dstString = utf8.sub(s, 1, maxlen)
  48. dstString = dstString..symbol
  49. end
  50. return dstString
  51. end
  52. -- replace all utf8 chars with mapping
  53. utf8.replace =
  54. function (s, map)
  55. return s:gsub(pattern, map)
  56. end
  57. -- reverse a utf8 string
  58. utf8.reverse =
  59. function (s)
  60. -- reverse the individual greater-than-single-byte characters
  61. s = s:gsub(pattern, function (c) return #c > 1 and c:reverse() end)
  62. return s:reverse()
  63. end
  64. -- strip non-ascii characters from a utf8 string
  65. utf8.strip =
  66. function (s)
  67. return s:gsub(pattern, function (c) return #c > 1 and '' end)
  68. end
  69. -- like string.sub() but i, j are utf8 strings
  70. -- a utf8-safe string.sub()
  71. utf8.sub =
  72. function (s, i, j)
  73. local l = utf8.len(s)
  74. i = posrelat(i, l)
  75. j = j and posrelat(j, l) or l
  76. if i < 1 then i = 1 end
  77. if j > l then j = l end
  78. if i > j then return '' end
  79. local diff = j - i
  80. local iter = utf8.chars(s, true)
  81. -- advance up to i
  82. for _ = 1, i - 1 do iter() end
  83. local c, b = select(2, iter())
  84. -- i and j are the same, single-charaacter sub
  85. if diff == 0 then
  86. return string.sub(s, b, b + c - 1)
  87. end
  88. i = b
  89. -- advance up to j
  90. for _ = 1, diff - 1 do iter() end
  91. c, b = select(2, iter())
  92. return string.sub(s, i, b + c - 1)
  93. end