package byteutil

Import Path
	github.com/gookit/goutil/byteutil (on go.dev)

Dependency Relation
	imports 10 packages, and imported by one package

Involved Source Files buffer.go Package byteutil provides some useful functions for byte slice. check.go conv.go encoder.go pool.go
Package-Level Type Names (total 7)
/* sort by: | */
Buffer wrap and extends the bytes.Buffer, add some useful methods and implements the io.Writer, io.Closer and stdio.Flusher interfaces Buffer bytes.Buffer custom error for testing FlushErr error SyncErr error Available returns how many bytes are unused in the buffer. AvailableBuffer returns an empty buffer with b.Available() capacity. This buffer is intended to be appended to and passed to an immediately succeeding [Buffer.Write] call. The buffer is only valid until the next write operation on b. Bytes returns a slice of length b.Len() holding the unread portion of the buffer. The slice is valid for use only until the next buffer modification (that is, only until the next call to a method like [Buffer.Read], [Buffer.Write], [Buffer.Reset], or [Buffer.Truncate]). The slice aliases the buffer content at least until the next buffer modification, so immediate changes to the slice will affect the result of future reads. Cap returns the capacity of the buffer's underlying byte slice, that is, the total space allocated for the buffer's data. Close buffer Flush buffer Grow grows the buffer's capacity, if necessary, to guarantee space for another n bytes. After Grow(n), at least n bytes can be written to the buffer without another allocation. If n is negative, Grow will panic. If the buffer can't grow it will panic with [ErrTooLarge]. Len returns the number of bytes of the unread portion of the buffer; b.Len() == len(b.Bytes()). Next returns a slice containing the next n bytes from the buffer, advancing the buffer as if the bytes had been returned by [Buffer.Read]. If there are fewer than n bytes in the buffer, Next returns the entire buffer. The slice is only valid until the next call to a read or write method. Peek returns the next n bytes without advancing the buffer. If Peek returns fewer than n bytes, it also returns [io.EOF]. The slice is only valid until the next call to a read or write method. The slice aliases the buffer content at least until the next buffer modification, so immediate changes to the slice will affect the result of future reads. PrintByte to buffer, ignore error. alias of WriteByte() Printf quick write message to buffer, ignore error. Println quick write message with newline to buffer, will ignore error. Read reads the next len(p) bytes from the buffer or until the buffer is drained. The return value n is the number of bytes read. If the buffer has no data to return, err is [io.EOF] (unless len(p) is zero); otherwise it is nil. ReadByte reads and returns the next byte from the buffer. If no byte is available, it returns error [io.EOF]. ReadBytes reads until the first occurrence of delim in the input, returning a slice containing the data up to and including the delimiter. If ReadBytes encounters an error before finding a delimiter, it returns the data read before the error and the error itself (often [io.EOF]). ReadBytes returns err != nil if and only if the returned data does not end in delim. ReadFrom reads data from r until EOF and appends it to the buffer, growing the buffer as needed. The return value n is the number of bytes read. Any error except io.EOF encountered during the read is also returned. If the buffer becomes too large, ReadFrom will panic with [ErrTooLarge]. ReadRune reads and returns the next UTF-8-encoded Unicode code point from the buffer. If no bytes are available, the error returned is io.EOF. If the bytes are an erroneous UTF-8 encoding, it consumes one byte and returns U+FFFD, 1. ReadString reads until the first occurrence of delim in the input, returning a string containing the data up to and including the delimiter. If ReadString encounters an error before finding a delimiter, it returns the data read before the error and the error itself (often [io.EOF]). ReadString returns err != nil if and only if the returned data does not end in delim. Reset resets the buffer to be empty, but it retains the underlying storage for use by future writes. Reset is the same as [Buffer.Truncate](0). ResetAndGet buffer string. ResetGet buffer string. alias of ResetAndGet() String returns the contents of the unread portion of the buffer as a string. If the [Buffer] is a nil pointer, it returns "<nil>". To build strings more efficiently, see the [strings.Builder] type. Sync anf flush buffer Truncate discards all but the first n unread bytes from the buffer but continues to use the same allocated storage. It panics if n is negative or greater than the length of the buffer. UnreadByte unreads the last byte returned by the most recent successful read operation that read at least one byte. If a write has happened since the last read, if the last read returned an error, or if the read read zero bytes, UnreadByte returns an error. UnreadRune unreads the last rune returned by [Buffer.ReadRune]. If the most recent read or write operation on the buffer was not a successful [Buffer.ReadRune], UnreadRune returns an error. (In this regard it is stricter than [Buffer.UnreadByte], which will unread the last byte from any read operation.) Write appends the contents of p to the buffer, growing the buffer as needed. The return value n is the length of p; err is always nil. If the buffer becomes too large, Write will panic with [ErrTooLarge]. WriteAny type value to buffer WriteAnyNl type value to buffer and end with newline WriteByte appends the byte c to the buffer, growing the buffer as needed. The returned error is always nil, but is included to match [bufio.Writer]'s WriteByte. If the buffer becomes too large, WriteByte will panic with [ErrTooLarge]. WriteRune appends the UTF-8 encoding of Unicode code point r to the buffer, returning its length and an error, which is always nil but is included to match [bufio.Writer]'s WriteRune. The buffer is grown as needed; if it becomes too large, WriteRune will panic with [ErrTooLarge]. WriteStr quiet write strings to buffer WriteStr1 quiet write one string to buffer WriteStr1Nl quiet write one string and end with newline WriteString appends the contents of s to the buffer, growing the buffer as needed. The return value n is the length of s; err is always nil. If the buffer becomes too large, WriteString will panic with [ErrTooLarge]. WriteStringNl write message to buffer and end with newline WriteStrings to buffer, ignore error. WriteTo writes data to w until the buffer is drained or an error occurs. The return value n is the number of bytes written; it always fits into an int, but it is int64 to match the [io.WriterTo] interface. Any error encountered during the write is also returned. Writef write message to buffer, ignore error. alias of Printf() Writeln write values to buffer and end with newline *Buffer : github.com/gookit/goutil/comdef.ByteStringWriter *Buffer : github.com/gookit/goutil/comdef.StringWriteStringer *Buffer : github.com/gookit/goutil/x/stdio.FlushCloseWriter *Buffer : github.com/gookit/goutil/x/stdio.Flusher *Buffer : github.com/gookit/goutil/x/stdio.FlushWriter *Buffer : github.com/gookit/goutil/x/stdio.SyncCloseWriter *Buffer : github.com/gookit/goutil/x/stdio.Syncer *Buffer : github.com/apache/arrow-go/v18/internal/hashing.ByteSlice *Buffer : github.com/apache/thrift/lib/go/thrift.Flusher *Buffer : github.com/klauspost/compress/flate.Reader *Buffer : github.com/miekg/dns.Writer *Buffer : github.com/multiformats/go-multistream.LazyConn *Buffer : github.com/pion/stun.Connection *Buffer : github.com/pion/stun/v3.Connection *Buffer : github.com/polarsignals/frostdb.Sync *Buffer : github.com/prometheus/common/expfmt.Closer *Buffer : github.com/quic-go/quic-go/quicvarint.Reader *Buffer : github.com/quic-go/quic-go/quicvarint.Writer *Buffer : compress/flate.Reader *Buffer : expvar.Var *Buffer : fmt.Stringer *Buffer : go.uber.org/fx.Printer *Buffer : go.uber.org/zap.Sink *Buffer : go.uber.org/zap/zapcore.WriteSyncer *Buffer : google.golang.org/protobuf/encoding/protodelim.Reader *Buffer : gorm.io/gorm/clause.Writer *Buffer : gorm.io/gorm/logger.Writer *Buffer : image/jpeg.Reader *Buffer : internal/bisect.Writer *Buffer : io.ByteReader *Buffer : io.ByteScanner *Buffer : io.ByteWriter *Buffer : io.Closer *Buffer : io.ReadCloser *Buffer : io.Reader *Buffer : io.ReaderFrom *Buffer : io.ReadWriteCloser *Buffer : io.ReadWriter *Buffer : io.RuneReader *Buffer : io.RuneScanner *Buffer : io.StringWriter *Buffer : io.WriteCloser *Buffer : io.Writer *Buffer : io.WriterTo func NewBuffer() *Buffer
BytesDecodeFunc type func NewStdEncoder(encFn BytesEncodeFunc, decFn BytesDecodeFunc) *StdEncoder
BytesEncodeFunc type func NewStdEncoder(encFn BytesEncodeFunc, decFn BytesDecodeFunc) *StdEncoder
BytesEncoder interface ( BytesEncoder) Decode(src []byte) ([]byte, error) ( BytesEncoder) Encode(src []byte) []byte *StdEncoder github.com/hamba/avro/v2/ocf.Codec (interface) *github.com/hamba/avro/v2/ocf.DeflateCodec *github.com/hamba/avro/v2/ocf.NullCodec *github.com/hamba/avro/v2/ocf.SnappyCodec *github.com/hamba/avro/v2/ocf.ZStandardCodec BytesEncoder : github.com/hamba/avro/v2/ocf.Codec
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 Get gets a []byte from the BytePool, or creates a new one if none are available in the pool. Put returns the given Buffer to the BytePool. Width returns the width of the byte arrays in this pool. WidthCap returns the cap width of the byte arrays in this pool. *ChanPool : net/http/httputil.BufferPool func NewChanPool(chSize int, width int, capWidth int) *ChanPool
StdEncoder implement the BytesEncoder Decode input Encode input *StdEncoder : BytesEncoder *StdEncoder : github.com/hamba/avro/v2/ocf.Codec func NewStdEncoder(encFn BytesEncodeFunc, decFn BytesDecodeFunc) *StdEncoder var B64Encoder *StdEncoder var HexEncoder *StdEncoder
ToBytesFunc convert any value to []byte
Package-Level Functions (total 22)
AppendAny append any value to byte slice
Cut bytes by one byte char. like bytes.Cut(), but sep is byte.
FirstLine from command output
IsAlphaChar returns true if the given character is a alphabet, otherwise false.
IsNumChar returns true if the given character is a numeric, otherwise false.
Md5 Generate a 32-bit md5 bytes
Md5Sum Generate a md5 bytes
NewBuffer instance
NewChanPool instance
NewStdEncoder instance
Random bytes generate
Reverse 反转字节数组 eg: ABCD -> DCBA
SafeBytes convert any value to []byte. use fmt.Sprint() on convert failed.
SafeCut bytes by one byte char. always return before and after
SafeCuts bytes by sub bytes. like the bytes.Cut(), but always return before and after
SafeString convert to string, return empty string on error.
ShortMd5 Generate a 16-bit md5 bytes. remove the first 8 and last 8 bytes from 32-bit md5.
String unsafe convert bytes to string
StrOrErr convert to string, return empty string on error.
ToBytes convert any value to []byte. return error on convert failed.
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.
ToString convert bytes to string
Package-Level Variables (total 2)
B64Encoder instance
HexEncoder instance