You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
5.1 KiB

  1. # glob.[go](https://golang.org)
  2. [![GoDoc][godoc-image]][godoc-url] [![Build Status][travis-image]][travis-url]
  3. > Go Globbing Library.
  4. ## Install
  5. ```shell
  6. go get github.com/gobwas/glob
  7. ```
  8. ## Example
  9. ```go
  10. package main
  11. import "github.com/gobwas/glob"
  12. func main() {
  13. var g glob.Glob
  14. // create simple glob
  15. g = glob.MustCompile("*.github.com")
  16. g.Match("api.github.com") // true
  17. // quote meta characters and then create simple glob
  18. g = glob.MustCompile(glob.QuoteMeta("*.github.com"))
  19. g.Match("*.github.com") // true
  20. // create new glob with set of delimiters as ["."]
  21. g = glob.MustCompile("api.*.com", '.')
  22. g.Match("api.github.com") // true
  23. g.Match("api.gi.hub.com") // false
  24. // create new glob with set of delimiters as ["."]
  25. // but now with super wildcard
  26. g = glob.MustCompile("api.**.com", '.')
  27. g.Match("api.github.com") // true
  28. g.Match("api.gi.hub.com") // true
  29. // create glob with single symbol wildcard
  30. g = glob.MustCompile("?at")
  31. g.Match("cat") // true
  32. g.Match("fat") // true
  33. g.Match("at") // false
  34. // create glob with single symbol wildcard and delimiters ['f']
  35. g = glob.MustCompile("?at", 'f')
  36. g.Match("cat") // true
  37. g.Match("fat") // false
  38. g.Match("at") // false
  39. // create glob with character-list matchers
  40. g = glob.MustCompile("[abc]at")
  41. g.Match("cat") // true
  42. g.Match("bat") // true
  43. g.Match("fat") // false
  44. g.Match("at") // false
  45. // create glob with character-list matchers
  46. g = glob.MustCompile("[!abc]at")
  47. g.Match("cat") // false
  48. g.Match("bat") // false
  49. g.Match("fat") // true
  50. g.Match("at") // false
  51. // create glob with character-range matchers
  52. g = glob.MustCompile("[a-c]at")
  53. g.Match("cat") // true
  54. g.Match("bat") // true
  55. g.Match("fat") // false
  56. g.Match("at") // false
  57. // create glob with character-range matchers
  58. g = glob.MustCompile("[!a-c]at")
  59. g.Match("cat") // false
  60. g.Match("bat") // false
  61. g.Match("fat") // true
  62. g.Match("at") // false
  63. // create glob with pattern-alternatives list
  64. g = glob.MustCompile("{cat,bat,[fr]at}")
  65. g.Match("cat") // true
  66. g.Match("bat") // true
  67. g.Match("fat") // true
  68. g.Match("rat") // true
  69. g.Match("at") // false
  70. g.Match("zat") // false
  71. }
  72. ```
  73. ## Performance
  74. This library is created for compile-once patterns. This means, that compilation could take time, but
  75. strings matching is done faster, than in case when always parsing template.
  76. 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.
  77. Run `go test -bench=.` from source root to see the benchmarks:
  78. Pattern | Fixture | Match | Speed (ns/op)
  79. --------|---------|-------|--------------
  80. `[a-z][!a-x]*cat*[h][!b]*eyes*` | `my cat has very bright eyes` | `true` | 432
  81. `[a-z][!a-x]*cat*[h][!b]*eyes*` | `my dog has very bright eyes` | `false` | 199
  82. `https://*.google.*` | `https://account.google.com` | `true` | 96
  83. `https://*.google.*` | `https://google.com` | `false` | 66
  84. `{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru}` | `http://yahoo.com` | `true` | 163
  85. `{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru}` | `http://google.com` | `false` | 197
  86. `{https://*gobwas.com,http://exclude.gobwas.com}` | `https://safe.gobwas.com` | `true` | 22
  87. `{https://*gobwas.com,http://exclude.gobwas.com}` | `http://safe.gobwas.com` | `false` | 24
  88. `abc*` | `abcdef` | `true` | 8.15
  89. `abc*` | `af` | `false` | 5.68
  90. `*def` | `abcdef` | `true` | 8.84
  91. `*def` | `af` | `false` | 5.74
  92. `ab*ef` | `abcdef` | `true` | 15.2
  93. `ab*ef` | `af` | `false` | 10.4
  94. The same things with `regexp` package:
  95. Pattern | Fixture | Match | Speed (ns/op)
  96. --------|---------|-------|--------------
  97. `^[a-z][^a-x].*cat.*[h][^b].*eyes.*$` | `my cat has very bright eyes` | `true` | 2553
  98. `^[a-z][^a-x].*cat.*[h][^b].*eyes.*$` | `my dog has very bright eyes` | `false` | 1383
  99. `^https:\/\/.*\.google\..*$` | `https://account.google.com` | `true` | 1205
  100. `^https:\/\/.*\.google\..*$` | `https://google.com` | `false` | 767
  101. `^(https:\/\/.*\.google\..*|.*yandex\..*|.*yahoo\..*|.*mail\.ru)$` | `http://yahoo.com` | `true` | 1435
  102. `^(https:\/\/.*\.google\..*|.*yandex\..*|.*yahoo\..*|.*mail\.ru)$` | `http://google.com` | `false` | 1674
  103. `^(https:\/\/.*gobwas\.com|http://exclude.gobwas.com)$` | `https://safe.gobwas.com` | `true` | 1039
  104. `^(https:\/\/.*gobwas\.com|http://exclude.gobwas.com)$` | `http://safe.gobwas.com` | `false` | 272
  105. `^abc.*$` | `abcdef` | `true` | 237
  106. `^abc.*$` | `af` | `false` | 100
  107. `^.*def$` | `abcdef` | `true` | 464
  108. `^.*def$` | `af` | `false` | 265
  109. `^ab.*ef$` | `abcdef` | `true` | 375
  110. `^ab.*ef$` | `af` | `false` | 145
  111. [godoc-image]: https://godoc.org/github.com/gobwas/glob?status.svg
  112. [godoc-url]: https://godoc.org/github.com/gobwas/glob
  113. [travis-image]: https://travis-ci.org/gobwas/glob.svg?branch=master
  114. [travis-url]: https://travis-ci.org/gobwas/glob
  115. ## Syntax
  116. Syntax is inspired by [standard wildcards](http://tldp.org/LDP/GNU-Linux-Tools-Summary/html/x11655.htm),
  117. except that `**` is aka super-asterisk, that do not sensitive for separators.