Advertisement
Guest User

Lua UTF8 Extension (using lua 5.3 utf8 module)

a guest
Jul 15th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.87 KB | None | 0 0
  1. local utf8 = require'utf8'
  2.  
  3. function string:len(str)
  4.     return self and utf8.len(self) or str and utf8.len(str)
  5. end
  6.  
  7. local oldsub = string.sub
  8.  
  9. function utf8.sub(self, a, b)
  10.     a = a and utf8.offset(self, a)
  11.     b = b and utf8.offset(self, b+1) - 1
  12.     return oldsub(self, a, b)
  13. end
  14.  
  15. function string:sub(str, a, b)
  16.     return self and utf8.sub(self, str, a) or utf8.sub(str, a, b)
  17. end
  18.  
  19. function utf8.reverse(str)
  20.     local t = {}
  21.     for _, c in utf8.codes(str) do table.insert(t, 1, utf8.char(c)) end
  22.     return table.concat(t)
  23. end
  24.  
  25. function string:reverse(str)
  26.     return self and utf8.reverse(self) or str and utf8.reverse(str)
  27. end
  28.  
  29. local utf8map = {
  30.     enaZ    = {upper = {0x0041, 0x005A}, lower = {0x0061, 0x007A}, shift = 0x20},
  31.     enàŽ  = {upper = {0x00C0, 0x00DF}, lower = {0x00E0, 0x00FF}, shift = 0x20},
  32.     enaZ  = {upper = {0xFF21, 0xFF3A}, lower = {0xFF41, 0xFF5A}, shift = 0x20},
  33.     ru    = {upper = {0x0410, 0x042F}, lower = {0x0430, 0x0409}, shift = 0x20},
  34.     ruѐЏ  = {upper = {0x0400, 0x040F}, lower = {0x0450, 0x045F}, shift = 0x50},
  35. }
  36.  
  37.  
  38. local function getUpper(s)
  39.     for k, v in pairs(utf8map) do
  40.         if s >= v.lower[1] and s <= v.lower[2] then
  41.             return utf8.char(s - v.shift)
  42.         end
  43.     end
  44.     return utf8.char(s)
  45. end
  46.  
  47. function utf8.upper(s)
  48.     local str = {}
  49.     for _, c in utf8.codes(s) do
  50.         table.insert(str, getUpper(c))
  51.     end
  52.     return table.concat(str)
  53. end
  54.  
  55. function string:upper(str)
  56.     return self and utf8.upper(self) or str and utf8.upper(str)
  57. end
  58.  
  59.  
  60. local function getLower(s)
  61.     for k, v in pairs(utf8map) do
  62.         if s >= v.upper[1] and s <= v.upper[2] then
  63.             return utf8.char(s + v.shift)
  64.         end
  65.     end
  66.     return utf8.char(s)
  67. end
  68.  
  69. function utf8.lower(s)
  70.     local str = {}
  71.     for _, c in utf8.codes(s) do
  72.         table.insert(str, getLower(c))
  73.     end
  74.     return table.concat(str)
  75. end
  76.  
  77. function string:lower(str)
  78.     return self and utf8.lower(self) or str and utf8.lower(str)
  79. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement