mem

package module
v0.0.0-...-4f98626 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2022 License: Apache-2.0 Imports: 7 Imported by: 158

README

Documentation

Overview

Package mem provides the mem.RO type that allows you to cheaply pass & access either a read-only []byte or a string.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Append

func Append(dest []byte, m RO) []byte

Append appends m to dest, and returns the possibly-reallocated dest.

func Contains

func Contains(m, substr RO) bool

Contains reports whether substr is within m.

func ContainsFold

func ContainsFold(s, substr RO) bool

ContainsFold is like Contains but uses Unicode case-folding for a case insensitive substring search.

func DecodeLastRune

func DecodeLastRune(m RO) (r rune, size int)

DecodeLastRune unpacks the last UTF-8 encoding in m and returns the rune and its width in bytes. If m is empty it returns (utf8.RuneError, 0). Otherwise, if the encoding is invalid, it returns (utf8.RuneError, 1). Both are impossible results for correct, non-empty UTF-8.

An encoding is invalid if it is incorrect UTF-8, encodes a rune that is out of range, or is not the shortest possible UTF-8 encoding for the value. No other validation is performed.

func DecodeRune

func DecodeRune(m RO) (r rune, size int)

DecodeRune unpacks the first UTF-8 encoding in m and returns the rune and its width in bytes. If m is empty it returns (utf8.RuneError, 0). Otherwise, if the encoding is invalid, it returns (utf8.RuneError, 1). Both are impossible results for correct, non-empty UTF-8.

An encoding is invalid if it is incorrect UTF-8, encodes a rune that is out of range, or is not the shortest possible UTF-8 encoding for the value. No other validation is performed.

func EqualFold

func EqualFold(m, m2 RO) bool

EqualFold reports whether s and t, interpreted as UTF-8 strings, are equal under Unicode case-folding, which is a more general form of case-insensitivity.

func FullRune

func FullRune(m RO) bool

FullRune reports whether the bytes in m begin with a full UTF-8 encoding of a rune. An invalid encoding is considered a full Rune since it will convert as a width-1 error rune.

func HasPrefix

func HasPrefix(m, prefix RO) bool

HasPrefix reports whether m starts with prefix.

func HasPrefixFold

func HasPrefixFold(s, prefix RO) bool

HasPrefixFold is like HasPrefix but uses Unicode case-folding, matching case insensitively.

func HasSuffix

func HasSuffix(m, suffix RO) bool

HasSuffix reports whether m ends with suffix.

func HasSuffixFold

func HasSuffixFold(s, suffix RO) bool

HasSuffixFold is like HasSuffix but uses Unicode case-folding, matching case insensitively.

func Index

func Index(m, substr RO) int

Index returns the index of the first instance of substr in m, or -1 if substr is not present in m.

func IndexByte

func IndexByte(m RO, c byte) int

IndexByte returns the index of the first instance of c in m, or -1 if c is not present in m.

func LastIndex

func LastIndex(m, substr RO) int

LastIndex returns the index of the last instance of substr in m, or -1 if substr is not present in m.

func LastIndexByte

func LastIndexByte(m RO, c byte) int

LastIndexByte returns the index into m of the last Unicode code point satisfying f(c), or -1 if none do.

func ParseFloat

func ParseFloat(m RO, bitSize int) (float64, error)

ParseFloat returns a float from, using strconv.ParseFloat.

func ParseInt

func ParseInt(m RO, base, bitSize int) (int64, error)

ParseInt returns a signed integer from m, using strconv.ParseInt.

func ParseUint

func ParseUint(m RO, base, bitSize int) (uint64, error)

ParseUint returns a unsigned integer from m, using strconv.ParseUint.

func RuneCount

func RuneCount(m RO) int

RuneCount returns the number of UTF-8 encoded runes in m. Erroneous and short encodings are treated as single runes of width 1 byte.

func ValidUTF8

func ValidUTF8(m RO) bool

ValidUTF8 reports whether m consists entirely of valid UTF-8 encoded runes.

Types

type RO

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

RO is a read-only view of some bytes of memory. It may be be backed by a string or []byte. Notably, unlike a string, the memory is not guaranteed to be immutable. While the length is fixed, the underlying bytes might change if interleaved with code that's modifying the underlying memory.

RO is a value type that's the same size of a Go string. Its various methods should inline & compile to the equivalent operations working on a string or []byte directly.

Unlike a Go string, RO is not 'comparable' (it can't be a map key or support ==). Use its Equal method to compare. This is done so an RO backed by a later-mutating []byte doesn't break invariants in Go's map implementation.

func AppendFields

func AppendFields(dst []RO, m RO) []RO

AppendFields is like strings.Fields, but is append-like and uses a mem.RO instead of a string.

func AppendFieldsFunc

func AppendFieldsFunc(dst []RO, m RO, f func(rune) bool) []RO

AppendFieldsFunc is like strings.FieldsFunc, but is append-like and uses a mem.RO instead of a string.

func B

func B(b []byte) RO

B returns a read-only view of the byte slice b.

The compiler should compile this call to nothing. Think of it as a free type conversion. The returned value is actually smaller than a []byte though (16 bytes instead of 24 bytes on 64-bit architectures).

func S

func S(s string) RO

S returns a read-only view of the string s.

The compiler should compile this call to nothing. Think of it as a free type conversion. The returned RO view is the same size as a string.

func TrimCutset

func TrimCutset(m, cutset RO) RO

TrimCutset returns a slice of the string s with all leading and trailing Unicode code points contained in cutset removed.

func TrimFunc

func TrimFunc(m RO, f func(rune) bool) RO

TrimFunc returns a slice of m with all leading and trailing Unicode code points c satisfying f(c) removed.

func TrimLeftCutset

func TrimLeftCutset(m, cutset RO) RO

TrimLeftCutset returns a slice of m with all leading Unicode code points contained in cutset removed.

To remove a prefix, use TrimPrefix instead.

func TrimLeftFunc

func TrimLeftFunc(m RO, f func(rune) bool) RO

TrimLeftFunc returns a slice of m with all leading Unicode code points c satisfying f(c) removed.

func TrimPrefix

func TrimPrefix(m, prefix RO) RO

TrimPrefix returns m without the provided leading prefix. If m doesn't start with prefix, m is returned unchanged.

func TrimRightCutset

func TrimRightCutset(m, cutset RO) RO

TrimRightCutset returns a slice of m with all trailing Unicode code points contained in cutset removed.

To remove a suffix, use TrimSuffix instead.

func TrimRightFunc

func TrimRightFunc(m RO, f func(rune) bool) RO

TrimRightFunc returns a slice of m with all trailing Unicode code points c satisfying f(c) removed.

func TrimSpace

func TrimSpace(m RO) RO

TrimSpace returns a slice of the string s, with all leading and trailing white space removed, as defined by Unicode.

func TrimSuffix

func TrimSuffix(m, suffix RO) RO

TrimSuffix returns m without the provided trailing suffix. If m doesn't end with suffix, m is returned unchanged.

func (RO) At

func (r RO) At(i int) byte

At returns r[i].

func (RO) Copy

func (r RO) Copy(dest []byte) int

Copy copies up to len(dest) bytes into dest from r and returns the number of bytes copied, the min(r.Len(), len(dest)).

func (RO) Equal

func (r RO) Equal(r2 RO) bool

Equal reports whether r and r2 are the same length and contain the same bytes.

func (RO) EqualBytes

func (r RO) EqualBytes(b []byte) bool

EqualBytes reports whether r and b are the same length and contain the same bytes.

func (RO) EqualString

func (r RO) EqualString(s string) bool

EqualString reports whether r and s are the same length and contain the same bytes.

func (RO) Len

func (r RO) Len() int

Len returns len(r).

func (RO) Less

func (r RO) Less(r2 RO) bool

Less reports whether r < r2.

func (RO) MapHash

func (r RO) MapHash() uint64

MapHash returns a hash of r's contents using runtime/maphash. The hash is stable for the lifetime of a process.

func (RO) Slice

func (r RO) Slice(from, to int) RO

Slice returns r[from:to].

func (RO) SliceFrom

func (r RO) SliceFrom(from int) RO

SliceFrom returns r[from:].

func (RO) SliceTo

func (r RO) SliceTo(to int) RO

SliceTo returns r[:to].

func (RO) StringCopy

func (r RO) StringCopy() string

StringCopy returns m's contents in a newly allocated string.

type Reader

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

Reader is like a bytes.Reader or strings.Reader.

func NewReader

func NewReader(m RO) *Reader

NewReader returns a new Reader that reads from m.

func (*Reader) Len

func (r *Reader) Len() int

func (*Reader) Read

func (r *Reader) Read(b []byte) (int, error)

func (*Reader) ReadAt

func (r *Reader) ReadAt(b []byte, off int64) (int, error)

func (*Reader) ReadByte

func (r *Reader) ReadByte() (byte, error)

func (*Reader) ReadRune

func (r *Reader) ReadRune() (ch rune, size int, err error)

func (*Reader) Seek

func (r *Reader) Seek(offset int64, whence int) (int64, error)

func (*Reader) Size

func (r *Reader) Size() int64

Jump to

Keyboard shortcuts

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