123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- package lazyregexp
- import (
- "os"
- "regexp"
- "strings"
- "sync"
- )
- type Regexp struct {
- str string
- once sync.Once
- rx *regexp.Regexp
- }
- func (r *Regexp) Regexp() *regexp.Regexp {
- r.once.Do(r.build)
- return r.rx
- }
- func (r *Regexp) build() {
- r.rx = regexp.MustCompile(r.str)
- r.str = ""
- }
- func (r *Regexp) Find(b []byte) []byte {
- return r.Regexp().Find(b)
- }
- func (r *Regexp) FindSubmatch(s []byte) [][]byte {
- return r.Regexp().FindSubmatch(s)
- }
- func (r *Regexp) FindStringSubmatch(s string) []string {
- return r.Regexp().FindStringSubmatch(s)
- }
- func (r *Regexp) FindStringSubmatchIndex(s string) []int {
- return r.Regexp().FindStringSubmatchIndex(s)
- }
- func (r *Regexp) ReplaceAllString(src, repl string) string {
- return r.Regexp().ReplaceAllString(src, repl)
- }
- func (r *Regexp) FindString(s string) string {
- return r.Regexp().FindString(s)
- }
- func (r *Regexp) FindAll(b []byte, n int) [][]byte {
- return r.Regexp().FindAll(b, n)
- }
- func (r *Regexp) FindAllString(s string, n int) []string {
- return r.Regexp().FindAllString(s, n)
- }
- func (r *Regexp) MatchString(s string) bool {
- return r.Regexp().MatchString(s)
- }
- func (r *Regexp) SubexpNames() []string {
- return r.Regexp().SubexpNames()
- }
- func (r *Regexp) FindAllStringSubmatch(s string, n int) [][]string {
- return r.Regexp().FindAllStringSubmatch(s, n)
- }
- func (r *Regexp) Split(s string, n int) []string {
- return r.Regexp().Split(s, n)
- }
- func (r *Regexp) ReplaceAllLiteralString(src, repl string) string {
- return r.Regexp().ReplaceAllLiteralString(src, repl)
- }
- func (r *Regexp) FindAllIndex(b []byte, n int) [][]int {
- return r.Regexp().FindAllIndex(b, n)
- }
- func (r *Regexp) Match(b []byte) bool {
- return r.Regexp().Match(b)
- }
- func (r *Regexp) ReplaceAllStringFunc(src string, repl func(string) string) string {
- return r.Regexp().ReplaceAllStringFunc(src, repl)
- }
- func (r *Regexp) ReplaceAll(src, repl []byte) []byte {
- return r.Regexp().ReplaceAll(src, repl)
- }
- var inTest = len(os.Args) > 0 && strings.HasSuffix(strings.TrimSuffix(os.Args[0], ".exe"), ".test")
- func New(str string) *Regexp {
- lr := &Regexp{str: str}
- if inTest {
-
- lr.Regexp()
- }
- return lr
- }
|