Skip to content

Conversation

@rotoclone
Copy link

Why?

I noticed that if the argument passed to before appears 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, before calls slice with a start of 0 and end of 0, and slice treats an end of 0 to mean that the end should be the end of the whole string. Ideally slice would 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 to return_after_or_before_and_after_last_or_before_last to skip calling slice entirely 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)] in lib.rs. A warning was added for unknown feature names in Rust 1.80.0, so deny(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 ""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant