Documentation
¶
Overview ¶
Package strutil provide some string,char,byte util functions
Index ¶
- Constants
- Variables
- func AnyToString(val interface{}, defaultAsErr bool) (str string, err error)
- 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 FilterEmail(s string) string
- func GenMd5(src interface{}) string
- func HasAllSubs(s string, subs []string) bool
- func HasOnePrefix(s string, subs []string) bool
- func HasOneSub(s string, subs []string) bool
- func Int(s string) (int, error)
- 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 IsEndOf(s, sub string) bool
- func IsNumeric(c byte) bool
- func IsSpace(c byte) bool
- func IsSpaceRune(r rune) bool
- func IsStartOf(s, sub string) bool
- func IsStartsOf(s string, subs []string) bool
- func IsSymbol(r rune) bool
- func Len(s string) int
- func LowerFirst(s string) string
- func Lowercase(s string) string
- func Md5(src interface{}) string
- func MicroTimeHexID() string
- func MicroTimeID() string
- func MustBool(s string) bool
- func MustInt(s string) int
- func MustString(in interface{}) string
- 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 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 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 Strings(s string, sep ...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 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 UpperFirst(s string) string
- func UpperWord(s string) string
- func Uppercase(s string) string
- func Utf8len(s string) int
- func ValidUtf8String(s string) bool
- type ByteChanPool
- type SimilarComparator
- type Str
Constants ¶
const ( AlphaBet = "abcdefghijklmnopqrstuvwxyz" AlphaNum = "abcdefghijklmnopqrstuvwxyz0123456789" AlphaNum2 = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" )
const ( PosLeft uint8 = iota PosRight )
Position for padding string
Variables ¶
var ( Equal = strings.EqualFold HasPrefix = strings.HasPrefix HasSuffix = strings.HasSuffix )
var ( ErrConvertFail = errors.New("convert data type is failure") ErrInvalidParam = errors.New("invalid input parameter") )
var ( // EscapeJS escape javascript string EscapeJS = template.JSEscapeString // EscapeHTML escape html string EscapeHTML = template.HTMLEscapeString )
Some alias methods.
var ( DefMinInt = 1000 DefMaxInt = 9999 )
global id:
https://github.com/rs/xid https://github.com/satori/go.uuid
var ( Ltrim = TrimLeft Rtrim = TrimRight )
Functions ¶
func AnyToString ¶ added in v0.3.10
AnyToString convert value to string
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 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 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 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
func PrettyJSON ¶
PrettyJSON get pretty Json string Deprecated
please use fmtutil.PrettyJSON() or jsonutil.Pretty() instead it
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 filter empty string node.
func ToIntSlice ¶ added in v0.2.2
ToIntSlice split string to slice and convert item to int.
Types ¶
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
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