// 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 lazyregexp is a thin wrapper over regexp, allowing the use of global // regexp variables without forcing them to be compiled at init.
package lazyregexp import ( ) // Regexp is a wrapper around regexp.Regexp, where the underlying regexp will be // compiled the first time it is needed. type Regexp struct { str string once sync.Once rx *regexp.Regexp } func ( *Regexp) () *regexp.Regexp { .once.Do(.build) return .rx } func ( *Regexp) () { .rx = regexp.MustCompile(.str) .str = "" } func ( *Regexp) ( []byte) [][]byte { return .re().FindSubmatch() } func ( *Regexp) ( string) []string { return .re().FindStringSubmatch() } func ( *Regexp) ( string) []int { return .re().FindStringSubmatchIndex() } func ( *Regexp) (, string) string { return .re().ReplaceAllString(, ) } func ( *Regexp) ( string) string { return .re().FindString() } func ( *Regexp) ( string, int) []string { return .re().FindAllString(, ) } func ( *Regexp) ( string) bool { return .re().MatchString() } func ( *Regexp) () []string { return .re().SubexpNames() } var inTest = len(os.Args) > 0 && strings.HasSuffix(strings.TrimSuffix(os.Args[0], ".exe"), ".test") // New creates a new lazy regexp, delaying the compiling work until it is first // needed. If the code is being run as part of tests, the regexp compiling will // happen immediately. func ( string) *Regexp { := &Regexp{str: } if inTest { // In tests, always compile the regexps early. .re() } return }