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

"bananas".contains("nana") returns false #16589

Closed
nham opened this issue Aug 18, 2014 · 1 comment · Fixed by #16590 or #16612
Closed

"bananas".contains("nana") returns false #16589

nham opened this issue Aug 18, 2014 · 1 comment · Fixed by #16590 or #16612

Comments

@nham
Copy link
Contributor

nham commented Aug 18, 2014

Currently, "bananas".contains("nana") returns false. It is the only substring of "bananas" for which this is true:

fn main() {
    let b = "bananas";
    for i in range(0, b.len()) {
        for j in range(i, b.len() + 1) {
            let curr = b.slice(i, j);
            println!("{} - {}", b.contains(curr), curr);
        }
    }
}

results in:

true - 
true - b
true - ba
true - ban
true - bana
true - banan
true - banana
true - bananas
true - 
true - a
true - an
true - ana
true - anan
true - anana
true - ananas
true - 
true - n
true - na
true - nan
false - nana
true - nanas
true - 
true - a
true - an
true - ana
true - anas
true - 
true - n
true - na
true - nas
true - 
true - a
true - as
true - 
true - s

I've done some preliminary work tracking this down. The problem seems to be in core::str::TwoWaySearcher.

@Gankra
Copy link
Contributor

Gankra commented Aug 18, 2014

"(x^i)anana".contains("nana") only passes for i = 0 (mod 4), for when the underflow is fixed.

So e.g. "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxanana".contains("nana") fails.

Edit: You can pad the tail with an arbitrary number of x's, it doesn't change the behaviour.

bors added a commit that referenced this issue Aug 19, 2014
This incidentally fixes #16589, because it will cause `MatchIndices` to use `NaiveSearcher` instead of `TwoWaySearcher`, but I'm not sure #16589 should be closed until the underlying problem in `TwoWaySearcher` is found.
@pcwalton pcwalton reopened this Aug 19, 2014
nham added a commit to nham/rust that referenced this issue Aug 20, 2014
There is a check in TwoWaySearcher::new to determine whether the needle
is periodic. This is needed because during searching when a match fails,
we cannot advance the position by the entire length of the needle when
it is periodic, but can only advance by the length of the period.

The reason "bananas".contains("nana") (and similar searches) were
returning false was because the periodicity check was wrong.

Closes rust-lang#16589
bors added a commit that referenced this issue Aug 20, 2014
(By chance, the `contains` example was actually what I was trying to write when I discovered #16589)
bors added a commit that referenced this issue Aug 23, 2014
There is a check in TwoWaySearcher::new to determine whether the needle is periodic. This is needed because during searching when a match fails, we cannot advance the position by the entire length of the needle when it is periodic, but can only advance by the length of the period.

The reason "bananas".contains("nana") (and similar searches) were returning false was because the periodicity check was wrong.

Closes #16589

Also, thanks to @gankro, who came up with many buggy examples.
bors added a commit to rust-lang-ci/rust that referenced this issue Feb 25, 2024
…eykril

feat: add unresolved-ident diagnostic

This should cover missing local variables and missing unit structs and the like. It's conservatively marked experimental
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants