aboutsummaryrefslogtreecommitdiff
path: root/src/stdlib/text.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-11-23 00:35:05 -0500
committerBruce Hill <bruce@bruce-hill.com>2025-11-23 00:35:05 -0500
commitcb9d3b1a2c2c59c368f6121a16a9ab928b0ff951 (patch)
treedf4c38c993ff78e2e4005058efb66ee1df6f3561 /src/stdlib/text.c
parenta453ebf215e5e3ec3b27fa5142af77d7e3ca0c92 (diff)
Added Text.find(text, target, start=1)
Diffstat (limited to 'src/stdlib/text.c')
-rw-r--r--src/stdlib/text.c17
1 files changed, 15 insertions, 2 deletions
diff --git a/src/stdlib/text.c b/src/stdlib/text.c
index febcafce..e51af49c 100644
--- a/src/stdlib/text.c
+++ b/src/stdlib/text.c
@@ -1057,8 +1057,8 @@ PUREFUNC public int32_t Text$compare(const void *va, const void *vb, const TypeI
bool _matches(TextIter_t *text_state, TextIter_t *target_state, int64_t pos) {
for (int64_t i = 0; i < (int64_t)target_state->stack[0].text.length; i++) {
int32_t text_i = Text$get_grapheme_fast(text_state, pos + i);
- int32_t prefix_i = Text$get_grapheme_fast(target_state, i);
- if (text_i != prefix_i) return false;
+ int32_t target_i = Text$get_grapheme_fast(target_state, i);
+ if (text_i != target_i) return false;
}
return true;
}
@@ -1107,6 +1107,19 @@ static bool _has_grapheme(TextIter_t *text, int32_t g) {
}
public
+OptionalInt_t Text$find(Text_t text, Text_t target, Int_t start) {
+ if (text.length < target.length) return NONE_INT;
+ if (target.length <= 0) return I(1);
+ TextIter_t text_state = NEW_TEXT_ITER_STATE(text), target_state = NEW_TEXT_ITER_STATE(target);
+ for (int64_t i = Int64$from_int(start, false) - 1; i < text.length - target.length + 1; i++) {
+ if (_matches(&text_state, &target_state, i)) {
+ return Int$from_int64(i + 1);
+ }
+ }
+ return NONE_INT;
+}
+
+public
Text_t Text$trim(Text_t text, Text_t to_trim, bool left, bool right) {
int64_t first = 0;
TextIter_t text_state = NEW_TEXT_ITER_STATE(text), trim_state = NEW_TEXT_ITER_STATE(to_trim);