代码拉取完成,页面将自动刷新
package gregex
import (
"regexp"
"sync"
)
var (
regexMu = sync.RWMutex{}
// Cache for regex object.
// Note that:
// 1. It uses sync.RWMutex ensuring the concurrent safety.
// 2. There's no expiring logic for this map.
regexMap = make(map[string]*regexp.Regexp)
)
// getRegexp returns *regexp.Regexp object with given <pattern>.
// It uses cache to enhance the performance for compiling regular expression pattern,
// which means, it will return the same *regexp.Regexp object with the same regular
// expression pattern.
//
// It is concurrent-safe for multiple goroutines.
func getRegexp(pattern string) (regex *regexp.Regexp, err error) {
// Retrieve the regular expression object using reading lock.
regexMu.RLock()
regex = regexMap[pattern]
regexMu.RUnlock()
if regex != nil {
return
}
// If it does not exist in the cache,
// it compiles the pattern and creates one.
regex, err = regexp.Compile(pattern)
if err != nil {
return
}
// Cache the result object using writing lock.
regexMu.Lock()
regexMap[pattern] = regex
regexMu.Unlock()
return
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。