Commit 59129c6a authored by Robert Griesemer's avatar Robert Griesemer

math/big: remove some string conversions in Int encoding

Change-Id: I1180aa3d30fb8563c8e6ecefeb3296af0a88f5a6
Reviewed-on: https://go-review.googlesource.com/14998Reviewed-by: default avatarAlan Donovan <adonovan@google.com>
parent 7fa5a11e
...@@ -43,14 +43,16 @@ func (z *Int) GobDecode(buf []byte) error { ...@@ -43,14 +43,16 @@ func (z *Int) GobDecode(buf []byte) error {
} }
// MarshalJSON implements the json.Marshaler interface. // MarshalJSON implements the json.Marshaler interface.
func (z *Int) MarshalJSON() ([]byte, error) { func (x *Int) MarshalJSON() ([]byte, error) {
// TODO(gri): get rid of the []byte/string conversions if x == nil {
return []byte(z.String()), nil return []byte("<nil>"), nil
}
return x.abs.itoa(x.neg, 10), nil
} }
// UnmarshalJSON implements the json.Unmarshaler interface. // UnmarshalJSON implements the json.Unmarshaler interface.
func (z *Int) UnmarshalJSON(text []byte) error { 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 { if _, ok := z.SetString(string(text), 0); !ok {
return fmt.Errorf("math/big: cannot unmarshal %q into a *big.Int", text) return fmt.Errorf("math/big: cannot unmarshal %q into a *big.Int", text)
} }
...@@ -58,12 +60,16 @@ func (z *Int) UnmarshalJSON(text []byte) error { ...@@ -58,12 +60,16 @@ func (z *Int) UnmarshalJSON(text []byte) error {
} }
// MarshalText implements the encoding.TextMarshaler interface. // MarshalText implements the encoding.TextMarshaler interface.
func (z *Int) MarshalText() (text []byte, err error) { func (x *Int) MarshalText() (text []byte, err error) {
return []byte(z.String()), nil if x == nil {
return []byte("<nil>"), nil
}
return x.abs.itoa(x.neg, 10), nil
} }
// UnmarshalText implements the encoding.TextUnmarshaler interface. // UnmarshalText implements the encoding.TextUnmarshaler interface.
func (z *Int) UnmarshalText(text []byte) error { func (z *Int) UnmarshalText(text []byte) error {
// TODO(gri): get rid of the []byte/string conversion
if _, ok := z.SetString(string(text), 0); !ok { if _, ok := z.SetString(string(text), 0); !ok {
return fmt.Errorf("math/big: cannot unmarshal %q into a *big.Int", text) return fmt.Errorf("math/big: cannot unmarshal %q into a *big.Int", text)
} }
......
...@@ -59,11 +59,13 @@ func (z *Rat) GobDecode(buf []byte) error { ...@@ -59,11 +59,13 @@ func (z *Rat) GobDecode(buf []byte) error {
// MarshalText implements the encoding.TextMarshaler interface. // MarshalText implements the encoding.TextMarshaler interface.
func (r *Rat) MarshalText() (text []byte, err error) { func (r *Rat) MarshalText() (text []byte, err error) {
// TODO(gri): get rid of the []byte/string conversion
return []byte(r.RatString()), nil return []byte(r.RatString()), nil
} }
// UnmarshalText implements the encoding.TextUnmarshaler interface. // UnmarshalText implements the encoding.TextUnmarshaler interface.
func (r *Rat) UnmarshalText(text []byte) error { func (r *Rat) UnmarshalText(text []byte) error {
// TODO(gri): get rid of the []byte/string conversion
if _, ok := r.SetString(string(text)); !ok { if _, ok := r.SetString(string(text)); !ok {
return fmt.Errorf("math/big: cannot unmarshal %q into a *big.Rat", text) return fmt.Errorf("math/big: cannot unmarshal %q into a *big.Rat", text)
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment