Namespace: go.std.strconv
v1.0Contents
Summary
Provides a low-level interface to the strconv package.
Package strconv implements conversions to and from string representations
of basic data types.
# Numeric Conversions
The most common numeric conversions are Atoi (string to int) and Itoa (int to string).
i, err := strconv.Atoi("-42")
s := strconv.Itoa(-42)
These assume decimal and the Go int type.
ParseBool, ParseFloat, ParseInt, and ParseUint convert strings to values:
b, err := strconv.ParseBool("true")
f, err := strconv.ParseFloat("3.1415", 64)
i, err := strconv.ParseInt("-42", 10, 64)
u, err := strconv.ParseUint("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 := "2147483647" // biggest int32
i64, err := strconv.ParseInt(s, 10, 32)
...
i := int32(i64)
FormatBool, FormatFloat, FormatInt, and FormatUint convert values to strings:
s := strconv.FormatBool(true)
s := strconv.FormatFloat(3.1415, 'E', -1, 64)
s := strconv.FormatInt(-42, 16)
s := strconv.FormatUint(42, 16)
AppendBool, AppendFloat, AppendInt, and AppendUint are similar but
append the formatted value to a destination slice.
# String Conversions
Quote and QuoteToASCII convert strings to quoted Go string literals.
The latter guarantees that the result is an ASCII string, by escaping
any non-ASCII Unicode with \u:
q := strconv.Quote("Hello, 世界")
q := strconv.QuoteToASCII("Hello, 世界")
QuoteRune and QuoteRuneToASCII are similar but accept runes and
return quoted Go rune literals.
Unquote and UnquoteChar unquote Go string and rune literals.
Index
- *NumError
- AppendBool
- AppendFloat
- AppendInt
- AppendQuote
- AppendQuoteRune
- AppendQuoteRuneToASCII
- AppendQuoteRuneToGraphic
- AppendQuoteToASCII
- AppendQuoteToGraphic
- AppendUint
- Atoi
- CanBackquote
- ErrRange
- ErrSyntax
- FormatBool
- FormatFloat
- FormatInt
- FormatUint
- IntSize
- IsGraphic
- IsPrint
- Itoa
- NumError
- ParseBool
- ParseFloat
- ParseInt
- ParseUint
- Quote
- QuoteRune
- QuoteRuneToASCII
- QuoteRuneToGraphic
- QuoteToASCII
- QuoteToGraphic
- QuotedPrefix
- Unquote
- UnquoteChar
- arrayOfNumError
Legend
-
Constant
Variable
Function
Macro
Special form
Type
GoVar
Receiver/Method
Constants
Constants are variables with :const true in their metadata. Joker currently does not recognize them as special; as such, it allows redefining them or their values.-
IntSize
Int v1.0IntSize is the size in bits of an int or uint value.
Variables
-
ErrRange
Var v1.0ErrRange indicates that a value is out of range for the target type.
-
ErrSyntax
Var v1.0ErrSyntax indicates that a value does not have the right syntax for the target type.
Functions, Macros, and Special Forms
-
AppendBool
Function v1.0(AppendBool dst b)
AppendBool appends "true" or "false", according to the value of b,
to dst and returns the extended buffer.
Go input arguments: (dst []byte, b bool)
Go returns: []byte
Joker input arguments: [^arrayOfByte dst, ^Boolean b]
Joker returns: ^arrayOfByte -
AppendFloat
Function v1.0(AppendFloat dst f fmt prec bitSize)
AppendFloat appends the string form of the floating-point number f,
as generated by FormatFloat, to dst and returns the extended buffer.
Go input arguments: (dst []byte, f float64, fmt byte, prec int, bitSize int)
Go returns: []byte
Joker input arguments: [^arrayOfByte dst, ^Double f, ^Byte fmt, ^Int prec, ^Int bitSize]
Joker returns: ^arrayOfByte -
AppendInt
Function v1.0(AppendInt dst i base)
AppendInt appends the string form of the integer i,
as generated by FormatInt, to dst and returns the extended buffer.
Go input arguments: (dst []byte, i int64, base int)
Go returns: []byte
Joker input arguments: [^arrayOfByte dst, ^BigInt i, ^Int base]
Joker returns: ^arrayOfByte -
AppendQuote
Function v1.0(AppendQuote dst s)
AppendQuote appends a double-quoted Go string literal representing s,
as generated by Quote, to dst and returns the extended buffer.
Go input arguments: (dst []byte, s string)
Go returns: []byte
Joker input arguments: [^arrayOfByte dst, ^String s]
Joker returns: ^arrayOfByte -
AppendQuoteRune
Function v1.0(AppendQuoteRune dst r)
AppendQuoteRune appends a single-quoted Go character literal representing the rune,
as generated by QuoteRune, to dst and returns the extended buffer.
Go input arguments: (dst []byte, r rune)
Go returns: []byte
Joker input arguments: [^arrayOfByte dst, ^Char r]
Joker returns: ^arrayOfByte -
AppendQuoteRuneToASCII
Function v1.0(AppendQuoteRuneToASCII dst r)
AppendQuoteRuneToASCII appends a single-quoted Go character literal representing the rune,
as generated by QuoteRuneToASCII, to dst and returns the extended buffer.
Go input arguments: (dst []byte, r rune)
Go returns: []byte
Joker input arguments: [^arrayOfByte dst, ^Char r]
Joker returns: ^arrayOfByte -
AppendQuoteRuneToGraphic
Function v1.0(AppendQuoteRuneToGraphic dst r)
AppendQuoteRuneToGraphic appends a single-quoted Go character literal representing the rune,
as generated by QuoteRuneToGraphic, to dst and returns the extended buffer.
Go input arguments: (dst []byte, r rune)
Go returns: []byte
Joker input arguments: [^arrayOfByte dst, ^Char r]
Joker returns: ^arrayOfByte -
AppendQuoteToASCII
Function v1.0(AppendQuoteToASCII dst s)
AppendQuoteToASCII appends a double-quoted Go string literal representing s,
as generated by QuoteToASCII, to dst and returns the extended buffer.
Go input arguments: (dst []byte, s string)
Go returns: []byte
Joker input arguments: [^arrayOfByte dst, ^String s]
Joker returns: ^arrayOfByte -
AppendQuoteToGraphic
Function v1.0(AppendQuoteToGraphic dst s)
AppendQuoteToGraphic appends a double-quoted Go string literal representing s,
as generated by QuoteToGraphic, to dst and returns the extended buffer.
Go input arguments: (dst []byte, s string)
Go returns: []byte
Joker input arguments: [^arrayOfByte dst, ^String s]
Joker returns: ^arrayOfByte -
AppendUint
Function v1.0(AppendUint dst i base)
AppendUint appends the string form of the unsigned integer i,
as generated by FormatUint, to dst and returns the extended buffer.
Go input arguments: (dst []byte, i uint64, base int)
Go returns: []byte
Joker input arguments: [^arrayOfByte dst, ^Number i, ^Int base]
Joker returns: ^arrayOfByte -
Atoi
Function v1.0(Atoi s)
Atoi is equivalent to ParseInt(s, 10, 0), converted to type int.
Go input arguments: (s string)
Go returns: (int, error)
Joker input arguments: [^String s]
Joker returns: [^Int, ^Error] -
CanBackquote
Function v1.0(CanBackquote s)
CanBackquote reports whether the string s can be represented
unchanged as a single-line backquoted string without control
characters other than tab.
Go input arguments: (s string)
Go returns: bool
Joker input arguments: [^String s]
Joker returns: ^Boolean -
FormatBool
Function v1.0(FormatBool b)
FormatBool returns "true" or "false" according to the value of b.
Go input arguments: (b bool)
Go returns: string
Joker input arguments: [^Boolean b]
Joker returns: ^String -
FormatFloat
Function v1.0(FormatFloat f fmt prec bitSize)
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, a binary exponent),
'e' (-d.dddde±dd, a decimal exponent),
'E' (-d.ddddE±dd, a decimal exponent),
'f' (-ddd.dddd, no exponent),
'g' ('e' for large exponents, 'f' otherwise),
'G' ('E' for large exponents, 'f' otherwise),
'x' (-0xd.ddddp±ddd, a hexadecimal fraction and binary exponent), or
'X' (-0Xd.ddddP±ddd, a hexadecimal fraction and binary exponent).
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.
Go input arguments: (f float64, fmt byte, prec int, bitSize int)
Go returns: string
Joker input arguments: [^Double f, ^Byte fmt, ^Int prec, ^Int bitSize]
Joker returns: ^String -
FormatInt
Function v1.0(FormatInt i base)
FormatInt returns the string representation of i in the given base,
for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
for digit values >= 10.
Go input arguments: (i int64, base int)
Go returns: string
Joker input arguments: [^BigInt i, ^Int base]
Joker returns: ^String -
FormatUint
Function v1.0(FormatUint i base)
FormatUint returns the string representation of i in the given base,
for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
for digit values >= 10.
Go input arguments: (i uint64, base int)
Go returns: string
Joker input arguments: [^Number i, ^Int base]
Joker returns: ^String -
IsGraphic
Function v1.0(IsGraphic r)
IsGraphic reports whether the rune is defined as a Graphic by Unicode. Such
characters include letters, marks, numbers, punctuation, symbols, and
spaces, from categories L, M, N, P, S, and Zs.
Go input arguments: (r rune)
Go returns: bool
Joker input arguments: [^Char r]
Joker returns: ^Boolean -
IsPrint
Function v1.0(IsPrint r)
IsPrint reports whether the rune is defined as printable by Go, with
the same definition as unicode.IsPrint: letters, numbers, punctuation,
symbols and ASCII space.
Go input arguments: (r rune)
Go returns: bool
Joker input arguments: [^Char r]
Joker returns: ^Boolean -
Itoa
Function v1.0(Itoa i)
Itoa is equivalent to FormatInt(int64(i), 10).
Go input arguments: (i int)
Go returns: string
Joker input arguments: [^Int i]
Joker returns: ^String -
ParseBool
Function v1.0(ParseBool str)
ParseBool returns the boolean value represented by the string.
It accepts 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False.
Any other value returns an error.
Go input arguments: (str string)
Go returns: (bool, error)
Joker input arguments: [^String str]
Joker returns: [^Boolean, ^Error] -
ParseFloat
Function v1.0(ParseFloat s bitSize)
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 numbers
as defined by the Go syntax for [floating-point literals].
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 string "NaN", and the (possibly signed) strings "Inf" and "Infinity"
as their respective special floating point values. It ignores case when matching.
[floating-point literals]: https://go.dev/ref/spec#Floating-point_literals
Go input arguments: (s string, bitSize int)
Go returns: (float64, error)
Joker input arguments: [^String s, ^Int bitSize]
Joker returns: [^Double, ^Error] -
ParseInt
Function v1.0(ParseInt s base bitSize)
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.
The string may begin with a leading sign: "+" or "-".
If the base argument is 0, the true base is implied by the string's
prefix following the sign (if present): 2 for "0b", 8 for "0" or "0o",
16 for "0x", and 10 otherwise. Also, for argument base 0 only,
underscore characters are permitted as defined by the Go syntax for
[integer literals].
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.
[integer literals]: https://go.dev/ref/spec#Integer_literals
Go input arguments: (s string, base int, bitSize int)
Go returns: (i int64, err error)
Joker input arguments: [^String s, ^Int base, ^Int bitSize]
Joker returns: [^BigInt i, ^Error err] -
ParseUint
Function v1.0(ParseUint s base bitSize)
ParseUint is like ParseInt but for unsigned numbers.
A sign prefix is not permitted.
Go input arguments: (s string, base int, bitSize int)
Go returns: (uint64, error)
Joker input arguments: [^String s, ^Int base, ^Int bitSize]
Joker returns: [^Number, ^Error] -
Quote
Function v1.0(Quote s)
Quote returns a double-quoted Go string literal representing s. The
returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for
control characters and non-printable characters as defined by
IsPrint.
Go input arguments: (s string)
Go returns: string
Joker input arguments: [^String s]
Joker returns: ^String -
QuoteRune
Function v1.0(QuoteRune r)
QuoteRune returns a single-quoted Go character literal representing the
rune. The returned string uses Go escape sequences (\t, \n, \xFF, \u0100)
for control characters and non-printable characters as defined by IsPrint.
If r is not a valid Unicode code point, it is interpreted as the Unicode
replacement character U+FFFD.
Go input arguments: (r rune)
Go returns: string
Joker input arguments: [^Char r]
Joker returns: ^String -
QuoteRuneToASCII
Function v1.0(QuoteRuneToASCII r)
QuoteRuneToASCII returns a single-quoted Go character literal representing
the rune. The returned string uses Go escape sequences (\t, \n, \xFF,
\u0100) for non-ASCII characters and non-printable characters as defined
by IsPrint.
If r is not a valid Unicode code point, it is interpreted as the Unicode
replacement character U+FFFD.
Go input arguments: (r rune)
Go returns: string
Joker input arguments: [^Char r]
Joker returns: ^String -
QuoteRuneToGraphic
Function v1.0(QuoteRuneToGraphic r)
QuoteRuneToGraphic returns a single-quoted Go character literal representing
the rune. If the rune is not a Unicode graphic character,
as defined by IsGraphic, the returned string will use a Go escape sequence
(\t, \n, \xFF, \u0100).
If r is not a valid Unicode code point, it is interpreted as the Unicode
replacement character U+FFFD.
Go input arguments: (r rune)
Go returns: string
Joker input arguments: [^Char r]
Joker returns: ^String -
QuoteToASCII
Function v1.0(QuoteToASCII s)
QuoteToASCII returns a double-quoted Go string literal representing s.
The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for
non-ASCII characters and non-printable characters as defined by IsPrint.
Go input arguments: (s string)
Go returns: string
Joker input arguments: [^String s]
Joker returns: ^String -
QuoteToGraphic
Function v1.0(QuoteToGraphic s)
QuoteToGraphic returns a double-quoted Go string literal representing s.
The returned string leaves Unicode graphic characters, as defined by
IsGraphic, unchanged and uses Go escape sequences (\t, \n, \xFF, \u0100)
for non-graphic characters.
Go input arguments: (s string)
Go returns: string
Joker input arguments: [^String s]
Joker returns: ^String -
QuotedPrefix
Function v1.0(QuotedPrefix s)
QuotedPrefix returns the quoted string (as understood by Unquote) at the prefix of s.
If s does not start with a valid quoted string, QuotedPrefix returns an error.
Go input arguments: (s string)
Go returns: (string, error)
Joker input arguments: [^String s]
Joker returns: [^String, ^Error] -
Unquote
Function v1.0(Unquote s)
Unquote interprets s as a single-quoted, double-quoted,
or backquoted Go string literal, returning the string value
that s quotes. (If s is single-quoted, it would be a Go
character literal; Unquote returns the corresponding
one-character string.)
Go input arguments: (s string)
Go returns: (string, error)
Joker input arguments: [^String s]
Joker returns: [^String, ^Error] -
UnquoteChar
Function v1.0(UnquoteChar s quote)
UnquoteChar decodes the first character or byte in the escaped string
or character literal represented by the string s.
It returns four values:
1. value, the decoded Unicode code point or byte value;
2. multibyte, a boolean indicating whether the decoded character requires a multibyte UTF-8 representation;
3. tail, the remainder of the string after the character; and
4. an error that will be nil if the character is syntactically valid.
The second argument, quote, specifies the type of literal being parsed
and therefore which escaped quote character is permitted.
If set to a single quote, it permits the sequence \' and disallows unescaped '.
If set to a double quote, it permits \" and disallows unescaped ".
If set to zero, it does not permit either escape and allows both quote characters to appear unescaped.
Go input arguments: (s string, quote byte)
Go returns: (value rune, multibyte bool, tail string, err error)
Joker input arguments: [^String s, ^Byte quote]
Joker returns: [^Char value, ^Boolean multibyte, ^String tail, ^Error err]
Types
-
*NumError
Concrete Type v1.0A NumError records a failed conversion.
-
Error
Receiver for *NumError v1.0([])
-
Unwrap
Receiver for *NumError v1.0([])
-
NumError
Concrete Type v1.0A NumError records a failed conversion.
-
arrayOfNumError
Concrete Type v1.0A NumError records a failed conversion.