Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Str.dropPrefix and Str.dropSuffix builtins #7007

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions crates/compiler/builtins/roc/Str.roc
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ module [
withCapacity,
withPrefix,
contains,
dropPrefix,
dropSuffix,
]

import Bool exposing [Bool]
Expand Down Expand Up @@ -752,6 +754,7 @@ countUtf8Bytes : Str -> U64

## string slice that does not do bounds checking or utf-8 verification
substringUnsafe : Str, U64, U64 -> Str
# = RocStr, start, len -> RocStr

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stray comment

Suggested change
# = RocStr, start, len -> RocStr


## Returns the given [Str] with each occurrence of a substring replaced.
## If the substring is not found, returns the original string.
Expand Down Expand Up @@ -1052,3 +1055,37 @@ contains = \haystack, needle ->
when firstMatch haystack needle is
Some _index -> Bool.true
None -> Bool.false

## Drops the given prefix [Str] from the start of a [Str]
## If the prefix is not found, returns the original string.
##
## ```roc
## expect Str.dropPrefix "bar" "foo" == "bar"
## expect Str.dropPrefix "foobar" "foo" == "bar"
## ```
dropPrefix : Str, Str -> Str
dropPrefix = \haystack, prefix ->
if Str.startsWith haystack prefix then
start = Str.countUtf8Bytes prefix
len = Num.subSaturated (Str.countUtf8Bytes haystack) start

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this subtraction guaranteed to never exercise the saturating behavior since start = Str.countUtf8Bytes prefix <= Str.countUtf8Bytes haystack since Str.startsWith haystack prefix? It would probably be more performant to use wrapping subtraction.


substringUnsafe haystack start len
else
haystack

## Drops the given suffix [Str] from the end of a [Str]
## If the suffix is not found, returns the original string.
##
## ```roc
## expect Str.dropSuffix "bar" "foo" == "bar"
## expect Str.dropSuffix "barfoo" "foo" == "bar"
## ```
dropSuffix : Str, Str -> Str
dropSuffix = \haystack, suffix ->
if Str.endsWith haystack suffix then
start = 0
len = Num.subSaturated (Str.countUtf8Bytes haystack) (Str.countUtf8Bytes suffix)

substringUnsafe haystack start len
else
haystack
2 changes: 2 additions & 0 deletions crates/compiler/module/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,8 @@ define_builtins! {
46 STR_REPLACE_FIRST: "replaceFirst"
47 STR_REPLACE_LAST: "replaceLast"
48 STR_RELEASE_EXCESS_CAPACITY: "releaseExcessCapacity"
49 STR_DROP_PREFIX: "dropPrefix"
50 STR_DROP_SUFFIX: "dropSuffix"
}
6 LIST: "List" => {
0 LIST_LIST: "List" exposed_apply_type=true // the List.List type alias
Expand Down
72 changes: 72 additions & 0 deletions crates/compiler/test_gen/src/gen_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1988,3 +1988,75 @@ fn str_contains_self() {
bool
);
}

#[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-dev", feature = "gen-wasm"))]
fn str_drop_prefix() {
assert_evals_to!(
r#"
Str.dropPrefix "" "foo"
"#,
RocResult::ok(RocStr::from("")),
RocResult<RocStr, ()>
);

assert_evals_to!(
r#"
Str.dropPrefix "bar" "foo"
"#,
RocResult::ok(RocStr::from("bar")),
RocResult<RocStr, ()>
);

assert_evals_to!(
r#"
Str.dropPrefix "foobar" "foo"
"#,
RocResult::ok(RocStr::from("bar")),
RocResult<RocStr, ()>
);

assert_evals_to!(
r#"
Str.dropPrefix "fooBarThisIsDefinitelyAReallyLongAndNotaShortString" "foo"
"#,
RocResult::ok(RocStr::from("BarThisIsDefinitelyAReallyLongAndNotaShortString")),
RocResult<RocStr, ()>
);
}

#[test]
#[cfg(any(feature = "gen-llvm", feature = "gen-dev", feature = "gen-wasm"))]
fn str_drop_suffix() {
assert_evals_to!(
r#"
Str.dropSuffix "" "foo"
"#,
RocResult::ok(RocStr::from("")),
RocResult<RocStr, ()>
);

assert_evals_to!(
r#"
Str.dropSuffix "bar" "foo"
"#,
RocResult::ok(RocStr::from("bar")),
RocResult<RocStr, ()>
);

assert_evals_to!(
r#"
Str.dropSuffix "barfoo" "foo"
"#,
RocResult::ok(RocStr::from("bar")),
RocResult<RocStr, ()>
);

assert_evals_to!(
r#"
Str.dropSuffix "BarThisIsDefinitelyAReallyLongAndNotaShortStringfoo" "foo"
"#,
RocResult::ok(RocStr::from("BarThisIsDefinitelyAReallyLongAndNotaShortString")),
RocResult<RocStr, ()>
);
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions crates/compiler/test_mono/generated/dbg_in_expect.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 20 additions & 20 deletions crates/compiler/test_mono/generated/encode_derived_string.txt

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading