parse

package
v0.0.0-...-9c1b276 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2022 License: GPL-3.0 Imports: 4 Imported by: 0

README

parse

Parse is a small and simple byte slice based parsing tool library.

API

parse.Integer(b []byte) (i int64, n int)

Integer() parse from the beginning of the given slice to a non-decimal-digit is met, returning the value found i and the number of digits parsed n.

As no provisions are made to prevent overflow this function keeps reading until a non-digit is met or end-of-slice.

parse.Float(b []byte) (f float64, n int)

Float() parse from the beginning of the given slice to a non-float-conforming byte is met, returning the value found f and the number of bytes parsed n.

Scientific (exponential) notation is supported, thereby matching same expression as regexp \d*(?:\.\d*)(?:[eE][-+]?\d+).

As no provisions are made to prevent overflow this function keeps reading until a non-float byte is met or end-of-slice.

parse.NewBuffer(size int) *Buffer

Creates a preallocated, bounded, byte-based buffer.

Buffer.Push(c byte) (err error)

Push() a byte on the buffer. Returns ErrOutOfBounds only if buffer was full prior to push.

Buffer.FetchData() (d []byte)

FetchData() return a slice representing the current (pushed) data, then the push position is set to 0 (start).

Buffer.GetData() (d []byte)

GetData() return a slice representing the current (pushed) data.

Buffer.Clear()

Clear() reset the push position to 0 (start).

Buffer.Empty() bool

Empty() return true if the buffer is empty (push position is 0), false otherwise.

Buffer.Full() bool

Full() return true if the buffer is full (push position equals inital set size), false otherwise.

Documentation

Overview

package parse implement parsing primitives

Index

Constants

This section is empty.

Variables

View Source
var ErrGUIDParse = errors.New("guid: Cannot parse")

ErrGUIDParse is returned if input couldn't be parsed

View Source
var (
	// ErrOutOfBounds returned when buffer run out of space.
	ErrOutOfBounds = errors.New("Buffer index out of bounds")
)

Functions

func Decimal

func Decimal(b []byte) (i uint64, n int)

Decimal parses all decimal digits from start of the given byte slice. If a non-digit is met, parsing stops. Return n - number of bytes read, and i - the value found.

func Float

func Float(b []byte) (f float64, n int)

Float parses a floating point value from start of the given byte slice. If a non-compliant byte is met, parsing stops. Return n - number of bytes read, and f - the value found.

func Guid

func Guid(in []byte) (guid [16]byte, err error)

Guid parses a byte slice and turn it into a GUID (16 byte array). Any dashes are ignored.

func Hex

func Hex(b []byte) (i uint64, n int)

Hex parses all hexadecimal digits from start of the given byte slice. If a non-hexdigit is met, parsing stops. Return n - number of bytes read, and i - the value found.

func Number

func Number(b []byte, base uint64) (i uint64, n int)

Number parses "digits" (0-9 and A-..., depending on base) from start of the given byte slice. Parsing stop if a non-"digit" is met. Use parse.Hex to parse known hexadecimals (is slightly faster) Return n - number of bytes read, and i - the value found.

Types

type Buffer

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

func NewBuffer

func NewBuffer(size int) *Buffer

NewBuffer creates a new bounded buffer for tokenizers. The buffer support byte appending

func (*Buffer) Clear

func (b *Buffer) Clear()

Clear the buffer.

func (*Buffer) Empty

func (b *Buffer) Empty() bool

Empty return true if the buffer is empty.

func (*Buffer) FetchData

func (b *Buffer) FetchData() (d []byte)

FetchData return the data currently written then clear the buffer.

func (*Buffer) Full

func (b *Buffer) Full() bool

Full return true if the buffer is full.)

func (*Buffer) GetData

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

GetData return the data currently written.

func (*Buffer) Push

func (b *Buffer) Push(c byte) (err error)

Push a byte on the buffer. Return ErrOutOfBounds if overflow.

func (*Buffer) ReadFrom

func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error)

func (*Buffer) WriteTo

func (b *Buffer) WriteTo(w io.Writer) (n int64, err error)

Jump to

Keyboard shortcuts

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