summaryrefslogtreecommitdiff
path: root/src/pkg/big
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2010-05-21 14:28:34 -0700
committerRobert Griesemer <gri@golang.org>2010-05-21 14:28:34 -0700
commit97bb02cadc689aa0a3592f6f9bd3b26636d51ec1 (patch)
treee6d417879c4a1a275aa4e3762c2cb56aedd38356 /src/pkg/big
parent80f292a3c290b295d33d2175a6d47e365dfd4cb3 (diff)
downloadgolang-97bb02cadc689aa0a3592f6f9bd3b26636d51ec1.tar.gz
big: fix Int.SetString comment, simplify implementation,
always return z for nat.scan R=rsc CC=golang-dev http://codereview.appspot.com/1236043
Diffstat (limited to 'src/pkg/big')
-rwxr-xr-xsrc/pkg/big/int.go30
-rwxr-xr-xsrc/pkg/big/nat.go2
2 files changed, 14 insertions, 18 deletions
diff --git a/src/pkg/big/int.go b/src/pkg/big/int.go
index e1e45858a..dd9179660 100755
--- a/src/pkg/big/int.go
+++ b/src/pkg/big/int.go
@@ -333,18 +333,18 @@ func (x *Int) Int64() int64 {
}
-// SetString sets z to the value of s, interpreted in the given base.
-// If base is 0 then SetString attempts to detect the base by at the prefix of
-// s. '0x' implies base 16, '0' implies base 8. Otherwise base 10 is assumed.
+// SetString sets z to the value of s, interpreted in the given base,
+// and returns z and a boolean indicating success. If SetString fails,
+// the value of z is undefined.
+//
+// If the base argument is 0, the string prefix determines the actual
+// conversion base. A prefix of ``0x'' or ``0X'' selects base 16; the
+// ``0'' prefix selects base 8, and a ``0b'' or ``0B'' prefix selects
+// base 2. Otherwise the selected base is 10.
+//
func (z *Int) SetString(s string, base int) (*Int, bool) {
- var scanned int
-
- if base == 1 || base > 16 {
- goto Error
- }
-
- if len(s) == 0 {
- goto Error
+ if len(s) == 0 || base < 0 || base == 1 || 16 < base {
+ return z, false
}
neg := false
@@ -353,18 +353,14 @@ func (z *Int) SetString(s string, base int) (*Int, bool) {
s = s[1:]
}
+ var scanned int
z.abs, _, scanned = z.abs.scan(s, base)
if scanned != len(s) {
- goto Error
+ return z, false
}
z.neg = len(z.abs) > 0 && neg // 0 has no sign
return z, true
-
-Error:
- z.abs = nil
- z.neg = false
- return z, false
}
diff --git a/src/pkg/big/nat.go b/src/pkg/big/nat.go
index b09893730..aa021e879 100755
--- a/src/pkg/big/nat.go
+++ b/src/pkg/big/nat.go
@@ -653,7 +653,7 @@ func (z nat) scan(s string, base int) (nat, int, int) {
// reject illegal bases or strings consisting only of prefix
if base < 2 || 16 < base || (base != 8 && i >= n) {
- return nil, 0, 0
+ return z, 0, 0
}
// convert string