aboutsummaryrefslogtreecommitdiff
path: root/builtins/text.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-05-19 14:38:44 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-05-19 14:38:44 -0400
commit7dddfb71a0ebf8dbbe8a9bdfc156d7471ff943bd (patch)
tree3a7941ff59accef371c12cc2f5859592095bf4a3 /builtins/text.c
parentb0d1daa0f3abe5f9128064388adfffccd2608ca5 (diff)
Add Text.slice()
Diffstat (limited to 'builtins/text.c')
-rw-r--r--builtins/text.c15
1 files changed, 15 insertions, 0 deletions
diff --git a/builtins/text.c b/builtins/text.c
index b4246980..9a9c3801 100644
--- a/builtins/text.c
+++ b/builtins/text.c
@@ -124,6 +124,21 @@ public uint32_t Text$hash(const CORD *cord)
return hash;
}
+public CORD Text$slice(CORD text, int64_t first, int64_t length)
+{
+ if (length == 0) return CORD_EMPTY;
+ int64_t len = CORD_len(text);
+
+ if (length < -len) return CORD_EMPTY;
+
+ if (first <= 0) first = len + first + 1;
+ if (first < 1) first = 1;
+ else if (first > len + 1) first = len + 1;
+
+ if (length < 0) length = len + length;
+ return CORD_substr(text, first - 1, length);
+}
+
public CORD Text$upper(CORD str)
{
if (!str) return str;