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.

80 lines
3.1 KiB

  1. # substring [![Build Status](https://travis-ci.org/toqueteos/substring.png?branch=master)](https://travis-ci.org/toqueteos/substring) [![GoDoc](http://godoc.org/github.com/toqueteos/substring?status.png)](http://godoc.org/github.com/toqueteos/substring) [![GitHub release](https://img.shields.io/github/release/toqueteos/substring.svg)](https://github.com/toqueteos/substring/releases)
  2. Simple and composable alternative to [regexp](http://golang.org/pkg/regexp/) package for fast substring searches.
  3. ## Installation
  4. The recommended way to install substring
  5. ```
  6. go get -t gopkg.in/toqueteos/substring.v1
  7. ```
  8. The `-t` flag is for fetching [gocheck](https://gopkg.in/check.v1), required for tests and benchmarks.
  9. ## Examples
  10. A basic example with two matchers:
  11. ```go
  12. package main
  13. import (
  14. "fmt"
  15. "regexp"
  16. "gopkg.in/toqueteos/substring.v1"
  17. )
  18. func main() {
  19. m1 := substring.After("assets/", substring.Or(
  20. substring.Has("jquery"),
  21. substring.Has("angular"),
  22. substring.Suffixes(".js", ".css", ".html"),
  23. ))
  24. fmt.Println(m1.Match("assets/angular/foo/bar")) //Prints: true
  25. fmt.Println(m1.Match("assets/js/file.js")) //Prints: true
  26. fmt.Println(m1.Match("assets/style/bar.css")) //Prints: true
  27. fmt.Println(m1.Match("assets/foo/bar.html")) //Prints: false
  28. fmt.Println(m1.Match("assets/js/qux.json")) //Prints: false
  29. fmt.Println(m1.Match("core/file.html")) //Prints: false
  30. fmt.Println(m1.Match("foobar/that.jsx")) //Prints: false
  31. m2 := substring.After("vendor/", substring.Suffixes(".css", ".js", ".less"))
  32. fmt.Println(m2.Match("foo/vendor/bar/qux.css")) //Prints: true
  33. fmt.Println(m2.Match("foo/var/qux.less")) //Prints: false
  34. re := regexp.MustCompile(`vendor\/.*\.(css|js|less)$`)
  35. fmt.Println(re.MatchString("foo/vendor/bar/qux.css")) //Prints: true
  36. fmt.Println(re.MatchString("foo/var/qux.less")) //Prints: false
  37. }
  38. ```
  39. ## How fast?
  40. It may vary depending on your use case but 1~2 orders of magnitude faster than `regexp` is pretty common.
  41. Test it out for yourself by running `go test -check.b`!
  42. ```
  43. $ go test -check.b
  44. PASS: lib_test.go:18: LibSuite.BenchmarkExample1 10000000 221 ns/op
  45. PASS: lib_test.go:23: LibSuite.BenchmarkExample2 10000000 229 ns/op
  46. PASS: lib_test.go:28: LibSuite.BenchmarkExample3 10000000 216 ns/op
  47. PASS: lib_test.go:33: LibSuite.BenchmarkExample4 10000000 208 ns/op
  48. PASS: lib_test.go:38: LibSuite.BenchmarkExample5 20000000 82.1 ns/op
  49. PASS: lib_test.go:48: LibSuite.BenchmarkExampleRe1 500000 4136 ns/op
  50. PASS: lib_test.go:53: LibSuite.BenchmarkExampleRe2 500000 5222 ns/op
  51. PASS: lib_test.go:58: LibSuite.BenchmarkExampleRe3 500000 5116 ns/op
  52. PASS: lib_test.go:63: LibSuite.BenchmarkExampleRe4 500000 4020 ns/op
  53. PASS: lib_test.go:68: LibSuite.BenchmarkExampleRe5 10000000 226 ns/op
  54. OK: 10 passed
  55. PASS
  56. ok gopkg.in/toqueteos/substring.v1 23.471s
  57. ```
  58. License
  59. -------
  60. MIT, see [LICENSE](LICENSE)