wrapper/string.go

1191 lines
24 KiB
Go
Raw Normal View History

2023-05-05 13:43:33 +08:00
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-05-05 11:59
package wrapper
import (
2023-05-16 12:12:18 +08:00
"crypto/md5"
2023-08-11 22:23:16 +08:00
"encoding/hex"
2023-05-05 13:43:33 +08:00
"errors"
2023-08-11 13:56:40 +08:00
"git.zhangdeman.cn/zhangdeman/serialize"
2023-08-11 15:15:13 +08:00
"github.com/axgle/mahonia"
2024-06-27 11:17:06 +08:00
"github.com/spaolacci/murmur3"
2023-05-16 12:12:18 +08:00
"io"
2023-08-11 15:15:13 +08:00
"math/rand"
2023-05-05 13:43:33 +08:00
"strings"
2023-08-11 15:15:13 +08:00
"time"
2023-05-05 13:43:33 +08:00
)
2023-08-11 15:15:13 +08:00
// StringFromRandom 从随机字符串生成String
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:50 2023/8/11
func StringFromRandom(length int, sourceCharList string) String {
if length == 0 {
return ""
}
if len(sourceCharList) == 0 {
//字符串为空,默认字符源为如下(去除易混淆的i/l):
sourceCharList = "0123456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNOPQRSTUVWXYZ"
}
strByte := []byte(sourceCharList)
var genStrByte = make([]byte, 0)
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < int(length); i++ {
genStrByte = append(genStrByte, strByte[r.Intn(len(strByte))])
}
return String(string(genStrByte))
}
2023-05-05 13:43:33 +08:00
// String 字符串类型包装
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:07 2023/5/5
type String string
// ToFloat32 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:26 2023/5/4
2023-05-08 18:29:48 +08:00
func (str String) ToFloat32() Float32Result {
2023-05-05 13:43:33 +08:00
var (
2023-05-08 18:29:48 +08:00
res Float32Result
2023-05-05 13:43:33 +08:00
)
2023-12-27 17:59:33 +08:00
res.Err = ConvertAssign(&res.Value, str)
2023-05-08 18:29:48 +08:00
return res
2023-05-05 13:43:33 +08:00
}
2023-05-16 12:08:22 +08:00
// ToFloat32Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:58 2023/5/16
func (str String) ToFloat32Ptr() Float32PtrResult {
res := str.ToFloat32()
if nil != res.Err {
return Float32PtrResult{
Value: nil,
Err: res.Err,
}
}
return Float32PtrResult{
Value: &res.Value,
Err: nil,
}
}
2023-05-05 13:43:33 +08:00
// ToFloat64 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:26 2023/5/4
2023-05-08 18:29:48 +08:00
func (str String) ToFloat64() Float64Result {
2023-05-05 13:43:33 +08:00
var (
2023-05-08 18:29:48 +08:00
res Float64Result
2023-05-05 13:43:33 +08:00
)
2023-12-27 17:59:33 +08:00
res.Err = ConvertAssign(&res.Value, str)
2023-05-08 18:29:48 +08:00
return res
2023-05-05 13:43:33 +08:00
}
2023-05-16 12:08:22 +08:00
// ToFloat64Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:59 2023/5/16
func (str String) ToFloat64Ptr() Float64PtrResult {
res := str.ToFloat64()
if nil != res.Err {
return Float64PtrResult{
Value: nil,
Err: res.Err,
}
}
return Float64PtrResult{
Value: &res.Value,
Err: nil,
}
}
2023-05-05 13:43:33 +08:00
// ToDouble 转double
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:33 2023/5/4
2023-05-08 18:29:48 +08:00
func (str String) ToDouble() Float64Result {
2023-05-05 13:43:33 +08:00
return str.ToFloat64()
}
2023-05-16 12:08:22 +08:00
// ToDoublePtr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:59 2023/5/16
func (str String) ToDoublePtr() Float64PtrResult {
return str.ToFloat64Ptr()
}
2023-05-05 13:43:33 +08:00
// ToNumber 转数字, 使用最高精度的float64
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:33 2023/5/4
2023-05-08 18:29:48 +08:00
func (str String) ToNumber() Float64Result {
2023-05-05 13:43:33 +08:00
return str.ToFloat64()
}
2023-05-16 12:08:22 +08:00
// ToNumberPtr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:00 2023/5/16
func (str String) ToNumberPtr() Float64PtrResult {
return str.ToFloat64Ptr()
}
2023-05-05 13:43:33 +08:00
// ToInt8 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
2023-05-08 18:29:48 +08:00
func (str String) ToInt8() Int8Result {
2023-05-05 13:43:33 +08:00
var (
2023-05-08 18:29:48 +08:00
res Int8Result
2023-05-05 13:43:33 +08:00
)
2023-12-27 17:59:33 +08:00
res.Err = ConvertAssign(&res.Value, str)
2023-05-08 18:29:48 +08:00
return res
2023-05-05 13:43:33 +08:00
}
2023-05-16 12:08:22 +08:00
// ToInt8Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:01 2023/5/16
func (str String) ToInt8Ptr() Int8PtrResult {
res := str.ToInt8()
if nil != res.Err {
return Int8PtrResult{
Value: nil,
Err: res.Err,
}
}
return Int8PtrResult{
Value: &res.Value,
Err: nil,
}
}
2023-05-05 13:43:33 +08:00
// ToInt16 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
2023-05-08 18:29:48 +08:00
func (str String) ToInt16() Int16Result {
2023-05-05 13:43:33 +08:00
var (
2023-05-08 18:29:48 +08:00
res Int16Result
2023-05-05 13:43:33 +08:00
)
2023-12-27 17:59:33 +08:00
res.Err = ConvertAssign(&res.Value, str)
2023-05-08 18:29:48 +08:00
return res
2023-05-05 13:43:33 +08:00
}
2023-05-16 12:08:22 +08:00
// ToInt16Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:02 2023/5/16
func (str String) ToInt16Ptr() Int16PtrResult {
res := str.ToInt16()
if nil != res.Err {
return Int16PtrResult{
Value: nil,
Err: res.Err,
}
}
return Int16PtrResult{
Value: &res.Value,
Err: nil,
}
}
2023-05-05 13:43:33 +08:00
// ToInt32 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
2023-05-08 18:29:48 +08:00
func (str String) ToInt32() Int32Result {
2023-05-05 13:43:33 +08:00
var (
2023-05-08 18:29:48 +08:00
res Int32Result
2023-05-05 13:43:33 +08:00
)
2023-12-27 17:59:33 +08:00
res.Err = ConvertAssign(&res.Value, str)
2023-05-08 18:29:48 +08:00
return res
2023-05-05 13:43:33 +08:00
}
2023-05-16 12:08:22 +08:00
// ToInt32Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:03 2023/5/16
func (str String) ToInt32Ptr() Int32PtrResult {
res := str.ToInt32()
if nil != res.Err {
return Int32PtrResult{
Value: nil,
Err: res.Err,
}
}
return Int32PtrResult{
Value: &res.Value,
Err: nil,
}
}
2023-05-05 13:43:33 +08:00
// ToInt64 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
2023-05-08 18:29:48 +08:00
func (str String) ToInt64() Int64Result {
2023-05-05 13:43:33 +08:00
var (
2023-05-08 18:29:48 +08:00
res Int64Result
2023-05-05 13:43:33 +08:00
)
2023-12-27 17:59:33 +08:00
res.Err = ConvertAssign(&res.Value, str)
2023-05-08 18:29:48 +08:00
return res
2023-05-05 13:43:33 +08:00
}
2023-05-16 12:08:22 +08:00
// ToInt64Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:03 2023/5/16
func (str String) ToInt64Ptr() Int64PtrResult {
res := str.ToInt64()
if nil != res.Err {
return Int64PtrResult{
Value: nil,
Err: res.Err,
}
}
return Int64PtrResult{
Value: &res.Value,
Err: nil,
}
}
2023-05-05 13:43:33 +08:00
// ToInt ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:47 2023/5/5
2023-05-08 18:29:48 +08:00
func (str String) ToInt() IntResult {
2023-05-05 13:43:33 +08:00
var (
2023-05-08 18:29:48 +08:00
res IntResult
2023-05-05 13:43:33 +08:00
)
2023-12-27 17:59:33 +08:00
res.Err = ConvertAssign(&res.Value, str)
2023-05-08 18:29:48 +08:00
return res
2023-05-05 13:43:33 +08:00
}
2023-05-16 12:08:22 +08:00
// ToIntPtr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:04 2023/5/16
func (str String) ToIntPtr() IntPtrResult {
res := str.ToInt()
if nil != res.Err {
return IntPtrResult{
Value: nil,
Err: res.Err,
}
}
return IntPtrResult{
Value: &res.Value,
Err: nil,
}
}
2023-05-05 13:43:33 +08:00
// ToUint8 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:30 2023/5/4
2023-05-08 18:29:48 +08:00
func (str String) ToUint8() Uint8Result {
2023-05-05 13:43:33 +08:00
var (
2023-05-08 18:29:48 +08:00
res Uint8Result
2023-05-05 13:43:33 +08:00
)
2023-12-27 17:59:33 +08:00
res.Err = ConvertAssign(&res.Value, str)
2023-05-08 18:29:48 +08:00
return res
2023-05-05 13:43:33 +08:00
}
2023-05-16 12:08:22 +08:00
// ToUint8Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:05 2023/5/16
func (str String) ToUint8Ptr() Uint8PtrResult {
res := str.ToUint8()
if nil != res.Err {
return Uint8PtrResult{
Value: nil,
Err: res.Err,
}
}
return Uint8PtrResult{
Value: &res.Value,
Err: nil,
}
}
2023-05-05 13:43:33 +08:00
// ToUint16 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
2023-05-08 18:29:48 +08:00
func (str String) ToUint16() Uint16Result {
2023-05-05 13:43:33 +08:00
var (
2023-05-08 18:29:48 +08:00
res Uint16Result
2023-05-05 13:43:33 +08:00
)
2023-12-27 17:59:33 +08:00
res.Err = ConvertAssign(&res.Value, str)
2023-05-08 18:29:48 +08:00
return res
2023-05-05 13:43:33 +08:00
}
2023-05-16 12:08:22 +08:00
// ToUint16Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:05 2023/5/16
func (str String) ToUint16Ptr() Uint16PtrResult {
res := str.ToUint16()
if nil != res.Err {
return Uint16PtrResult{
Value: nil,
Err: res.Err,
}
}
return Uint16PtrResult{
Value: &res.Value,
Err: nil,
}
}
2023-05-05 13:43:33 +08:00
// ToUint32 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
2023-05-08 18:29:48 +08:00
func (str String) ToUint32() Uint32Result {
2023-05-05 13:43:33 +08:00
var (
2023-05-08 18:29:48 +08:00
res Uint32Result
2023-05-05 13:43:33 +08:00
)
2023-12-27 17:59:33 +08:00
res.Err = ConvertAssign(&res.Value, str)
2023-05-08 18:29:48 +08:00
return res
2023-05-05 13:43:33 +08:00
}
2023-05-16 12:08:22 +08:00
// ToUint32Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:05 2023/5/16
func (str String) ToUint32Ptr() Uint32PtrResult {
res := str.ToUint32()
if nil != res.Err {
return Uint32PtrResult{
Value: nil,
Err: res.Err,
}
}
return Uint32PtrResult{
Value: &res.Value,
Err: nil,
}
}
2023-05-05 13:43:33 +08:00
// ToUint64 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
2023-05-08 18:29:48 +08:00
func (str String) ToUint64() Uint64Result {
2023-05-05 13:43:33 +08:00
var (
2023-05-08 18:29:48 +08:00
res Uint64Result
2023-05-05 13:43:33 +08:00
)
2023-12-27 17:59:33 +08:00
res.Err = ConvertAssign(&res.Value, str)
2023-05-08 18:29:48 +08:00
return res
2023-05-05 13:43:33 +08:00
}
2023-05-16 12:08:22 +08:00
// ToUint64Ptr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:06 2023/5/16
func (str String) ToUint64Ptr() Uint64PtrResult {
res := str.ToUint64()
if nil != res.Err {
return Uint64PtrResult{
Value: nil,
Err: res.Err,
}
}
return Uint64PtrResult{
Value: &res.Value,
Err: nil,
}
}
2023-05-05 13:43:33 +08:00
// ToUint ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
2023-05-08 18:29:48 +08:00
func (str String) ToUint() UintResult {
2023-05-05 13:43:33 +08:00
var (
2023-05-08 18:29:48 +08:00
res UintResult
2023-05-05 13:43:33 +08:00
)
2023-12-27 17:59:33 +08:00
res.Err = ConvertAssign(&res.Value, str)
2023-05-08 18:29:48 +08:00
return res
2023-05-05 13:43:33 +08:00
}
2023-05-16 12:08:22 +08:00
// ToUintPtr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:06 2023/5/16
func (str String) ToUintPtr() UintPtrResult {
res := str.ToUint()
if nil != res.Err {
return UintPtrResult{
Value: nil,
Err: res.Err,
}
}
return UintPtrResult{
Value: &res.Value,
Err: nil,
}
}
2023-05-05 13:43:33 +08:00
// ToBool ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:34 2023/5/4
2023-05-08 18:29:48 +08:00
func (str String) ToBool() BoolResult {
2023-05-05 13:43:33 +08:00
var (
2023-05-08 18:29:48 +08:00
res BoolResult
2023-05-05 13:43:33 +08:00
)
2023-12-27 17:59:33 +08:00
res.Err = ConvertAssign(&res.Value, str)
2023-05-08 18:29:48 +08:00
return res
2023-05-05 13:43:33 +08:00
}
2023-05-16 12:08:22 +08:00
// ToBoolPtr ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:07 2023/5/16
func (str String) ToBoolPtr() BoolPtrResult {
res := str.ToBool()
if nil != res.Err {
return BoolPtrResult{
Value: nil,
Err: res.Err,
}
}
return BoolPtrResult{
Value: &res.Value,
Err: nil,
}
}
2023-07-03 15:59:37 +08:00
// ToStringPtr 转换成字符串指针
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:57 2023/7/3
func (str String) ToStringPtr() StringPtrResult {
val := str.Value()
return StringPtrResult{
Value: &val,
Err: nil,
}
}
2023-05-05 13:43:33 +08:00
// ToObject ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:35 2023/5/4
2023-05-08 18:29:48 +08:00
func (str String) ToObject() ObjectResult {
2023-05-05 13:43:33 +08:00
var (
2023-05-08 18:29:48 +08:00
res = ObjectResult{
2024-06-08 20:06:35 +08:00
Value: map[string]any{},
2023-05-08 18:29:48 +08:00
Err: nil,
}
2023-05-05 13:43:33 +08:00
)
2023-08-11 13:56:40 +08:00
res.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &res.Value)
2023-05-08 18:29:48 +08:00
return res
2023-05-05 13:43:33 +08:00
}
// ToStruct ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:38 2023/5/4
2024-06-08 20:06:35 +08:00
func (str String) ToStruct(receiver any) error {
2023-05-05 13:43:33 +08:00
if nil == receiver {
return errors.New("receiver is nil")
}
2023-08-11 13:56:40 +08:00
return serialize.JSON.UnmarshalWithNumber([]byte(str), receiver)
2023-05-05 13:43:33 +08:00
}
// ToInt8Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:55 2023/5/4
2023-05-08 18:29:48 +08:00
func (str String) ToInt8Slice(splitChar ...string) Int8SliceResult {
result := Int8SliceResult{
Value: []int8{},
Err: nil,
}
2023-05-05 13:43:33 +08:00
isSplit := len(splitChar) > 0
if !isSplit {
2023-08-11 13:56:40 +08:00
result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
2023-05-08 18:29:48 +08:00
return result
2023-05-05 13:43:33 +08:00
}
2023-05-08 18:29:48 +08:00
var (
res = make([]int8, 0)
)
2023-05-05 13:43:33 +08:00
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
2023-05-08 18:29:48 +08:00
if itemVal := String(item).ToInt8(); nil != itemVal.Err {
result.Err = itemVal.Err
return result
2023-05-05 13:43:33 +08:00
} else {
2023-05-08 18:29:48 +08:00
res = append(res, itemVal.Value)
2023-05-05 13:43:33 +08:00
}
}
2023-05-08 18:29:48 +08:00
result.Value = res
return result
2023-05-05 13:43:33 +08:00
}
// ToInt16Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:01 2023/5/4
2023-05-08 18:29:48 +08:00
func (str String) ToInt16Slice(splitChar ...string) Int16SliceResult {
result := Int16SliceResult{
Value: []int16{},
Err: nil,
}
2023-05-05 13:43:33 +08:00
isSplit := len(splitChar) > 0
if !isSplit {
2023-08-11 13:56:40 +08:00
result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
2023-05-08 18:29:48 +08:00
return result
2023-05-05 13:43:33 +08:00
}
2023-05-08 18:29:48 +08:00
res := make([]int16, 0)
2023-05-05 13:43:33 +08:00
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
2023-05-08 18:29:48 +08:00
if itemVal := String(item).ToInt16(); nil != itemVal.Err {
result.Err = itemVal.Err
return result
2023-05-05 13:43:33 +08:00
} else {
2023-05-08 18:29:48 +08:00
res = append(res, itemVal.Value)
2023-05-05 13:43:33 +08:00
}
}
2023-05-08 18:29:48 +08:00
result.Value = res
return result
2023-05-05 13:43:33 +08:00
}
// ToInt32Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:03 2023/5/4
2023-05-08 18:29:48 +08:00
func (str String) ToInt32Slice(splitChar ...string) Int32SliceResult {
result := Int32SliceResult{
Value: []int32{},
Err: nil,
}
2023-05-05 13:43:33 +08:00
isSplit := len(splitChar) > 0
if !isSplit {
2023-08-11 13:56:40 +08:00
result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
2023-05-08 18:29:48 +08:00
return result
2023-05-05 13:43:33 +08:00
}
2023-05-08 18:29:48 +08:00
res := make([]int32, 0)
2023-05-05 13:43:33 +08:00
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
2023-05-08 18:29:48 +08:00
if itemVal := String(item).ToInt32(); nil != itemVal.Err {
result.Err = itemVal.Err
return result
2023-05-05 13:43:33 +08:00
} else {
2023-05-08 18:29:48 +08:00
res = append(res, itemVal.Value)
2023-05-05 13:43:33 +08:00
}
}
2023-05-08 18:29:48 +08:00
result.Value = res
return result
2023-05-05 13:43:33 +08:00
}
// ToInt64Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:04 2023/5/4
2023-05-08 18:29:48 +08:00
func (str String) ToInt64Slice(splitChar ...string) Int64SliceResult {
result := Int64SliceResult{
Value: []int64{},
Err: nil,
}
2023-05-05 13:43:33 +08:00
isSplit := len(splitChar) > 0
if !isSplit {
2023-08-11 13:56:40 +08:00
result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
2023-05-08 18:29:48 +08:00
return result
2023-05-05 13:43:33 +08:00
}
2023-05-08 18:29:48 +08:00
res := make([]int64, 0)
2023-05-05 13:43:33 +08:00
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
2023-05-08 18:29:48 +08:00
if itemVal := String(item).ToInt64(); nil != itemVal.Err {
return result
2023-05-05 13:43:33 +08:00
} else {
2023-05-08 18:29:48 +08:00
res = append(res, itemVal.Value)
2023-05-05 13:43:33 +08:00
}
}
2023-05-08 18:29:48 +08:00
result.Value = res
return result
2023-05-05 13:43:33 +08:00
}
// ToIntSlice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:04 2023/5/4
2023-05-08 18:29:48 +08:00
func (str String) ToIntSlice(splitChar ...string) IntSliceResult {
result := IntSliceResult{
Value: []int{},
Err: nil,
}
2023-05-05 13:43:33 +08:00
isSplit := len(splitChar) > 0
if !isSplit {
2023-08-11 13:56:40 +08:00
result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
2023-05-08 18:29:48 +08:00
return result
2023-05-05 13:43:33 +08:00
}
2023-05-08 18:29:48 +08:00
res := make([]int, 0)
2023-05-05 13:43:33 +08:00
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
2023-05-08 18:29:48 +08:00
if itemVal := String(item).ToInt(); nil != itemVal.Err {
result.Err = itemVal.Err
return result
2023-05-05 13:43:33 +08:00
} else {
2023-05-08 18:29:48 +08:00
res = append(res, itemVal.Value)
2023-05-05 13:43:33 +08:00
}
}
2023-05-08 18:29:48 +08:00
result.Value = res
return result
2023-05-05 13:43:33 +08:00
}
// ToUint8Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:50 2023/5/5
2023-05-08 18:29:48 +08:00
func (str String) ToUint8Slice(splitChar ...string) Uint8SliceResult {
result := Uint8SliceResult{
Value: []uint8{},
Err: nil,
}
2023-05-05 13:43:33 +08:00
isSplit := len(splitChar) > 0
if !isSplit {
2023-08-11 13:56:40 +08:00
result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
2023-05-08 18:29:48 +08:00
return result
2023-05-05 13:43:33 +08:00
}
2023-05-08 18:29:48 +08:00
res := make([]uint8, 0)
2023-05-05 13:43:33 +08:00
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
2023-05-08 18:29:48 +08:00
if itemVal := String(item).ToUint8(); nil != itemVal.Err {
result.Err = itemVal.Err
return result
2023-05-05 13:43:33 +08:00
} else {
2023-05-08 18:29:48 +08:00
res = append(res, itemVal.Value)
2023-05-05 13:43:33 +08:00
}
}
2023-05-08 18:29:48 +08:00
result.Value = res
return result
2023-05-05 13:43:33 +08:00
}
// ToUint16Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:52 2023/5/5
2023-05-08 18:29:48 +08:00
func (str String) ToUint16Slice(splitChar ...string) Uint16SliceResult {
result := Uint16SliceResult{
Value: []uint16{},
Err: nil,
}
2023-05-05 13:43:33 +08:00
isSplit := len(splitChar) > 0
if !isSplit {
2023-08-11 13:56:40 +08:00
result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
2023-05-08 18:29:48 +08:00
return result
2023-05-05 13:43:33 +08:00
}
2023-05-08 18:29:48 +08:00
res := make([]uint16, 0)
2023-05-05 13:43:33 +08:00
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
2023-05-08 18:29:48 +08:00
if itemVal := String(item).ToUint16(); nil != itemVal.Err {
result.Err = itemVal.Err
return result
2023-05-05 13:43:33 +08:00
} else {
2023-05-08 18:29:48 +08:00
res = append(res, itemVal.Value)
2023-05-05 13:43:33 +08:00
}
}
2023-05-08 18:29:48 +08:00
result.Value = res
return result
2023-05-05 13:43:33 +08:00
}
// ToUint32Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:53 2023/5/5
2023-05-08 18:29:48 +08:00
func (str String) ToUint32Slice(splitChar ...string) Uint32SliceResult {
result := Uint32SliceResult{
Value: []uint32{},
Err: nil,
}
2023-05-05 13:43:33 +08:00
isSplit := len(splitChar) > 0
if !isSplit {
2023-08-11 13:56:40 +08:00
result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
2023-05-08 18:29:48 +08:00
return result
2023-05-05 13:43:33 +08:00
}
2023-05-08 18:29:48 +08:00
res := make([]uint32, 0)
2023-05-05 13:43:33 +08:00
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
2023-05-08 18:29:48 +08:00
if itemVal := String(item).ToUint32(); nil != itemVal.Err {
result.Err = itemVal.Err
return result
2023-05-05 13:43:33 +08:00
} else {
2023-05-08 18:29:48 +08:00
res = append(res, itemVal.Value)
2023-05-05 13:43:33 +08:00
}
}
2023-05-08 18:29:48 +08:00
result.Value = res
return result
2023-05-05 13:43:33 +08:00
}
// ToUint64Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:54 2023/5/5
2023-05-08 18:29:48 +08:00
func (str String) ToUint64Slice(splitChar ...string) Uint64SliceResult {
result := Uint64SliceResult{
Value: []uint64{},
Err: nil,
}
2023-05-05 13:43:33 +08:00
isSplit := len(splitChar) > 0
if !isSplit {
2023-08-11 13:56:40 +08:00
result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
2023-05-08 18:29:48 +08:00
return result
2023-05-05 13:43:33 +08:00
}
2023-05-08 18:29:48 +08:00
res := make([]uint64, 0)
2023-05-05 13:43:33 +08:00
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
2023-05-08 18:29:48 +08:00
if itemVal := String(item).ToUint64(); nil != itemVal.Err {
result.Err = itemVal.Err
return result
2023-05-05 13:43:33 +08:00
} else {
2023-05-08 18:29:48 +08:00
res = append(res, itemVal.Value)
2023-05-05 13:43:33 +08:00
}
}
2023-05-08 18:29:48 +08:00
return result
2023-05-05 13:43:33 +08:00
}
// ToUintSlice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:54 2023/5/5
2023-05-08 18:29:48 +08:00
func (str String) ToUintSlice(splitChar ...string) UintSliceResult {
result := UintSliceResult{
Value: []uint{},
Err: nil,
}
2023-05-05 13:43:33 +08:00
isSplit := len(splitChar) > 0
if !isSplit {
2023-08-11 13:56:40 +08:00
result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
2023-05-08 18:29:48 +08:00
return result
2023-05-05 13:43:33 +08:00
}
2023-05-08 18:29:48 +08:00
res := make([]uint, 0)
2023-05-05 13:43:33 +08:00
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
2023-05-08 18:29:48 +08:00
if itemVal := String(item).ToUint(); nil != itemVal.Err {
result.Err = itemVal.Err
return result
2023-05-05 13:43:33 +08:00
} else {
2023-05-08 18:29:48 +08:00
res = append(res, itemVal.Value)
2023-05-05 13:43:33 +08:00
}
}
2023-05-08 18:29:48 +08:00
result.Value = res
return result
2023-05-05 13:43:33 +08:00
}
// ToBoolSlice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:55 2023/5/5
2023-05-08 18:29:48 +08:00
func (str String) ToBoolSlice(splitChar ...string) BoolSliceResult {
result := BoolSliceResult{
Value: []bool{},
Err: nil,
}
2023-05-05 13:43:33 +08:00
isSplit := len(splitChar) > 0
if !isSplit {
2023-08-11 13:56:40 +08:00
result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
2023-05-08 18:29:48 +08:00
return result
2023-05-05 13:43:33 +08:00
}
2023-05-08 18:29:48 +08:00
res := make([]bool, 0)
2023-05-05 13:43:33 +08:00
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
2023-05-08 18:29:48 +08:00
if itemVal := String(item).ToBool(); nil != itemVal.Err {
result.Err = itemVal.Err
return result
2023-05-05 13:43:33 +08:00
} else {
2023-05-08 18:29:48 +08:00
res = append(res, itemVal.Value)
2023-05-05 13:43:33 +08:00
}
}
2023-05-08 18:29:48 +08:00
result.Value = res
return result
2023-05-05 13:43:33 +08:00
}
// ToFloat32Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:56 2023/5/5
2023-05-08 18:29:48 +08:00
func (str String) ToFloat32Slice(splitChar ...string) Float32SliceResult {
result := Float32SliceResult{
Value: []float32{},
Err: nil,
}
2023-05-05 13:43:33 +08:00
isSplit := len(splitChar) > 0
if !isSplit {
2023-08-11 13:56:40 +08:00
result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
2023-05-08 18:29:48 +08:00
return result
2023-05-05 13:43:33 +08:00
}
2023-05-08 18:29:48 +08:00
res := make([]float32, 0)
2023-05-05 13:43:33 +08:00
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
2023-05-08 18:29:48 +08:00
if itemVal := String(item).ToFloat32(); nil != itemVal.Err {
result.Err = itemVal.Err
return result
2023-05-05 13:43:33 +08:00
} else {
2023-05-08 18:29:48 +08:00
res = append(res, itemVal.Value)
2023-05-05 13:43:33 +08:00
}
}
2023-05-08 18:29:48 +08:00
return result
2023-05-05 13:43:33 +08:00
}
// ToFloat64Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:56 2023/5/5
2023-05-08 18:29:48 +08:00
func (str String) ToFloat64Slice(splitChar ...string) Float64SliceResult {
result := Float64SliceResult{
Value: []float64{},
Err: nil,
}
2023-05-05 13:43:33 +08:00
isSplit := len(splitChar) > 0
if !isSplit {
2023-08-11 13:56:40 +08:00
result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
2023-05-08 18:29:48 +08:00
return result
2023-05-05 13:43:33 +08:00
}
2023-05-08 18:29:48 +08:00
res := make([]float64, 0)
2023-05-05 13:43:33 +08:00
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
2023-05-08 18:29:48 +08:00
if itemVal := String(item).ToFloat64(); nil != itemVal.Err {
result.Err = itemVal.Err
return result
2023-05-05 13:43:33 +08:00
} else {
2023-05-08 18:29:48 +08:00
res = append(res, itemVal.Value)
2023-05-05 13:43:33 +08:00
}
}
2023-05-08 18:29:48 +08:00
result.Value = res
return result
2023-05-05 13:43:33 +08:00
}
// ToDoubleSlice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:57 2023/5/5
2023-05-08 18:29:48 +08:00
func (str String) ToDoubleSlice(splitChar ...string) Float64SliceResult {
2023-05-05 13:43:33 +08:00
return str.ToFloat64Slice(splitChar...)
}
// ToNumberSlice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:57 2023/5/5
2023-05-08 18:29:48 +08:00
func (str String) ToNumberSlice(splitChar ...string) Float64SliceResult {
2023-05-05 13:43:33 +08:00
return str.ToFloat64Slice(splitChar...)
}
2023-05-05 15:02:45 +08:00
2023-09-04 20:35:42 +08:00
// ToDuration 转换为时间格式
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:31 2023/9/4
func (str String) ToDuration(timeUnit time.Duration) DurationResult {
int64Val := str.ToInt64()
if nil != int64Val.Err {
return DurationResult{
Value: 0,
Err: int64Val.Err,
}
}
return Int(int64Val.Value).ToDuration(timeUnit)
}
2023-08-06 15:13:38 +08:00
// ToStringSlice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:11 2023/8/6
func (str String) ToStringSlice(splitChar ...string) StringSliceResult {
result := StringSliceResult{
Value: []string{},
Err: nil,
}
isSplit := len(splitChar) > 0
if !isSplit {
2023-08-11 13:56:40 +08:00
result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
2023-08-06 15:13:38 +08:00
return result
}
result.Value = strings.Split(string(str), splitChar[0])
return result
}
2023-05-05 15:02:45 +08:00
// ToAnySlice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:01 2023/5/5
2024-01-23 10:42:03 +08:00
func (str String) ToAnySlice(splitCharList ...string) AnySliceResult {
2023-05-08 18:29:48 +08:00
result := AnySliceResult{
2024-06-08 20:06:35 +08:00
Value: []any{},
2023-05-08 18:29:48 +08:00
Err: nil,
2023-05-05 15:02:45 +08:00
}
2023-05-08 18:29:48 +08:00
2024-01-23 10:42:03 +08:00
if len(splitCharList) == 0 {
result.Err = serialize.JSON.UnmarshalWithNumber([]byte(str), &result.Value)
return result
}
valArr := strings.Split(str.Value(), splitCharList[0])
2024-06-08 20:06:35 +08:00
valList := make([]any, 0)
2024-01-23 10:42:03 +08:00
for _, item := range valArr {
v := String(item)
if res := v.ToInt64(); nil == res.Err {
valList = append(valList, res.Value)
} else if res := v.ToUint64(); nil == res.Err {
valList = append(valList, res.Value)
} else if res := v.ToFloat64(); nil == res.Err {
valList = append(valList, res.Value)
} else if res := v.ToBool(); nil == res.Err {
valList = append(valList, res.Value)
} else {
valList = append(valList, item)
}
}
result.Value = valList
2023-05-08 18:29:48 +08:00
return result
2023-05-05 15:02:45 +08:00
}
2023-05-16 12:12:18 +08:00
// Md5 计算Md5值
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:08 2023/5/16
func (str String) Md5() StringResult {
h := md5.New()
_, err := io.WriteString(h, str.Value())
if nil != err {
return StringResult{
Value: "",
Err: err,
}
}
return StringResult{
2023-08-11 22:23:16 +08:00
Value: hex.EncodeToString(h.Sum(nil)),
2023-05-16 12:12:18 +08:00
Err: nil,
}
}
// Value ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:11 2023/5/16
func (str String) Value() string {
return string(str)
}
2023-08-11 15:15:13 +08:00
// GetLetterList 获取字母列表
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:53 2023/8/11
func (str String) GetLetterList() []string {
return []string{
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
}
}
// SnakeCaseToCamel 蛇形字符串转换为驼峰
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 4:58 下午 2021/10/25
func (str String) SnakeCaseToCamel() string {
if len(str) == 0 {
return ""
}
builder := strings.Builder{}
index := 0
if str[0] >= 'a' && str[0] <= 'z' {
builder.WriteByte(str[0] - ('a' - 'A'))
index = 1
}
for i := index; i < len(str); i++ {
if str[i] == '_' && i+1 < len(str) {
if str[i+1] >= 'a' && str[i+1] <= 'z' {
builder.WriteByte(str[i+1] - ('a' - 'A'))
i++
continue
}
}
// 将ID转为大写
if str[i] == 'd' && i-1 >= 0 && (str[i-1] == 'i' || str[i-1] == 'I') && (i+1 == len(str) || i+1 < len(str) && str[i+1] == '_') {
builder.WriteByte('d' - ('a' - 'A'))
continue
}
builder.WriteByte(str[i])
}
return builder.String()
}
// Convert 字符串编码转换
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:38 2022/7/9
func (str String) Convert(sourceCode string, targetCode string) String {
sourceCoder := mahonia.NewDecoder(sourceCode)
sourceResult := sourceCoder.ConvertString(str.Value())
targetCoder := mahonia.NewDecoder(targetCode)
_, cdata, _ := targetCoder.Translate([]byte(sourceResult), true)
return String(string(cdata))
}
// ClearChar 清理指定字符
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 14:59 2023/8/11
func (str String) ClearChar(charList ...string) String {
if len(charList) == 0 {
return str
}
formatStr := str.Value()
for _, item := range charList {
formatStr = strings.ReplaceAll(formatStr, item, "")
}
return String(formatStr)
}
// ReplaceChineseChar 替换常见的中文符号
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:59 2023/4/4
func (str String) ReplaceChineseChar() String {
charTable := map[string]string{
"": "(",
"": ")",
"": ":",
"": ",",
"。": ".",
"【": "]",
"】": "]",
}
return str.ReplaceChar(charTable)
}
// ReplaceChar 替换指定字符
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:01 2023/8/11
func (str String) ReplaceChar(charTable map[string]string) String {
if len(charTable) == 0 {
return str
}
formatStr := str.Value()
for k, v := range charTable {
formatStr = strings.ReplaceAll(formatStr, k, v)
}
return String(formatStr)
}
// HasSubStr 是否包含指定的子串
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:37 2024/4/19
func (str String) HasSubStr(subStrList []string) bool {
if len(subStrList) == 0 {
return true
}
v := str.Value()
for _, item := range subStrList {
if strings.Contains(v, item) {
return true
}
}
return false
}
2024-06-27 11:17:06 +08:00
// HashNumber ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:05 2024/6/27
func (str String) HashNumber() Uint64Result {
return Uint64Result{
Value: murmur3.Sum64([]byte(str.Value())),
Err: nil,
}
}