protect against 0 width in string:wrap()

develop
myk002 2022-04-29 11:29:00 -07:00
parent b9c36c1e63
commit af47434f52
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
3 changed files with 5 additions and 1 deletions

@ -439,6 +439,7 @@ end
-- multiple lines. If width is not specified, 72 is used. -- multiple lines. If width is not specified, 72 is used.
function string:wrap(width) function string:wrap(width)
width = width or 72 width = width or 72
if width <= 0 then error('expected width > 0; got: '..tostring(width)) end
local wrapped_text = {} local wrapped_text = {}
for line in self:gmatch('[^\n]*') do for line in self:gmatch('[^\n]*') do
local line_start_pos = 1 local line_start_pos = 1

@ -558,7 +558,8 @@ WrappedLabel.ATTRS{
} }
function WrappedLabel:getWrappedText(width) function WrappedLabel:getWrappedText(width)
if not self.text_to_wrap then return nil end -- 0 width can happen if the parent has 0 width
if not self.text_to_wrap or width <= 0 then return nil end
local text_to_wrap = getval(self.text_to_wrap) local text_to_wrap = getval(self.text_to_wrap)
if type(text_to_wrap) == 'table' then if type(text_to_wrap) == 'table' then
text_to_wrap = table.concat(text_to_wrap, NEWLINE) text_to_wrap = table.concat(text_to_wrap, NEWLINE)

@ -67,6 +67,8 @@ function test.wrap()
expect.eq('hel\nlo\nwor\nld', ('hello world'):wrap(3)) expect.eq('hel\nlo\nwor\nld', ('hello world'):wrap(3))
expect.eq('hel\nloo\nwor\nldo', ('helloo worldo'):wrap(3)) expect.eq('hel\nloo\nwor\nldo', ('helloo worldo'):wrap(3))
expect.eq('', (''):wrap()) expect.eq('', (''):wrap())
expect.error_match('expected width > 0', function() ('somestr'):wrap(0) end)
end end
function test.escape_pattern() function test.escape_pattern()