Implemented format_concatted_string
.
This commit is contained in:
parent
f3cddec4d0
commit
a8cbe42a05
62
pstring.lua
62
pstring.lua
|
@ -12,10 +12,11 @@ TODO
|
|||
-- Constants
|
||||
|
||||
local NR_CHARS_IN_LONG_STRING = 40
|
||||
local MAX_HORIZONAL_CHARACTER = 80
|
||||
local SHORT_STR_DELIMITER = '\''
|
||||
local STRING_CONT_INDICATOR = '...'
|
||||
|
||||
|
||||
--------
|
||||
|
||||
local CHAR_TO_STR_REPR = {}
|
||||
do
|
||||
|
@ -30,6 +31,8 @@ do
|
|||
CHAR_TO_STR_REPR[13] = '\\r'
|
||||
CHAR_TO_STR_REPR[92] = '\\\\'
|
||||
CHAR_TO_STR_REPR[127] = '\\127'
|
||||
|
||||
CHAR_TO_STR_REPR[SHORT_STR_DELIMITER:byte()] = '\\'..SHORT_STR_DELIMITER
|
||||
end
|
||||
|
||||
local CHARACTERS_THAT_REQUIRE_ESCAPE_SEQ = '[%z\001-\008\011-\031\127]'
|
||||
|
@ -77,22 +80,35 @@ end
|
|||
|
||||
local function format_shortform_string (str, depth, l)
|
||||
l[#l+1] = SHORT_STR_DELIMITER
|
||||
l[#l+1] = escape_string(str):gsub(SHORT_STR_DELIMITER, '\\'..SHORT_STR_DELIMITER)
|
||||
l[#l+1] = escape_string(str)
|
||||
l[#l+1] = SHORT_STR_DELIMITER
|
||||
end
|
||||
|
||||
local function format_cut_string (str, depth, l)
|
||||
-- Calculate string
|
||||
local str = escape_string(str)
|
||||
:gsub(SHORT_STR_DELIMITER, '\\'..SHORT_STR_DELIMITER)
|
||||
:sub(1, NR_CHARS_IN_LONG_STRING - #STRING_CONT_INDICATOR)
|
||||
local function safe_cut (str, si, ei)
|
||||
|
||||
-- Error checking
|
||||
assert(type(str) == 'string')
|
||||
assert(type(si) == 'number' or si == nil)
|
||||
assert(type(ei) == 'number' or ei == nil)
|
||||
|
||||
-- Calculate
|
||||
local cut_str = str:sub(si, ei)
|
||||
|
||||
-- Search for the number of backslashes just before the send of the string.
|
||||
-- If that number is even, it's a sequence of backslashes, if not it's a
|
||||
-- broken escape string.
|
||||
local start_of_backslashes, start_of_digits = str:match '()\\*()%d?%d?$'
|
||||
local start_of_backslashes, start_of_digits = cut_str:match '()\\*()%d?%d?$'
|
||||
local nr_backslashes_before_end = start_of_digits - start_of_backslashes
|
||||
if nr_backslashes_before_end % 2 == 1 then str = str:sub(1, start_of_backslashes - 1) end
|
||||
if nr_backslashes_before_end % 2 == 1 then cut_str = cut_str:sub(1, start_of_backslashes - 1) end
|
||||
|
||||
return cut_str
|
||||
end
|
||||
|
||||
|
||||
local function format_cut_string (str, depth, l)
|
||||
-- Calculate string
|
||||
local str = escape_string(str)
|
||||
str = safe_cut(str, 1, NR_CHARS_IN_LONG_STRING - #STRING_CONT_INDICATOR)
|
||||
|
||||
-- Format
|
||||
l[#l+1] = SHORT_STR_DELIMITER
|
||||
|
@ -102,7 +118,33 @@ local function format_cut_string (str, depth, l)
|
|||
end
|
||||
|
||||
local function format_concatted_string (str, depth, l)
|
||||
error '[pretty.string/internal]: format_concatted_string not implemented yet!'
|
||||
-- Cuts the string up into smaller individual substrings, each Concatted
|
||||
-- together. Is uglier compared to longform, but is at least idempotent.
|
||||
|
||||
-- Error checking
|
||||
assert( type(str) == 'string' )
|
||||
assert(type(depth) == 'number' and type(l) == 'table')
|
||||
|
||||
-- Calculate
|
||||
local width_without_overhead = MAX_HORIZONAL_CHARACTER - 2*#SHORT_STR_DELIMITER - #' ..'
|
||||
local str = escape_string(str)
|
||||
|
||||
-- Cut strings
|
||||
local sub_strings, str_i = {}, 1
|
||||
repeat
|
||||
local sub_str = safe_cut(str, str_i, str_i + width_without_overhead - 1)
|
||||
str_i = str_i + #sub_str
|
||||
sub_strings[#sub_strings+1] = sub_str
|
||||
until str_i >= #str
|
||||
|
||||
-- Format them
|
||||
for _, sub_str in ipairs(sub_strings) do
|
||||
l[#l+1] = SHORT_STR_DELIMITER
|
||||
l[#l+1] = sub_str
|
||||
l[#l+1] = SHORT_STR_DELIMITER
|
||||
l[#l+1] = ' ..\n'
|
||||
end
|
||||
l[#l] = ''
|
||||
end
|
||||
|
||||
local function format_longform_string (str, depth, l)
|
||||
|
|
|
@ -134,7 +134,17 @@ format_test {
|
|||
--------------------------------------------------------------------------------
|
||||
-- Concatted Strings
|
||||
|
||||
-- TODO
|
||||
format_test {
|
||||
name = 'Concatted string basics',
|
||||
input = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc vestibulum tempus ligula. Sed \004\002\000 ac lobortis mi.',
|
||||
expect = [['Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc vestibulum te' ..]]..'\n'..[['mpus ligula. Sed \004\002\000 ac lobortis mi.']],
|
||||
}
|
||||
|
||||
format_test {
|
||||
name = 'Concatted string basics 2',
|
||||
input = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.\004\002\000Nunc vestibulum tempus ligula. Sed ac lobortis mi.',
|
||||
expect = [['Lorem ipsum dolor sit amet, consectetur adipiscing elit.\004\002\000Nunc ve' ..]]..'\n'..[['stibulum tempus ligula. Sed ac lobortis mi.']],
|
||||
}
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
|
|
@ -4,6 +4,6 @@ package.path = package.path .. ';./test/?.lua;./src/?.lua'
|
|||
local TEST_SUITE = require("TestSuite").new('pretty')
|
||||
TEST_SUITE:enableStrictGlobal()
|
||||
TEST_SUITE:addModules('test/test_*')
|
||||
TEST_SUITE:addModuleTests 'pretty'
|
||||
--TEST_SUITE:addModuleTests 'pretty'
|
||||
TEST_SUITE:setOptions(...)
|
||||
TEST_SUITE:runTests()
|
||||
|
|
Loading…
Reference in New Issue
Block a user