From a4118e04fe345a4d8d95cff52af00b7ca3e4f43a Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Thu, 29 Sep 2022 21:06:51 +0200 Subject: [PATCH] Fix text.lines & deprecate text.split_lines --- text.lua | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/text.lua b/text.lua index 667cd9e..3fe6226 100644 --- a/text.lua +++ b/text.lua @@ -91,9 +91,35 @@ end split_unlimited = split_without_limit -function split_lines(text, limit) return modlib.text.split(text, "\r?\n", limit, true) end +--! Does not support Macintosh pre-OSX CR-only line endings +--! Deprecated in favor of the `lines` iterator below +function split_lines(text, limit) + return modlib.text.split(text, "\r?\n", limit, true) +end + +-- When reading from a file, directly use `io.lines` instead +-- Lines are possibly empty substrings separated by CR, LF or CRLF +-- A trailing linefeed is ignored +function lines(str) + local line_start = 1 + -- Line iterator + return function() + if line_start > #str then + return + end + local linefeed_start, _, linefeed = str:find("([\r\n][\r\n]?)", line_start) + local line + if linefeed_start then + line = str:sub(line_start, linefeed_start - 1) + line_start = linefeed_start + (linefeed == "\r\n" and 2 or 1) + else + line = str:sub(line_start) + line_start = #str + 1 + end + return line + end +end -function lines(text) return text:gmatch"[^\r\n]*" end local zero = string.byte"0" local nine = string.byte"9"