github.com/shohhei1126/hugo@v0.42.2-0.20180623210752-3d5928889ad7/tpl/strings/regexp.go (about)

     1  // Copyright 2017 The Hugo Authors. All rights reserved.
     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  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package strings
    15  
    16  import (
    17  	"regexp"
    18  	"sync"
    19  
    20  	"github.com/spf13/cast"
    21  )
    22  
    23  // FindRE returns a list of strings that match the regular expression. By default all matches
    24  // will be included. The number of matches can be limited with an optional third parameter.
    25  func (ns *Namespace) FindRE(expr string, content interface{}, limit ...interface{}) ([]string, error) {
    26  	re, err := reCache.Get(expr)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  
    31  	conv, err := cast.ToStringE(content)
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	if len(limit) == 0 {
    37  		return re.FindAllString(conv, -1), nil
    38  	}
    39  
    40  	lim, err := cast.ToIntE(limit[0])
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	return re.FindAllString(conv, lim), nil
    46  }
    47  
    48  // ReplaceRE returns a copy of s, replacing all matches of the regular
    49  // expression pattern with the replacement text repl.
    50  func (ns *Namespace) ReplaceRE(pattern, repl, s interface{}) (_ string, err error) {
    51  	sp, err := cast.ToStringE(pattern)
    52  	if err != nil {
    53  		return
    54  	}
    55  
    56  	sr, err := cast.ToStringE(repl)
    57  	if err != nil {
    58  		return
    59  	}
    60  
    61  	ss, err := cast.ToStringE(s)
    62  	if err != nil {
    63  		return
    64  	}
    65  
    66  	re, err := reCache.Get(sp)
    67  	if err != nil {
    68  		return "", err
    69  	}
    70  
    71  	return re.ReplaceAllString(ss, sr), nil
    72  }
    73  
    74  // regexpCache represents a cache of regexp objects protected by a mutex.
    75  type regexpCache struct {
    76  	mu sync.RWMutex
    77  	re map[string]*regexp.Regexp
    78  }
    79  
    80  // Get retrieves a regexp object from the cache based upon the pattern.
    81  // If the pattern is not found in the cache, create one
    82  func (rc *regexpCache) Get(pattern string) (re *regexp.Regexp, err error) {
    83  	var ok bool
    84  
    85  	if re, ok = rc.get(pattern); !ok {
    86  		re, err = regexp.Compile(pattern)
    87  		if err != nil {
    88  			return nil, err
    89  		}
    90  		rc.set(pattern, re)
    91  	}
    92  
    93  	return re, nil
    94  }
    95  
    96  func (rc *regexpCache) get(key string) (re *regexp.Regexp, ok bool) {
    97  	rc.mu.RLock()
    98  	re, ok = rc.re[key]
    99  	rc.mu.RUnlock()
   100  	return
   101  }
   102  
   103  func (rc *regexpCache) set(key string, re *regexp.Regexp) {
   104  	rc.mu.Lock()
   105  	rc.re[key] = re
   106  	rc.mu.Unlock()
   107  }
   108  
   109  var reCache = regexpCache{re: make(map[string]*regexp.Regexp)}