builtin(str): add withPrefix

Closes #4142

Signed-off-by: Prajwal S N <prajwalnadig21@gmail.com>
This commit is contained in:
Prajwal S N 2022-10-08 12:52:10 +05:30
parent f0786121fb
commit d6526fa425
No known key found for this signature in database
GPG key ID: D0FECEE245BC2695
3 changed files with 30 additions and 0 deletions

View file

@ -43,6 +43,7 @@ interface Str
appendScalar,
walkScalars,
walkScalarsUntil,
withPrefix,
]
imports [
Bool.{ Bool },
@ -561,3 +562,7 @@ strToNumHelp = \string ->
Ok result.aresult
else
Err InvalidNumStr
## Adds the specified prefix to the string, like a reversed Str.concat
withPrefix : Str, Str -> Str
withPrefix = \str, prefix -> Str.concat prefix str

View file

@ -1296,6 +1296,7 @@ define_builtins! {
50 STR_REPLACE_EACH: "replaceEach"
51 STR_REPLACE_FIRST: "replaceFirst"
52 STR_REPLACE_LAST: "replaceLast"
53 STR_WITH_PREFIX: "withPrefix"
}
6 LIST: "List" => {
0 LIST_LIST: "List" exposed_apply_type=true // the List.List type alias

View file

@ -1930,3 +1930,27 @@ fn when_on_strings() {
i64
);
}
#[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-wasm"))]
fn str_with_prefix() {
assert_evals_to!(
indoc!(
r#"
Str.withPrefix "world!" "Hello "
"#
),
RocStr::from("Hello world!"),
RocStr
);
assert_evals_to!(
indoc!(
r#"
"two" |> Str.withPrefix "Forty "
"#
),
RocStr::from("Forty two"),
RocStr
);
}