diff --git a/crates/compiler/builtins/roc/Str.roc b/crates/compiler/builtins/roc/Str.roc index d789a01353..f79973f922 100644 --- a/crates/compiler/builtins/roc/Str.roc +++ b/crates/compiler/builtins/roc/Str.roc @@ -44,6 +44,7 @@ interface Str walkScalars, walkScalarsUntil, withCapacity, + withPrefix, ] imports [ Bool.{ Bool }, @@ -565,3 +566,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 diff --git a/crates/compiler/module/src/symbol.rs b/crates/compiler/module/src/symbol.rs index 50712f1764..b78549de60 100644 --- a/crates/compiler/module/src/symbol.rs +++ b/crates/compiler/module/src/symbol.rs @@ -1297,6 +1297,7 @@ define_builtins! { 51 STR_REPLACE_FIRST: "replaceFirst" 52 STR_REPLACE_LAST: "replaceLast" 53 STR_WITH_CAPACITY: "withCapacity" + 54 STR_WITH_PREFIX: "withPrefix" } 6 LIST: "List" => { 0 LIST_LIST: "List" exposed_apply_type=true // the List.List type alias diff --git a/crates/compiler/test_gen/src/gen_str.rs b/crates/compiler/test_gen/src/gen_str.rs index d38e8e0fcb..c1bd9385f3 100644 --- a/crates/compiler/test_gen/src/gen_str.rs +++ b/crates/compiler/test_gen/src/gen_str.rs @@ -1958,3 +1958,27 @@ fn with_capacity_concat() { RocStr ); } + +#[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 + ); +}