Skip to content

Add ability to compile a pattern#99

Open
lukemassa wants to merge 5 commits into
bmatcuk:masterfrom
lukemassa:add_compiled_pattern_matcher
Open

Add ability to compile a pattern#99
lukemassa wants to merge 5 commits into
bmatcuk:masterfrom
lukemassa:add_compiled_pattern_matcher

Conversation

@lukemassa

@lukemassa lukemassa commented Jan 23, 2025

Copy link
Copy Markdown
Contributor

What

Add ability to compile a Pattern once and use it for subsequent matches.

Note this is currently a naive implementation that just calls the existing Match(), I wanted to firstly gauge interest of the maintainers to see if this is a feature worth implementing, and also to discuss what the various exported identifiers should be.

Why

ValidatePattern(), allows the user to inspect whether there is an error before saving the pattern for later. This goes a step further, and removes the error from the analogous Match() function, since it cannot fail once compiled, making it a bit cleaner to use.

Additionally, presumably a compiled pattern could be made to be more efficient, though I haven't dug into the implementation there yet. My naive implementation is only very slightly better than Match(), since it makes use of the existing MatchUnvalidated, which skips some validation and therefore saves time.

I modeled the methods and signatures after regexp in go's standard library (quoted in part here):

func MatchString(pattern string, s string) (matched bool, err error)
type Regexp struct{ ... }
    func Compile(expr string) (*Regexp, error)
    func MustCompile(str string) *Regexp
    func (re *Regexp) Match(s string) bool

https://pkg.go.dev/regexp

Usage

    pattern, err := doublestar.Compile("foo/*")
    if err != nil {
        log.Fatal(err)
    }
    if pattern.Match("foo/bar") {
        fmt.Println("Pattern matches foo/bar")
    }
    if pattern.Match("bar/foo") {
        fmt.Println("Pattern matches bar/foo")
    }
    
    // Prints "Pattern matches foo/bar"

Testing

The compiled version is run through the same suite of tests as the normal version, which also serves to make sure they are in lock-step.

References

Closes: #85

@lukemassa lukemassa marked this pull request as ready for review January 24, 2025 19:32
@lukemassa lukemassa force-pushed the add_compiled_pattern_matcher branch 2 times, most recently from 45146b6 to 15cbf42 Compare January 25, 2025 17:50
@bmatcuk bmatcuk force-pushed the master branch 2 times, most recently from d82f9b0 to 06f110a Compare July 12, 2025 19:11
@codecov

codecov Bot commented Sep 30, 2025

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 60.00000% with 6 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (master@9fded31). Learn more about missing BASE report.

Files with missing lines Patch % Lines
match.go 60.00% 6 Missing ⚠️

❌ Your patch check has failed because the patch coverage (60.00%) is below the target coverage (70.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@            Coverage Diff            @@
##             master      #99   +/-   ##
=========================================
  Coverage          ?   85.64%           
=========================================
  Files             ?        5           
  Lines             ?      989           
  Branches          ?        0           
=========================================
  Hits              ?      847           
  Misses            ?      112           
  Partials          ?       30           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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 this pull request may close these issues.

Feature request: add functionality to compile globs

1 participant