Skip to content

Import path: gitlab.soludian.com/soludian/fountain/libs/base/byte_util

byte_util

go
import "gitlab.soludian.com/soludian/fountain/libs/base/byte_util"

Index

func AppendAny

go
func AppendAny(dst []byte, v any) []byte

AppendAny append any value to byte slice

func Cut

go
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

go
func FirstLine(bs []byte) []byte

FirstLine from command output

func FromString

go
func FromString(s string) []byte

FromString converts a string to a byte slice without copy.

func IsNumChar

go
func IsNumChar(c byte) bool

IsNumChar returns true if the given character is a numeric, otherwise false.

func Random

go
func Random(length int) ([]byte, error)

Random bytes generate

func SafeBytes

go
func SafeBytes(v any) []byte

SafeBytes convert any value to []byte. use fmt.Sprint() on convert failed.

func SafeCut

go
func SafeCut(bs []byte, sep byte) (before, after []byte)

SafeCut bytes by one byte char. always return before and after

func SafeCuts

go
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

go
func SafeString(bs []byte, err error) string

SafeString convert to string, return empty string on error.

func StrOrErr

go
func StrOrErr(bs []byte, err error) (string, error)

StrOrErr convert to string, return empty string on error.

func String

go
func String(b []byte) string

String converts a byte slice to a string without copy.

func ToBytes

go
func ToBytes(v any) ([]byte, error)

ToBytes convert any value to []byte. return error on convert failed.

func ToBytesWithFunc

go
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

go
func ToString(b []byte) string

BytesToString 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

go
type Buffer struct {
    bytes.Buffer
    // custom error for testing
    CloseErr error
    FlushErr error
    SyncErr  error
}

func NewBuffer

go
func NewBuffer() *Buffer

NewBuffer instance

func (*Buffer) Close

go
func (b *Buffer) Close() error

Close buffer

func (*Buffer) Flush

go
func (b *Buffer) Flush() error

Flush buffer

func (*Buffer) PrintByte

go
func (b *Buffer) PrintByte(c byte)

PrintByte to buffer, ignore error. alias of WriteByte()

func (*Buffer) Printf

go
func (b *Buffer) Printf(tpl string, vs ...any)

Printf quiet write message to buffer, ignore error.

func (*Buffer) ResetAndGet

go
func (b *Buffer) ResetAndGet() string

ResetAndGet buffer string.

func (*Buffer) ResetGet

go
func (b *Buffer) ResetGet() string

ResetGet buffer string. alias of ResetAndGet()

func (*Buffer) Sync

go
func (b *Buffer) Sync() error

Sync anf flush buffer

func (*Buffer) WriteAny

go
func (b *Buffer) WriteAny(vs ...any)

WriteAny type value to buffer

func (*Buffer) WriteAnyNl

go
func (b *Buffer) WriteAnyNl(vs ...any)

WriteAnyNl type value to buffer and end with newline

func (*Buffer) WriteStr

go
func (b *Buffer) WriteStr(ss ...string)

WriteStr quiet write strings to buffer

func (*Buffer) WriteStr1

go
func (b *Buffer) WriteStr1(s string)

WriteStr1 quiet write one string to buffer

func (*Buffer) WriteStr1Nl

go
func (b *Buffer) WriteStr1Nl(s string)

WriteStr1Nl quiet write one string and end with newline

func (*Buffer) WriteStringNl

go
func (b *Buffer) WriteStringNl(ss ...string)

WriteStringNl write message to buffer and end with newline

func (*Buffer) WriteStrings

go
func (b *Buffer) WriteStrings(ss []string)

WriteStrings to buffer, ignore error.

func (*Buffer) Writeln

go
func (b *Buffer) Writeln(vs ...any)

Writeln write values to buffer and end with newline

type BufferInput

BufferInput type;

go
type BufferInput struct {
    // contains filtered or unexported fields
}

func NewBufferBinInput

go
func NewBufferBinInput(buf []byte) *BufferInput

NewBufferInput type;

func (*BufferInput) Buf

go
func (m *BufferInput) Buf() ([]byte, error)

Buf type;

func (*BufferInput) Byte

go
func (m *BufferInput) Byte() byte

Byte type;

func (*BufferInput) Bytes

go
func (m *BufferInput) Bytes(size int) []byte

Bytes type;

func (*BufferInput) Double

go
func (m *BufferInput) Double() float64

Double type;

func (*BufferInput) Dump

go
func (m *BufferInput) Dump() string

Dump type;

func (*BufferInput) DumpSize

go
func (m *BufferInput) DumpSize(size int) string

DumpSize type;

func (*BufferInput) Error

go
func (m *BufferInput) Error() error

Error type;

func (*BufferInput) Int16

go
func (m *BufferInput) Int16() int16

Int16 type;

func (*BufferInput) Int32

go
func (m *BufferInput) Int32() int32

Int32 type;

func (*BufferInput) Int64

go
func (m *BufferInput) Int64() int64

Int64 type;

func (*BufferInput) UInt16

go
func (m *BufferInput) UInt16() uint16

UInt16 type;

func (*BufferInput) UInt32

go
func (m *BufferInput) UInt32() uint32

UInt32 type;

func (*BufferInput) UInt64

go
func (m *BufferInput) UInt64() uint64

UInt64 type;

type BufferOutput

go
type BufferOutput struct {
    // contains filtered or unexported fields
}

func NewBufferBinOutput

go
func NewBufferBinOutput(cap int) *BufferOutput

func (*BufferOutput) Buf

go
func (e *BufferOutput) Buf() []byte

func (*BufferOutput) Byte

go
func (e *BufferOutput) Byte(s byte)

func (*BufferOutput) ByteSize

go
func (e *BufferOutput) ByteSize() int

func (*BufferOutput) Bytes

go
func (e *BufferOutput) Bytes(s []byte)

func (*BufferOutput) Double

go
func (e *BufferOutput) Double(s float64)

func (*BufferOutput) Int16

go
func (e *BufferOutput) Int16(s int16)

func (*BufferOutput) Int32

go
func (e *BufferOutput) Int32(s int32)

func (*BufferOutput) Int64

go
func (e *BufferOutput) Int64(s int64)

func (*BufferOutput) Len

go
func (e *BufferOutput) Len() int

func (*BufferOutput) UInt16

go
func (e *BufferOutput) UInt16(s uint16)

func (*BufferOutput) UInt32

go
func (e *BufferOutput) UInt32(s uint32)

func (*BufferOutput) UInt64

go
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

go
type ChanPool struct {
    // contains filtered or unexported fields
}

func NewChanPool

go
func NewChanPool(chSize int, width int, capWidth int) *ChanPool

NewChanPool instance

func (*ChanPool) Get

go
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

go
func (bp *ChanPool) Put(b []byte)

Put returns the given Buffer to the BytePool.

func (*ChanPool) Width

go
func (bp *ChanPool) Width() (n int)

Width returns the width of the byte arrays in this pool.

func (*ChanPool) WidthCap

go
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

go
type ToBytesFunc = func(v any) ([]byte, error)

Generated by gomarkdoc