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

Binary search is used with incorrectly-sorted array #75

Closed
SimonSapin opened this issue Apr 19, 2015 · 4 comments
Closed

Binary search is used with incorrectly-sorted array #75

SimonSapin opened this issue Apr 19, 2015 · 4 comments

Comments

@SimonSapin
Copy link
Contributor

The “dynamic” matching of CharClass uses a binary search within char ranges, which relies on the input being sorted. The input is indeed sorted in its “natural” order, but the comparison function in case-insensitive mode uses a different order.

This leads to incorrect results:

assert!(Regex::new(r"(?i)[a_]+$").unwrap().is_match("A_"));

The above fails, because _ in ASCII is between upper-case and lower-case letters. The comparison function maps the 'a'..'a' range to its upper case 'A'..'A', which has a different order relative to '_'..'_'.

Compare with e.g. the code below, which succeeds.

assert!(Regex::new(r"(?i)[a=]+$").unwrap().is_match("A="));

The comparison function has a FIXME to move the case mapping outside of it and have the Vec of ranges be already mapped. I assume this was intended for performance, but I think it’ll also fix this issue. I’ll try it.

@BurntSushi
Copy link
Member

Nice find! If you find an easy fix, that would be awesome. Otherwise, I am in the process of rewriting the parser with the intention of fixing a bunch of little bugaboos like this. (Although I didn't realize this caused buggy behavior!)

@SimonSapin
Copy link
Contributor Author

I have a fix, but I’m also tracking down another bug. Let we write up an issue.

@SimonSapin
Copy link
Contributor Author

#75 is that issue. #78 has fixes for both and #55.

@BurntSushi
Copy link
Member

Awesome! I'm currently out, but I'll review as soon as I get home and get this merged. Thank you!

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 a pull request may close this issue.

2 participants