Files
EdgeAPI/internal/utils/strings.go
GoEdgeLab 5a17ae9d79 v1.4.1
2024-07-27 14:15:25 +08:00

146 lines
2.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Copyright 2021 GoEdge CDN goedge.cdn@gmail.com. All rights reserved.
package utils
import (
"github.com/iwind/TeaGo/lists"
"strings"
)
// SplitStrings 分隔字符串
// 忽略其中为空的片段
func SplitStrings(s string, glue string) []string {
var result = []string{}
if len(s) > 0 {
for _, p := range strings.Split(s, glue) {
p = strings.TrimSpace(p)
if len(p) > 0 {
result = append(result, p)
}
}
}
return result
}
// ContainsStringInsensitive 检查是否包含某个字符串,并且不区分大小写
func ContainsStringInsensitive(list []string, search string) bool {
search = strings.ToLower(search)
for _, s := range list {
if strings.ToLower(s) == search {
return true
}
}
return false
}
// Similar 计算相似度
// between 0-1
func Similar(s1 string, s2 string) float32 {
var r1s = []rune(s1)
var r2s = []rune(s2)
var l1 = len(r1s)
var l2 = len(r2s)
if l1 > l2 {
r1s, r2s = r2s, r1s
}
if len(r1s) == 0 {
return 0
}
var count = 0
for _, r := range r1s {
for index, r2 := range r2s {
if r == r2 {
count++
r2s = r2s[index+1:]
break
}
}
}
return (float32(count)/float32(l1) + float32(count)/float32(l2)) / 2
}
// LimitString 限制字符串长度
func LimitString(s string, maxLength int) string {
if len(s) <= maxLength {
return s
}
if maxLength <= 0 {
return ""
}
var runes = []rune(s)
var rs = len(runes)
for i := 0; i < rs; i++ {
if len(string(runes[:i+1])) > maxLength {
return string(runes[:i])
}
}
return s
}
// SplitKeywordArgs 分隔关键词参数
// 支持hello, "hello", name:hello, name:"hello", name:\"hello\"
func SplitKeywordArgs(s string) (args []splitArg) {
var value []rune
var beginQuote = false
var runes = []rune(s)
for index, r := range runes {
if r == '"' && (index == 0 || runes[index-1] != '\\') {
beginQuote = !beginQuote
continue
}
if !beginQuote && (r == ' ' || r == '\t' || r == '\n' || r == '\r') {
if len(value) > 0 {
args = append(args, parseKeywordValue(string(value)))
value = nil
}
} else {
value = append(value, r)
}
}
if len(value) > 0 {
args = append(args, parseKeywordValue(string(value)))
}
return
}
type splitArg struct {
Key string
Value string
}
func (this *splitArg) String() string {
if len(this.Key) > 0 {
return this.Key + ":" + this.Value
}
return this.Value
}
func parseKeywordValue(value string) (arg splitArg) {
var colonIndex = strings.Index(value, ":")
if colonIndex > 0 {
arg.Key = value[:colonIndex]
arg.Value = value[colonIndex+1:]
} else {
arg.Value = value
}
return
}
func ListIsGreaterEqualThanOther(list []string, otherList []string) bool {
for _, v := range otherList {
if !lists.Contains(list, v) {
return false
}
}
return true
}