aboutsummaryrefslogtreecommitdiff
path: root/src/stdlib
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-09-06 14:46:15 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-09-06 14:46:15 -0400
commitd8a48f64111f542f3afeb5d6e47ff092f9278d9f (patch)
tree07c364503025bb2a26edd7b26f1ba9e8d25340f6 /src/stdlib
parent12345a85d9c7d7a56ddf323247a4bdf347021b73 (diff)
parent73246764f88f6f652316ee0c138a990d836698a7 (diff)
Merge branch 'main' into optional-list-indexing
Diffstat (limited to 'src/stdlib')
-rw-r--r--src/stdlib/integers.c2
-rw-r--r--src/stdlib/text.c43
-rw-r--r--src/stdlib/text.h1
3 files changed, 25 insertions, 21 deletions
diff --git a/src/stdlib/integers.c b/src/stdlib/integers.c
index 5dc9ac55..863bb42d 100644
--- a/src/stdlib/integers.c
+++ b/src/stdlib/integers.c
@@ -430,7 +430,7 @@ OptionalInt_t Int$parse(Text_t text, Text_t *remainder) {
else if (*end != '\0') return NONE_INT;
result = mpz_init_set_str(i, str + 2, 2);
} else {
- const char *end = str + 2 + strspn(str + 2, "0123456789");
+ const char *end = str + strspn(str, "0123456789");
if (remainder) *remainder = Text$from_str(end);
else if (*end != '\0') return NONE_INT;
result = mpz_init_set_str(i, str, 10);
diff --git a/src/stdlib/text.c b/src/stdlib/text.c
index 1dd2003e..bc5d4af3 100644
--- a/src/stdlib/text.c
+++ b/src/stdlib/text.c
@@ -1393,17 +1393,8 @@ Text_t Text$title(Text_t text, Text_t language) {
}
public
-Text_t Text$quoted(Text_t text, bool colorize, Text_t quotation_mark) {
- if (quotation_mark.length != 1) fail("Invalid quote text: ", quotation_mark, " (must have length == 1)");
-
+Text_t Text$escaped(Text_t text, bool colorize, Text_t extra_escapes) {
Text_t ret = colorize ? Text("\x1b[35m") : EMPTY_TEXT;
- if (!Text$equal_values(quotation_mark, Text("\"")) && !Text$equal_values(quotation_mark, Text("'"))
- && !Text$equal_values(quotation_mark, Text("`")))
- ret = concat2_assuming_safe(ret, Text("$"));
-
- ret = concat2_assuming_safe(ret, quotation_mark);
- int32_t quote_char = Text$get_grapheme(quotation_mark, 0);
-
#define flush_unquoted() \
({ \
if (unquoted_span > 0) { \
@@ -1457,15 +1448,18 @@ Text_t Text$quoted(Text_t text, bool colorize, Text_t quotation_mark) {
break;
}
default: {
- if (g == quote_char) {
- flush_unquoted();
- if (colorize) ret = concat2_assuming_safe(ret, Text("\x1b[34;1m"));
- ret = concat2_assuming_safe(ret, Text("\\"));
- ret = concat2_assuming_safe(ret, quotation_mark);
- if (colorize) ret = concat2_assuming_safe(ret, Text("\x1b[0;35m"));
- } else {
- unquoted_span += 1;
+ TextIter_t esc_state = NEW_TEXT_ITER_STATE(extra_escapes);
+ for (int64_t j = 0; j < extra_escapes.length; j++) {
+ int32_t esc = Text$get_grapheme_fast(&esc_state, j);
+ if (g == esc) {
+ flush_unquoted();
+ if (colorize) ret = concat2_assuming_safe(ret, Text("\x1b[34;1m"));
+ ret = concat2_assuming_safe(ret, Text("\\"));
+ if (colorize) ret = concat2_assuming_safe(ret, Text("\x1b[0;35m"));
+ break;
+ }
}
+ unquoted_span += 1;
break;
}
}
@@ -1473,10 +1467,19 @@ Text_t Text$quoted(Text_t text, bool colorize, Text_t quotation_mark) {
flush_unquoted();
#undef add_escaped
#undef flush_unquoted
-
- ret = concat2_assuming_safe(ret, quotation_mark);
if (colorize) ret = concat2_assuming_safe(ret, Text("\x1b[m"));
+ return ret;
+}
+
+public
+Text_t Text$quoted(Text_t text, bool colorize, Text_t quotation_mark) {
+ if (quotation_mark.length != 1) fail("Invalid quote text: ", quotation_mark, " (must have length == 1)");
+ Text_t ret = Text$escaped(text, colorize, quotation_mark);
+ if (!(Text$equal_values(quotation_mark, Text("\"")) || Text$equal_values(quotation_mark, Text("'"))
+ || Text$equal_values(quotation_mark, Text("`"))))
+ ret = Texts("$", quotation_mark, ret, quotation_mark);
+ else ret = Texts(quotation_mark, ret, quotation_mark);
return ret;
}
diff --git a/src/stdlib/text.h b/src/stdlib/text.h
index 97b8f90f..0b341eb2 100644
--- a/src/stdlib/text.h
+++ b/src/stdlib/text.h
@@ -76,6 +76,7 @@ Text_t Text$upper(Text_t text, Text_t language);
Text_t Text$lower(Text_t text, Text_t language);
Text_t Text$title(Text_t text, Text_t language);
Text_t Text$as_text(const void *text, bool colorize, const TypeInfo_t *info);
+Text_t Text$escaped(Text_t text, bool colorize, Text_t extra_escapes);
Text_t Text$quoted(Text_t str, bool colorize, Text_t quotation_mark);
PUREFUNC bool Text$starts_with(Text_t text, Text_t prefix, Text_t *remainder);
PUREFUNC bool Text$ends_with(Text_t text, Text_t suffix, Text_t *remainder);