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

Inconsistency between documentation and code implementation #26

Open
YichiZhang0613 opened this issue Feb 21, 2024 · 2 comments
Open

Inconsistency between documentation and code implementation #26

YichiZhang0613 opened this issue Feb 21, 2024 · 2 comments

Comments

@YichiZhang0613
Copy link

I noticed a possible inconsistency between documentation and code implementation in rust-algorithms/src/string_proc.rs. The details can be found in the following code. The code does not check whether pattern is not empty before it is used but the constraint exists in the documentation.

    /// # Panics
    ///
    /// Panics if pattern is empty.
    pub fn new(pattern: &'a [C]) -> Self {
        let mut fail = Vec::with_capacity(pattern.len());
        fail.push(0);
        let mut len = 0;
        for ch in &pattern[1..] {
            while len > 0 && pattern[len] != *ch {
                len = fail[len - 1];
            }
            if pattern[len] == *ch {
                len += 1;
            }
            fail.push(len);
        }
        Self { pattern, fail }
    }
@EbTech
Copy link
Owner

EbTech commented Feb 23, 2024

I believe it panics on &pattern[1..], doesn't it?

@YichiZhang0613
Copy link
Author

I believe it panics on &pattern[1..], doesn't it?

If pattern is empty as your documentation read then the code would panic. So I think the code should check whether pattern is empty before it is used.

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

No branches or pull requests

2 participants