diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-09-06 00:03:28 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-09-06 00:03:28 -0400 |
| commit | 44892df4c5686b292a058ca19eaba1e852fe42f3 (patch) | |
| tree | b01b46ef7e9496e1971616e3b55e124dfd217cbb /builtins/text.c | |
| parent | 1000423d2b351f1f5edbb3c9a08898883ba47f3e (diff) | |
Add Text.trim()
Diffstat (limited to 'builtins/text.c')
| -rw-r--r-- | builtins/text.c | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/builtins/text.c b/builtins/text.c index be304184..7316dabc 100644 --- a/builtins/text.c +++ b/builtins/text.c @@ -1904,6 +1904,27 @@ public Text_t Text$replace(Text_t text, Pattern_t pattern, Text_t replacement, P return ret; } +public Text_t Text$trim(Text_t text, Pattern_t pattern, bool trim_left, bool trim_right) +{ + int64_t first = 0, last = text.length-1; + if (trim_left) { + int64_t match_len = match(text, pattern, 0, 0, NULL, 0); + if (match_len > 0) + first = match_len; + } + + if (trim_right) { + for (int64_t i = text.length-1; i >= first; i--) { + int64_t match_len = match(text, pattern, i, 0, NULL, 0); + if (match_len > 0 && i + match_len == text.length) + last = i-1; + // else + // break; + } + } + return Text$slice(text, I(first+1), I(last+1)); +} + public Text_t Text$map(Text_t text, Pattern_t pattern, closure_t fn) { Text_t ret = {.length=0}; |
