summaryrefslogtreecommitdiff
path: root/script/encoder/utf16be.lua
blob: 5fc19b2cb5ced667e194c1e1f36dbc083fab011f (plain)
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
local function tochar(code)
    return string.char((code >> 8) & 0xFF, code & 0xFF)
end

local function tobyte(s, i)
    local h, l = string.byte(s, i, i+1)
    return (h << 8) | l
end

local function char(code)
    if code <= 0xFFFF then
        return tochar(code)
    end
    code = code - 0x10000
    return tochar(0xD800 + (code >> 10))..tochar(0xDC00 + (code & 0x3FF))
end

local m = {}

function m.encode(s)
    local r = {}
    for _, c in utf8.codes(s, true) do
        r[#r+1] = char(c)
    end
    return table.concat(r)
end

function m.decode(s)
    local r = {}
    local i = 1
    while i < #s do
        local code1 = tobyte(s, i)
        if code1 >= 0xD800 and code1 < 0xE000 then
            i = i + 2
            local code2 = tobyte(s, i)
            local code = 0x10000 + ((code1 - 0xD800) << 10) + ((code2 - 0xDC00) & 0x3FF)
            r[#r+1] = utf8.char(code)
        else
            r[#r+1] = utf8.char(code1)
        end
        i = i + 2
    end
    return table.concat(r)
end

return m