pax_global_header00006660000000000000000000000064146234471650014526gustar00rootroot0000000000000052 comment=71c1b4e1049734961a7015b954ceb4130dadbf08 becheran-wildmatch-go-71c1b4e/000077500000000000000000000000001462344716500162625ustar00rootroot00000000000000becheran-wildmatch-go-71c1b4e/.github/000077500000000000000000000000001462344716500176225ustar00rootroot00000000000000becheran-wildmatch-go-71c1b4e/.github/workflows/000077500000000000000000000000001462344716500216575ustar00rootroot00000000000000becheran-wildmatch-go-71c1b4e/.github/workflows/go.yml000066400000000000000000000006631462344716500230140ustar00rootroot00000000000000name: CI on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - name: Set up Go uses: actions/setup-go@v2 with: go-version: 1.15 - name: Build run: go build -v ./... - name: Test run: go test -v ./... - name: Markup Link Checker (mlc) uses: becheran/mlc@v0.14.0 becheran-wildmatch-go-71c1b4e/.gitignore000066400000000000000000000004151462344716500202520ustar00rootroot00000000000000# Binaries for programs and plugins *.exe *.exe~ *.dll *.so *.dylib # Test binary, built with `go test -c` *.test # Output of the go coverage tool, specifically when used with LiteIDE *.out # Dependency directories (remove the comment below to include it) # vendor/ becheran-wildmatch-go-71c1b4e/LICENSE000066400000000000000000000020551462344716500172710ustar00rootroot00000000000000MIT License Copyright (c) 2021 Armin Becher Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. becheran-wildmatch-go-71c1b4e/README.md000066400000000000000000000027451462344716500175510ustar00rootroot00000000000000# wildmatch go [![](https://godoc.org/github.com/becheran/wildmatch-go?status.svg)](https://godoc.org/github.com/becheran/wildmatch-go) [![GoDev](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/github.com/becheran/wildmatch-go) [![Sourcegraph](https://sourcegraph.com/github.com/becheran/wildmatch-go/-/badge.svg)](https://sourcegraph.com/github.com/becheran/wildmatch-go?badge) [![Go Report Card](https://goreportcard.com/badge/becheran/wildmatch-go)](https://goreportcard.com/report/becheran/wildmatch-go) ![GitHub Workflow Status](https://github.com/becheran/wildmatch-go/workflows/CI/badge.svg) *golang* library of the original *rust* [wildmatch library](https://github.com/becheran/wildmatch). ``` sh go get github.com/becheran/wildmatch-go ``` Match strings against a simple wildcard pattern. Tests a wildcard pattern `p` against an input string `s`. Returns true only when `p` matches the entirety of `s`. See also the example described on [wikipedia](https://en.wikipedia.org/wiki/Matching_wildcards) for matching wildcards. - `?` matches exactly one occurrence of any character. - `*` matches arbitrary many (including zero) occurrences of any character. - No escape characters are defined. For example the pattern `ca?` will match `cat` or `car`. The pattern `https://*` will match all https urls, such as `https://google.de` or `https://github.com/becheran/wildmatch`. The library only depends on the go standard library. becheran-wildmatch-go-71c1b4e/go.mod000066400000000000000000000000611462344716500173650ustar00rootroot00000000000000module github.com/becheran/wildmatch-go go 1.15 becheran-wildmatch-go-71c1b4e/wildmatch.go000066400000000000000000000046361462344716500205760ustar00rootroot00000000000000// Package wildmatch used to match strings against a simple wildcard pattern. // Tests a wildcard pattern `p` against an input string `s`. Returns true only when `p` matches the entirety of `s`. // // See also the example described on [wikipedia](https://en.wikipedia.org/wiki/Matching_wildcards) for matching wildcards. // // No escape characters are defined. // // - `?` matches exactly one occurrence of any character. // - `*` matches arbitrary many (including zero) occurrences of any character. // // Examples matching wildcards: // ``` go // import "github.com/becheran/wildmatch-go" // wildmatch.NewWildMatch("cat").Matches("cat") // wildmatch.NewWildMatch("*cat*").Matches("dog_cat_dog") // wildmatch.NewWildMatch("c?t").Matches("cat") // wildmatch.NewWildMatch("c?t").Matches("cot") // ``` // Examples not matching wildcards: // ``` go // import "github.com/becheran/wildmatch-go" // wildmatch.NewWildMatch("dog").Matches("cat") // wildmatch.NewWildMatch("*d").Matches("cat") // wildmatch.NewWildMatch("????").Matches("cat") // wildmatch.NewWildMatch("?").Matches("cat") // ``` package wildmatch /// WildMatch is a wildcard matcher used to match strings. type WildMatch struct { pattern string } func (w *WildMatch) String() string { return w.pattern } // NewWildMatch creates new pattern matcher. func NewWildMatch(pattern string) *WildMatch { simplified := "" prevWasStar := false for _, currentChar := range pattern { if currentChar == '*' { if !prevWasStar { simplified += "*" } prevWasStar = true } else { prevWasStar = false simplified += string(currentChar) } } return &WildMatch{ pattern: simplified, } } // Matches indicates whether the matcher finds a match in the input string. func (w *WildMatch) Matches(input string) bool { if w.pattern == "" { return input == "" } idxInput := 0 idxPattern := 0 idxStart := -1 idxMatch := 0 for idxInput < len(input) { if idxPattern < len(w.pattern) && w.pattern[idxPattern] == '*' { idxStart = idxPattern idxMatch = idxInput idxPattern++ } else if idxPattern < len(w.pattern) && (w.pattern[idxPattern] == '?' || w.pattern[idxPattern] == input[idxInput]) { idxInput++ idxPattern++ } else if idxStart != -1 { idxPattern = idxStart + 1 idxMatch++ idxInput = idxMatch } else { return false } } for idxPattern < len(w.pattern) && w.pattern[idxPattern] == '*' { idxPattern++ } return idxPattern == len(w.pattern) } becheran-wildmatch-go-71c1b4e/wildmatch_test.go000066400000000000000000000111571462344716500216310ustar00rootroot00000000000000package wildmatch_test import ( "bytes" "math/rand" "testing" "github.com/becheran/wildmatch-go" ) func TestIsMatchRandom(t *testing.T) { const permutations = 1_000 randomString := func(length int) string { const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*?" b := make([]byte, length) for i := range b { b[i] = letterBytes[rand.Intn(len(letterBytes))] } return string(b) } for patterIdx := 0; patterIdx < permutations; patterIdx++ { pattern := make([]byte, 100) for i := range pattern { pattern[i] = randomString(1)[0] } for i := 0; i < rand.Intn(25); i++ { pattern[rand.Intn(len(pattern))] = '*' } for i := 0; i < rand.Intn(25); i++ { pattern[rand.Intn(len(pattern))] = '?' } for inputIdx := 0; inputIdx < permutations; inputIdx++ { input := bytes.NewBuffer(nil) for _, p := range pattern { if p == '*' { input.WriteString(randomString(rand.Intn(15))) } else if p == '?' { input.WriteString(randomString(1)) } else { input.WriteByte(p) } } m := wildmatch.NewWildMatch(string(pattern)) if !m.Matches(input.String()) { t.Fatalf("Pattern\n%s\nInput:\n%s", m, input) } } } } func TestIsMatch(t *testing.T) { pattern := []struct { pattern string target string result bool }{ // Match {pattern: "a*b", target: "a*xb", result: true}, {pattern: "**", target: "cat", result: true}, {pattern: "*", target: "cat", result: true}, {pattern: "*?*", target: "cat", result: true}, {pattern: "c*", target: "cat", result: true}, {pattern: "c?*", target: "cat", result: true}, {pattern: "???", target: "cat", result: true}, {pattern: "c?t", target: "cat", result: true}, {pattern: "cat", target: "cat", result: true}, {pattern: "*cat", target: "cat", result: true}, {pattern: "cat*", target: "cat", result: true}, {pattern: "*cat*", target: "d&(*og_cat_dog", result: true}, {pattern: "*?*", target: "d&(*og_cat_dog", result: true}, {pattern: "*a*", target: "d&(*og_cat_dog", result: true}, {pattern: "*", target: "*", result: true}, {pattern: "*", target: "?", result: true}, {pattern: "?", target: "?", result: true}, {pattern: "wildcats", target: "wildcats", result: true}, {pattern: "wild*cats", target: "wild?cats", result: true}, {pattern: "wi*ca*s", target: "wildcats", result: true}, {pattern: "wi*ca?s", target: "wildcats", result: true}, {pattern: "*o?", target: "hog_cat_dog", result: true}, {pattern: "*o?", target: "cat_dog", result: true}, {pattern: "*at_dog", target: "cat_dog", result: true}, {pattern: " ", target: " ", result: true}, {pattern: "* ", target: "\n ", result: true}, {pattern: "\n", target: "\n", result: true}, {pattern: "*32", target: "432", result: true}, {pattern: "*32", target: "332", result: true}, {pattern: "*32", target: "3332", result: true}, {pattern: "33*", target: "333", result: true}, {pattern: " ", target: " ", result: true}, // No Match {pattern: "*d*", target: "cat", result: false}, {pattern: "*d", target: "cat", result: false}, {pattern: "d*", target: "cat", result: false}, {pattern: "*c", target: "cat", result: false}, {pattern: "?", target: "cat", result: false}, {pattern: "??", target: "cat", result: false}, {pattern: "????", target: "cat", result: false}, {pattern: "?????", target: "cat", result: false}, {pattern: "*????", target: "cat", result: false}, {pattern: "cats", target: "cat", result: false}, {pattern: "cat?", target: "cat", result: false}, {pattern: "cacat", target: "cat", result: false}, {pattern: "cat*dog", target: "cat", result: false}, {pattern: "cat?", target: "wildcats", result: false}, {pattern: "cat*", target: "wildcats", result: false}, {pattern: "*x*", target: "wildcats", result: false}, {pattern: "*a", target: "wildcats", result: false}, {pattern: "", target: "wildcats", result: false}, {pattern: " ", target: "wildcats", result: false}, {pattern: "???", target: "wildcats", result: false}, {pattern: " ", target: "\n", result: false}, {pattern: " ", target: "\t", result: false}, } for _, p := range pattern { t.Run(p.pattern+"_"+p.target, func(t *testing.T) { m := wildmatch.NewWildMatch(p.pattern) result := m.Matches(p.target) if result != p.result { t.Fatalf("expected: %v, got: %v", p.result, result) } }) } } func TestString(t *testing.T) { const input = "Lorem?ipsum*dolore*ea* ?????ata*." m := wildmatch.NewWildMatch(input) if m.String() != input { t.Fatalf("expected: %v, got: %v", input, m.String()) } } func Benchmark_PatternCompile(b *testing.B) { for n := 0; n < b.N; n++ { wildmatch.NewWildMatch("Lorem?ipsum*dolore*ea* ?????ata*.") } }