wildcard.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2018, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. // Package wildcard implements a very simple wildcard matcher which supports
  20. // only the term '*', which matches any sequence of characters. The match
  21. // function, WildcardMatch, both parses the pattern and matches the target;
  22. // there is no compile stage and WildcardMatch can be a drop in replacement
  23. // anywhere a normal string comparison is done.
  24. //
  25. // This package is very similar to and inspired by github.com/ryanuber/go-
  26. // glob, but with performance optimizations; github.com/gobwas/glob offers a
  27. // much richer glob syntax and faster performance for cases where a compiled
  28. // glob state can be maintained.
  29. //
  30. // Performance comparison:
  31. //
  32. // wildcard:
  33. //
  34. // BenchmarkFixedMatch-4 100000000 14.0 ns/op
  35. // BenchmarkPrefixMatch-4 50000000 26.2 ns/op
  36. // BenchmarkSuffixMatch-4 50000000 25.8 ns/op
  37. // BenchmarkMultipleMatch-4 10000000 167 ns/op
  38. //
  39. // github.com/ryanuber/go-glob:
  40. //
  41. // BenchmarkFixedGoGlob-4 30000000 58.3 ns/op
  42. // BenchmarkPrefixGoGlob-4 20000000 106 ns/op
  43. // BenchmarkSuffixGoGlob-4 20000000 105 ns/op
  44. // BenchmarkMultipleGoGlob-4 5000000 270 ns/op
  45. //
  46. // github.com/gobwas/glob with precompile:
  47. //
  48. // BenchmarkFixedGlobPrecompile-4 100000000 14.1 ns/op
  49. // BenchmarkPrefixGlobPrecompile-4 200000000 6.66 ns/op
  50. // BenchmarkSuffixGlobPrecompile-4 200000000 7.31 ns/op
  51. // BenchmarkMultipleGlobPrecompile-4 10000000 151 ns/op
  52. //
  53. // github.com/gobwas/glob with compile-and-match:
  54. //
  55. // BenchmarkFixedGlob-4 300000 4120 ns/op
  56. // BenchmarkPrefixGlob-4 1000000 1502 ns/op
  57. // BenchmarkSuffixGlob-4 1000000 1501 ns/op
  58. // BenchmarkMultipleGlob-4 300000 5203 ns/op
  59. //
  60. package wildcard
  61. import (
  62. "strings"
  63. )
  64. func Match(pattern, target string) bool {
  65. wildcard := "*"
  66. for n := 0; ; n++ {
  67. if pattern == wildcard {
  68. return true
  69. }
  70. i := strings.Index(pattern, wildcard)
  71. if n == 0 {
  72. if i == -1 {
  73. return pattern == target
  74. } else if i == 0 {
  75. pattern = pattern[i+1:]
  76. } else if i > 0 {
  77. if !strings.HasPrefix(target, pattern[:i]) {
  78. return false
  79. }
  80. pattern = pattern[i+1:]
  81. target = target[i:]
  82. }
  83. } else {
  84. if i == -1 {
  85. return strings.HasSuffix(target, pattern)
  86. } else if i == 0 {
  87. pattern = pattern[i+1:]
  88. } else if i > 0 {
  89. j := strings.Index(target, pattern[:i])
  90. if j == -1 {
  91. return false
  92. }
  93. pattern = pattern[i+1:]
  94. target = target[j+i+1:]
  95. }
  96. }
  97. }
  98. }