-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstring.utf8.lua
More file actions
53 lines (46 loc) · 1013 Bytes
/
string.utf8.lua
File metadata and controls
53 lines (46 loc) · 1013 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
--[[
name: string.utf8.lua
description: Utf8 string handler - credits to Bolodefchoco#0000
]]--
do
-- Based on Luvit's ustring
local charLength = function(byte)
if bit32.rshift(byte, 7) == 0x00 then
return 1
elseif bit32.rshift(byte, 5) == 0x06 then
return 2
elseif bit32.rshift(byte, 4) == 0x0E then
return 3
elseif bit32.rshift(byte, 3) == 0x1E then
return 4
end
return 0
end
local sub, byte = string.sub, string.byte
string.utf8 = function(str)
local utf8str = { }
local index, append = 1, 0
local charLen
for i = 1, #str do
repeat
local char = sub(str, i, i)
local byte = byte(char)
if append ~= 0 then
utf8str[index] = utf8str[index] .. char
append = append - 1
if append == 0 then
index = index + 1
end
break
end
charLen = charLength(byte)
utf8str[index] = char
if charLen == 1 then
index = index + 1
end
append = append + charLen - 1
until true
end
return utf8str
end
end