Skip to content

std/strconv

import "github.com/eugenioenko/goalchemy/std/strconv"

Package strconv converts between integers, Booleans, and their string forms, and formats floating-point numbers.

const IntSize = 64

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

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

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

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

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

func AppendFloat(dst []byte, f float64, fmt byte, prec, bitSize int) []byte

AppendFloat appends the string form of the floating-point number f, as generated by FormatFloat, to dst and returns the extended buffer.

func AppendInt(dst []byte, i int64, base int) []byte

AppendInt appends the string form of i in the given base to dst.

func AppendQuote(dst []byte, s string) []byte

AppendQuote appends the double-quoted Go string literal of s to dst.

func AppendQuoteRune(dst []byte, r rune) []byte

AppendQuoteRune appends the single-quoted character literal of r to dst.

func AppendQuoteRuneToASCII(dst []byte, r rune) []byte

AppendQuoteRuneToASCII appends the ASCII-only character literal of r to dst.

func AppendQuoteToASCII(dst []byte, s string) []byte

AppendQuoteToASCII appends the ASCII-only quoted form of s to dst.

func AppendUint(dst []byte, u uint64, base int) []byte

AppendUint appends the string form of u in the given base to dst.

func Atoi(s string) (int, error)

Atoi parses a decimal int, accepting an optional sign.

func CanBackquote(s string) bool

CanBackquote reports whether s can be represented unchanged as a single-line backquoted string without control characters other than tab.

func FormatBool(b bool) string

FormatBool returns “true” or “false”.

func FormatFloat(f float64, fmt byte, prec, bitSize int) string

FormatFloat converts the floating-point number f to a string, according to the format fmt and precision prec. It rounds the result assuming that the original was obtained from a floating-point value of bitSize bits (32 for float32, 64 for float64). The format fmt is one of ‘b’ (-ddddp±ddd), ‘e’ (-d.dddde±dd), ‘E’ (-d.ddddE±dd), ‘f’ (-ddd.dddd), ‘g’ (‘e’ for large exponents, ‘f’ otherwise), ‘G’ (‘E’ for large exponents, ‘f’ otherwise), ‘x’ (-0xd.ddddp±ddd) or ‘X’ (-0Xd.ddddP±ddd). The precision prec controls the number of digits (excluding the exponent) printed by the ‘e’, ‘E’, ‘f’, ‘g’, ‘G’, ‘x’, and ‘X’ formats. For ‘e’, ‘E’, ‘f’, ‘x’, and ‘X’, it is the number of digits after the decimal point. For ‘g’ and ‘G’ it is the maximum number of significant digits (trailing zeros are removed). The special precision -1 uses the smallest number of digits necessary such that ParseFloat will return f exactly.

func FormatInt(i int64, base int) string

FormatInt returns i in the given base, 2 to 36, using lowercase letters.

func FormatUint(u uint64, base int) string

FormatUint returns u in the given base, 2 to 36, using lowercase letters.

func IsPrint(r rune) bool

IsPrint reports whether r is printable as defined by Go: letters, marks, numbers, punctuation, symbols and the ASCII space.

func Itoa(i int) string

Itoa returns the decimal form of i.

func ParseBool(str string) (bool, error)

ParseBool accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, and False.

func ParseFloat(s string, 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. It accepts Go’s decimal and hexadecimal floating-point syntax, with “NaN”, “Inf” and “Infinity” in any case, and returns the nearest value under IEEE 754 round-half-to-even. Errors are *NumError values wrapping ErrSyntax or ErrRange; on ErrRange the result is ±Inf.

func ParseInt(s string, base int, bitSize int) (int64, error)

ParseInt parses a signed integer like ParseUint and checks that it fits in bitSize bits (0 means 64).

func ParseUint(s string, base int, bitSize int) (uint64, error)

ParseUint parses s in the given base (0 or 2 to 36) and checks that the result fits in bitSize bits (0 means 64). Base 0 accepts 0b, 0o, 0, and 0x prefixes and underscores between digits.

func Quote(s string) string

Quote returns s as a double-quoted Go string literal, escaping non-printable runes and invalid UTF-8 bytes.

func QuoteRune(r rune) string

QuoteRune returns r as a single-quoted Go character literal. An invalid rune is quoted as the Unicode replacement character.

func QuoteRuneToASCII(r rune) string

QuoteRuneToASCII is like QuoteRune but escapes every non-ASCII rune.

func QuoteToASCII(s string) string

QuoteToASCII is like Quote but escapes every non-ASCII rune.

type NumError struct {
Func string
Num string
Err error
}

NumError records a failed conversion.

func (e *NumError) Error() string
func (e *NumError) Unwrap() error