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 | |
| parent | 9214e621bf7fdaec29b872a5b0e757806fa61b40 (diff) | |
Add Text:split() and use that with an empty pattern instead of Text:clusters()
Diffstat (limited to 'builtins')
| -rw-r--r-- | builtins/text.c | 23 | ||||
| -rw-r--r-- | builtins/text.h | 1 |
2 files changed, 24 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; diff --git a/builtins/text.h b/builtins/text.h index a4697507..cca18a2c 100644 --- a/builtins/text.h +++ b/builtins/text.h @@ -60,6 +60,7 @@ Text_t Text$title(Text_t text); Text_t Text$as_text(const void *text, bool colorize, const TypeInfo *info); Text_t Text$quoted(Text_t str, bool colorize); Text_t Text$replace(Text_t str, Text_t pat, Text_t replacement); +array_t Text$split(Text_t text, Text_t pattern); Int_t Text$find(Text_t text, Text_t pattern, Int_t i, int64_t *match_length); bool Text$has(Text_t text, Text_t pattern); const char *Text$as_c_string(Text_t text); |
