Import path:
gitlab.soludian.com/soludian/fountain/libs/base/byte_util
byte_util
import "gitlab.soludian.com/soludian/fountain/libs/base/byte_util"Index
- func AppendAny(dst []byte, v any) []byte
- func Cut(bs []byte, sep byte) (before, after []byte, found bool)
- func FirstLine(bs []byte) []byte
- func FromString(s string) []byte
- func IsNumChar(c byte) bool
- 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 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 NewBuffer() *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) 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 BufferInput
- func NewBufferBinInput(buf []byte) *BufferInput
- func (m *BufferInput) Buf() ([]byte, error)
- func (m *BufferInput) Byte() byte
- func (m *BufferInput) Bytes(size int) []byte
- func (m *BufferInput) Double() float64
- func (m *BufferInput) Dump() string
- func (m *BufferInput) DumpSize(size int) string
- func (m *BufferInput) Error() error
- func (m *BufferInput) Int16() int16
- func (m *BufferInput) Int32() int32
- func (m *BufferInput) Int64() int64
- func (m *BufferInput) UInt16() uint16
- func (m *BufferInput) UInt32() uint32
- func (m *BufferInput) UInt64() uint64
- type BufferOutput
- func NewBufferBinOutput(cap int) *BufferOutput
- func (e *BufferOutput) Buf() []byte
- func (e *BufferOutput) Byte(s byte)
- func (e *BufferOutput) ByteSize() int
- func (e *BufferOutput) Bytes(s []byte)
- func (e *BufferOutput) Double(s float64)
- func (e *BufferOutput) Int16(s int16)
- func (e *BufferOutput) Int32(s int32)
- func (e *BufferOutput) Int64(s int64)
- func (e *BufferOutput) Len() int
- func (e *BufferOutput) UInt16(s uint16)
- func (e *BufferOutput) UInt32(s uint32)
- func (e *BufferOutput) UInt64(s uint64)
- type ChanPool
- type ToBytesFunc
func AppendAny
func AppendAny(dst []byte, v any) []byteAppendAny append any value to byte slice
func Cut
func Cut(bs []byte, sep byte) (before, after []byte, found bool)Cut bytes by one byte char. like bytes.Cut(), but sep is byte.
func FirstLine
func FirstLine(bs []byte) []byteFirstLine from command output
func FromString
func FromString(s string) []byteFromString converts a string to a byte slice without copy.
func IsNumChar
func IsNumChar(c byte) boolIsNumChar returns true if the given character is a numeric, otherwise false.
func Random
func Random(length int) ([]byte, error)Random bytes generate
func SafeBytes
func SafeBytes(v any) []byteSafeBytes convert any value to []byte. use fmt.Sprint() on convert failed.
func SafeCut
func SafeCut(bs []byte, sep byte) (before, after []byte)SafeCut bytes by one byte char. always return before and after
func SafeCuts
func SafeCuts(bs []byte, sep []byte) (before, after []byte)SafeCuts bytes by sub bytes. like the bytes.Cut(), but always return before and after
func SafeString
func SafeString(bs []byte, err error) stringSafeString convert to string, return empty string on error.
func StrOrErr
func StrOrErr(bs []byte, err error) (string, error)StrOrErr convert to string, return empty string on error.
func String
func String(b []byte) stringString converts a byte slice to a string without copy.
func ToBytes
func ToBytes(v any) ([]byte, error)ToBytes convert any value to []byte. return error on convert failed.
func ToBytesWithFunc
func ToBytesWithFunc(v any, usrFn ToBytesFunc) ([]byte, error)ToBytesWithFunc convert any value to []byte with custom fallback func.
refer the string_util.ToStringWithFunc
On not convert:
- If usrFn is nil, will return com_def.ErrConvType.
- If usrFn is not nil, will call it to convert.
func ToString
func ToString(b []byte) stringBytesToString converts a byte slice to a string without copy.
type Buffer
Buffer wrap and extends the bytes.Buffer, add some useful methods and implements the io.Writer, io.Closer and stdio.Flusher interfaces
type Buffer struct {
bytes.Buffer
// custom error for testing
CloseErr error
FlushErr error
SyncErr error
}func NewBuffer
func NewBuffer() *BufferNewBuffer instance
func (*Buffer) Close
func (b *Buffer) Close() errorClose buffer
func (*Buffer) Flush
func (b *Buffer) Flush() errorFlush buffer
func (*Buffer) PrintByte
func (b *Buffer) PrintByte(c byte)PrintByte to buffer, ignore error. alias of WriteByte()
func (*Buffer) Printf
func (b *Buffer) Printf(tpl string, vs ...any)Printf quiet write message to buffer, ignore error.
func (*Buffer) ResetAndGet
func (b *Buffer) ResetAndGet() stringResetAndGet buffer string.
func (*Buffer) ResetGet
func (b *Buffer) ResetGet() stringResetGet buffer string. alias of ResetAndGet()
func (*Buffer) Sync
func (b *Buffer) Sync() errorSync anf flush buffer
func (*Buffer) WriteAny
func (b *Buffer) WriteAny(vs ...any)WriteAny type value to buffer
func (*Buffer) WriteAnyNl
func (b *Buffer) WriteAnyNl(vs ...any)WriteAnyNl type value to buffer and end with newline
func (*Buffer) WriteStr
func (b *Buffer) WriteStr(ss ...string)WriteStr quiet write strings to buffer
func (*Buffer) WriteStr1
func (b *Buffer) WriteStr1(s string)WriteStr1 quiet write one string to buffer
func (*Buffer) WriteStr1Nl
func (b *Buffer) WriteStr1Nl(s string)WriteStr1Nl quiet write one string and end with newline
func (*Buffer) WriteStringNl
func (b *Buffer) WriteStringNl(ss ...string)WriteStringNl write message to buffer and end with newline
func (*Buffer) WriteStrings
func (b *Buffer) WriteStrings(ss []string)WriteStrings to buffer, ignore error.
func (*Buffer) Writeln
func (b *Buffer) Writeln(vs ...any)Writeln write values to buffer and end with newline
type BufferInput
BufferInput type;
type BufferInput struct {
// contains filtered or unexported fields
}func NewBufferBinInput
func NewBufferBinInput(buf []byte) *BufferInputNewBufferInput type;
func (*BufferInput) Buf
func (m *BufferInput) Buf() ([]byte, error)Buf type;
func (*BufferInput) Byte
func (m *BufferInput) Byte() byteByte type;
func (*BufferInput) Bytes
func (m *BufferInput) Bytes(size int) []byteBytes type;
func (*BufferInput) Double
func (m *BufferInput) Double() float64Double type;
func (*BufferInput) Dump
func (m *BufferInput) Dump() stringDump type;
func (*BufferInput) DumpSize
func (m *BufferInput) DumpSize(size int) stringDumpSize type;
func (*BufferInput) Error
func (m *BufferInput) Error() errorError type;
func (*BufferInput) Int16
func (m *BufferInput) Int16() int16Int16 type;
func (*BufferInput) Int32
func (m *BufferInput) Int32() int32Int32 type;
func (*BufferInput) Int64
func (m *BufferInput) Int64() int64Int64 type;
func (*BufferInput) UInt16
func (m *BufferInput) UInt16() uint16UInt16 type;
func (*BufferInput) UInt32
func (m *BufferInput) UInt32() uint32UInt32 type;
func (*BufferInput) UInt64
func (m *BufferInput) UInt64() uint64UInt64 type;
type BufferOutput
type BufferOutput struct {
// contains filtered or unexported fields
}func NewBufferBinOutput
func NewBufferBinOutput(cap int) *BufferOutputfunc (*BufferOutput) Buf
func (e *BufferOutput) Buf() []bytefunc (*BufferOutput) Byte
func (e *BufferOutput) Byte(s byte)func (*BufferOutput) ByteSize
func (e *BufferOutput) ByteSize() intfunc (*BufferOutput) Bytes
func (e *BufferOutput) Bytes(s []byte)func (*BufferOutput) Double
func (e *BufferOutput) Double(s float64)func (*BufferOutput) Int16
func (e *BufferOutput) Int16(s int16)func (*BufferOutput) Int32
func (e *BufferOutput) Int32(s int32)func (*BufferOutput) Int64
func (e *BufferOutput) Int64(s int64)func (*BufferOutput) Len
func (e *BufferOutput) Len() intfunc (*BufferOutput) UInt16
func (e *BufferOutput) UInt16(s uint16)func (*BufferOutput) UInt32
func (e *BufferOutput) UInt32(s uint32)func (*BufferOutput) UInt64
func (e *BufferOutput) UInt64(s uint64)type ChanPool
ChanPool struct
Usage:
bp := string_util.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
type ChanPool struct {
// contains filtered or unexported fields
}func NewChanPool
func NewChanPool(chSize int, width int, capWidth int) *ChanPoolNewChanPool instance
func (*ChanPool) Get
func (bp *ChanPool) Get() (b []byte)Get gets a []byte from the BytePool, or creates a new one if none are available in the pool.
func (*ChanPool) Put
func (bp *ChanPool) Put(b []byte)Put returns the given Buffer to the BytePool.
func (*ChanPool) Width
func (bp *ChanPool) Width() (n int)Width returns the width of the byte arrays in this pool.
func (*ChanPool) WidthCap
func (bp *ChanPool) WidthCap() (n int)WidthCap returns the cap width of the byte arrays in this pool.
type ToBytesFunc
ToBytesFunc convert any value to []byte
type ToBytesFunc = func(v any) ([]byte, error)Generated by gomarkdoc