• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

gobwas/glob: Go glob

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

gobwas/glob

开源软件地址:

https://github.com/gobwas/glob

开源编程语言:

Go 99.5%

开源软件介绍:

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:

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

The same things with regexp package:

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

Syntax

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




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
nu7hatch/gouuid: Go binding for libuuid发布时间:2022-06-13
下一篇:
kr/pretty: Pretty printing for Go values发布时间:2022-06-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap