2023-01-21 11:31:23 +00:00
|
|
|
package strings
|
2023-01-13 04:31:35 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"fmt"
|
2023-02-06 04:13:38 +00:00
|
|
|
"golang.org/x/exp/constraints"
|
2023-01-13 04:31:35 +00:00
|
|
|
"io"
|
2023-02-06 04:13:38 +00:00
|
|
|
"strconv"
|
2023-01-13 04:31:35 +00:00
|
|
|
"strings"
|
2023-03-27 03:37:24 +00:00
|
|
|
"unicode"
|
2023-01-13 04:31:35 +00:00
|
|
|
)
|
|
|
|
|
2023-01-21 11:31:23 +00:00
|
|
|
func Join(s ...string) (str string) {
|
2023-01-13 04:31:35 +00:00
|
|
|
if len(s) == 1 {
|
|
|
|
return s[0]
|
|
|
|
} else if len(s) > 1 {
|
|
|
|
b := strings.Builder{}
|
|
|
|
for _, s2 := range s {
|
|
|
|
b.WriteString(s2)
|
|
|
|
}
|
|
|
|
str = b.String()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-04 11:46:06 +00:00
|
|
|
func FirstUpper(s string) string {
|
|
|
|
if len(s) < 1 {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return Join(strings.ToUpper(s[:1]), s[1:])
|
|
|
|
}
|
|
|
|
|
2023-02-06 04:13:38 +00:00
|
|
|
func ToInteger[T constraints.Integer](s string, defaults T) T {
|
|
|
|
if s == "" {
|
|
|
|
return defaults
|
|
|
|
}
|
|
|
|
i, err := strconv.ParseInt(s, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return defaults
|
|
|
|
}
|
|
|
|
return T(i)
|
|
|
|
}
|
2023-02-21 12:31:00 +00:00
|
|
|
func ToInt[T constraints.Integer](s string) T {
|
|
|
|
defaults := T(0)
|
|
|
|
if s == "" {
|
|
|
|
return defaults
|
|
|
|
}
|
|
|
|
i, err := strconv.ParseInt(s, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return defaults
|
|
|
|
}
|
|
|
|
return T(i)
|
|
|
|
}
|
2023-02-06 04:13:38 +00:00
|
|
|
|
2023-01-21 11:31:23 +00:00
|
|
|
func Md5(str string) string {
|
2023-01-13 04:31:35 +00:00
|
|
|
h := md5.New()
|
|
|
|
_, err := io.WriteString(h, str)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%x", h.Sum(nil))
|
|
|
|
}
|
2023-02-15 16:58:59 +00:00
|
|
|
|
|
|
|
func BuilderJoin(s *strings.Builder, str ...string) {
|
|
|
|
for _, ss := range str {
|
|
|
|
s.WriteString(ss)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BuilderFormat(s *strings.Builder, format string, args ...any) {
|
|
|
|
s.WriteString(fmt.Sprintf(format, args...))
|
|
|
|
}
|
|
|
|
|
|
|
|
type Builder struct {
|
|
|
|
*strings.Builder
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBuilder() *Builder {
|
|
|
|
return &Builder{&strings.Builder{}}
|
|
|
|
}
|
|
|
|
|
2023-02-16 03:53:24 +00:00
|
|
|
func (b *Builder) WriteString(s ...string) (count int) {
|
2023-02-15 16:58:59 +00:00
|
|
|
for _, ss := range s {
|
2023-02-16 03:53:24 +00:00
|
|
|
i, _ := b.Builder.WriteString(ss)
|
2023-02-15 16:58:59 +00:00
|
|
|
count += i
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2023-03-01 15:01:38 +00:00
|
|
|
|
|
|
|
func Replace(s string, replace map[string]string) string {
|
|
|
|
for k, v := range replace {
|
|
|
|
s = strings.ReplaceAll(s, k, v)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
2023-07-04 15:36:25 +00:00
|
|
|
|
|
|
|
// Replaces replace string by slice as order
|
|
|
|
//
|
2023-07-05 15:23:47 +00:00
|
|
|
// []string sub []string like {old1,old2,old3,....,newString} all old[x] will be replaced by lasted newString
|
|
|
|
func Replaces(s string, replace ...[]string) string {
|
2023-06-12 15:08:46 +00:00
|
|
|
for _, v := range replace {
|
|
|
|
if len(v) < 1 {
|
|
|
|
continue
|
|
|
|
} else if len(v) == 1 {
|
|
|
|
s = strings.ReplaceAll(s, v[0], "")
|
|
|
|
} else {
|
2023-07-04 15:36:25 +00:00
|
|
|
for _, s2 := range v[0 : len(v)-1] {
|
|
|
|
s = strings.ReplaceAll(s, s2, v[len(v)-1])
|
|
|
|
}
|
2023-06-12 15:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
2023-02-16 03:53:24 +00:00
|
|
|
func (b *Builder) Sprintf(format string, a ...any) int {
|
|
|
|
i, _ := fmt.Fprintf(b, format, a...)
|
|
|
|
return i
|
2023-02-15 16:58:59 +00:00
|
|
|
}
|
2023-02-24 16:56:52 +00:00
|
|
|
|
|
|
|
// CutSpecialDuplicate '\t', '\n', '\v', '\f', '\r', ' ', U+0085 (NEL), U+00A0 (NBSP)
|
|
|
|
func CutSpecialDuplicate(s, char string) string {
|
|
|
|
return strings.Join(strings.Fields(s), char)
|
|
|
|
}
|
2023-03-27 03:37:24 +00:00
|
|
|
|
|
|
|
// CamelCaseTo 驼峰单词转下划线或横杠单词 //分隔符
|
|
|
|
func CamelCaseTo(s string, delimiter rune) string {
|
|
|
|
var output []rune
|
|
|
|
for i, r := range s {
|
|
|
|
if i == 0 {
|
|
|
|
output = append(output, unicode.ToLower(r))
|
|
|
|
} else {
|
|
|
|
if unicode.IsUpper(r) {
|
|
|
|
output = append(output, delimiter)
|
|
|
|
}
|
|
|
|
output = append(output, unicode.ToLower(r))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string(output)
|
|
|
|
}
|