Skip to content

Polyfill utf8.char #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions lua-parser/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,48 @@ opid: -- includes additional operators from Lua 5.3 and all relational operator
| 'and' | 'or' | 'unm' | 'len' | 'bnot' | 'not'
]]

local utf8_char = (utf8 or {
char = function(...)
local results = { ... }
local n = select("#", ...)

for i = 1, n do
local a = results[i]

if type(a) ~= "number" then
a = tonumber(a) or error("bad argument #" .. i .. " to 'char' (number expected, got " .. type(a) .. ")", 2)
end

if not (0 <= a) or a > 1114111 or a % 1 ~= 0 then
error("bad argument #" .. i .. " to 'char' (expected an integer in the range [0, 1114111], got " .. a .. ")", 2)
end

if a >= 128 then
local _1 = a % 64
local b = (a - _1) / 64

if a >= 2048 then
local _64 = b % 64
local c = (b - _64) / 64

if a >= 65536 then
local _4096 = c % 64
local d = (c - _4096) / 64
results[i] = string.char(d + 240, _4096 + 128, _64 + 128, _1 + 128)
else
results[i] = string.char(c + 224, _64 + 128, _1 + 128)
end
else
results[i] = string.char(b + 192, _1 + 128)
end
else
results[i] = string.char(a)
end
end
return table.concat(results, nil, 1, n)
end
}).char

local lpeg = require "lpeglabel"

lpeg.locale(lpeg)
Expand Down Expand Up @@ -416,10 +458,7 @@ local G = { V"Lua",
* expect(C(xdigit^1), "DigitUEsc") * Cc(16)
* expect("}", "CBraceUEsc")
/ tonumber
/ (utf8 and utf8.char or string.char) -- true max is \u{10FFFF}
-- utf8.char needs Lua 5.3
-- string.char works only until \u{FF}

/ utf8_char
+ throw("EscSeq")
);

Expand Down