github.com/dolthub/go-mysql-server@v0.18.0/internal/regex/regex_go.go (about)

     1  // Copyright 2020-2021 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package regex
    16  
    17  import (
    18  	"regexp"
    19  	"time"
    20  )
    21  
    22  // Go holds go regex engine Matcher.
    23  type Go struct {
    24  	reg *regexp.Regexp
    25  }
    26  
    27  // Match implements Matcher interface.
    28  func (r *Go) Match(s string) bool {
    29  	t := time.Now()
    30  	defer MatchHistogram.With("string", s, "duration", "seconds").Observe(time.Since(t).Seconds())
    31  
    32  	return r.reg.MatchString(s)
    33  }
    34  
    35  // Dispose implements Disposer interface.
    36  func (*Go) Dispose() {}
    37  
    38  // NewGo creates a new Matcher using go regex engine.
    39  func NewGo(re string) (Matcher, Disposer, error) {
    40  	t := time.Now()
    41  	reg, err := regexp.Compile(re)
    42  	if err != nil {
    43  		return nil, nil, err
    44  	}
    45  	CompileHistogram.With("regex", re, "duration", "seconds").Observe(time.Since(t).Seconds())
    46  
    47  	r := Go{
    48  		reg: reg,
    49  	}
    50  	return &r, &r, nil
    51  }
    52  
    53  func init() {
    54  	err := Register("go", NewGo)
    55  	if err != nil {
    56  		panic(err.Error())
    57  	}
    58  }