Go glob
Go to file
gobwas b7874cc364 cleanup specials 2016-02-25 00:42:13 +03:00
cmd fixes 2016-02-24 20:23:24 +03:00
match cleanup 2016-02-24 23:10:13 +03:00
runes optimizations with cached segments 2016-02-23 14:46:20 +03:00
strings use pool of segments 2016-02-02 22:03:37 +03:00
.gitignore Tune, new feature test 2016-01-14 21:32:02 +03:00
.travis.yml Tune, new feature test 2016-01-14 21:32:02 +03:00
bench.sh tune script 2016-02-24 23:25:01 +03:00
compiler.go todo 2016-02-25 00:41:52 +03:00
compiler_test.go Use of constuctors every where, optimizations 2016-02-24 12:36:15 +03:00
glob.go add quote meta func 2016-02-25 00:31:30 +03:00
glob_test.go add quote meta func 2016-02-25 00:31:30 +03:00
lexer.go cleanup specials 2016-02-25 00:42:13 +03:00
lexer_test.go Progress 2016-01-08 20:14:31 +03:00
parser.go Fixes 2016-01-09 02:34:41 +03:00
parser_test.go Progress 2016-01-08 20:14:31 +03:00
readme.md update result of benchmarks 2016-02-24 23:33:18 +03:00

readme.md

glob.go

GoDoc Build Status

Go Globbing Library.

Install

    go get github.com/gobwas/glob

Example


package main

import "github.com/gobwas/glob"

func main() {
    var g glob.Glob
    
    // create simple glob
    g = glob.MustCompile("*.github.com")
    g.Match("api.github.com") // true
    
    // create new glob with set of delimiters as ["."]
    g = glob.MustCompile("api.*.com", '.')
    g.Match("api.github.com") // true
    g.Match("api.gi.hub.com") // false
    
    // create new glob with set of delimiters as ["."]
    // but now with super wildcard
    g = glob.MustCompile("api.**.com", '.')
    g.Match("api.github.com") // true
    g.Match("api.gi.hub.com") // true
        
    // create glob with single symbol wildcard
    g = glob.MustCompile("?at")
    g.Match("cat") // true
    g.Match("fat") // true
    g.Match("at") // false
    
    // create glob with single symbol wildcard and delimiters ['f']
    g = glob.MustCompile("?at", 'f')
    g.Match("cat") // true
    g.Match("fat") // false
    g.Match("at") // false 
    
    // create glob with character-list matchers 
    g = glob.MustCompile("[abc]at")
    g.Match("cat") // true
    g.Match("bat") // true
    g.Match("fat") // false
    g.Match("at") // false
    
    // create glob with character-list matchers 
    g = glob.MustCompile("[!abc]at")
    g.Match("cat") // false
    g.Match("bat") // false
    g.Match("fat") // true
    g.Match("at") // false 
    
    // create glob with character-range matchers 
    g = glob.MustCompile("[a-c]at")
    g.Match("cat") // true
    g.Match("bat") // true
    g.Match("fat") // false
    g.Match("at") // false
    
    // create glob with character-range matchers 
    g = glob.MustCompile("[!a-c]at")
    g.Match("cat") // false
    g.Match("bat") // false
    g.Match("fat") // true
    g.Match("at") // false 
    
    
    // create glob with pattern-alternatives list 
    g = glob.MustCompile("{cat,bat,[fr]at}")
    g.Match("cat") // true
    g.Match("bat") // true
    g.Match("fat") // true
    g.Match("rat") // true
    g.Match("at") // false 
    g.Match("zat") // false 
}

Performance

This library is created for compile-once patterns. This means, that compilation could take time, but strings matching is done faster, than in case when always parsing template.

If you will not use compiled glob.Glob object, and do g := glob.MustCompile(pattern); g.Match(...) every time, then your code will be much more slower.

Run go test -bench=. from source root to see the benchmarks:

Pattern Fixture Match Operations Speed (ns/op)
[a-z][!a-x]*cat*[h][!b]*eyes* my cat has very bright eyes true 2000000 432
[a-z][!a-x]*cat*[h][!b]*eyes* my dog has very bright eyes false 10000000 199
https://*.google.* https://account.google.com true 10000000 96
https://*.google.* https://google.com false 20000000 66
{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru} http://yahoo.com true 10000000 163
{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru} http://google.com false 10000000 197
{https://*gobwas.com,http://exclude.gobwas.com} https://safe.gobwas.com true 100000000 22
{https://*gobwas.com,http://exclude.gobwas.com} http://safe.gobwas.com false 50000000 24
abc* abcdef true 200000000 8.15
abc* af false 300000000 5.68
*def abcdef true 200000000 8.84
*def af false 300000000 5.74
ab*ef abcdef true 100000000 15.2
ab*ef af false 100000000 10.4

The same things with regexp package:

Pattern Fixture Match Operations Speed (ns/op)
^[a-z][^a-x].*cat.*[h][^b].*eyes.*$ my cat has very bright eyes true 500000 2553
^[a-z][^a-x].*cat.*[h][^b].*eyes.*$ my dog has very bright eyes false 1000000 1383
^https:\/\/.*\.google\..*$ https://account.google.com true 1000000 1205
^https:\/\/.*\.google\..*$ https://google.com false 2000000 767
`^(https://..google.. .yandex.. .yahoo.. .*mail.ru)$` http://yahoo.com
`^(https://..google.. .yandex.. .yahoo.. .*mail.ru)$` http://google.com
`^(https://.*gobwas.com http://exclude.gobwas.com)$` https://safe.gobwas.com true 1000000
`^(https://.*gobwas.com http://exclude.gobwas.com)$` http://safe.gobwas.com false 5000000
^abc.*$ abcdef true 5000000 237
^abc.*$ af false 20000000 100
^.*def$ abcdef true 5000000 464
^.*def$ af false 5000000 265
^ab.*ef$ abcdef true 5000000 375
^ab.*ef$ af false 10000000 145

Syntax

Syntax is inspired by standard wildcards, except that ** is aka super-asterisk, that do not sensitive for separators.