aboutsummaryrefslogtreecommitdiff
path: root/builtins/text.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-09-02 23:26:55 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-09-02 23:26:55 -0400
commit5d6fa135b1eadbceac04e5456fabb7e53feedc10 (patch)
tree4d84f88a0a23446f30740e756b6052b1674355ae /builtins/text.c
parent9d2e9c02fe7eab797bc6131a5ba1129771a9f3a2 (diff)
Add Text:find_all()
Diffstat (limited to 'builtins/text.c')
-rw-r--r--builtins/text.c20
1 files changed, 20 insertions, 0 deletions
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};