sre_engine: resume the tail match at prefix_skip, not past the whole prefix - #8473
Merged
Merged
Conversation
…prefix `search_info_literal`'s len>1 arm reset the tail-match cursor by advancing one character past the matched prefix. That position equals the INFO block's prefix_skip boundary only when the prefix ends where the skip does; for a pattern like `ab(cd)`, `_get_literal_prefix` reports prefix_len=4 with prefix_skip=2, so the tail resumed two characters too far. `state.cursor` already holds `req.start + skip`, which is what `sre_lib.h` SRE(search) computes as `ptr - (prefix_len - prefix_skip - 1)`, and which the len==1 arm above already uses. Take it unconditionally. Searching "xabcdcd" for `ab(cd)` returned span (1, 7) instead of (1, 5); over a 600-case corpus stamped with CPython's own answers, 94 cases disagreed before this change and none after. The wrong resume also dropped matches outright, not only widened them. Assisted-by: Claude
Contributor
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThe literal search cursor now resumes at the post-prefix skip position. A regression test covers patterns with a literal prefix longer than the skip value. ChangesLiteral search cursor handling
Estimated code review effort: 2 (Simple) | ~10 minutes Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
youknowone
marked this pull request as ready for review
August 9, 2026 04:34
kyokuping
pushed a commit
to kyokuping/RustPython
that referenced
this pull request
Aug 9, 2026
…prefix (RustPython#8473) `search_info_literal`'s len>1 arm reset the tail-match cursor by advancing one character past the matched prefix. That position equals the INFO block's prefix_skip boundary only when the prefix ends where the skip does; for a pattern like `ab(cd)`, `_get_literal_prefix` reports prefix_len=4 with prefix_skip=2, so the tail resumed two characters too far. `state.cursor` already holds `req.start + skip`, which is what `sre_lib.h` SRE(search) computes as `ptr - (prefix_len - prefix_skip - 1)`, and which the len==1 arm above already uses. Take it unconditionally. Searching "xabcdcd" for `ab(cd)` returned span (1, 7) instead of (1, 5); over a 600-case corpus stamped with CPython's own answers, 94 cases disagreed before this change and none after. The wrong resume also dropped matches outright, not only widened them. Assisted-by: Claude
youknowone
added a commit
that referenced
this pull request
Sep 16, 2026
…prefix (#8473) `search_info_literal`'s len>1 arm reset the tail-match cursor by advancing one character past the matched prefix. That position equals the INFO block's prefix_skip boundary only when the prefix ends where the skip does; for a pattern like `ab(cd)`, `_get_literal_prefix` reports prefix_len=4 with prefix_skip=2, so the tail resumed two characters too far. `state.cursor` already holds `req.start + skip`, which is what `sre_lib.h` SRE(search) computes as `ptr - (prefix_len - prefix_skip - 1)`, and which the len==1 arm above already uses. Take it unconditionally. Searching "xabcdcd" for `ab(cd)` returned span (1, 7) instead of (1, 5); over a 600-case corpus stamped with CPython's own answers, 94 cases disagreed before this change and none after. The wrong resume also dropped matches outright, not only widened them. Assisted-by: Claude
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
re.searchreturns wrong spans — and misses matches outright — for any pattern whose literal prefix extends past the INFO block'sprefix_skipboundary.re.match/re.fullmatchare unaffected — only thesearchliteral-prefix fast path is wrong. A non-capturing group is also fine, because-(?:ab)setsSRE_INFO_LITERALand returns early.Cause
search_info_literal'slen > 1arm advanced the tail-match cursor one character past the matched prefix:ctx.cursorsits on the last prefix character, soadvance_charlands onstart + prefix_len. The tail has to resume atstart + prefix_skip; the two agree only when the prefix happens to end where the skip does._get_literal_prefixextends the prefix through a capturing group while pinningprefix_skipat the group boundary, so the two routinely differ.ab(cd)compiles toprefix_len=4, prefix_skip=2:so the tail resumed two characters too far and re-matched
cdat 5..7.The code position was already advanced correctly by
ctx.skip_code(2 * skip)— only the string cursor was wrong.state.cursoralready holds exactly the right value:state.reset(req, req.start)followed byS::skip(&mut state.cursor, skip)makes itreq.start + skip. That matchessre_lib.hSRE(search), which setsstate->ptr = ptr - (prefix_len - prefix_skip - 1), and it is what thelen == 1arm a few lines above already does (next_ctx.skip_char::<S>(skip)). So the conditional can just go away.Verification
Both the pristine and patched engine were linked against a corpus stamped with CPython's own
re.searchanswers (30 patterns x 20 subjects = 600 cases), so each is judged against CPython rather than against the other:Many of the 94 are
want=(2,6) got=(-1,-1)— the wrong resume drops matches, it does not only widen them.The added
tests/tests.rs::search_literal_prefix_longer_than_skipfails on the unpatched engine (assertion left == right, right: 5) and passes with the fix. The crate's other 19 tests are unchanged and still pass, including the existinginfo_literalandinfo_single, which cover the same function's other arms.— commented by Claude
Summary by CodeRabbit
Bug Fixes
Tests