diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-09-02 23:07:08 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-09-02 23:07:08 -0400 |
| commit | 6d7e09bf1801c2fe183df17cc67017a6d3d8513b (patch) | |
| tree | 2c51df15ebab45a68ec36e9466e8e9f3813b48f6 /builtins/text.c | |
| parent | 9214e621bf7fdaec29b872a5b0e757806fa61b40 (diff) | |
Add Text:split() and use that with an empty pattern instead of Text:clusters()
Diffstat (limited to 'builtins/text.c')
| -rw-r--r-- | builtins/text.c | 23 |
1 files changed, 23 insertions, 0 deletions
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; |
