Rewrited split_lines to avoid a string.split bug if first line empty

This commit is contained in:
Pierre-Yves Rollo 2018-11-01 12:25:47 +01:00
parent 95c9da849d
commit 06d35ec9bf
1 changed files with 10 additions and 10 deletions

View File

@ -55,17 +55,17 @@ local function char_to_codepoint(str)
end
-- Split multiline text into array of lines, with <maxlines> maximum lines.
-- Can not use minetest string.split as it has bug if first line(s) empty
local function split_lines(text, maxlines)
local splits = text:split("\n")
if maxlines then
local lines = {}
for num = 1,maxlines do
lines[num] = splits[num]
end
return lines
else
return splits
end
local lines = {}
local pos = 1
repeat
local found = string.find(text, "\n", pos)
found = found or #text + 1
lines[#lines + 1] = string.sub(text, pos, found - 1)
pos = found + 1
until (maxlines and (#lines >= maxlines)) or (pos > (#text + 1))
return lines
end
--------------------------------------------------------------------------------