mirror of
https://github.com/golang/go
synced 2024-11-26 08:17:59 -07:00
strconv: document parsing of leading +/-
Explicitly document the handling of a sign prefix, and the interaction between the sign and base prefixes. Fixes #46641. Change-Id: I3cd6773e3f074fe671a944a05a79d2408137fcd4 Reviewed-on: https://go-review.googlesource.com/c/go/+/325875 Trust: Damien Neil <dneil@google.com> Run-TryBot: Damien Neil <dneil@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
parent
df35ade067
commit
1402b27d46
@ -57,6 +57,8 @@ const IntSize = intSize
|
|||||||
const maxUint64 = 1<<64 - 1
|
const maxUint64 = 1<<64 - 1
|
||||||
|
|
||||||
// ParseUint is like ParseInt but for unsigned numbers.
|
// ParseUint is like ParseInt but for unsigned numbers.
|
||||||
|
//
|
||||||
|
// A sign prefix is not permitted.
|
||||||
func ParseUint(s string, base int, bitSize int) (uint64, error) {
|
func ParseUint(s string, base int, bitSize int) (uint64, error) {
|
||||||
const fnParseUint = "ParseUint"
|
const fnParseUint = "ParseUint"
|
||||||
|
|
||||||
@ -159,10 +161,13 @@ func ParseUint(s string, base int, bitSize int) (uint64, error) {
|
|||||||
// ParseInt interprets a string s in the given base (0, 2 to 36) and
|
// 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.
|
// 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
|
// If the base argument is 0, the true base is implied by the string's
|
||||||
// prefix: 2 for "0b", 8 for "0" or "0o", 16 for "0x", and 10 otherwise.
|
// prefix following the sign (if present): 2 for "0b", 8 for "0" or "0o",
|
||||||
// Also, for argument base 0 only, underscore characters are permitted
|
// 16 for "0x", and 10 otherwise. Also, for argument base 0 only,
|
||||||
// as defined by the Go syntax for integer literals.
|
// underscore characters are permitted as defined by the Go syntax for
|
||||||
|
// integer literals.
|
||||||
//
|
//
|
||||||
// The bitSize argument specifies the integer type
|
// The bitSize argument specifies the integer type
|
||||||
// that the result must fit into. Bit sizes 0, 8, 16, 32, and 64
|
// that the result must fit into. Bit sizes 0, 8, 16, 32, and 64
|
||||||
|
@ -33,6 +33,9 @@ var parseUint64Tests = []parseUint64Test{
|
|||||||
{"_12345", 0, ErrSyntax},
|
{"_12345", 0, ErrSyntax},
|
||||||
{"1__2345", 0, ErrSyntax},
|
{"1__2345", 0, ErrSyntax},
|
||||||
{"12345_", 0, ErrSyntax},
|
{"12345_", 0, ErrSyntax},
|
||||||
|
{"-0", 0, ErrSyntax},
|
||||||
|
{"-1", 0, ErrSyntax},
|
||||||
|
{"+1", 0, ErrSyntax},
|
||||||
}
|
}
|
||||||
|
|
||||||
type parseUint64BaseTest struct {
|
type parseUint64BaseTest struct {
|
||||||
@ -140,8 +143,10 @@ var parseInt64Tests = []parseInt64Test{
|
|||||||
{"", 0, ErrSyntax},
|
{"", 0, ErrSyntax},
|
||||||
{"0", 0, nil},
|
{"0", 0, nil},
|
||||||
{"-0", 0, nil},
|
{"-0", 0, nil},
|
||||||
|
{"+0", 0, nil},
|
||||||
{"1", 1, nil},
|
{"1", 1, nil},
|
||||||
{"-1", -1, nil},
|
{"-1", -1, nil},
|
||||||
|
{"+1", 1, nil},
|
||||||
{"12345", 12345, nil},
|
{"12345", 12345, nil},
|
||||||
{"-12345", -12345, nil},
|
{"-12345", -12345, nil},
|
||||||
{"012345", 12345, nil},
|
{"012345", 12345, nil},
|
||||||
@ -236,6 +241,11 @@ var parseInt64BaseTests = []parseInt64BaseTest{
|
|||||||
{"0__12345", 0, 0, ErrSyntax},
|
{"0__12345", 0, 0, ErrSyntax},
|
||||||
{"01234__5", 0, 0, ErrSyntax},
|
{"01234__5", 0, 0, ErrSyntax},
|
||||||
{"012345_", 0, 0, ErrSyntax},
|
{"012345_", 0, 0, ErrSyntax},
|
||||||
|
|
||||||
|
{"+0xf", 0, 0xf, nil},
|
||||||
|
{"-0xf", 0, -0xf, nil},
|
||||||
|
{"0x+f", 0, 0, ErrSyntax},
|
||||||
|
{"0x-f", 0, 0, ErrSyntax},
|
||||||
}
|
}
|
||||||
|
|
||||||
type parseUint32Test struct {
|
type parseUint32Test struct {
|
||||||
|
Loading…
Reference in New Issue
Block a user