cursor

package
v0.17.1 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cursor reads CBOR from a byte slice in a single pass. A Cursor knows CBOR but no Go types, so a decoder built on top decides what to read and the Cursor reads it.

A general-purpose CBOR decoder checks that the whole input is well-formed and then decodes it, accepting every form the standard allows. A Cursor checks as it reads, and reads only the forms Juno stores. These are integers, strings, felts as four limbs and struct fields by number, with Skip for what is not wanted. Anything else, and anything malformed, is an error.

Its primitives are pinned to the examples of RFC 8949 Appendix A, and its felts to literal bytes. The examples are at https://www.rfc-editor.org/rfc/rfc8949.html#appendix-A

Index

Constants

View Source
const (
	MajorUint = 0 << 5

	MajorTag = 6 << 5
)

Major types (RFC 8949, section 3.1); only two are exported.

View Source
const (
	Done    = -1
	Unknown = -2
)

Done is returned by NextField and NextWanted when the struct's map has no pairs left, and is closed. Unknown is returned by NextField when the key names no field, and its value is next, to skip.

View Source
const (
	Limbs = 4
)

Limbs is the number of limbs in a felt, each a 64-bit value. feltHead is the head of its array, and feltLen the length of the uint64 form Juno writes, which is the array head and four limbs, each an eight-byte integer with its own head byte.

Variables

View Source
var (
	ErrTruncated   = errors.New("cbor: unexpected end of input")
	ErrSyntax      = errors.New("cbor: malformed item")
	ErrType        = errors.New("cbor: item does not fit the destination")
	ErrDepth       = errors.New("cbor: nesting too deep")
	ErrUnsupported = errors.New("cbor: unsupported encoding")
)

Functions

func Felt

func Felt[F ~[Limbs]uint64](c *Cursor, dst *F) error

Felt reads a felt into any type with a felt's layout.

func LimbSlice

func LimbSlice[F ~[Limbs]uint64](c *Cursor) ([]F, error)

LimbSlice reads an array of felts, or null as a nil slice. F is any type with a felt's layout.

Types

type Cursor

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

A Cursor walks a CBOR buffer one item at a time. It is not safe for concurrent use. depth is how many arrays and maps are open around the next item, and scratch is room for a value that is read and then copied, such as a map key.

func NewCursor

func NewCursor(data []byte) Cursor

NewCursor returns a Cursor positioned at the start of data.

func (*Cursor) ArrayLen

func (c *Cursor) ArrayLen() (int, error)

ArrayLen reads an array head and returns its element count, capped by the bytes left. The caller must call Leave after reading the elements.

func (*Cursor) BigInt

func (c *Cursor) BigInt() (*big.Int, error)

BigInt reads an integer of any width, either a plain integer or a tag 2 or 3 bignum.

func (*Cursor) Bool

func (c *Cursor) Bool() (bool, error)

Bool reads a boolean.

func (*Cursor) Bytes

func (c *Cursor) Bytes() ([]byte, error)

Bytes reads a byte string into a new slice. An empty string gives an empty, non-nil slice.

func (*Cursor) BytesBorrowed

func (c *Cursor) BytesBorrowed() ([]byte, error)

BytesBorrowed is Bytes without the copy, so the result aliases the input.

func (*Cursor) BytesInto

func (c *Cursor) BytesInto(dst []byte) error

BytesInto reads a byte string of exactly len(dst) bytes into dst.

func (*Cursor) Int64

func (c *Cursor) Int64() (int64, error)

Int64 reads a signed integer that fits int64.

func (*Cursor) Key

func (c *Cursor) Key() (key []byte, isText bool, err error)

Key reads a map key. A text key is checked as UTF-8 and returned as a sub-slice of the input; an integer key is consumed and reported as not text.

func (*Cursor) Leave

func (c *Cursor) Leave()

Leave closes the array or map that ArrayLen or MapLen opened.

func (*Cursor) Len

func (c *Cursor) Len() int

Len returns the number of unread bytes.

func (*Cursor) Limbs

func (c *Cursor) Limbs(dst *[Limbs]uint64) error

Limbs reads a felt, an array of four unsigned integers that are the limbs of its Montgomery form. dst is written only on success.

func (*Cursor) MapLen

func (c *Cursor) MapLen() (int, error)

MapLen reads a map head and returns its pair count, capped by the bytes left. The caller must call Leave after reading the elements.

func (*Cursor) NextField

func (c *Cursor) NextField(s *Struct) (int, error)

NextField returns the field the next key names, Unknown if it names none, or Done once no pairs are left. A field named twice is ErrUnsupported. Stored maps are canonical, so the key expected next is compared first.

func (*Cursor) NextWanted

func (c *Cursor) NextWanted(s *Struct) (int, error)

NextWanted is NextField for a struct read in part. It skips the pairs of other keys, in any order, and the first of two keys naming a field wins.

func (*Cursor) Null

func (c *Cursor) Null() bool

Null consumes the next item if it is null or undefined, and reports whether it did.

func (*Cursor) OpenStruct

func (c *Cursor) OpenStruct(fields Fields) (Struct, error)

OpenStruct reads the head of a struct's map. A null struct has no pairs.

func (*Cursor) Peek

func (c *Cursor) Peek() (byte, error)

Peek returns the next head byte without consuming it.

func (*Cursor) Pos

func (c *Cursor) Pos() int

Pos returns the number of bytes consumed so far.

func (*Cursor) Remaining

func (c *Cursor) Remaining() []byte

Remaining returns the unread suffix of the input. It aliases the input buffer.

func (*Cursor) Scratch

func (c *Cursor) Scratch() unsafe.Pointer

Scratch returns room for four words. Its contents are lost when the Cursor is reused.

func (*Cursor) Skip

func (c *Cursor) Skip() error

Skip consumes the next item and checks its syntax, but not UTF-8. Indefinite-length items and tags 0-3, whose content only a reader that returns the value can check, are ErrUnsupported. It consumes nothing on error.

func (*Cursor) String

func (c *Cursor) String() (string, error)

String reads a text string into a new string and checks it is valid UTF-8.

func (*Cursor) Tag

func (c *Cursor) Tag() (uint64, error)

Tag reads a tag number. The content item follows.

func (*Cursor) Uint64

func (c *Cursor) Uint64() (uint64, error)

Uint64 reads an unsigned integer. A negative one is a type error.

type Fields

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

Fields matches the keys of a struct's map to field numbers, by an exact name first and then ignoring case. order holds each key as encoded, in canonical order (shorter first, then bytewise), names the wire name of each field by number, and all has the bit of every field set. The zero Fields has no fields.

func NewFields

func NewFields(names ...string) (Fields, error)

NewFields returns the Fields of a struct whose field i has the wire name names[i]. It returns an error if there are more than 64 names, or a name is not ASCII, is 256 bytes or more, or repeats another ignoring case.

type Struct

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

Struct is the progress of reading one struct's map. It holds the pairs left, where in fields.order the next key is expected, a bit for each field read, and whether a map was opened, which Done closes.

Jump to

Keyboard shortcuts

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