byteutil

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

README

Bytes Util

Provide some commonly bytes util functions.

Install

go get github.com/zhangyiming748/pretty/byteutil

Go docs

Functions API

var HexEncoder = NewStdEncoder(func(src []byte) []byte { ... }, func(src []byte) ([]byte, error) { ... }) ...
func Md5(src any) []byte
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 Provide some bytes utils functions or structs

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 FirstLine

func FirstLine(bs []byte) []byte

FirstLine from command output

func Md5

func Md5(src any) []byte

Md5 Generate a 32-bit md5 bytes

func SafeString

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

SafeString 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.

Types

type Buffer

type Buffer struct {
	bytes.Buffer
}

Buffer wrap and extends the bytes.Buffer

func NewBuffer

func NewBuffer() *Buffer

NewBuffer instance

func (*Buffer) MustWriteString

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

MustWriteString to buffer

func (*Buffer) QuietWriteByte

func (b *Buffer) QuietWriteByte(c byte)

QuietWriteByte to buffer

func (*Buffer) QuietWriteString

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

QuietWriteString to buffer

func (*Buffer) QuietWritef

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

QuietWritef write message to buffer

func (*Buffer) QuietWriteln

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

QuietWriteln write message to buffer with newline

func (*Buffer) ResetAndGet

func (b *Buffer) ResetAndGet() string

ResetAndGet buffer string.

func (*Buffer) WriteAny

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

WriteAny type value to buffer

func (*Buffer) Writeln

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

Writeln write message to buffer with newline

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(maxSize 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 func(src []byte) []byte, decFn func(src []byte) ([]byte, error)) *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

Jump to

Keyboard shortcuts

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