Skip to content

Commit

Permalink
Use bash command when assigning valuables
Browse files Browse the repository at this point in the history
  • Loading branch information
lens0021 committed Dec 24, 2024
1 parent 5b6c12e commit d417abd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/std/text.ab
Original file line number Diff line number Diff line change
Expand Up @@ -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_once(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.
Expand Down
5 changes: 5 additions & 0 deletions src/tests/stdlib/replace.ab
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
5 changes: 5 additions & 0 deletions src/tests/stdlib/replace_once.ab
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit d417abd

Please sign in to comment.