]> Cypherpunks repositories - gostls13.git/commitdiff
math/big: remove some string conversions in Int encoding
authorRobert Griesemer <gri@golang.org>
Fri, 25 Sep 2015 22:11:14 +0000 (15:11 -0700)
committerRobert Griesemer <gri@golang.org>
Fri, 25 Sep 2015 22:25:52 +0000 (22:25 +0000)
Change-Id: I1180aa3d30fb8563c8e6ecefeb3296af0a88f5a6
Reviewed-on: https://go-review.googlesource.com/14998
Reviewed-by: Alan Donovan <adonovan@google.com>
src/math/big/intmarsh.go
src/math/big/ratmarsh.go

index 3c1efec8de410c1602ad093c8cbd1c9665f57257..ec1eeb4003aa93b303e973c48e5d0c53fd38ed47 100644 (file)
@@ -43,14 +43,16 @@ func (z *Int) GobDecode(buf []byte) error {
 }
 
 // MarshalJSON implements the json.Marshaler interface.
-func (z *Int) MarshalJSON() ([]byte, error) {
-       // TODO(gri): get rid of the []byte/string conversions
-       return []byte(z.String()), nil
+func (x *Int) MarshalJSON() ([]byte, error) {
+       if x == nil {
+               return []byte("<nil>"), nil
+       }
+       return x.abs.itoa(x.neg, 10), nil
 }
 
 // UnmarshalJSON implements the json.Unmarshaler interface.
 func (z *Int) UnmarshalJSON(text []byte) error {
-       // TODO(gri): get rid of the []byte/string conversions
+       // TODO(gri): get rid of the []byte/string conversion
        if _, ok := z.SetString(string(text), 0); !ok {
                return fmt.Errorf("math/big: cannot unmarshal %q into a *big.Int", text)
        }
@@ -58,12 +60,16 @@ func (z *Int) UnmarshalJSON(text []byte) error {
 }
 
 // MarshalText implements the encoding.TextMarshaler interface.
-func (z *Int) MarshalText() (text []byte, err error) {
-       return []byte(z.String()), nil
+func (x *Int) MarshalText() (text []byte, err error) {
+       if x == nil {
+               return []byte("<nil>"), nil
+       }
+       return x.abs.itoa(x.neg, 10), nil
 }
 
 // UnmarshalText implements the encoding.TextUnmarshaler interface.
 func (z *Int) UnmarshalText(text []byte) error {
+       // TODO(gri): get rid of the []byte/string conversion
        if _, ok := z.SetString(string(text), 0); !ok {
                return fmt.Errorf("math/big: cannot unmarshal %q into a *big.Int", text)
        }
index b79cbe665232eb25565a9061a44f29efda8a2069..6bb9d8af60880feb0c8b7ea9a3bf9d61f3a3fb23 100644 (file)
@@ -59,11 +59,13 @@ func (z *Rat) GobDecode(buf []byte) error {
 
 // MarshalText implements the encoding.TextMarshaler interface.
 func (r *Rat) MarshalText() (text []byte, err error) {
+       // TODO(gri): get rid of the []byte/string conversion
        return []byte(r.RatString()), nil
 }
 
 // UnmarshalText implements the encoding.TextUnmarshaler interface.
 func (r *Rat) UnmarshalText(text []byte) error {
+       // TODO(gri): get rid of the []byte/string conversion
        if _, ok := r.SetString(string(text)); !ok {
                return fmt.Errorf("math/big: cannot unmarshal %q into a *big.Rat", text)
        }