From 5d6fa135b1eadbceac04e5456fabb7e53feedc10 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 2 Sep 2024 23:26:55 -0400 Subject: Add Text:find_all() --- builtins/text.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'builtins/text.c') diff --git a/builtins/text.c b/builtins/text.c index 4ec3d835..2f3fbb46 100644 --- a/builtins/text.c +++ b/builtins/text.c @@ -1402,6 +1402,26 @@ public Text_t Text$quoted(Text_t text, bool colorize) #undef add_escaped } +public array_t Text$find_all(Text_t text, Text_t pattern) +{ + if (pattern.length == 0) // special case + return (array_t){.length=0}; + + array_t matches = {}; + + 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 match = Text$slice(text, found, Int$plus(found, Int64_to_Int(len-1))); + Array$insert(&matches, &match, I_small(0), sizeof(Text_t)); + i = Int$plus(found, Int64_to_Int(len)); + } + + return matches; +} + public Text_t Text$replace(Text_t text, Text_t pattern, Text_t replacement) { Text_t ret = {.length=0}; -- cgit v1.2.3