Skip to content

Commit b863cd7

Browse files
committed
Update encoderH.lua
Make encoding work with Lua 5.1 The hex escape codes in strings (like "\x7f") were added in Lua 5.2. But before this, decimal escape codes (like "\127") did exists. Replacing the hex escape codes with decimal ones makes hjson-lua work with Lua 5.1. Worth noting that \00 does not work in patterns. So this change uses the %z character class to match character 0x00.
1 parent be5f82b commit b863cd7

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

hjson/encoderH.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,28 @@ local escape_char_map = {
99
["\t"] = "\\t"
1010
}
1111

12-
local COMMONRANGE = "\x7f-\x9f" -- // TODO: add unicode escape sequences
12+
local COMMONRANGE = "\127-\159" -- // TODO: add unicode escape sequences
1313

1414
local function containsSequences(s, sequences)
1515
for _, v in ipairs(sequences) do if s:find(v) then return true end end
1616
return false
1717
end
1818

1919
local function needsEscape(s)
20-
return containsSequences(s, {'[\\"\x00-\x1f' .. COMMONRANGE .. "]"})
20+
return containsSequences(s, {"%z", '[\\"\001-\031' .. COMMONRANGE .. "]"})
2121
end
2222

2323
local function needsQuotes(s)
2424
local sequences = {
2525
"^%s", '^"', "^'", "^#", "^/%*", "^//", "^{", "^}", "^%[", "^%]", "^:",
26-
"^,", "%s$", "[\x00-\x1f" .. COMMONRANGE .. "]"
26+
"^,", "%s$", "%z", "[\001-\031" .. COMMONRANGE .. "]"
2727
}
2828
return containsSequences(s, sequences)
2929
end
3030

3131
local function needsEscapeML(s)
3232
local sequences = {
33-
"'''", "^[\\s]+$", "[\x00-\x08\x0b\x0c\x0e-\x1f" .. COMMONRANGE .. "]"
33+
"'''", "^[\\s]+$", "%z", "[\01-\08\011\012\014-\031" .. COMMONRANGE .. "]"
3434
}
3535
return containsSequences(s, sequences)
3636
end

0 commit comments

Comments
 (0)