Skip to content

Commit

Permalink
Optionally use sed extended regex syntax (amber-lang#453)
Browse files Browse the repository at this point in the history
* Optionally use sed extended regex syntax.

* Update src/std/text.ab

Co-authored-by: Phoenix Himself <pkaras.it@gmail.com>

* Add comment stating that sed extended regex may fail on some older Linux variants.

* Change regex used to detect GNU sed.

* Remove duplicate unsafe from stdlib function.

---------

Co-authored-by: Phoenix Himself <pkaras.it@gmail.com>
  • Loading branch information
2 people authored and Mte90 committed Sep 19, 2024
1 parent 8347c74 commit 2d0a1ef
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
19 changes: 15 additions & 4 deletions src/std/text.ab
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,22 @@ pub fun replace(source, pattern, replacement) {
/// Replaces all occurences of a regex pattern in the content with the provided replacement text.
///
/// Function uses `sed`
pub fun replace_regex(source: Text, pattern: Text, replacement: Text): Text {
return unsafe $echo "{source}" | sed -e "s/{pattern}/{replacement}/g"$
pub fun replace_regex(source: Text, pattern: Text, replacement: Text, extended: Bool = false): Text {
unsafe {
if extended {
// GNU sed versions 4.0 through 4.2 support extended regex syntax,
// but only via the "-r" option; use that if the version information
// contains "GNU sed".
$re='\bCopyright\b.+\bFree Software Foundation\b'; [[ \$(sed --version 2>/dev/null) =~ \$re ]]$
let flag = status == 0 then "-r" else "-E"
return $echo "{source}" | sed {flag} -e "s/{pattern}/{replacement}/g"$
} else {
return $echo "{source}" | sed -e "s/{pattern}/{replacement}/g"$
}
}
}

/// Splits the input `text` into an array of substrings using the specified `delimiter`.
/// Splits the input `text` into an array of substrings using the specified `delimiter`.
pub fun split(text: Text, delimiter: Text): [Text] {
let result = [Text]
unsafe $IFS="{delimiter}" read -rd '' -a {nameof result} < <(printf %s "\${nameof text}")$
Expand All @@ -29,7 +40,7 @@ pub fun lines(text: Text): [Text] {

/// Splits a `text` into an array of substrings based on space character.
pub fun words(text: Text): [Text] {
return split(text, " ")
return split(text, " ")
}

/// Merges text using the delimeter specified.
Expand Down
8 changes: 0 additions & 8 deletions src/tests/stdlib/replace_regex.ab

This file was deleted.

8 changes: 8 additions & 0 deletions src/tests/stdlib/replace_regex_basic.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * from "std/text"

// Output
// abc[123]def

main {
echo replace_regex("abc123def", "\([0-9][0-9]*\)", "[\1]")
}
10 changes: 10 additions & 0 deletions src/tests/stdlib/replace_regex_ext.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * from "std/text"

// Output
// abc[123]def

main {
// This will fail on any system where sed does not support extended
// regex syntax, via "-r" on GNU sed and "-E" on all other versions.
echo replace_regex("abc123def", "([0-9]+)", "[\1]", true)
}

0 comments on commit 2d0a1ef

Please sign in to comment.