Created
October 22, 2020 07:54
lua UTF16 BE lib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local utf16 = {} | |
-- Big Endian | |
function utf16.toutf8(s) | |
local surrogate | |
return (s:gsub("..", function(utf16) | |
local cp = string.unpack(">H", utf16) | |
if (cp & 0xFC00) == 0xD800 then | |
surrogate = cp | |
return "" | |
else | |
if surrogate then | |
cp = ((surrogate - 0xD800) << 10) + (cp - 0xDC00) + 0x10000 | |
surrogate = nil | |
end | |
return utf8.char(cp) | |
end | |
end)) | |
end | |
function utf16.fromutf8(s) | |
return (s:gsub(utf8.charpattern, function (u8) | |
local cp = utf8.codepoint(u8) | |
if cp <= 0xffff then | |
return string.pack(">H", cp) | |
else | |
cp = cp - 0x10000 | |
local surrogate = (cp >> 10) + 0xD800 | |
local lo = (cp & 0x3ff) + 0xDC00 | |
return string.pack(">HH", surrogate, lo) | |
end | |
end)) | |
end | |
--local s = utf16.fromutf8("😀") | |
--assert(utf16.toutf8(s) == "😀") | |
return utf16 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment