Skip to content

Commit

Permalink
Fix < 5.1 and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lens0021 committed Dec 16, 2024
1 parent a2c6012 commit 32e30b0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/std/text.ab
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ pub fun replace(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, "\\", "\\\\")
replace = replace(replace, "\\", "\\\\")
if bash_version() >= 502 {
replace = replace(replace, "\\", "\\\\")
}

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

Expand Down
8 changes: 6 additions & 2 deletions src/tests/stdlib/replace.ab
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import * from "std/text"
// Output
// apple apple
// apple
// banana\\
// Succeeded
// one\\two\\\\three\\\\\\
// one\two\three\\

main {
echo replace("banana banana", "banana", "apple")
echo replace("\\", "\\", "apple")
echo replace("banana\\", "\\", "\\\\")
echo replace("Succinctly\\", "inctly\\", "eeded")
echo replace("one\\two\\\\three\\\\\\", "\\", "\\\\")
echo replace("one\\two\\\\three\\\\\\", "\\\\", "\\")
}
8 changes: 8 additions & 0 deletions src/tests/stdlib/replace_once.ab
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import * from "std/text"

// Output
// Succeeded
// one\\two\\three\\\.
// one\two\three\\\.

main {
echo replace_once("Succinctly\\", "inctly\\", "eeded")
echo replace_once("one\\two\\\\three\\\\\\.", "\\", "\\\\")
echo replace_once("one\\two\\\\three\\\\\\.", "\\\\", "\\")
}

0 comments on commit 32e30b0

Please sign in to comment.