feat: implement Longest() for stdlib compatibility (Fixes #26) - #27
Merged
Merged
Conversation
…#26) - Add longest bool flag to Regex struct - Implement SetLongest() in meta.Engine and PikeVM - Modify searchUnanchoredAt and searchUnanchoredWithCapturesAt for longest support - Modify searchAt and searchAtWithCaptures for longest support - Fix misleading documentation (was claiming leftmost-longest by default) - Add TestLongest and TestLongestMatchesStdlib tests - No performance regression in default (leftmost-first) mode Default: leftmost-first (Perl semantics) - first alternative wins Longest(): leftmost-longest (POSIX semantics) - longest match wins Example: re := coregex.MustCompile(`(a|ab)`) re.FindString("ab") // "a" (default: first branch wins) re.Longest() re.FindString("ab") // "ab" (longest match wins)
Benchmark ComparisonComparing Summary:
|
The linter was complaining about gocognit (>40) and nestif (>6) in the longest match logic. Extracted match comparison into isBetterMatch() helper method and simplified the remaining match detection blocks.
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.
Summary
Implements proper
Longest()method that switches from leftmost-first (Perl) to leftmost-longest (POSIX) matching semantics.Before:
Longest()was a no-op stub with misleading documentation claiming coregex used leftmost-longest by default.After:
Longest()actually works, matching Go stdlibregexp.Regexp.Longest()behavior.Changes
longest boolflag toRegexstructSetLongest()inmeta.Engineandnfa.PikeVMsearchUnanchoredAt()searchUnanchoredWithCapturesAt()searchAt()searchAtWithCaptures()Behavior
(a|ab)(#|#!)(cat|catalog)Performance
No regression in default mode!
Test plan
go test ./...passesTestLongest- verifies behavior change after Longest()TestLongestMatchesStdlib- verifies match with Go stdlibFixes #26