Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local utf8 = require'utf8'
- function string:len(str)
- return self and utf8.len(self) or str and utf8.len(str)
- end
- local oldsub = string.sub
- function utf8.sub(self, a, b)
- a = a and utf8.offset(self, a)
- b = b and utf8.offset(self, b+1) - 1
- return oldsub(self, a, b)
- end
- function string:sub(str, a, b)
- return self and utf8.sub(self, str, a) or utf8.sub(str, a, b)
- end
- function utf8.reverse(str)
- local t = {}
- for _, c in utf8.codes(str) do table.insert(t, 1, utf8.char(c)) end
- return table.concat(t)
- end
- function string:reverse(str)
- return self and utf8.reverse(self) or str and utf8.reverse(str)
- end
- local utf8map = {
- enaZ = {upper = {0x0041, 0x005A}, lower = {0x0061, 0x007A}, shift = 0x20},
- enàŽ = {upper = {0x00C0, 0x00DF}, lower = {0x00E0, 0x00FF}, shift = 0x20},
- enaZ = {upper = {0xFF21, 0xFF3A}, lower = {0xFF41, 0xFF5A}, shift = 0x20},
- ru = {upper = {0x0410, 0x042F}, lower = {0x0430, 0x0409}, shift = 0x20},
- ruѐЏ = {upper = {0x0400, 0x040F}, lower = {0x0450, 0x045F}, shift = 0x50},
- }
- local function getUpper(s)
- for k, v in pairs(utf8map) do
- if s >= v.lower[1] and s <= v.lower[2] then
- return utf8.char(s - v.shift)
- end
- end
- return utf8.char(s)
- end
- function utf8.upper(s)
- local str = {}
- for _, c in utf8.codes(s) do
- table.insert(str, getUpper(c))
- end
- return table.concat(str)
- end
- function string:upper(str)
- return self and utf8.upper(self) or str and utf8.upper(str)
- end
- local function getLower(s)
- for k, v in pairs(utf8map) do
- if s >= v.upper[1] and s <= v.upper[2] then
- return utf8.char(s + v.shift)
- end
- end
- return utf8.char(s)
- end
- function utf8.lower(s)
- local str = {}
- for _, c in utf8.codes(s) do
- table.insert(str, getLower(c))
- end
- return table.concat(str)
- end
- function string:lower(str)
- return self and utf8.lower(self) or str and utf8.lower(str)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement