pax_global_header00006660000000000000000000000064134616475330014526gustar00rootroot0000000000000052 comment=45a9d7489f27f1cce19e326b617cad9576f6b260 golang-github-ryanuber-go-glob-1.0.0/000077500000000000000000000000001346164753300174245ustar00rootroot00000000000000golang-github-ryanuber-go-glob-1.0.0/.travis.yml000066400000000000000000000000661346164753300215370ustar00rootroot00000000000000language: go go: - tip script: - go test -v ./... golang-github-ryanuber-go-glob-1.0.0/LICENSE000066400000000000000000000020641346164753300204330ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2014 Ryan Uber 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. golang-github-ryanuber-go-glob-1.0.0/README.md000066400000000000000000000022151346164753300207030ustar00rootroot00000000000000# String globbing in golang [![Build Status](https://travis-ci.org/ryanuber/go-glob.svg)](https://travis-ci.org/ryanuber/go-glob) `go-glob` is a single-function library implementing basic string glob support. Globs are an extremely user-friendly way of supporting string matching without requiring knowledge of regular expressions or Go's particular regex engine. Most people understand that if you put a `*` character somewhere in a string, it is treated as a wildcard. Surprisingly, this functionality isn't found in Go's standard library, except for `path.Match`, which is intended to be used while comparing paths (not arbitrary strings), and contains specialized logic for this use case. A better solution might be a POSIX basic (non-ERE) regular expression engine for Go, which doesn't exist currently. Example ======= ``` package main import "github.com/ryanuber/go-glob" func main() { glob.Glob("*World!", "Hello, World!") // true glob.Glob("Hello,*", "Hello, World!") // true glob.Glob("*ello,*", "Hello, World!") // true glob.Glob("World!", "Hello, World!") // false glob.Glob("/home/*", "/home/ryanuber/.bashrc") // true } ``` golang-github-ryanuber-go-glob-1.0.0/glob.go000066400000000000000000000025221346164753300206770ustar00rootroot00000000000000package glob import "strings" // The character which is treated like a glob const GLOB = "*" // Glob will test a string pattern, potentially containing globs, against a // subject string. The result is a simple true/false, determining whether or // not the glob pattern matched the subject text. func Glob(pattern, subj string) bool { // Empty pattern can only match empty subject if pattern == "" { return subj == pattern } // If the pattern _is_ a glob, it matches everything if pattern == GLOB { return true } parts := strings.Split(pattern, GLOB) if len(parts) == 1 { // No globs in pattern, so test for equality return subj == pattern } leadingGlob := strings.HasPrefix(pattern, GLOB) trailingGlob := strings.HasSuffix(pattern, GLOB) end := len(parts) - 1 // Go over the leading parts and ensure they match. for i := 0; i < end; i++ { idx := strings.Index(subj, parts[i]) switch i { case 0: // Check the first section. Requires special handling. if !leadingGlob && idx != 0 { return false } default: // Check that the middle parts match. if idx < 0 { return false } } // Trim evaluated text from subj as we loop over the pattern. subj = subj[idx+len(parts[i]):] } // Reached the last section. Requires special handling. return trailingGlob || strings.HasSuffix(subj, parts[end]) } golang-github-ryanuber-go-glob-1.0.0/glob_test.go000066400000000000000000000046651346164753300217500ustar00rootroot00000000000000package glob import ( "strings" "testing" ) func testGlobMatch(t *testing.T, pattern, subj string) { if !Glob(pattern, subj) { t.Fatalf("%s should match %s", pattern, subj) } } func testGlobNoMatch(t *testing.T, pattern, subj string) { if Glob(pattern, subj) { t.Fatalf("%s should not match %s", pattern, subj) } } func TestEmptyPattern(t *testing.T) { testGlobMatch(t, "", "") testGlobNoMatch(t, "", "test") } func TestEmptySubject(t *testing.T) { for _, pattern := range []string{ "", "*", "**", "***", "****************", strings.Repeat("*", 1000000), } { testGlobMatch(t, pattern, "") } for _, pattern := range []string{ // No globs/non-glob characters "test", "*test*", // Trailing characters "*x", "*****************x", strings.Repeat("*", 1000000) + "x", // Leading characters "x*", "x*****************", "x" + strings.Repeat("*", 1000000), // Mixed leading/trailing characters "x*x", "x****************x", "x" + strings.Repeat("*", 1000000) + "x", } { testGlobNoMatch(t, pattern, "") } } func TestPatternWithoutGlobs(t *testing.T) { testGlobMatch(t, "test", "test") } func TestGlob(t *testing.T) { // Matches for _, pattern := range []string{ "*test", // Leading glob "this*", // Trailing glob "this*test", // Middle glob "*is *", // String in between two globs "*is*a*", // Lots of globs "**test**", // Double glob characters "**is**a***test*", // Varying number of globs "* *", // White space between globs "*", // Lone glob "**********", // Nothing but globs "*Ѿ*", // Unicode with globs "*is a ϗѾ *", // Mixed ASCII/unicode } { testGlobMatch(t, pattern, "this is a ϗѾ test") } // Non-matches for _, pattern := range []string{ "test*", // Implicit substring match "*is", // Partial match "*no*", // Globs without a match between them " ", // Plain white space "* ", // Trailing white space " *", // Leading white space "*ʤ*", // Non-matching unicode "this*this is a test", // Repeated prefix } { testGlobNoMatch(t, pattern, "this is a test") } } func BenchmarkGlob(b *testing.B) { for i := 0; i < b.N; i++ { if !Glob("*quick*fox*dog", "The quick brown fox jumped over the lazy dog") { b.Fatalf("should match") } } } golang-github-ryanuber-go-glob-1.0.0/go.mod000066400000000000000000000000431346164753300205270ustar00rootroot00000000000000module github.com/ryanuber/go-glob