Return empty string when chop::before is called with the subject's prefix #27
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Why?
I noticed that if the argument passed to
beforeappears at the beginning of the subject string, the entire subject string is returned instead of an empty string as I expected.I discovered that this was happening because in that situation,
beforecallsslicewith astartof 0 andendof 0, andslicetreats anendof 0 to mean that the end should be the end of the whole string. Ideallyslicewould be able to differentiate between "the end index should be 0" and "the end index should be the final index", but that would require a change to the function's signature, and since it's part of the crate's public API I didn't think it would be feasible. So instead I added a check toreturn_after_or_before_and_after_last_or_before_lastto skip callingsliceentirely and just return an empty string if it's asked to find characters before something and that something is at the beginning of the subject string.I also added
#![allow(unexpected_cfgs)]inlib.rs. A warning was added for unknown feature names in Rust 1.80.0, sodeny(warnings)was turning the warning on#![cfg_attr(all(test, feature = "nightly"), feature(test))]into a compile error.Without this change
"this is something"._before("this")returns"this is something"With this change
"this is something"._before("this")returns""