1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-05 03:23:49 +08:00

84 lines
1.6 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.

// Package param provide some util for param parse
package param
import (
"math"
"regexp"
"strings"
"sync"
"github.com/tidwall/gjson"
)
// EnsureBool 判断给定的p是否可表示为合法Bool类型,否则返回defaultVal
//
// 支持的合法类型有
//
// type bool
//
// type gjson.True or gjson.False
//
// type string "true","yes","1" or "false","no","0" (case insensitive)
func EnsureBool(p any, defaultVal bool) bool {
var str string
if b, ok := p.(bool); ok {
return b
}
if j, ok := p.(gjson.Result); ok {
if !j.Exists() {
return defaultVal
}
switch j.Type { // nolint: exhaustive
case gjson.True:
return true
case gjson.False:
return false
case gjson.String:
str = j.Str
default:
return defaultVal
}
} else if s, ok := p.(string); ok {
str = s
}
str = strings.ToLower(str)
switch str {
case "true", "yes", "1":
return true
case "false", "no", "0":
return false
default:
return defaultVal
}
}
var (
// once lazy compile the reg
once sync.Once
// reg is splitURL regex pattern.
reg *regexp.Regexp
)
// SplitURL 将给定URL字符串分割为两部分用于URL预处理防止风控
func SplitURL(s string) []string {
once.Do(func() { // lazy init.
reg = regexp.MustCompile(`(?i)[a-z\d][-a-z\d]{0,62}(\.[a-z\d][-a-z\d]{0,62})+\.?`)
})
idx := reg.FindAllStringIndex(s, -1)
if len(idx) == 0 {
return []string{s}
}
var result []string
last := 0
for i := 0; i < len(idx); i++ {
if len(idx[i]) != 2 {
continue
}
m := int(math.Abs(float64(idx[i][0]-idx[i][1]))/1.5) + idx[i][0]
result = append(result, s[last:m])
last = m
}
result = append(result, s[last:])
return result
}