From f93dde14496ef784df6b7b3e1de1030a868be985 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Thu, 23 Jan 2025 15:33:56 -0500 Subject: Overhaul of Text implementation to be more like Cords and have much better performance for long sequences of repeated concatenation. --- stdlib/text.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'stdlib/text.h') diff --git a/stdlib/text.h b/stdlib/text.h index 79c094af..64cf86f5 100644 --- a/stdlib/text.h +++ b/stdlib/text.h @@ -13,18 +13,24 @@ #include "types.h" #include "util.h" +#define MAX_TEXT_DEPTH 48 + typedef struct { - Text_t text; - int64_t subtext, sum_of_previous_subtexts; + struct { + Text_t text; + int64_t offset; + } stack[MAX_TEXT_DEPTH]; + int64_t stack_index; } TextIter_t; +#define NEW_TEXT_ITER_STATE(t) (TextIter_t){.stack={{t, 0}}, .stack_index=0} + int printf_text(FILE *stream, const struct printf_info *info, const void *const args[]); int printf_text_size(const struct printf_info *info, size_t n, int argtypes[n], int sizes[n]); #define Text(str) ((Text_t){.length=sizeof(str)-1, .tag=TEXT_ASCII, .ascii="" str}) int Text$print(FILE *stream, Text_t t); -void Text$visualize(Text_t t); Text_t Text$_concat(int n, Text_t items[n]); #define Text$concat(...) Text$_concat(sizeof((Text_t[]){__VA_ARGS__})/sizeof(Text_t), (Text_t[]){__VA_ARGS__}) #define Texts(...) Text$concat(__VA_ARGS__) @@ -69,11 +75,12 @@ void Text$deserialize(FILE *in, void *out, Array_t *, const TypeInfo_t *); MACROLIKE int32_t Text$get_grapheme(Text_t text, int64_t index) { - TextIter_t state = {text, 0, 0}; + TextIter_t state = NEW_TEXT_ITER_STATE(text); return Text$get_grapheme_fast(&state, index); } extern const TypeInfo_t Text$info; +extern Text_t EMPTY_TEXT; #define Text$metamethods ((metamethods_t){ \ .as_text=Text$as_text, \ -- cgit v1.2.3