2
0

lazyre.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2018 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package lazyregexp is a thin wrapper over regexp, allowing the use of global
  5. // regexp variables without forcing them to be compiled at init.
  6. package lazyregexp
  7. import (
  8. "os"
  9. "regexp"
  10. "strings"
  11. "sync"
  12. )
  13. // Regexp is a wrapper around regexp.Regexp, where the underlying regexp will be
  14. // compiled the first time it is needed.
  15. type Regexp struct {
  16. str string
  17. once sync.Once
  18. rx *regexp.Regexp
  19. }
  20. func (r *Regexp) Regexp() *regexp.Regexp {
  21. r.once.Do(r.build)
  22. return r.rx
  23. }
  24. func (r *Regexp) build() {
  25. r.rx = regexp.MustCompile(r.str)
  26. r.str = ""
  27. }
  28. func (r *Regexp) Find(b []byte) []byte {
  29. return r.Regexp().Find(b)
  30. }
  31. func (r *Regexp) FindSubmatch(s []byte) [][]byte {
  32. return r.Regexp().FindSubmatch(s)
  33. }
  34. func (r *Regexp) FindStringSubmatch(s string) []string {
  35. return r.Regexp().FindStringSubmatch(s)
  36. }
  37. func (r *Regexp) FindStringSubmatchIndex(s string) []int {
  38. return r.Regexp().FindStringSubmatchIndex(s)
  39. }
  40. func (r *Regexp) ReplaceAllString(src, repl string) string {
  41. return r.Regexp().ReplaceAllString(src, repl)
  42. }
  43. func (r *Regexp) FindString(s string) string {
  44. return r.Regexp().FindString(s)
  45. }
  46. func (r *Regexp) FindAll(b []byte, n int) [][]byte {
  47. return r.Regexp().FindAll(b, n)
  48. }
  49. func (r *Regexp) FindAllString(s string, n int) []string {
  50. return r.Regexp().FindAllString(s, n)
  51. }
  52. func (r *Regexp) MatchString(s string) bool {
  53. return r.Regexp().MatchString(s)
  54. }
  55. func (r *Regexp) SubexpNames() []string {
  56. return r.Regexp().SubexpNames()
  57. }
  58. func (r *Regexp) FindAllStringSubmatch(s string, n int) [][]string {
  59. return r.Regexp().FindAllStringSubmatch(s, n)
  60. }
  61. func (r *Regexp) Split(s string, n int) []string {
  62. return r.Regexp().Split(s, n)
  63. }
  64. func (r *Regexp) ReplaceAllLiteralString(src, repl string) string {
  65. return r.Regexp().ReplaceAllLiteralString(src, repl)
  66. }
  67. func (r *Regexp) FindAllIndex(b []byte, n int) [][]int {
  68. return r.Regexp().FindAllIndex(b, n)
  69. }
  70. func (r *Regexp) Match(b []byte) bool {
  71. return r.Regexp().Match(b)
  72. }
  73. func (r *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) string {
  74. return r.Regexp().ReplaceAllStringFunc(src, repl)
  75. }
  76. func (r *Regexp) ReplaceAll(src, repl []byte) []byte {
  77. return r.Regexp().ReplaceAll(src, repl)
  78. }
  79. var inTest = len(os.Args) > 0 && strings.HasSuffix(strings.TrimSuffix(os.Args[0], ".exe"), ".test")
  80. // New creates a new lazy regexp, delaying the compiling work until it is first
  81. // needed. If the code is being run as part of tests, the regexp compiling will
  82. // happen immediately.
  83. func New(str string) *Regexp {
  84. lr := &Regexp{str: str}
  85. if inTest {
  86. // In tests, always compile the regexps early.
  87. lr.Regexp()
  88. }
  89. return lr
  90. }