buffer

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2024 License: MIT Imports: 5 Imported by: 0

README

Buffer

This is enhanced copy of Uber's buffer with several additions like:

  • Updated Append functions
  • Truncate and Trim function
  • Dumb content hashing function
  • io.Reader and io.Writer implementation
  • New buffer from bytes

Copyright (c) 2016 Uber Technologies, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package buffer provides a thin wrapper around a byte slice. Unlike the standard library's bytes.Buffer, it supports a portion of the strconv package's zero-allocation formatters.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

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

Buffer is a thin wrapper around a byte slice. It's intended to be pooled, so the only way to construct one is via a Pool

func (*Buffer) AppendBool

func (b *Buffer) AppendBool(v bool)

AppendBool appends a bool to the underlying buffer

func (*Buffer) AppendByte

func (b *Buffer) AppendByte(v byte)

AppendByte writes a single byte to the Buffer

func (*Buffer) AppendFloat

func (b *Buffer) AppendFloat(f float64, bitSize int)

AppendFloat appends a float to the underlying buffer. It doesn't quote NaN or +/- Inf

func (*Buffer) AppendInt

func (b *Buffer) AppendInt(i int64)

AppendInt appends an integer to the underlying buffer (assuming base 10)

func (*Buffer) AppendString

func (b *Buffer) AppendString(s string)

AppendString writes a string to the Buffer

func (*Buffer) AppendTime

func (b *Buffer) AppendTime(t time.Time, layout string)

AppendTime appends the time formatted using the specified layout

func (*Buffer) AppendUint

func (b *Buffer) AppendUint(i uint64)

AppendUint appends an unsigned integer to the underlying buffer (assuming base 10)

func (*Buffer) Bytes

func (b *Buffer) Bytes() []byte

Bytes returns a mutable reference to the underlying byte slice

func (*Buffer) Cap

func (b *Buffer) Cap() int

Cap returns the capacity of the underlying byte slice

func (*Buffer) Free

func (b *Buffer) Free()

Free returns the Buffer to its Pool

Callers must not retain references to the Buffer after calling Free

func (*Buffer) Hash

func (b *Buffer) Hash() uint64

Hash returns crc64 ECMA checksum

func (*Buffer) Len

func (b *Buffer) Len() int

Len returns the length of the underlying byte slice

func (*Buffer) Read

func (b *Buffer) Read(p []byte) (n int, err error)

Read implements io.Reader

func (*Buffer) ReadReset

func (b *Buffer) ReadReset()

ReadReset resets the underlying read pointers to reread the underlying buffer CAUTION!!! this is not the safest operation

func (*Buffer) Reset

func (b *Buffer) Reset()

Reset resets the underlying byte slice. Subsequent writes re-use the slice's backing array

func (*Buffer) String

func (b *Buffer) String() string

String returns a string copy of the underlying byte slice

func (*Buffer) TrimNewline

func (b *Buffer) TrimNewline()

TrimNewline trims any final "\n" byte from the end of the buffer

func (*Buffer) Truncate

func (b *Buffer) Truncate(n int)

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

func (*Buffer) Write

func (b *Buffer) Write(bs []byte) (int, error)

Write implements io.Writer

type Pool

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

A Pool is a type-safe wrapper around a sync.Pool

func NewPool

func NewPool() Pool

NewPool constructs a new Pool

func (Pool) Get

func (p Pool) Get() *Buffer

Get retrieves a Buffer from the pool, creating one if necessary

func (Pool) GetFromBytes

func (p Pool) GetFromBytes(bs []byte) *Buffer

GetFromBytes retrieves a Buffer from the pool, with allocated bytes slice

Jump to

Keyboard shortcuts

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