Removed some debug prints, and improved code structure
This commit is contained in:
parent
6f7e767b68
commit
6021ff8ec6
36
pstring.lua
36
pstring.lua
|
@ -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
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
function escape_string (str)
|
||||||
-- Attempts to escape the string, to a format that is both a valid Lua
|
-- Attempts to escape the string, to a format that is both a valid Lua
|
||||||
-- constant, and ledible unicode.
|
-- constant, and ledible unicode.
|
||||||
|
|
||||||
-- TODO: Escape invalid unicode sequences.
|
|
||||||
|
|
||||||
-- Error checking
|
-- Error checking
|
||||||
assert(type(str) == 'string')
|
assert(type(str) == 'string')
|
||||||
|
|
||||||
-- First escape the easy ones.
|
-- Escape single bytes
|
||||||
local str = str:gsub('.', function (char) return CHAR_TO_STR_REPR[char:byte()] end)
|
local str, count = str:gsub('.', ESCAPE_SINGLE_BYTE), 0
|
||||||
-- Escape malformed continuation characters
|
|
||||||
repeat
|
-- Escape malformed continuation bytes
|
||||||
local count
|
repeat str, count = str:gsub('([^\128-\255])([\128-\191])', ESCAPE_MALFORMED_CONT_BYTE)
|
||||||
str, count = str:gsub('([^\128-\255])([\128-\191])', function(a, b) print(a,b) return a..'\\' .. b:byte() end)
|
|
||||||
until count == 0
|
until count == 0
|
||||||
-- Escape malformed start characters
|
|
||||||
repeat
|
-- Escape malformed start bytes
|
||||||
local count
|
repeat str, count = str:gsub('([\191-\255])([^\128-\191])', ESCAPE_MALFORMED_START_BYTE)
|
||||||
str, count = str:gsub('([\191-\255])([^\128-\191])', function(a, b) print(a,b) return '\\'..a:byte() .. b end)
|
|
||||||
until count == 0
|
until count == 0
|
||||||
-- return
|
|
||||||
|
-- Done, lets return
|
||||||
return str
|
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)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user