From 1122ea1f2903b083ada0cb676b8944b24764fcad Mon Sep 17 00:00:00 2001 From: lens0021 Date: Fri, 15 Nov 2024 03:02:37 +0900 Subject: [PATCH] Escape backslashes in search and replacement --- src/std/text.ab | 8 ++++++-- src/tests/stdlib/replace.ab | 2 ++ src/tests/stdlib/replace_once.ab | 2 +- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/std/text.ab b/src/std/text.ab index 74766491..feab4587 100644 --- a/src/std/text.ab +++ b/src/std/text.ab @@ -1,11 +1,15 @@ /// Replaces all occurrences of a pattern in the content with the provided replace text. pub fun replace(source, search, replace) { - return "\$\{source//{search}/{replace}}" + search = trust $ echo \$\{{nameof search}//\\\\/\\\\\\\\} $ + replace = trust $ echo \$\{{nameof replace}//\\\\/\\\\\\\\} $ + return trust $ echo \$\{{nameof source}//{search}/{replace}} $ } /// Replaces the first occurrence of a pattern in the content with the provided replace text. pub fun replace_once(source, search, replace) { - return "\$\{source/{search}/{replace}}" + search = replace(search, "\\", "\\\\") + replace = replace(replace, "\\", "\\\\") + return trust $ echo \$\{{nameof source}/{search}/{replace}} $ } /// Replaces all occurrences of a regex pattern in the content with the provided replace text. diff --git a/src/tests/stdlib/replace.ab b/src/tests/stdlib/replace.ab index 093bae0f..9ad48966 100644 --- a/src/tests/stdlib/replace.ab +++ b/src/tests/stdlib/replace.ab @@ -2,7 +2,9 @@ import * from "std/text" // Output // apple apple +// apple main { echo replace("banana banana", "banana", "apple") + echo replace("\\", "\\", "apple") } diff --git a/src/tests/stdlib/replace_once.ab b/src/tests/stdlib/replace_once.ab index b7f9f46f..e27816c1 100644 --- a/src/tests/stdlib/replace_once.ab +++ b/src/tests/stdlib/replace_once.ab @@ -1,4 +1,4 @@ import * from "std/text" main { - echo replace_once("Succinctly", "inctly", "eeded") + echo replace_once("Succinctly\\", "inctly\\", "eeded") }