🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

github.com/gobwas/glob

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/gobwas/glob

v0.2.3
Source
Go
Version published
Created
Source

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
    
    // quote meta characters and then create simple glob 
    g = glob.MustCompile(glob.QuoteMeta("*.github.com"))
    g.Match("*.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:

PatternFixtureMatchSpeed (ns/op)
[a-z][!a-x]*cat*[h][!b]*eyes*my cat has very bright eyestrue432
[a-z][!a-x]*cat*[h][!b]*eyes*my dog has very bright eyesfalse199
https://*.google.*https://account.google.comtrue96
https://*.google.*https://google.comfalse66
{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru}http://yahoo.comtrue163
{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru}http://google.comfalse197
{https://*gobwas.com,http://exclude.gobwas.com}https://safe.gobwas.comtrue22
{https://*gobwas.com,http://exclude.gobwas.com}http://safe.gobwas.comfalse24
abc*abcdeftrue8.15
abc*affalse5.68
*defabcdeftrue8.84
*defaffalse5.74
ab*efabcdeftrue15.2
ab*efaffalse10.4

The same things with regexp package:

PatternFixtureMatchSpeed (ns/op)
^[a-z][^a-x].*cat.*[h][^b].*eyes.*$my cat has very bright eyestrue2553
^[a-z][^a-x].*cat.*[h][^b].*eyes.*$my dog has very bright eyesfalse1383
^https:\/\/.*\.google\..*$https://account.google.comtrue1205
^https:\/\/.*\.google\..*$https://google.comfalse767
`^(https://..google...yandex...yahoo...*mail.ru)$`
`^(https://..google...yandex...yahoo...*mail.ru)$`
`^(https://.*gobwas.comhttp://exclude.gobwas.com)$`https://safe.gobwas.comtrue
`^(https://.*gobwas.comhttp://exclude.gobwas.com)$`http://safe.gobwas.comfalse
^abc.*$abcdeftrue237
^abc.*$affalse100
^.*def$abcdeftrue464
^.*def$affalse265
^ab.*ef$abcdeftrue375
^ab.*ef$affalse145

Syntax

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

FAQs

Package last updated on 08 Feb 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts