Skip to content

Commit

Permalink
Replace text interpolation with use of nameof
Browse files Browse the repository at this point in the history
  • Loading branch information
lens0021 committed Dec 24, 2024
1 parent c979ae1 commit c62ff11
Showing 1 changed file with 6 additions and 21 deletions.
27 changes: 6 additions & 21 deletions src/std/text.ab
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
// 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) {
// 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 {
trust $ {nameof replace}="\$\{{nameof replace}//\\\\/\\\\\\\\}" $
}

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

/// Replaces the first occurrence of a pattern in the content with the provided replace text.
pub fun replace_one(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}}" $
let result = ""
trust $ {nameof result}="\$\{{nameof source}/\"\$\{{nameof search}}\"/\"\$\{{nameof replace}}\"}" $
return result
}

/// Replaces all occurrences of a regex pattern in the content with the provided replace text.
Expand Down

0 comments on commit c62ff11

Please sign in to comment.