internal

package
v3.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package internal contains helper types and functions used by the evaluation logic, if they are self-contained enough to be tested independently.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseHexUint64

func ParseHexUint64(data []byte) (uint64, bool)

ParseHexUint64 is equivalent to using strconv.ParseUint with base 16, except that it operates directly on a byte slice without having to allocate a string. (A version of strconv.ParseUint that operates on a byte slice has been a frequently requested Go feature for years, but as of the time this comment was written, those requests have not been acted on.)

Types

type LocalBuffer

type LocalBuffer struct {
	// Data is the current content of the buffer.
	Data []byte
}

LocalBuffer is a simplified equivalent to bytes.Buffer that allows usage of a preallocated local byte slice.

The bytes.Buffer type is very efficient except in one regard: it must always allocate the byte slice on the heap, since its internal "buf" field starts out as nil and cannot be directly modified. LocalBuffer exports this field so it can be initialized to a slice which, if allowed by Go's escape analysis, can remain on the stack as long as its capacity is not exceeded. For example:

buf := LocalBuffer{Data: make([]byte, 0, 100)}
buf.AppendString("some data") // etc.
result := buf.Data

In the example, as long as no more than 100 bytes are written to the buffer, there are no heap allocations. As soon as the capacity is exceeded, the byte slice is moved to the heap and its capacity expands exponentially, similar to bytes.Buffer.

To simplify the API, the LocalBuffer methods return nothing; that's why they're named Append rather than Write, because Write has a connotation of imitating the io.Writer API.

func (*LocalBuffer) Append

func (b *LocalBuffer) Append(data []byte)

Append appends a byte slice to the buffer.

func (*LocalBuffer) AppendByte

func (b *LocalBuffer) AppendByte(ch byte)

AppendByte appends a single byte to the buffer.

func (*LocalBuffer) AppendInt

func (b *LocalBuffer) AppendInt(n int)

AppendInt appends the base-10 string representation of an integer to the buffer.

func (*LocalBuffer) AppendString

func (b *LocalBuffer) AppendString(s string)

AppendString appends the bytes of a string to the buffer.

Jump to

Keyboard shortcuts

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