Documentation
¶
Overview ¶
Package byteutil provides some useful functions for byte slice.
Index ¶
- Variables
- func AppendAny(dst []byte, v any) []byte
- func Cut(bs []byte, sep byte) (before, after []byte, found bool)
- func FirstLine(bs []byte) []byte
- func IsNumChar(c byte) bool
- func Md5(src any) []byte
- func Random(length int) ([]byte, error)
- func SafeBytes(v any) []byte
- func SafeCut(bs []byte, sep byte) (before, after []byte)
- func SafeCuts(bs []byte, sep []byte) (before, after []byte)
- func SafeString(bs []byte, err error) string
- func ShortMd5(src any) []byte
- func StrOrErr(bs []byte, err error) (string, error)
- func String(b []byte) string
- func ToBytes(v any) ([]byte, error)
- func ToBytesWithFunc(v any, usrFn ToBytesFunc) ([]byte, error)
- func ToString(b []byte) string
- type Buffer
- func (b *Buffer) Close() error
- func (b *Buffer) Flush() error
- func (b *Buffer) PrintByte(c byte)
- func (b *Buffer) Printf(tpl string, vs ...any)
- func (b *Buffer) Println(vs ...any)
- func (b *Buffer) ResetAndGet() string
- func (b *Buffer) ResetGet() string
- func (b *Buffer) Sync() error
- func (b *Buffer) WriteAny(vs ...any)
- func (b *Buffer) WriteAnyNl(vs ...any)
- func (b *Buffer) WriteStr(ss ...string)
- func (b *Buffer) WriteStr1(s string)
- func (b *Buffer) WriteStr1Nl(s string)
- func (b *Buffer) WriteStringNl(ss ...string)
- func (b *Buffer) WriteStrings(ss []string)
- func (b *Buffer) Writeln(vs ...any)
- type BytesDecodeFunc
- type BytesEncodeFunc
- type BytesEncoder
- type ChanPool
- type StdEncoder
- type ToBytesFunc
Constants ¶
This section is empty.
Variables ¶
var ( // HexEncoder instance HexEncoder = NewStdEncoder(func(src []byte) []byte { dst := make([]byte, hex.EncodedLen(len(src))) hex.Encode(dst, src) return dst }, func(src []byte) ([]byte, error) { n, err := hex.Decode(src, src) return src[:n], err }) // B64Encoder instance B64Encoder = NewStdEncoder(func(src []byte) []byte { b64Dst := make([]byte, base64.StdEncoding.EncodedLen(len(src))) base64.StdEncoding.Encode(b64Dst, src) return b64Dst }, func(src []byte) ([]byte, error) { dBuf := make([]byte, base64.StdEncoding.DecodedLen(len(src))) n, err := base64.StdEncoding.Decode(dBuf, src) if err != nil { return nil, err } return dBuf[:n], err }) )
Functions ¶
func SafeCuts ¶
SafeCuts bytes by sub bytes. like the bytes.Cut(), but always return before and after
func SafeString ¶
SafeString convert to string, return empty string on error.
func ShortMd5 ¶
ShortMd5 Generate a 16-bit md5 bytes. remove first 8 and last 8 bytes from 32-bit md5.
func ToBytesWithFunc ¶
func ToBytesWithFunc(v any, usrFn ToBytesFunc) ([]byte, error)
ToBytesWithFunc convert any value to []byte with custom fallback func.
refer the strutil.ToStringWithFunc
On not convert:
- If usrFn is nil, will return comdef.ErrConvType.
- If usrFn is not nil, will call it to convert.
Types ¶
type Buffer ¶
type Buffer struct { bytes.Buffer // custom error for testing CloseErr error FlushErr error SyncErr error }
Buffer wrap and extends the bytes.Buffer, add some useful methods and implements the io.Writer, io.Closer and stdio.Flusher interfaces
func (*Buffer) WriteAnyNl ¶
WriteAnyNl type value to buffer and end with newline
func (*Buffer) WriteStr1Nl ¶
WriteStr1Nl quiet write one string and end with newline
func (*Buffer) WriteStringNl ¶
WriteStringNl write message to buffer and end with newline
func (*Buffer) WriteStrings ¶
WriteStrings to buffer, ignore error.
type BytesEncoder ¶
BytesEncoder interface
type ChanPool ¶
type ChanPool struct {
// contains filtered or unexported fields
}
ChanPool 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 NewChanPool ¶
NewChanPool instance
func (*ChanPool) Get ¶
Get gets a []byte from the BytePool, or creates a new one if none are available in the pool.
type StdEncoder ¶
type StdEncoder struct {
// contains filtered or unexported fields
}
StdEncoder implement the BytesEncoder
func NewStdEncoder ¶
func NewStdEncoder(encFn BytesEncodeFunc, decFn BytesDecodeFunc) *StdEncoder
NewStdEncoder instance
type ToBytesFunc ¶
ToBytesFunc convert any value to []byte