From e226da0ccbec3a771fd635cbc4018d1f883f0157 Mon Sep 17 00:00:00 2001 From: lens0021 Date: Tue, 24 Dec 2024 20:52:37 +0900 Subject: [PATCH] Use bash command when assigning valuables --- src/std/text.ab | 17 ++++++++++------- src/tests/stdlib/replace_once.ab | 5 +++++ src/tests/stdlib/text_replace.ab | 5 +++++ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/std/text.ab b/src/std/text.ab index 49b908d2..1d79401c 100644 --- a/src/std/text.ab +++ b/src/std/text.ab @@ -8,23 +8,26 @@ fun bash_version(): Num { /// Replaces all occurrences of a pattern in the content with the provided replace text. pub fun replace(source, search, replace) { - search = trust $ echo \$\{{nameof search}//\\\\/\\\\\\\\} $ + // Here we use commands for assigning values to avoid the side effect of Amber + // Escape `\` as `\\`. + trust $ {nameof search}="\$\{{nameof search}//\\\\/\\\\\\\}" $ if bash_version() >= 502 { - replace = trust $ echo \$\{{nameof replace}//\\\\/\\\\\\\\} $ + trust $ {nameof replace}="\$\{{nameof replace}//\\\\/\\\\\\\\}" $ } else { - replace = trust $ echo \$\{{nameof replace}//\\\\/\\\\} $ + trust $ {nameof replace}="\$\{{nameof replace}//\\\\/\\\\}" $ } - return trust $ echo \$\{{nameof source}//{search}/{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_one(source, search, replace) { - search = replace(search, "\\", "\\\\") + // Here we use commands for assigning values to avoid the side effect of Amber + trust $ {nameof search}="\$\{{nameof search}//\\\\/\\\\\\\}" $ if bash_version() >= 502 { - replace = replace(replace, "\\", "\\\\") + trust $ {nameof replace}="\$\{{nameof replace}//\\\\/\\\\\\\\}" $ } - return trust $ echo \$\{{nameof source}/{search}/{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_once.ab b/src/tests/stdlib/replace_once.ab index 5b2254d9..66821b5c 100644 --- a/src/tests/stdlib/replace_once.ab +++ b/src/tests/stdlib/replace_once.ab @@ -6,9 +6,14 @@ import * from "std/text" // first..second // third // fourth +// mono +// di +// tri main { echo replace_once("one one one", "one", "TWO") echo replace_once("a\\b\\c\\d", "\\", "\\\\") echo replace_once("first\nsecond\nthird\nfourth", "\n", "..") + // other newlines should not be touched + echo replace_once("mono\ndo\ntri", "do\n", "di\n") } diff --git a/src/tests/stdlib/text_replace.ab b/src/tests/stdlib/text_replace.ab index 702aef86..94b2a957 100644 --- a/src/tests/stdlib/text_replace.ab +++ b/src/tests/stdlib/text_replace.ab @@ -4,8 +4,13 @@ import * from "std/text" // TWO TWO TWO // a\\b\\c\\d // first..second..third..fourth +// mono +// di +// tri main { echo replace("one one one", "one", "TWO") echo replace("a\\b\\c\\d", "\\", "\\\\") echo replace("first\nsecond\nthird\nfourth", "\n", "..") + // other newlines should not be touched + echo replace("mono\ndo\ntri", "do\n", "di\n") }