From 6d7e09bf1801c2fe183df17cc67017a6d3d8513b Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 2 Sep 2024 23:07:08 -0400 Subject: Add Text:split() and use that with an empty pattern instead of Text:clusters() --- builtins/text.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'builtins/text.c') diff --git a/builtins/text.c b/builtins/text.c index ff1f5ba2..a1a98a00 100644 --- a/builtins/text.c +++ b/builtins/text.c @@ -1426,6 +1426,29 @@ public Text_t Text$replace(Text_t text, Text_t pattern, Text_t replacement) return ret; } +public array_t Text$split(Text_t text, Text_t pattern) +{ + if (pattern.length == 0) // special case + return Text$clusters(text); + + array_t chunks = {}; + + Int_t i = I_small(1); + for (;;) { + int64_t len; + Int_t found = Text$find(text, pattern, i, &len); + if (I_is_zero(found)) break; + Text_t chunk = Text$slice(text, i, Int$minus(found, I_small(1))); + Array$insert(&chunks, &chunk, I_small(0), sizeof(Text_t)); + i = Int$plus(found, Int64_to_Int(len)); + } + + Text_t last_chunk = Text$slice(text, i, Int64_to_Int(text.length)); + Array$insert(&chunks, &last_chunk, I_small(0), sizeof(Text_t)); + + return chunks; +} + public Text_t Text$format(const char *fmt, ...) { va_list args; -- cgit v1.2.3