-
-
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?
Conversation
Useful information Re utf-8 validation https://roc.zulipchat.com/#narrow/stream/304641-ideas/topic/builtins.20for.20parsing/near/463098204 Thank you @timotree3 |
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 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.
@@ -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 |
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
# = RocStr, start, len -> RocStr |
I agree with your reasoning as long as |
Thank you for your contribution! Sometimes PRs end up staying open for a long time without activity, which can make the list of open PRs get long and time-consuming to review. To keep things manageable for reviewers, this bot automatically closes PRs that haven’t had activity in 60 days. This PR hasn’t had activity in 30 days, so it will be automatically closed if there is no more activity in the next 30 days. Keep in mind that PRs marked |
Implements the builtins from #664
Note I changed the api from what was described in the issue. This was easier to implement and test, and I think will be nicer to use.
dropPrefix : Str, Str -> Result Str {}
todropPrefix : Str, Str -> Str
dropSuffix : Str, Str -> Result Str {}
todropSuffix : Str, Str -> Str
Also, I am not 100% around the use of
substringUnsafe
here. I think it's ok because we are starting with a valid utf8 string, and using a valid utf8 string to drop so I don't think there is anyway to drop and now have invalid utf8. Is there a way to test this? maybe we should do some fuzzing here?