Escape regex metacharacters in over-long search patterns - #33
Merged
Conversation
`SPECIAL_CHARS_REGEX` escaped its own leading bracket, so the character class never opened and matched nothing. The replacement string was a JS-ism too: Dart's `replaceAll` does not expand `$&`, so even with a correct class every metacharacter would have been replaced by the literal text `\$&` rather than escaped. The net effect was that no escaping happened at all. Patterns longer than `maxPatternLength` go down the regex path and were compiled raw, so an unbalanced `(`, an unterminated `[` or a trailing `\` threw a FormatException, and other metacharacters were quietly treated as regex syntax rather than matched literally. Fix the character class and switch to `replaceAllMapped` so the match can actually be referenced. Three of the five new tests fail without this change with the FormatExceptions above; the other two pin down the literal-matching behaviour, which the old code happened to get right. Both defects date back to the original Fuse.js port in 01e0f6a. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.
This issue was found by Claude as I was evaluating this library. I agree with the findings of the agent, and it closes an existing open issue (#23).
Claude report
SPECIAL_CHARS_REGEXescaped its own leading bracket, so the character class never opened and matched nothing. The replacement string was a JS-ism too: Dart'sreplaceAlldoes not expand$&, so even with a correct class every metacharacter would have been replaced by the literal text\$&rather than escaped.The net effect was that no escaping happened at all. Patterns longer than
maxPatternLengthgo down the regex path and were compiled raw, so an unbalanced(, an unterminated[or a trailing\threw a FormatException, and other metacharacters were quietly treated as regex syntax rather than matched literally.Fix the character class and switch to
replaceAllMappedso the match can actually be referenced. Three of the five new tests fail without this change with the FormatExceptions above; the other two pin down the literal-matching behaviour, which the old code happened to get right.Both defects date back to the original Fuse.js port in 01e0f6a.