package strutilimport ()var (// ErrDateLayout errorErrDateLayout = errors.New("invalid date layout string")// ErrInvalidParam errorErrInvalidParam = errors.New("invalid input for parse time")// some regex for convert string. toSnakeReg = regexp.MustCompile("[A-Z][a-z]") toCamelRegs = map[string]*regexp.Regexp{" ": regexp.MustCompile(" +[a-zA-Z]"),"-": regexp.MustCompile("-+[a-zA-Z]"),"_": regexp.MustCompile("_+[a-zA-Z]"), })// Internal func refers:// strconv.QuoteRune()// strconv.QuoteToASCII()// strconv.AppendQuote()// strconv.AppendQuoteRune()// Quote alias of strings.Quotefunc ( string) string { returnstrconv.Quote() }// Unquote remove start and end quotes by single-quote or double-quote//// tip: strconv.Unquote cannot unquote single-quotefunc ( string) string { := len()if < 2 {return } , := [0], [-1]varboolif == '"' && == '"' { = true } elseif == '\'' && == '\'' { = true }if { = [1 : -1] // exclude quotes }// strconv.Unquote cannot unquote single-quote // if ns, err := strconv.Unquote(s); err == nil { // return ns // }return}// Join alias of strings.Joinfunc ( string, ...string) string { returnstrings.Join(, ) }// JoinList alias of strings.Joinfunc ( string, []string) string { returnstrings.Join(, ) }// JoinComma quick join strings by commafunc ( []string) string { returnstrings.Join(, ",") }// JoinAny type to stringfunc ( string, ...any) string { := make([]string, 0, len())for , := range { = append(, QuietString()) }returnstrings.Join(, )}// Implode alias of strings.Joinfunc ( string, ...string) string { returnstrings.Join(, ) }/************************************************************* * region value to string *************************************************************/// String convert value to string, return error on failedfunc ( any) (string, error) { returnToStringWith() }// ToString convert value to string, return error on failedfunc ( any) (string, error) { returnToStringWith() }// StringOrErr convert value to string, return error on failedfunc ( any) (string, error) { returnToStringWith() }// QuietString convert value to string, will ignore error. same as SafeString()func ( any) string { returnSafeString() }// SafeString convert value to string. Will ignore errorfunc ( any) string { , := AnyToString(, false)return}// StringOrPanic convert value to string, will panic on errorfunc ( any) string { returnMustString() }// MustString convert value to string. will panic on errorfunc ( any) string { , := ToStringWith()if != nil {panic() }return}// StringOrDefault convert any value to string, return default value on failedfunc ( any, string) string { returnStringOr(, ) }// StringOr convert any value to string, return default value on failedfunc ( any, string) string { , := ToStringWith()if != nil {return }return}// AnyToString convert any value to string.//// For defaultAsErr://// - False will use fmt.Sprint convert unsupported type// - True will return error on convert fail.func ( any, bool) ( string, error) {varcomfunc.ConvOptionFnif ! { = comfunc.WithUserConvFn(comfunc.StrBySprintFn) }returncomfunc.ToStringWith(, )}// ToStringWith try to convert value to string. can with some option func, more see comfunc.ConvOption.func ( any, ...comfunc.ConvOptionFn) (string, error) {returncomfunc.ToStringWith(, ...)}/************************************************************* * region string value to bool *************************************************************/// ToBool convert string to boolfunc ( string) (bool, error) {returncomfunc.StrToBool(strings.TrimSpace())}// QuietBool convert to bool, will ignore errorfunc ( string) bool { returnSafeBool() }// SafeBool convert to bool and will ignore errorfunc ( string) bool { , := comfunc.StrToBool(strings.TrimSpace())return}// MustBool convert to bool and will panic on errorfunc ( string) bool { , := ToBool()if != nil {panic() }return}// Bool parse string to bool. like strconv.ParseBool()func ( string) (bool, error) {returncomfunc.StrToBool(strings.TrimSpace())}/************************************************************* * region string value to int *************************************************************/// Int convert string to int, alias of ToInt()func ( string) (int, error) {returnstrconv.Atoi(strings.TrimSpace())}// ToInt convert string to int, return error on failfunc ( string) (int, error) {returnstrconv.Atoi(strings.TrimSpace())}// IntOrDefault convert string to int, return default value on failfunc ( string, int) int {returnIntOr(, )}// IntOr convert string to int, return default value on failfunc ( string, int) int { , := ToInt()if != nil {return }return}// SafeInt convert string to int, will ignore errorfunc ( string) int { , := ToInt()return}// QuietInt convert string to int, will ignore errorfunc ( string) int { returnSafeInt() }// MustInt convert string to int, will panic on errorfunc ( string) int { returnIntOrPanic() }// IntOrPanic convert value to int, will panic on errorfunc ( string) int { , := ToInt()if != nil {panic() }return}/************************************************************* * region convert string to int64 *************************************************************/// Int64 convert string to int, will ignore errorfunc ( string) int64 { returnSafeInt64() }// QuietInt64 convert string to int, will ignore errorfunc ( string) int64 { returnSafeInt64() }// SafeInt64 convert string to int, will ignore errorfunc ( string) int64 { , := Int64OrErr()return}// ToInt64 convert string to int, return error on failfunc ( string) (int64, error) {returnstrconv.ParseInt(, 10, 0)}// Int64OrDefault convert string to int, return default value on failfunc ( string, int64) int64 {returnInt64Or(, )}// Int64Or convert string to int, return default value on failfunc ( string, int64) int64 { , := ToInt64()if != nil {return }return}// Int64OrErr convert string to int, return error on failfunc ( string) (int64, error) {returnstrconv.ParseInt(, 10, 0)}// MustInt64 convert value to int, will panic on errorfunc ( string) int64 { returnInt64OrPanic() }// Int64OrPanic convert value to int, will panic on errorfunc ( string) int64 { , := strconv.ParseInt(, 10, 0)if != nil {panic() }return}/************************************************************* * region string value to uint *************************************************************/// Uint convert string to uint, will ignore errorfunc ( string) uint64 { returnSafeUint() }// SafeUint convert string to uint, will ignore errorfunc ( string) uint64 { , := UintOrErr()return}// ToUint convert string to uint, return error on fail. alias of UintOrErr()func ( string) (uint64, error) {returnstrconv.ParseUint(, 10, 0)}// UintOrErr convert string to uint, return error on failfunc ( string) (uint64, error) {returnstrconv.ParseUint(, 10, 0)}// MustUint convert value to uint, will panic on error. alias of UintOrPanic()func ( string) uint64 { returnUintOrPanic() }// UintOrPanic convert value to uint, will panic on errorfunc ( string) uint64 { , := UintOrErr()if != nil {panic() }return}// UintOrDefault convert string to uint, return default value on failfunc ( string, uint64) uint64 {returnUintOr(, )}// UintOr convert string to uint, return default value on failfunc ( string, uint64) uint64 { , := UintOrErr()if != nil {return }return}/************************************************************* * region string value to byte * refer from https://github.com/valyala/fastjson/blob/master/util.go *************************************************************/// Byte2str convert bytes to stringfunc ( []byte) string {return *(*string)(unsafe.Pointer(&))}// Byte2string convert bytes to stringfunc ( []byte) string {return *(*string)(unsafe.Pointer(&))}// ToBytes convert string to bytesfunc ( string) ( []byte) { := (*reflect.StringHeader)(unsafe.Pointer(&)) := (*reflect.SliceHeader)(unsafe.Pointer(&)) .Data = .Data .Len = .Len .Cap = .Lenreturn}/************************************************************* * region string to int/string slice, time.Time *************************************************************/// Ints alias of the ToIntSlice(). default sep is comma(,)func ( string, ...string) []int { , := ToIntSlice(, ...)return}// ToInts alias of the ToIntSlice(). default sep is comma(,)func ( string, ...string) ([]int, error) { returnToIntSlice(, ...) }// ToIntSlice split string to slice and convert item to int.//// Default sep is commafunc ( string, ...string) ( []int, error) { := ToSlice(, ...)for , := range { , := mathutil.ToInt()if != nil {return []int{}, } = append(, ) }return}// ToArray alias of the ToSlice()func ( string, ...string) []string { returnToSlice(, ...) }// Strings alias of the ToSlice()func ( string, ...string) []string { returnToSlice(, ...) }// ToStrings alias of the ToSlice()func ( string, ...string) []string { returnToSlice(, ...) }// ToSlice split string to array.func ( string, ...string) []string {iflen() > 0 {returnSplit(, [0]) }returnSplit(, ",")}// ToDuration parses a duration string. such as "300ms", "-1.5h" or "2h45m".// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".func ( string) (time.Duration, error) {returncomfunc.ToDuration()}
The pages are generated with Goldsv0.8.4. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.