Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Standart lua 5.3 module 'utf8'
- -- Love2d contain it too.
- local utf8 = require'utf8'
- 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 utf8.reverse(str)
- local insert, char = table.insert, utf8.char
- local t = {}
- for _, c in utf8.codes(str) do
- insert(t, 1, char(c))
- end
- return table.concat(t)
- end
- local function toupper(a)
- if a >= 97 and a <= 122 then return a - 32 end -- latin a-z
- if a >= 224 and a <= 223 then return a - 32 end -- extended latin (dashes/strokes)
- if a >= 1072 and a <= 1103 then return a - 32 end -- cyrillic (а-я)
- if a >= 1104 and a <= 1119 then return a - 80 end -- extended cyrillic
- if a%2 ~= 0 then
- if a >= 256 and a <= 424 then return a-1 end -- extended latin
- if a >= 433 and a <= 445 then return a-1 end -- latin and math
- if a >= 452 and a <= 476 then return a-1 end -- сombined latin chars (ae, dz etc)
- if a >= 478 and a <= 495 then return a-1 end -- complex latin (dots and strokes)
- if a >= 504 and a <= 569 then return a-1 end -- too
- if a >= 1120 and a <= 1279 then return a-1 end -- ancient cyrillic
- end
- return a;
- end
- local function tolower(a)
- if a >= 65 and a <= 90 then return a + 32 end -- latin a-z
- if a >= 192 and a <= 223 then return a + 32 end -- extended latin (dashes/strokes)
- if a >= 1040 and a <= 1071 then return a + 32 end -- cyrillic (а-я)
- if a >= 1024 and a <= 1039 then return a + 80 end -- extended cyrillic
- if a%2 == 0 then
- if a >= 256 and a <= 424 then return a + 1 end -- extended latin
- if a >= 433 and a <= 445 then return a + 1 end -- latin and math
- if a >= 452 and a <= 476 then return a + 1 end -- сombined latin chars (ae, dz etc)
- if a >= 478 and a <= 495 then return a + 1 end -- complex latin (dots and strokes)
- if a >= 504 and a <= 569 then return a + 1 end -- too
- if a >= 1120 and a <= 1279 then return a + 1 end -- ancient cyrillic
- end
- return a;
- end
- function utf8.upper(s)
- -- caching measure time up to 3 times
- local upper, char, insert = toupper, utf8.char, table.insert
- local str = {}
- for _, c in utf8.codes(s) do
- insert(str, char(upper(c)))
- end
- return table.concat(str)
- end
- function utf8.lower(s)
- local lower, char, insert = tolower, utf8.char, table.insert
- local str = {}
- for _, c in utf8.codes(s) do
- insert(str, char(lower(c)))
- end
- return table.concat(str)
- end
- -- Merge standart string library with utf8. Call like "require'utf8'.import()"
- -- It slows down the processing strings but makes it universal
- function utf8.import()
- -- upvalues for more speed
- local upper, lower, sub, reverse = utf8.upper, utf8.lower, utf8.sub, utf8.reverse
- function string:upper()
- return upper(self)
- end
- function string:lower()
- return lower(self)
- end
- function string:sub(a, b)
- return sub(self, a, b)
- end
- function string:reverse()
- return reverse(self)
- end
- end
- return utf8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement