1
0

Removed some debug prints, and improved code structure

This commit is contained in:
Jon Michael Aanes 2017-07-22 13:53:15 +02:00
parent 6f7e767b68
commit 6021ff8ec6

View File

@ -40,33 +40,37 @@ local CHARACTERS_THAT_REQUIRE_ESCAPE_SEQ = '[%z\001-\008\011-\031\127]'
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- Util -- Util
local function requires_weird_escape_seq (str) local function does_string_require_escaping (str)
return not not str:find(CHARACTERS_THAT_REQUIRE_ESCAPE_SEQ) return not not str:find(CHARACTERS_THAT_REQUIRE_ESCAPE_SEQ)
end end
local function escape_string (str) local escape_string do
-- Attempts to escape the string, to a format that is both a valid Lua
-- constant, and ledible unicode.
-- TODO: Escape invalid unicode sequences. local ESCAPE_SINGLE_BYTE = function (char) return CHAR_TO_STR_REPR[char:byte()] end
local ESCAPE_MALFORMED_CONT_BYTE = function (a, b) return a..'\\' .. b:byte() end
local ESCAPE_MALFORMED_START_BYTE = function (a, b) return '\\'..a:byte() .. b end
-- Error checking function escape_string (str)
assert(type(str) == 'string') -- Attempts to escape the string, to a format that is both a valid Lua
-- constant, and ledible unicode.
-- First escape the easy ones. -- Error checking
local str = str:gsub('.', function (char) return CHAR_TO_STR_REPR[char:byte()] end) assert(type(str) == 'string')
-- Escape malformed continuation characters
repeat -- Escape single bytes
local count local str, count = str:gsub('.', ESCAPE_SINGLE_BYTE), 0
str, count = str:gsub('([^\128-\255])([\128-\191])', function(a, b) print(a,b) return a..'\\' .. b:byte() end)
until count == 0 -- Escape malformed continuation bytes
-- Escape malformed start characters repeat str, count = str:gsub('([^\128-\255])([\128-\191])', ESCAPE_MALFORMED_CONT_BYTE)
repeat until count == 0
local count
str, count = str:gsub('([\191-\255])([^\128-\191])', function(a, b) print(a,b) return '\\'..a:byte() .. b end) -- Escape malformed start bytes
until count == 0 repeat str, count = str:gsub('([\191-\255])([^\128-\191])', ESCAPE_MALFORMED_START_BYTE)
-- return until count == 0
return str
-- Done, lets return
return str
end
end end
local function smallest_secure_longform_string_level (str) local function smallest_secure_longform_string_level (str)
@ -189,7 +193,7 @@ return function (str, depth, l)
return format_shortform_string(str, depth, l) return format_shortform_string(str, depth, l)
elseif depth > 0 then elseif depth > 0 then
return format_cut_string (str, depth, l) return format_cut_string (str, depth, l)
elseif requires_weird_escape_seq (str) then elseif does_string_require_escaping (str) then
return format_concatted_string(str, depth, l) return format_concatted_string(str, depth, l)
else else
return format_longform_string(str, depth, l) return format_longform_string(str, depth, l)