bytesconv

package
v0.0.0-...-711ff2a Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2024 License: BSD-3-Clause Imports: 3 Imported by: 0

README

This is a partial copy of strconv.Parse* from Go 1.13.6, converted to use []byte (and stripped of the overly complex extFloat fast-path). It makes me sad that we have to do this, but see golang.org/issue/2632. We can eliminate this if golang.org/issue/43752 (or more generally, golang.org/issue/2205) gets fixed.

Documentation

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 is equivalent to ParseInt(s, 10, 0), converted to type int.

func ParseFloat

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

ParseFloat converts the 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.

ParseFloat accepts decimal and hexadecimal floating-point number syntax. If s is well-formed and near a valid floating-point number, ParseFloat returns the nearest floating-point number rounded using IEEE754 unbiased rounding. (Parsing a hexadecimal floating-point value only rounds when there are more bits in the hexadecimal representation than will fit in the mantissa.)

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.

ParseFloat recognizes the strings "NaN", "+Inf", and "-Inf" as their respective special floating point values. It ignores case when matching.

func ParseInt

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

ParseInt interprets a string s in the given base (0, 2 to 36) and bit size (0 to 64) and returns the corresponding value i.

If base == 0, the base is implied by the string's prefix: base 2 for "0b", base 8 for "0" or "0o", base 16 for "0x", and base 10 otherwise. Also, for base == 0 only, underscore characters are permitted per the Go integer literal syntax. If base is below 0, is 1, or is above 36, an error is returned.

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. If bitSize is below 0 or above 64, an error is returned.

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  string // the input
	Err  error  // the reason the conversion failed (e.g. ErrRange, ErrSyntax, etc.)
}

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