From 53bb6915dbdd2c62f62870462b0516010d5f7bdf Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Wed, 28 Sep 2022 21:31:39 +0200 Subject: [PATCH] Add text.i(chars|bytes) --- text.lua | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/text.lua b/text.lua index 65bace7..f81f65b 100644 --- a/text.lua +++ b/text.lua @@ -123,5 +123,27 @@ function is_identifier(text) return (not keywords[text]) and text:match"^[A-Za-z_][A-Za-z%d_]*$" end +local function inextchar(text, i) + if i >= #text then return end + i = i + 1 + return i, text:sub(i, i) +end + +function ichars(text, start) + -- Iterator over `index, character` + return inextchar, text, (start or 1) - 1 +end + +local function inextbyte(text, i) + if i >= #text then return end + i = i + 1 + return i, text:byte(i, i) +end + +function ibytes(text, start) + -- Iterator over `index, byte` + return inextbyte, text, (start or 1) - 1 +end + -- Export environment return _ENV