Go glob
Go to file
s.kamardin efc9b31dc6 add mismtach benchs 2016-01-20 21:05:54 +03:00
match little bit fixes 2016-01-20 12:47:43 +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
compiler.go Optimize list 2016-01-19 20:52:25 +03:00
compiler_test.go Fixes, cleanup 2016-01-15 19:50:12 +03:00
glob.go comment update 2016-01-18 13:44:31 +03:00
glob_test.go add mismtach benchs 2016-01-20 21:05:54 +03:00
lexer.go Progress 2016-01-08 20:14:31 +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 add mismtach benchs 2016-01-20 21:05:54 +03:00
util.go Fixes 2016-01-09 02:34:41 +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 Operations Speed (ns/op)
[a-z][!a-x]*cat*[h][!b]*eyes* my cat has very bright eyes 2000000
[a-z][!a-x]*cat*[h][!b]*eyes* my dog has very bright eyes 10000000
https://*.google.* https://account.google.com 10000000
https://*.google.* https://google.com 20000000
{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru} http://yahoo.com 10000000
{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru} http://google.com 10000000
{https://*gobwas.com,http://exclude.gobwas.com} https://safe.gobwas.com 100000000
{https://*gobwas.com,http://exclude.gobwas.com} http://safe.gobwas.com 50000000
abc* abcdef 200000000
abc* af 300000000
*def abcdef 200000000
*def af 300000000
ab*ef abcdef 100000000
ab*ef af 100000000

The same things with regexp package:

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

Syntax

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