Initial commit: web_hghl_server

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-10-26 16:28:14 +08:00
co-authored by Claude Opus 4.7
commit 65fca60622
653 changed files with 135901 additions and 0 deletions
+100
View File
@@ -0,0 +1,100 @@
package utils
import (
"math/rand/v2"
"strings"
"github.com/gosimple/slug"
)
// BinarySearch BinarySearch
func BinarySearch(s []string, t string, n int) (idx int) {
idx = -1
if n == 0 {
return
}
start, end := 0, n-1
for start <= end {
mid := (start + end) >> 1
if s[mid] < t {
start = mid + 1
} else if s[mid] > t {
end = mid - 1
} else {
idx = mid
break
}
}
return
}
var YesOrNo = map[bool]string{
true: "是",
false: "否",
}
func GetRandomLetters(n int) string {
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
b := make([]byte, n)
for i := range b {
b[i] = letters[rand.IntN(len(letters))]
}
return string(b)
}
func GetPrefix(s string, n int) string {
runes := []rune(s)
if len(runes) <= n {
return s
}
return string(runes[:n])
}
func GetSlugInfo(text string) string {
return SanitizeSlug(slug.Make(text))
}
// SanitizeSlug 去掉 slug 中的横杠等特殊符号,只保留字母和数字
func SanitizeSlug(s string) string {
s = strings.TrimSpace(s)
if s == "" {
return ""
}
var b strings.Builder
b.Grow(len(s))
for _, r := range s {
if r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' || r >= '0' && r <= '9' {
b.WriteRune(r)
}
}
return b.String()
}
// IsSlugAllDigits 判断 slug 是否全为数字
func IsSlugAllDigits(s string) bool {
s = strings.TrimSpace(s)
if s == "" {
return false
}
for _, r := range s {
if r < '0' || r > '9' {
return false
}
}
return true
}
// MakeUniqueSlug 生成唯一 slug;exists 返回 true 表示已被占用。生成结果不能全是数字。
func MakeUniqueSlug(source string, exists func(string) bool) string {
pinyin := GetSlugInfo(source)
if pinyin == "" {
pinyin = GetRandomLetters(5)
} else if IsSlugAllDigits(pinyin) {
pinyin = pinyin + GetRandomLetters(3)
}
finalSlug := pinyin
for exists(finalSlug) {
finalSlug = pinyin + GetRandomLetters(3)
}
return finalSlug
}