74

GitHub - gobwas/glob: Go glob

 5 years ago
source link: https://github.com/gobwas/glob
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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
    
    // 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.


Recommend

  • 195
    • Github github.com 7 years ago
    • Cache

    GitHub - matryx/calcflow

    Calcflow A Virtual Reality Tool for Mathematical Modeling! The repository contains the open-sourced code to Calcflow, a powerful mathematical visualization tool designed to give students, educators, and engineers a better grasp on...

  • 217
    • Github github.com 7 years ago
    • Cache

    Release 5.0 · javaee/glassfish · GitHub

    Release 5.0 · javaee/glassfish · GitHub ...

  • 465

    README.md ViaBTC Exchange Server ViaBTC Exchange Server is a trading backend with high-speed performance, designed for cryptocurrency exchanges. It can...

  • 204
    • Github github.com 7 years ago
    • Cache

    GitHub - Kyubyong/pytorch_exercises

    Pytorch Exercises Pytorch is one of the most popular deep learning libraries as of 2017. One possible way of familiarizing yourself with it, I think, is to practice with simple quizzes. That's where this project comes in. The outline will...

  • 113
    • Github github.com 7 years ago
    • Cache

    GitHub - Bytom/bytom: Bytom

    Bytom Official golang implementation of the Bytom protocol. Automated builds are available for stable releases and the unstable master branch. Binary archives are published at

  • 338
    • Github github.com 7 years ago
    • Cache

    GitHub - tmux/tmux: tmux source code

    Welcome to tmux! tmux is a terminal multiplexer: it enables a number of terminals to be created, accessed, and controlled from a single screen. tmux may be detached from a screen and continue running in the background, then later reattach...

  • 380
    • 掘金 juejin.im 7 years ago
    • Cache

    玩转Github的新姿势-github-cli

    惊鸿一瞥 这是一个可以帮助你在命令行中完成Github的各种操作的cli工具。 创建它的初衷是因为我是一个命令行狂热爱好者,与此同时我也是一个Github的fans,每当我在命令行环境中进行开发工作时,如果此时我想看看Github上又诞生了什么新的有趣的开源项

  • 109

    README.md ws

  • 45

    Couldn’t find any package by glob ‘dotnet-sdk-6.0’If you encounter errors when install dotnet-sdk on Linux using dpkg or apt-get you may need to purge your microsoft packages like so:sudo dpkg --purge packages-mic...

  • 6

    Fix missing attribute merge on glob foreign re-exports #114012

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK