aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES.md1
-rw-r--r--src/stdlib/text.c2
-rw-r--r--test/text.tm2
3 files changed, 5 insertions, 0 deletions
diff --git a/CHANGES.md b/CHANGES.md
index a714c5fa..22a1249b 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -54,6 +54,7 @@
- `&` references failed to propagate when accessing fields like
`foo.baz.method()` when `foo` is a `&Foo` and `baz.method()` takes a `&Baz`.
- Optional paths no longer fail to compile when you check them for `none`.
+ - Text replacement no longer infinitely loops when given an empty text to replace.
## v0.3
diff --git a/src/stdlib/text.c b/src/stdlib/text.c
index 8e800c8a..febcafce 100644
--- a/src/stdlib/text.c
+++ b/src/stdlib/text.c
@@ -1135,6 +1135,7 @@ Text_t Text$translate(Text_t text, Table_t translations) {
struct {
Text_t target, replacement;
} *entry = replacement_list.data + r * replacement_list.stride;
+ if (entry->target.length <= 0) continue;
TextIter_t target_state = NEW_TEXT_ITER_STATE(entry->target);
if (_matches(&text_state, &target_state, i)) {
if (i > span_start) result = concat2(result, Text$slice(text, I(span_start + 1), I(i)));
@@ -1156,6 +1157,7 @@ Text_t Text$translate(Text_t text, Table_t translations) {
public
Text_t Text$replace(Text_t text, Text_t target, Text_t replacement) {
+ if (target.length <= 0) return text;
TextIter_t text_state = NEW_TEXT_ITER_STATE(text), target_state = NEW_TEXT_ITER_STATE(target);
Text_t result = EMPTY_TEXT;
int64_t span_start = 0;
diff --git a/test/text.tm b/test/text.tm
index 6631b94e..094da8f8 100644
--- a/test/text.tm
+++ b/test/text.tm
@@ -57,6 +57,8 @@ func main()
assert "xxxx".replace("x", "") == ""
assert "xxxx".replace("y", "") == "xxxx"
assert "One two three four five six".replace("e ", "") == "Ontwo threfour fivsix"
+ assert "Hello".replace("", "xxx") == "Hello"
+ assert "".replace("", "xxx") == ""
assert amelie.has(amelie2) == yes