-
-
Notifications
You must be signed in to change notification settings - Fork 292
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -367,6 +367,8 @@ module [ | |
withCapacity, | ||
withPrefix, | ||
contains, | ||
dropPrefix, | ||
dropSuffix, | ||
] | ||
|
||
import Bool exposing [Bool] | ||
|
@@ -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 | ||
|
||
## Returns the given [Str] with each occurrence of a substring replaced. | ||
## If the substring is not found, returns the original string. | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this subtraction guaranteed to never exercise the saturating behavior since |
||
|
||
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 |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stray comment