diff --git a/src/std/text.ab b/src/std/text.ab index 560d738b..ec30bcaf 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") }