Skip to content

Commit c62ff11

Browse files
committed
Replace text interpolation with use of nameof
1 parent c979ae1 commit c62ff11

File tree

1 file changed

+6
-21
lines changed

1 file changed

+6
-21
lines changed

Diff for: src/std/text.ab

+6-21
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,17 @@
1-
// Replace this with builtin if/when we get around to writing one.
2-
#[allow_absurd_cast]
3-
fun bash_version(): Num {
4-
let major = trust $ echo "\$\{BASH_VERSINFO[0]}" $ as Num
5-
let minor = trust $ echo "\$\{BASH_VERSINFO[1]}" $ as Num
6-
return (major * 100) + minor
7-
}
8-
91
/// Replaces all occurrences of a pattern in the content with the provided replace text.
102
pub fun replace(source, search, replace) {
113
// Here we use commands for assigning values to avoid the side effect of Amber
12-
// Escape `\` as `\\`.
13-
trust $ {nameof search}="\$\{{nameof search}//\\\\/\\\\\\\}" $
14-
if bash_version() >= 502 {
15-
trust $ {nameof replace}="\$\{{nameof replace}//\\\\/\\\\\\\\}" $
16-
}
17-
18-
return trust $ echo "\$\{{nameof source}//{search}/{replace}}" $
4+
let result = ""
5+
trust $ {nameof result}="\$\{{nameof source}//\"\$\{{nameof search}}\"/\"\$\{{nameof replace}}\"}" $
6+
return result
197
}
208

219
/// Replaces the first occurrence of a pattern in the content with the provided replace text.
2210
pub fun replace_one(source, search, replace) {
2311
// Here we use commands for assigning values to avoid the side effect of Amber
24-
trust $ {nameof search}="\$\{{nameof search}//\\\\/\\\\\\\}" $
25-
if bash_version() >= 502 {
26-
trust $ {nameof replace}="\$\{{nameof replace}//\\\\/\\\\\\\\}" $
27-
}
28-
29-
return trust $ echo "\$\{{nameof source}/{search}/{replace}}" $
12+
let result = ""
13+
trust $ {nameof result}="\$\{{nameof source}/\"\$\{{nameof search}}\"/\"\$\{{nameof replace}}\"}" $
14+
return result
3015
}
3116

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

0 commit comments

Comments
 (0)