byteutil

package
v0.6.15 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 4, 2024 License: MIT Imports: 10 Imported by: 5

README

Bytes Util

Provide some commonly bytes util functions.

Install

go get github.com/gookit/goutil/byteutil

Go docs

Functions API

Note: doc by run go doc ./byteutil

func AppendAny(dst []byte, v any) []byte
func FirstLine(bs []byte) []byte
func IsNumChar(c byte) bool
func Md5(src any) []byte
func Random(length int) ([]byte, error)
func SafeString(bs []byte, err error) string
func StrOrErr(bs []byte, err error) (string, error)
func String(b []byte) string
func ToString(b []byte) string
type Buffer struct{ ... }
func NewBuffer() *Buffer
type BytesEncoder interface{ ... }
type ChanPool struct{ ... }
func NewChanPool(maxSize int, width int, capWidth int) *ChanPool
type StdEncoder struct{ ... }
func NewStdEncoder(encFn func(src []byte) []byte, decFn func(src []byte) ([]byte, error)) *StdEncoder

Code Check & Testing

gofmt -w -l ./
golint ./...

Testing:

go test -v ./byteutil/...

Test limit by regexp:

go test -v -run ^TestSetByKeys ./byteutil/...

Documentation

Overview

Package byteutil provides some useful functions for byte slice.

Index

Constants

This section is empty.

Variables

View Source
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 AppendAny added in v0.6.9

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

AppendAny append any value to byte slice

func Cut added in v0.6.9

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 added in v0.6.2

func FirstLine(bs []byte) []byte

FirstLine from command output

func IsNumChar added in v0.6.5

func IsNumChar(c byte) bool

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

func Md5

func Md5(src any) []byte

Md5 Generate a 32-bit md5 bytes

func Random added in v0.6.9

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

Random bytes generate

func SafeBytes added in v0.6.12

func SafeBytes(v any) []byte

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

func SafeCut added in v0.6.12

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

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

func SafeCuts added in v0.6.12

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 added in v0.6.2

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

SafeString convert to string, return empty string on error.

func ShortMd5 added in v0.6.13

func ShortMd5(src any) []byte

ShortMd5 Generate a 16-bit md5 bytes. remove first 8 and last 8 bytes from 32-bit md5.

func StrOrErr added in v0.6.2

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

StrOrErr convert to string, return empty string on error.

func String added in v0.6.8

func String(b []byte) string

String unsafe convert bytes to string

func ToBytes added in v0.6.12

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

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

func ToBytesWithFunc added in v0.6.12

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.

func ToString added in v0.6.8

func ToString(b []byte) string

ToString convert bytes to string

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 NewBuffer

func NewBuffer() *Buffer

NewBuffer instance

func (*Buffer) Close added in v0.6.10

func (b *Buffer) Close() error

Close buffer

func (*Buffer) Flush added in v0.6.10

func (b *Buffer) Flush() error

Flush buffer

func (*Buffer) PrintByte added in v0.6.10

func (b *Buffer) PrintByte(c byte)

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

func (*Buffer) Printf added in v0.6.10

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

Printf quiet write message to buffer, ignore error.

func (*Buffer) ResetAndGet

func (b *Buffer) ResetAndGet() string

ResetAndGet buffer string.

func (*Buffer) ResetGet added in v0.6.10

func (b *Buffer) ResetGet() string

ResetGet buffer string. alias of ResetAndGet()

func (*Buffer) Sync added in v0.6.11

func (b *Buffer) Sync() error

Sync anf flush buffer

func (*Buffer) WriteAny

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

WriteAny type value to buffer

func (*Buffer) WriteAnyNl added in v0.6.10

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

WriteAnyNl type value to buffer and end with newline

func (*Buffer) WriteStr added in v0.6.10

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

WriteStr quiet write strings to buffer

func (*Buffer) WriteStr1 added in v0.6.10

func (b *Buffer) WriteStr1(s string)

WriteStr1 quiet write one string to buffer

func (*Buffer) WriteStr1Nl added in v0.6.10

func (b *Buffer) WriteStr1Nl(s string)

WriteStr1Nl quiet write one string and end with newline

func (*Buffer) WriteStringNl added in v0.6.10

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

WriteStringNl write message to buffer and end with newline

func (*Buffer) WriteStrings added in v0.6.10

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 BytesDecodeFunc added in v0.6.12

type BytesDecodeFunc func(src []byte) ([]byte, error)

BytesDecodeFunc type

type BytesEncodeFunc added in v0.6.12

type BytesEncodeFunc func(src []byte) []byte

BytesEncodeFunc type

type BytesEncoder

type BytesEncoder interface {
	Encode(src []byte) []byte
	Decode(src []byte) ([]byte, error)
}

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

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

NewChanPool 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 StdEncoder

type StdEncoder struct {
	// contains filtered or unexported fields
}

StdEncoder implement the BytesEncoder

func NewStdEncoder

func NewStdEncoder(encFn BytesEncodeFunc, decFn BytesDecodeFunc) *StdEncoder

NewStdEncoder instance

func (*StdEncoder) Decode

func (e *StdEncoder) Decode(src []byte) ([]byte, error)

Decode input

func (*StdEncoder) Encode

func (e *StdEncoder) Encode(src []byte) []byte

Encode input

type ToBytesFunc added in v0.6.12

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

ToBytesFunc convert any value to []byte

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL