add Str.contains to builtins

This commit is contained in:
Isaac Van Doren 2023-10-14 14:56:51 -05:00
parent 1c37d8adb1
commit 886b855594
No known key found for this signature in database
GPG Key ID: CFA524CD470E5B94
3 changed files with 74 additions and 0 deletions

View File

@ -135,6 +135,7 @@ interface Str
withCapacity, withCapacity,
withPrefix, withPrefix,
graphemes, graphemes,
contains,
] ]
imports [ imports [
Bool.{ Bool, Eq }, Bool.{ Bool, Eq },
@ -995,3 +996,15 @@ strToNumHelp = \string ->
## ``` ## ```
withPrefix : Str, Str -> Str withPrefix : Str, Str -> Str
withPrefix = \str, prefix -> Str.concat prefix str withPrefix = \str, prefix -> Str.concat prefix str
## Determines whether or not the first Str contains the second.
## ```
## expect Str.contains "foobarbaz" "bar"
## expect !(Str.contains "apple" "orange")
## expect Str.contains "anything" ""
## ```
contains : Str, Str -> Bool
contains = \haystack, needle ->
when firstMatch haystack needle is
Some _index -> Bool.true
None -> Bool.false

View File

@ -1346,6 +1346,7 @@ define_builtins! {
56 STR_IS_VALID_SCALAR: "isValidScalar" 56 STR_IS_VALID_SCALAR: "isValidScalar"
57 STR_RELEASE_EXCESS_CAPACITY: "releaseExcessCapacity" 57 STR_RELEASE_EXCESS_CAPACITY: "releaseExcessCapacity"
58 STR_WALK_UTF8: "walkUtf8" 58 STR_WALK_UTF8: "walkUtf8"
59 STR_CONTAINS: "contains"
} }
6 LIST: "List" => { 6 LIST: "List" => {
0 LIST_LIST: "List" exposed_apply_type=true // the List.List type alias 0 LIST_LIST: "List" exposed_apply_type=true // the List.List type alias

View File

@ -2144,3 +2144,63 @@ fn release_excess_capacity_empty() {
|value: RocStr| (value.capacity(), value) |value: RocStr| (value.capacity(), value)
); );
} }
#[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))]
fn str_contains_positive() {
assert_evals_to!(
r#"
Str.contains "foobarbaz" "bar"
"#,
true,
bool
);
}
#[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))]
fn str_contains_negative() {
assert_evals_to!(
r#"
Str.contains "apple" "orange"
"#,
false,
bool
);
}
#[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))]
fn str_contains_empty_positive() {
assert_evals_to!(
r#"
Str.contains "anything" ""
"#,
true,
bool
);
}
#[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))]
fn str_contains_empty_negative() {
assert_evals_to!(
r#"
Str.contains "" "anything"
"#,
false,
bool
);
}
#[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-dev"))]
fn str_contains_self() {
assert_evals_to!(
r#"
Str.contains "self" "self"
"#,
true,
bool
);
}