Documentation
¶
Overview ¶
Package strutil provide some string,char,byte util functions
Index ¶
- Constants
- Variables
- func AddSlashes(s string) string
- func AnyToString(val interface{}, defaultAsErr bool) (str string, err error)
- func B32Decode(str string) string
- func B32Encode(str string) string
- func B64Decode(str string) string
- func B64Encode(str string) string
- func Base64(str string) string
- func Bool(s string) (bool, error)
- func Byte2str(b []byte) string
- func Byte2string(b []byte) string
- func BytePos(s string, bt byte) int
- func Camel(s string, sep ...string) string
- func CamelCase(s string, sep ...string) string
- func Cut(s, sep string) (before string, after string, found bool)
- func EscapeHTML(s string) string
- func EscapeJS(s string) string
- func FilterEmail(s string) string
- func GenMd5(src interface{}) string
- func HasAllSubs(s string, subs []string) bool
- func HasOnePrefix(s string, prefixes []string) bool
- func HasOneSub(s string, subs []string) bool
- func HasPrefix(s string, prefix string) bool
- func HasSuffix(s string, suffix string) bool
- func Implode(sep string, ss ...string) string
- func Int(s string) (int, error)
- func Int64(s string) int64
- func Int64OrErr(s string) (int64, error)
- func Int64OrPanic(s string) int64
- func IntOrPanic(s string) int
- func Ints(s string, sep ...string) []int
- func IsAlphaNum(c uint8) bool
- func IsAlphabet(char uint8) bool
- func IsBlank(s string) bool
- func IsBlankBytes(bs []byte) bool
- func IsEmpty(s string) bool
- func IsEndOf(s, suffix string) bool
- func IsNotBlank(s string) bool
- func IsNumeric(c byte) bool
- func IsSpace(c byte) bool
- func IsSpaceRune(r rune) bool
- func IsStartOf(s, prefix string) bool
- func IsStartsOf(s string, prefixes []string) bool
- func IsSymbol(r rune) bool
- func IsValidUtf8(s string) bool
- func Join(sep string, ss ...string) string
- func JoinSubs(sep string, ss []string) string
- func LTrim(s string, cutSet ...string) string
- func Len(s string) int
- func Lower(s string) string
- func LowerFirst(s string) string
- func Lowercase(s string) string
- func Ltrim(s string, cutSet ...string) string
- func MD5(src interface{}) string
- func Md5(src interface{}) string
- func MicroTimeHexID() string
- func MicroTimeID() string
- func MustBool(s string) bool
- func MustCut(s, sep string) (before string, after string)
- func MustInt(s string) int
- func MustString(in interface{}) string
- func MustToTime(s string, layouts ...string) time.Time
- func PadLeft(s, pad string, length int) string
- func PadRight(s, pad string, length int) string
- func Padding(s, pad string, length int, pos uint8) string
- func PrettyJSON(v interface{}) (string, error)
- func QuietBool(s string) bool
- func QuietInt(s string) int
- func QuietInt64(s string) int64
- func QuietString(in interface{}) string
- func RTrim(s string, cutSet ...string) string
- func RandomBytes(length int) ([]byte, error)
- func RandomChars(ln int) string
- func RandomCharsV2(ln int) string
- func RandomCharsV3(ln int) string
- func RandomString(length int) (string, error)
- func RenderTemplate(input string, data interface{}, fns template.FuncMap, isFile ...bool) string
- func RenderText(input string, data interface{}, fns template.FuncMap, isFile ...bool) string
- func Repeat(s string, times int) string
- func RepeatBytes(char byte, times int) (chars []byte)
- func RepeatRune(char rune, times int) (chars []rune)
- func Replaces(str string, pairs map[string]string) string
- func Rtrim(s string, cutSet ...string) string
- func RuneLen(s string) int
- func RunePos(s string, ru rune) int
- func Similarity(s, t string, rate float32) (float32, bool)
- func Snake(s string, sep ...string) string
- func SnakeCase(s string, sep ...string) string
- func Split(s, sep string) (ss []string)
- func SplitN(s, sep string, n int) (ss []string)
- func SplitNTrimmed(s, sep string, n int) (ss []string)
- func SplitNValid(s, sep string, n int) (ss []string)
- func SplitTrimmed(s, sep string) (ss []string)
- func SplitValid(s, sep string) (ss []string)
- func StrPos(s, sub string) int
- func String(val interface{}) (string, error)
- func StringOrErr(val interface{}) (string, error)
- func Strings(s string, sep ...string) []string
- func StripSlashes(s string) string
- func Substr(s string, pos, length int) string
- func ToArray(s string, sep ...string) []string
- func ToBool(s string) (bool, error)
- func ToBytes(s string) (b []byte)
- func ToDuration(s string) (time.Duration, error)
- func ToInt(s string) (int, error)
- func ToIntSlice(s string, sep ...string) (ints []int, err error)
- func ToInts(s string, sep ...string) ([]int, error)
- func ToSlice(s string, sep ...string) []string
- func ToString(val interface{}) (string, error)
- func ToStrings(s string, sep ...string) []string
- func ToTime(s string, layouts ...string) (t time.Time, err error)
- func Trim(s string, cutSet ...string) string
- func TrimLeft(s string, cutSet ...string) string
- func TrimRight(s string, cutSet ...string) string
- func URLDecode(s string) string
- func URLEncode(s string) string
- func Upper(s string) string
- func UpperFirst(s string) string
- func UpperWord(s string) string
- func Uppercase(s string) string
- func Utf8Len(s string) int
- func Utf8len(s string) int
- func VersionCompare(v1, v2, op string) bool
- type BaseEncoder
- type Buffer
- type ByteChanPool
- type SimilarComparator
- type StrVal
- type Value
- func (s Value) Bool() bool
- func (s Value) Bytes() []byte
- func (s Value) HasPrefix(sub string) bool
- func (s Value) HasSuffix(sub string) bool
- func (s Value) Int() int
- func (s Value) Int64() int64
- func (s Value) IsEmpty() bool
- func (s Value) IsEndWith(sub string) bool
- func (s Value) IsStartWith(sub string) bool
- func (s *Value) Set(val string) error
- func (s Value) Split(sep string) []string
- func (s Value) SplitN(sep string, n int) []string
- func (s Value) String() string
- func (s Value) TrimSpace() Value
- func (s Value) Val() string
Constants ¶
const ( Numbers = "0123456789" AlphaBet = "abcdefghijklmnopqrstuvwxyz" AlphaBet1 = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz" AlphaNum = "abcdefghijklmnopqrstuvwxyz0123456789" AlphaNum2 = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" AlphaNum3 = "0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz" )
some consts string chars
const ( PosLeft uint8 = iota PosRight )
Position for padding string
Variables ¶
var ( ErrConvertFail = errors.New("convert data type is failure") ErrInvalidParam = errors.New("invalid input parameter") )
var ( DefMinInt = 1000 DefMaxInt = 9999 )
global id:
https://github.com/rs/xid https://github.com/satori/go.uuid
var Equal = strings.EqualFold
Equal check
var (
Title = strings.ToTitle
)
Title Some alias methods.
Functions ¶
func AddSlashes ¶ added in v0.5.6
AddSlashes add slashes for the string.
func AnyToString ¶ added in v0.3.10
AnyToString convert value to string.
if defaultAsErr is False, will use fmt.Sprint convert complex type
func Byte2string ¶ added in v0.3.14
Byte2string convert bytes to string
func CamelCase ¶ added in v0.2.2
CamelCase convert string to camel case.
Support:
"range_price" -> "rangePrice" "range price" -> "rangePrice" "range-price" -> "rangePrice"
func FilterEmail ¶ added in v0.2.2
FilterEmail filter email, clear invalid chars.
func HasAllSubs ¶ added in v0.4.0
HasAllSubs all substr in the given string.
func HasOnePrefix ¶ added in v0.4.0
HasOnePrefix the string start withs one of the subs
func Int64OrErr ¶ added in v0.5.5
Int64OrErr convert string to int
func Int64OrPanic ¶ added in v0.5.5
Int64OrPanic convert value to int, will panic on error
func IntOrPanic ¶ added in v0.4.1
IntOrPanic convert value to int, will panic on error
func IsAlphaNum ¶ added in v0.3.11
IsAlphaNum reports whether the byte is an ASCII letter, number, or underscore
func IsBlankBytes ¶ added in v0.3.12
IsBlankBytes returns true if the given []byte is all space characters.
func IsNotBlank ¶ added in v0.4.1
IsNotBlank returns true if the given string is not blank.
func IsNumeric ¶ added in v0.3.12
IsNumeric returns true if the given character is a numeric, otherwise false.
func IsSpace ¶ added in v0.3.12
IsSpace returns true if the given character is a space, otherwise false.
func IsSpaceRune ¶ added in v0.3.12
IsSpaceRune returns true if the given rune is a space, otherwise false.
func IsStartsOf ¶ added in v0.4.0
IsStartsOf alias of the HasOnePrefix
func IsValidUtf8 ¶ added in v0.5.6
IsValidUtf8 valid utf8 string check
func MicroTimeHexID ¶ added in v0.3.6
func MicroTimeHexID() string
MicroTimeHexID generate. return like: 5b5f0588af1761ad3(len: 16-17)
func MicroTimeID ¶ added in v0.3.6
func MicroTimeID() string
MicroTimeID generate. return like: 16074145697981929446(len: 20)
func MustString ¶ added in v0.2.2
func MustString(in interface{}) string
MustString convert value to string, TODO will panic on error
func MustToTime ¶ added in v0.5.1
MustToTime convert date string to time.Time
func PrettyJSON ¶
PrettyJSON get pretty Json string Deprecated: please use fmtutil.PrettyJSON() or jsonutil.Pretty() instead it
func QuietInt64 ¶ added in v0.5.5
QuietInt64 convert string to int, will ignore error
func QuietString ¶ added in v0.5.3
func QuietString(in interface{}) string
QuietString convert value to string, will ignore error
func RandomChars ¶ added in v0.3.6
RandomChars generate give length random chars at `a-z`
func RandomCharsV2 ¶ added in v0.3.6
RandomCharsV2 generate give length random chars in `0-9a-z`
func RandomCharsV3 ¶ added in v0.3.6
RandomCharsV3 generate give length random chars in `0-9a-zA-Z`
func RandomString ¶
RandomString generate. Example:
// this will give us a 44 byte, base64 encoded output token, err := RandomString(32) if err != nil { // Serve an appropriately vague error to the // user, but log the details internally. }
func RenderTemplate ¶
RenderTemplate render text template
func RenderText ¶ added in v0.2.4
RenderText render text template
func RepeatBytes ¶ added in v0.3.10
RepeatBytes repeat a byte char.
func RepeatRune ¶
RepeatRune repeat a rune char.
func Replaces ¶
Replaces replace multi strings
pairs: {old1: new1, old2: new2, ...}
Can also use:
strings.NewReplacer("old1", "new1", "old2", "new2").Replace(str)
func SplitNTrimmed ¶ added in v0.4.0
SplitNTrimmed split string to slice. will trim space for each node, but not filter empty
func SplitNValid ¶ added in v0.4.0
SplitNValid string to slice. will filter empty string node.
func SplitTrimmed ¶ added in v0.4.0
SplitTrimmed split string to slice. will trim space for each node, but not filter empty
func SplitValid ¶ added in v0.4.0
SplitValid string to slice. will trim each item and filter empty string node.
func StringOrErr ¶ added in v0.5.3
StringOrErr convert value to string, return error on failed
func StripSlashes ¶ added in v0.5.6
StripSlashes strip slashes for the string.
func ToDuration ¶ added in v0.5.6
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 ToIntSlice ¶ added in v0.2.2
ToIntSlice split string to slice and convert item to int.
func VersionCompare ¶ added in v0.5.5
VersionCompare for two version string.
Types ¶
type BaseEncoder ¶ added in v0.5.3
type BaseEncoder struct { // Base value Base int }
BaseEncoder struct
func NewBaseEncoder ¶ added in v0.5.3
func NewBaseEncoder(base int) *BaseEncoder
NewBaseEncoder instance
func (*BaseEncoder) Decode ¶ added in v0.5.3
func (be *BaseEncoder) Decode(s string) (string, error)
Decode handle
func (*BaseEncoder) Encode ¶ added in v0.5.3
func (be *BaseEncoder) Encode(s string) string
Encode handle
type Buffer ¶ added in v0.5.3
Buffer wrap and extends the bytes.Buffer
func (*Buffer) MustWriteString ¶ added in v0.5.3
MustWriteString to buffer
func (*Buffer) QuietWriteByte ¶ added in v0.5.5
QuietWriteByte to buffer
func (*Buffer) QuietWriteString ¶ added in v0.5.3
QuietWriteString to buffer
func (*Buffer) QuietWritef ¶ added in v0.5.3
QuietWritef write message to buffer
func (*Buffer) QuietWriteln ¶ added in v0.5.3
QuietWriteln write message to buffer with newline
type ByteChanPool ¶ added in v0.4.0
type ByteChanPool struct {
// contains filtered or unexported fields
}
ByteChanPool struct
Usage:
bp := strutil.NewByteChanPool(500, 1024, 1024) buf:=bp.Get() defer bp.Put(buf) // use buf do something ...
refer https://www.flysnow.org/2020/08/21/golang-chan-byte-pool.html from https://github.com/minio/minio/blob/master/internal/bpool/bpool.go
func NewByteChanPool ¶ added in v0.4.0
func NewByteChanPool(maxSize int, width int, capWidth int) *ByteChanPool
NewByteChanPool instance
func (*ByteChanPool) Get ¶ added in v0.4.0
func (bp *ByteChanPool) Get() (b []byte)
Get gets a []byte from the BytePool, or creates a new one if none are available in the pool.
func (*ByteChanPool) Put ¶ added in v0.4.0
func (bp *ByteChanPool) Put(b []byte)
Put returns the given Buffer to the BytePool.
func (*ByteChanPool) Width ¶ added in v0.4.0
func (bp *ByteChanPool) Width() (n int)
Width returns the width of the byte arrays in this pool.
func (*ByteChanPool) WidthCap ¶ added in v0.4.0
func (bp *ByteChanPool) WidthCap() (n int)
WidthCap returns the cap width of the byte arrays in this pool.
type SimilarComparator ¶
type SimilarComparator struct {
// contains filtered or unexported fields
}
SimilarComparator definition links:
https://github.com/mkideal/cli/blob/master/fuzzy.go