liyujie
2025-08-28 d9927380ed7c8366f762049be9f3fee225860833
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
 
package doc
 
import (
   "os"
   "regexp"
   "strings"
   "sync"
)
 
type lazyRE struct {
   str  string
   once sync.Once
   rx   *regexp.Regexp
}
 
func (r *lazyRE) re() *regexp.Regexp {
   r.once.Do(r.build)
   return r.rx
}
 
func (r *lazyRE) build() {
   r.rx = regexp.MustCompile(r.str)
   r.str = ""
}
 
func (r *lazyRE) FindStringSubmatchIndex(s string) []int {
   return r.re().FindStringSubmatchIndex(s)
}
 
func (r *lazyRE) ReplaceAllString(src, repl string) string {
   return r.re().ReplaceAllString(src, repl)
}
 
func (r *lazyRE) MatchString(s string) bool {
   return r.re().MatchString(s)
}
 
var inTest = len(os.Args) > 0 && strings.HasSuffix(strings.TrimSuffix(os.Args[0], ".exe"), ".test")
 
func newLazyRE(str string) *lazyRE {
   lr := &lazyRE{str: str}
   if inTest {
       // In tests, always compile the regexps early.
       lr.re()
   }
   return lr
}