From 96532aff0019e6c95923344f6d6f9258c1f71a7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 5 May 2023 10:59:24 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E7=89=88string2other?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- string_convert.go | 258 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 258 insertions(+) diff --git a/string_convert.go b/string_convert.go index 767b26e..3ac30ee 100644 --- a/string_convert.go +++ b/string_convert.go @@ -393,3 +393,261 @@ func (sc *stringConvert) ToIntSlice(str string, splitChar ...string) ([]int, err } return res, nil } + +// ToUint8Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:50 2023/5/5 +func (sc *stringConvert) ToUint8Slice(str string, splitChar ...string) ([]uint8, error) { + var ( + err error + res []uint8 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]uint8, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToUint8(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToUint16Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:52 2023/5/5 +func (sc *stringConvert) ToUint16Slice(str string, splitChar ...string) ([]uint16, error) { + var ( + err error + res []uint16 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]uint16, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToUint16(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToUint32Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:53 2023/5/5 +func (sc *stringConvert) ToUint32Slice(str string, splitChar ...string) ([]uint32, error) { + var ( + err error + res []uint32 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]uint32, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToUint32(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToUint64Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:54 2023/5/5 +func (sc *stringConvert) ToUint64Slice(str string, splitChar ...string) ([]uint64, error) { + var ( + err error + res []uint64 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]uint64, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToUint64(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToUintSlice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:54 2023/5/5 +func (sc *stringConvert) ToUintSlice(str string, splitChar ...string) ([]uint, error) { + var ( + err error + res []uint + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]uint, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToUint(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToBoolSlice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:55 2023/5/5 +func (sc *stringConvert) ToBoolSlice(str string, splitChar ...string) ([]bool, error) { + var ( + err error + res []bool + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]bool, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToBool(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToFloat32Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:56 2023/5/5 +func (sc *stringConvert) ToFloat32Slice(str string, splitChar ...string) ([]float32, error) { + var ( + err error + res []float32 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]float32, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToFloat32(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToFloat64Slice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:56 2023/5/5 +func (sc *stringConvert) ToFloat64Slice(str string, splitChar ...string) ([]float64, error) { + var ( + err error + res []float64 + ) + + isSplit := len(splitChar) > 0 + if !isSplit { + if err = JSON.UnmarshalWithNumber([]byte(str), &res); nil != err { + return nil, err + } + return res, nil + } + res = make([]float64, 0) + arr := strings.Split(str, splitChar[0]) + for _, item := range arr { + if itemVal, err := sc.ToFloat64(item); nil != err { + return nil, err + } else { + res = append(res, itemVal) + } + } + return res, nil +} + +// ToDoubleSlice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:57 2023/5/5 +func (sc *stringConvert) ToDoubleSlice(str string, splitChar ...string) ([]float64, error) { + return sc.ToFloat64Slice(str, splitChar...) +} + +// ToNumberSlice ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 10:57 2023/5/5 +func (sc *stringConvert) ToNumberSlice(str string, splitChar ...string) ([]float64, error) { + return sc.ToFloat64Slice(str, splitChar...) +}