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
18 changes: 12 additions & 6 deletions src/std/text.ab
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
/// Replaces all occurences of a pattern in the content with the provided replace text.
/// 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 a command to avoid GH-646
let result = ""
trust $ {nameof result}="\$\{{nameof source}//\"\$\{{nameof search}}\"/\"\$\{{nameof replace}}\"}" $
return result
}

/// 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 a command to avoid GH-646
let result = ""
trust $ {nameof result}="\$\{{nameof source}/\"\$\{{nameof search}}\"/\"\$\{{nameof replace}}\"}" $
return result
}

/// 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 +51,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
13 changes: 11 additions & 2 deletions src/tests/stdlib/text_replace.ab
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
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