mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-27 14:39:37 +08:00
代码生成
This commit is contained in:
44
base/utils/regexp.go
Normal file
44
base/utils/regexp.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"pandax/base/global"
|
||||
"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 {
|
||||
global.Log.Warnf(`regexp.Compile failed for pattern "%s"`, pattern)
|
||||
return
|
||||
}
|
||||
// Cache the result object using writing lock.
|
||||
regexMu.Lock()
|
||||
regexMap[pattern] = regex
|
||||
regexMu.Unlock()
|
||||
return
|
||||
}
|
||||
@@ -22,6 +22,34 @@ func UnicodeIndex(str, substr string) int {
|
||||
return result
|
||||
}
|
||||
|
||||
func ReplaceString(pattern, replace, src string) (string, error) {
|
||||
if r, err := GetRegexp(pattern); err == nil {
|
||||
return r.ReplaceAllString(src, replace), nil
|
||||
} else {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
func Contains(haystack, needle string, startOffset ...int) int {
|
||||
length := len(haystack)
|
||||
offset := 0
|
||||
if len(startOffset) > 0 {
|
||||
offset = startOffset[0]
|
||||
}
|
||||
if length == 0 || offset > length || -offset > length {
|
||||
return -1
|
||||
}
|
||||
|
||||
if offset < 0 {
|
||||
offset += length
|
||||
}
|
||||
pos := strings.Index(strings.ToLower(haystack[offset:]), strings.ToLower(needle))
|
||||
if pos == -1 {
|
||||
return -1
|
||||
}
|
||||
return pos + offset
|
||||
}
|
||||
|
||||
// 字符串模板解析
|
||||
func TemplateResolve(temp string, data interface{}) string {
|
||||
t, _ := template.New("string-temp").Parse(temp)
|
||||
@@ -59,6 +87,14 @@ func ReverStrTemplate(temp, str string, res map[string]interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
func B2S(bs []uint8) string {
|
||||
ba := []byte{}
|
||||
for _, b := range bs {
|
||||
ba = append(ba, byte(b))
|
||||
}
|
||||
return string(ba)
|
||||
}
|
||||
|
||||
func IdsStrToIdsIntGroup(keys string) []int64 {
|
||||
IDS := make([]int64, 0)
|
||||
ids := strings.Split(keys, ",")
|
||||
|
||||
Reference in New Issue
Block a user