summaryrefslogtreecommitdiff
path: root/script/utility.lua
diff options
context:
space:
mode:
Diffstat (limited to 'script/utility.lua')
-rw-r--r--script/utility.lua25
1 files changed, 22 insertions, 3 deletions
diff --git a/script/utility.lua b/script/utility.lua
index a1eec7eb..a98bef92 100644
--- a/script/utility.lua
+++ b/script/utility.lua
@@ -547,24 +547,43 @@ function m.tableMultiRemove(t, index)
end
end
-function m.eachLine(text)
+---遍历文本的每一行
+---@param text string
+---@param keepNL boolean # 保留换行符
+---@return fun(text:string):string
+function m.eachLine(text, keepNL)
local offset = 1
local lineCount = 0
+ local lastLine
return function ()
if offset > #text then
+ if not lastLine then
+ lastLine = ''
+ return ''
+ end
return nil
end
lineCount = lineCount + 1
local nl = text:find('[\r\n]', offset)
if not nl then
- local lastLine = text:sub(offset)
+ lastLine = text:sub(offset)
offset = #text + 1
return lastLine
end
- local line = text:sub(offset, nl - 1)
+ local line
if text:sub(nl, nl + 1) == '\r\n' then
+ if keepNL then
+ line = text:sub(offset, nl + 1)
+ else
+ line = text:sub(offset, nl - 1)
+ end
offset = nl + 2
else
+ if keepNL then
+ line = text:sub(offset, nl)
+ else
+ line = text:sub(offset, nl - 1)
+ end
offset = nl + 1
end
return line