-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.luau
59 lines (49 loc) · 1.27 KB
/
init.luau
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
54
55
56
57
58
59
--!strict
local function from16(decimal)
return string.format("%X", decimal) -- 대문자 16진수
end
function from2(num: number): string
if num == 0 then return '0' end
local result = '';
while true do
if num < 1.1 then
break
end
result = `{num % 2}{result}`;
num = math.floor(num / 2);
end
return result
end
export type UTF16Impl = {
__index: UTF16Impl,
codepoint: (str: string) -> (number),
encode: (str: string) -> (string),
decode: (str: string) -> (string),
}
local utf16 = {} :: UTF16Impl
utf16.__index = utf16
function utf16.codepoint(str)
return utf8.codepoint(str);
end
function utf16.decode(str)
return utf8.char(tonumber(str, 16) :: number);
end
function utf16.encode(str)
local codePoint = utf8.codepoint(str);
local str16 = from16(codePoint);
local result = '';
if tonumber(str16, 16) <= 0xFFFF then
result = str16;
for i=1, 4 - #str16 do
result = `0{result}`;
end
else
local str2 = from2(codePoint);
for i=1, 20 - #str2 do
str2 = `0{str2}`;
end
result = from16(tonumber(`110110{str2:sub(1, 10)}110111{str2:sub(11)}`, 2) :: number);
end
return result;
end
return utf16