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
5// Package lazyregexp is a thin wrapper over regexp, allowing the use of global
6// regexp variables without forcing them to be compiled at init.
7package lazyregexp
8
9import (
10	"os"
11	"regexp"
12	"strings"
13	"sync"
14)
15
16// Regexp is a wrapper around regexp.Regexp, where the underlying regexp will be
17// compiled the first time it is needed.
18type Regexp struct {
19	str  string
20	once sync.Once
21	rx   *regexp.Regexp
22}
23
24func (r *Regexp) re() *regexp.Regexp {
25	r.once.Do(r.build)
26	return r.rx
27}
28
29func (r *Regexp) build() {
30	r.rx = regexp.MustCompile(r.str)
31	r.str = ""
32}
33
34func (r *Regexp) MatchString(s string) bool {
35	return r.re().MatchString(s)
36}
37
38var inTest = len(os.Args) > 0 && strings.HasSuffix(strings.TrimSuffix(os.Args[0], ".exe"), ".test")
39
40// New creates a new lazy regexp, delaying the compiling work until it is first
41// needed. If the code is being run as part of tests, the regexp compiling will
42// happen immediately.
43func New(str string) *Regexp {
44	lr := &Regexp{str: str}
45	if inTest {
46		// In tests, always compile the regexps early.
47		lr.re()
48	}
49	return lr
50}
51