aboutsummaryrefslogtreecommitdiff
path: root/src/math/big/ratmarsh.go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2015-09-25 14:56:19 -0700
committerRobert Griesemer <gri@golang.org>2015-09-25 22:25:36 +0000
commit7fa5a11ea1e876bd9d72b0ed2e2821597f76a495 (patch)
tree707de8871669c5ed15bc34ac535527c129ad0b2e /src/math/big/ratmarsh.go
parente937eeeccdbf07df30ad4ea8ebd3cc20742b3200 (diff)
downloadgo-7fa5a11ea1e876bd9d72b0ed2e2821597f76a495.tar.gz
go-7fa5a11ea1e876bd9d72b0ed2e2821597f76a495.zip
math/big: move Int/Rat gob/json/xml functionality in separate files
Like int/rat/float conversions, move this functionality into separate implementation and test files. No implementation changes besides the move. Change-Id: If19c45f5a72a57b95cbce2329724693ae5a4807d Reviewed-on: https://go-review.googlesource.com/14997 Reviewed-by: Alan Donovan <adonovan@google.com>
Diffstat (limited to 'src/math/big/ratmarsh.go')
-rw-r--r--src/math/big/ratmarsh.go71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/math/big/ratmarsh.go b/src/math/big/ratmarsh.go
new file mode 100644
index 0000000000..b79cbe6652
--- /dev/null
+++ b/src/math/big/ratmarsh.go
@@ -0,0 +1,71 @@
+// Copyright 2015 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// This file implements encoding/decoding of Rats.
+
+package big
+
+import (
+ "encoding/binary"
+ "errors"
+ "fmt"
+)
+
+// Gob codec version. Permits backward-compatible changes to the encoding.
+const ratGobVersion byte = 1
+
+// GobEncode implements the gob.GobEncoder interface.
+func (x *Rat) GobEncode() ([]byte, error) {
+ if x == nil {
+ return nil, nil
+ }
+ buf := make([]byte, 1+4+(len(x.a.abs)+len(x.b.abs))*_S) // extra bytes for version and sign bit (1), and numerator length (4)
+ i := x.b.abs.bytes(buf)
+ j := x.a.abs.bytes(buf[:i])
+ n := i - j
+ if int(uint32(n)) != n {
+ // this should never happen
+ return nil, errors.New("Rat.GobEncode: numerator too large")
+ }
+ binary.BigEndian.PutUint32(buf[j-4:j], uint32(n))
+ j -= 1 + 4
+ b := ratGobVersion << 1 // make space for sign bit
+ if x.a.neg {
+ b |= 1
+ }
+ buf[j] = b
+ return buf[j:], nil
+}
+
+// GobDecode implements the gob.GobDecoder interface.
+func (z *Rat) GobDecode(buf []byte) error {
+ if len(buf) == 0 {
+ // Other side sent a nil or default value.
+ *z = Rat{}
+ return nil
+ }
+ b := buf[0]
+ if b>>1 != ratGobVersion {
+ return fmt.Errorf("Rat.GobDecode: encoding version %d not supported", b>>1)
+ }
+ const j = 1 + 4
+ i := j + binary.BigEndian.Uint32(buf[j-4:j])
+ z.a.neg = b&1 != 0
+ z.a.abs = z.a.abs.setBytes(buf[j:i])
+ z.b.abs = z.b.abs.setBytes(buf[i:])
+ return nil
+}
+
+// MarshalText implements the encoding.TextMarshaler interface.
+func (r *Rat) MarshalText() (text []byte, err error) {
+ return []byte(r.RatString()), nil
+}
+
+// UnmarshalText implements the encoding.TextUnmarshaler interface.
+func (r *Rat) UnmarshalText(text []byte) error {
+ if _, ok := r.SetString(string(text)); !ok {
+ return fmt.Errorf("math/big: cannot unmarshal %q into a *big.Rat", text)
+ }
+ return nil
+}