aboutsummaryrefslogtreecommitdiff
path: root/src/stdlib
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-09-01 12:51:19 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-09-01 12:51:19 -0400
commit639d5ddfca562e5b3645955551be244b5e8ca9c6 (patch)
tree2a2fba7b2d442b5cf7e9dd2e1f876e4f799a6bdf /src/stdlib
parent0a7062e2d711b5ac7fb71e873f293a8f0d0e8bc6 (diff)
Make text indexing optional
Diffstat (limited to 'src/stdlib')
-rw-r--r--src/stdlib/text.c5
-rw-r--r--src/stdlib/text.h12
2 files changed, 15 insertions, 2 deletions
diff --git a/src/stdlib/text.c b/src/stdlib/text.c
index ed4023a4..f4533be5 100644
--- a/src/stdlib/text.c
+++ b/src/stdlib/text.c
@@ -725,7 +725,10 @@ Text_t Text$reversed(Text_t text) {
}
public
-PUREFUNC Text_t Text$cluster(Text_t text, Int_t index) { return Text$slice(text, index, index); }
+PUREFUNC OptionalText_t Text$cluster(Text_t text, Int_t index) {
+ Text_t slice = Text$slice(text, index, index);
+ return slice.length <= 0 ? NONE_TEXT : slice;
+}
static Text_t Text$from_components(List_t graphemes, Table_t unique_clusters) {
size_t blob_size = (sizeof(int32_t[unique_clusters.entries.length]) + sizeof(uint8_t[graphemes.length]));
diff --git a/src/stdlib/text.h b/src/stdlib/text.h
index 5fa95675..c08e5267 100644
--- a/src/stdlib/text.h
+++ b/src/stdlib/text.h
@@ -41,7 +41,17 @@ Text_t Text$slice(Text_t text, Int_t first_int, Int_t last_int);
Text_t Text$from(Text_t text, Int_t first);
Text_t Text$to(Text_t text, Int_t last);
Text_t Text$reversed(Text_t text);
-Text_t Text$cluster(Text_t text, Int_t index_int);
+OptionalText_t Text$cluster(Text_t text, Int_t index_int);
+#define Text$cluster_checked(text_expr, index_expr, start, end) \
+ ({ \
+ const Text_t text = text_expr; \
+ Int_t index = index_expr; \
+ OptionalText_t cluster = Text$cluster(text, index); \
+ if (unlikely(cluster.length < 0)) \
+ fail_source(__SOURCE_FILE__, start, end, "Invalid text index: ", index, " (text has length ", \
+ (int64_t)text.length, ")\n"); \
+ cluster; \
+ })
OptionalText_t Text$from_str(const char *str);
OptionalText_t Text$from_strn(const char *str, size_t len);
PUREFUNC uint64_t Text$hash(const void *text, const TypeInfo_t *);