Skip to content

Commit

Permalink
This might be more performant in some cases
Browse files Browse the repository at this point in the history
  • Loading branch information
0x6e6562 committed Feb 28, 2014
1 parent 56e77e1 commit 105ba38
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

var (
bigOne = big.NewInt(1)
zero = big.NewInt(0)
)

// Marshaler is the interface implemented by objects that can marshal
Expand Down Expand Up @@ -725,7 +725,7 @@ func unmarshalDecimal(info *TypeInfo, data []byte, value interface{}) error {
func decBigInt2C(data []byte) *big.Int {
n := new(big.Int).SetBytes(data)
if len(data) > 0 && data[0]&0x80 > 0 {
n.Sub(n, new(big.Int).Lsh(bigOne, uint(len(data))*8))
n.Sub(n, exp2(new(big.Int), len(data)*8))
}
return n
}
Expand All @@ -743,8 +743,8 @@ func encBigInt2C(n *big.Int) []byte {
}
return b
case -1:
length := uint(n.BitLen()/8+1) * 8
b := new(big.Int).Add(n, new(big.Int).Lsh(bigOne, length)).Bytes()
length := (n.BitLen()/8 + 1) * 8
b := new(big.Int).Add(n, exp2(new(big.Int), length)).Bytes()
// When the most significant bit is on a byte
// boundary, we can get some extra significant
// bits, so strip them off when that happens.
Expand All @@ -756,6 +756,13 @@ func encBigInt2C(n *big.Int) []byte {
return nil
}

// exp2 sets z to 2**n.
func exp2(z *big.Int, n int) *big.Int {
z.Set(zero)
z.SetBit(z, n, 1)
return z
}

func marshalTimestamp(info *TypeInfo, value interface{}) ([]byte, error) {
switch v := value.(type) {
case Marshaler:
Expand Down

0 comments on commit 105ba38

Please sign in to comment.