strconv: use switch for '+'/'-' prefix handling

Follow the approach used in strconv's readFloat, decimal.set, and Atoi,
where leading '+' and '-' are handled using a switch for clarity and
consistency.

Change-Id: I41eff34ce90b5ac43fcdbc0bb88910d6d5fb4d39
GitHub-Last-Rev: 0c9d2efb5a828515fa00afdba8c436aa31fb0e53
GitHub-Pull-Request: golang/go#73185
Reviewed-on: https://go-review.googlesource.com/c/go/+/663257
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
zhi.wang 2025-04-06 13:08:57 +00:00 committed by Gopher Robot
parent b2819d13db
commit 8054d2da5b
2 changed files with 18 additions and 15 deletions

View File

@ -77,12 +77,12 @@ func (b *decimal) set(s string) (ok bool) {
if i >= len(s) { if i >= len(s) {
return return
} }
switch { switch s[i] {
case s[i] == '+': case '+':
i++
case '-':
i++ i++
case s[i] == '-':
b.neg = true b.neg = true
i++
} }
// digits // digits
@ -135,9 +135,10 @@ func (b *decimal) set(s string) (ok bool) {
return return
} }
esign := 1 esign := 1
if s[i] == '+' { switch s[i] {
case '+':
i++ i++
} else if s[i] == '-' { case '-':
i++ i++
esign = -1 esign = -1
} }
@ -176,12 +177,12 @@ func readFloat(s string) (mantissa uint64, exp int, neg, trunc, hex bool, i int,
if i >= len(s) { if i >= len(s) {
return return
} }
switch { switch s[i] {
case s[i] == '+': case '+':
i++
case '-':
i++ i++
case s[i] == '-':
neg = true neg = true
i++
} }
// digits // digits
@ -268,9 +269,10 @@ loop:
return return
} }
esign := 1 esign := 1
if s[i] == '+' { switch s[i] {
case '+':
i++ i++
} else if s[i] == '-' { case '-':
i++ i++
esign = -1 esign = -1
} }

View File

@ -204,11 +204,12 @@ func ParseInt(s string, base int, bitSize int) (i int64, err error) {
// Pick off leading sign. // Pick off leading sign.
s0 := s s0 := s
neg := false neg := false
if s[0] == '+' { switch s[0] {
case '+':
s = s[1:]
case '-':
s = s[1:] s = s[1:]
} else if s[0] == '-' {
neg = true neg = true
s = s[1:]
} }
// Convert unsigned and check range. // Convert unsigned and check range.