bytesconv

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2016 License: BSD-3-Clause Imports: 3 Imported by: 1

README

tl;dr: strconv.ParseXxx with []byte arguments.

Package bytesconv implements conversions from []byte representations
of basic data types. These are the routines missing from the standard
library's bytesconv. When parsing input data, which naturally arrives as
[]byte, these make parsing the input free of garbage creation. (Using
the bytesconv.ParseXxx functions creates a temporary string which the compiler
isn't (in 1.7) good enough to eliminate. There's a long discussion about
the need for these []byte parsing functions in the go, and the masters
are still, years later, waiting for the compiler to get it right. I can't
wait for my code to get it right, hence this largely C&Ved package.)

Note that only conversions from []byte are implemented. Converting to []byte
is already handled by the standard library's strconv package's AppendXxx
functions.

Numeric Conversions

ParseBool, ParseFloat, ParseInt, and ParseUint convert strings to values:

     b, err := bytesconv.ParseBool([]byte("true"))
     f, err := bytesconv.ParseFloat([]byte("3.1415"), 64)
     i, err := bytesconv.ParseInt([]byte("-42"), 10, 64)
     u, err := bytesconv.ParseUint([]byte("42"), 10, 64)

The parse functions return the widest type (float64, int64, and uint64),
but if the size argument specifies a narrower width the result can be
converted to that narrower type without data loss:

     s := []byte("2147483647") // biggest int32
     i64, err := bytesconv.ParseInt(s, 10, 32)
     ...
     i := int32(i64)

Documentation

Overview

Package bytesconv implements conversions from []byte representations of basic data types. These are the routines missing from the standard library's bytesconv. When parsing input data, which naturally arrives as []byte, these make parsing the input free of garbage creation. (Using the bytesconv.ParseXxx functions creates a temporary string which the compiler isn't (in 1.7) good enough to eliminate. There's a long discussion about the need for these []byte parsing functions in the go, and the masters are still, years later, waiting for the compiler to get it right. I can't wait for my code to get it right, hence this largely C&Ved package.)

Note that only conversions from []byte are implemented. Converting to []byte is already handled by the standard library's strconv package's AppendXxx functions.

Numeric Conversions

ParseBool, ParseFloat, ParseInt, and ParseUint convert strings to values:

b, err := bytesconv.ParseBool([]byte("true"))
f, err := bytesconv.ParseFloat([]byte("3.1415"), 64)
i, err := bytesconv.ParseInt([]byte("-42"), 10, 64)
u, err := bytesconv.ParseUint([]byte("42"), 10, 64)

The parse functions return the widest type (float64, int64, and uint64), but if the size argument specifies a narrower width the result can be converted to that narrower type without data loss:

s := []byte("2147483647") // biggest int32
i64, err := bytesconv.ParseInt(s, 10, 32)
...
i := int32(i64)

Index

Constants

View Source
const IntSize = intSize

IntSize is the size in bits of an int or uint value.

Variables

View Source
var ErrRange = errors.New("value out of range")

ErrRange indicates that a value is out of range for the target type.

View Source
var ErrSyntax = errors.New("invalid syntax")

ErrSyntax indicates that a value does not have the right syntax for the target type.

Functions

func Atoi

func Atoi(s []byte) (int, error)

Atoi returns the result of ParseInt(s, 10, 0) converted to type int.

func ParseBool

func ParseBool(str []byte) (bool, error)

ParseBool returns the boolean value represented by the string in []byte. It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False. Any other value returns an error.

func ParseFloat

func ParseFloat(s []byte, bitSize int) (float64, error)

ParseFloat converts the []byte containing string s to a floating-point number with the precision specified by bitSize: 32 for float32, or 64 for float64. When bitSize=32, the result still has type float64, but it will be convertible to float32 without changing its value.

If s is well-formed and near a valid floating point number, ParseFloat returns the nearest floating point number rounded using IEEE754 unbiased rounding.

The errors that ParseFloat returns have concrete type *NumError and include err.Num = s.

If s is not syntactically well-formed, ParseFloat returns err.Err = ErrSyntax.

If s is syntactically well-formed but is more than 1/2 ULP away from the largest floating point number of the given size, ParseFloat returns f = ±Inf, err.Err = ErrRange.

func ParseInt

func ParseInt(s []byte, base int, bitSize int) (i int64, err error)

ParseInt interprets a []byte containing string s in the given base (2 to 36) and returns the corresponding value i. If base == 0, the base is implied by the string's prefix: base 16 for "0x", base 8 for "0", and base 10 otherwise.

The bitSize argument specifies the integer type that the result must fit into. Bit sizes 0, 8, 16, 32, and 64 correspond to int, int8, int16, int32, and int64.

The errors that ParseInt returns have concrete type *NumError and include err.Num = s. If s is empty or contains invalid digits, err.Err = ErrSyntax and the returned value is 0; if the value corresponding to s cannot be represented by a signed integer of the given size, err.Err = ErrRange and the returned value is the maximum magnitude integer of the appropriate bitSize and sign.

func ParseUint

func ParseUint(s []byte, base int, bitSize int) (uint64, error)

ParseUint is like ParseInt but for unsigned numbers.

Types

type NumError

type NumError struct {
	Func string // the failing function (ParseBool, ParseInt, ParseUint, ParseFloat)
	Num  []byte // the input
	Err  error  // the reason the conversion failed (ErrRange, ErrSyntax)
}

A NumError records a failed conversion.

func (*NumError) Error

func (e *NumError) Error() string

Jump to

Keyboard shortcuts

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