Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace amber text interpolation with bash parameter expansion #592

Open
wants to merge 13 commits into
base: staging
Choose a base branch
from
34 changes: 28 additions & 6 deletions src/std/text.ab
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
/// Replaces all occurences of a pattern in the content with the provided replace text.
// Replace this with builtin if/when we get around to writing one.
#[allow_absurd_cast]
fun bash_version(): Num {
let major = trust $ echo "\$\{BASH_VERSINFO[0]}" $ as Num
let minor = trust $ echo "\$\{BASH_VERSINFO[1]}" $ as Num
return (major * 100) + minor
}

/// Replaces all occurrences of a pattern in the content with the provided replace text.
pub fun replace(source, search, replace) {
return "\$\{source//{search}/{replace}}"
// Here we use commands for assigning values to avoid the side effect of Amber
// Escape `\` as `\\`.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will explain the side effect by writing a ticker later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've narrowed down the issue. #646

trust $ {nameof search}="\$\{{nameof search}//\\\\/\\\\\\\}" $
if bash_version() >= 502 {
trust $ {nameof replace}="\$\{{nameof replace}//\\\\/\\\\\\\\}" $
} else {
trust $ {nameof replace}="\$\{{nameof replace}//\\\\/\\\\}" $
}
lens0021 marked this conversation as resolved.
Show resolved Hide resolved
return trust $ echo "\$\{{nameof source}//{search}/{replace}}" $
}

/// Replaces the first occurence of a pattern in the content with the provided replace text.
/// Replaces the first occurrence of a pattern in the content with the provided replace text.
pub fun replace_one(source, search, replace) {
return "\$\{source/{search}/{replace}}"
// Here we use commands for assigning values to avoid the side effect of Amber
trust $ {nameof search}="\$\{{nameof search}//\\\\/\\\\\\\}" $
if bash_version() >= 502 {
trust $ {nameof replace}="\$\{{nameof replace}//\\\\/\\\\\\\\}" $
}

return trust $ echo "\$\{{nameof source}/{search}/{replace}}" $
}

/// Replaces all occurences of a regex pattern in the content with the provided replace text.
/// Replaces all occurrences of a regex pattern in the content with the provided replace text.
///
/// Function uses `sed`
pub fun replace_regex(source: Text, search: Text, replace: Text, extended: Bool = false): Text {
Expand Down Expand Up @@ -45,7 +67,7 @@ pub fun split_words(text: Text): [Text] {
return split(text, " ")
}

/// Merges text using the delimeter specified.
/// Merges text using the delimiter specified.
pub fun join(list: [Text], delimiter: Text): Text {
return trust $ IFS="{delimiter}" ; echo "\$\{{nameof list}[*]}" $
}
Expand Down
14 changes: 11 additions & 3 deletions src/tests/stdlib/text_replace.ab
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import * from "std/text"

// Output
// apple apple

// TWO TWO TWO
// a\\b\\c\\d
// first..second..third..fourth
// mono
// di
// tri
lens0021 marked this conversation as resolved.
Show resolved Hide resolved
main {
echo replace("banana banana", "banana", "apple")
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")
}
16 changes: 15 additions & 1 deletion src/tests/stdlib/text_replace_one.ab
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import * from "std/text"

// Output
// TWO one one
// a\\b\c\d
// first..second
// third
// fourth
// mono
// di
// tri

main {
echo replace_one("Succinctly", "inctly", "eeded")
echo replace_one("one one one", "one", "TWO")
echo replace_one("a\\b\\c\\d", "\\", "\\\\")
echo replace_one("first\nsecond\nthird\nfourth", "\n", "..")
// other newlines should not be touched
echo replace_one("mono\ndo\ntri", "do\n", "di\n")
}
Loading