2021-11-23 09:40:57 +01:00
|
|
|
package decimal
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errShortBytes = errors.New("msgp: too few bytes left to read object")
|
|
|
|
)
|
|
|
|
|
|
|
|
// MarshalMsg implements msgp.Marshaler
|
2021-12-06 02:09:53 +01:00
|
|
|
// Note: limit to 31 digits, if d.IntPart size large than 31, will be lose.
|
2021-12-06 02:22:32 +01:00
|
|
|
func (d Decimal) MarshalMsg(b []byte) ([]byte, error) {
|
|
|
|
o := require(b, d.Msgsize())
|
2021-11-23 09:40:57 +01:00
|
|
|
str := d.String()
|
|
|
|
sz := len(str)
|
2021-12-06 02:09:53 +01:00
|
|
|
// limit to 31 digits
|
2021-12-06 02:22:32 +01:00
|
|
|
// note, if d.IntPart size large than 3, will be lose.
|
2021-12-06 02:09:53 +01:00
|
|
|
if sz > 31 {
|
|
|
|
sz = 31
|
|
|
|
// if last char is '.' then limit to 30 digits
|
|
|
|
if str[30] == '.' {
|
|
|
|
sz = 30
|
2021-11-23 09:40:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
str = str[:sz]
|
|
|
|
}
|
|
|
|
|
2021-12-06 04:35:43 +01:00
|
|
|
o, n := ensure(o, 1+sz)
|
2021-11-23 09:40:57 +01:00
|
|
|
o[n] = byte(0xa0 | sz)
|
|
|
|
n++
|
|
|
|
|
|
|
|
return o[:n+copy(o[n:], str)], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalMsg implements msgp.Unmarshaler
|
2021-12-06 02:22:32 +01:00
|
|
|
func (d *Decimal) UnmarshalMsg(b []byte) ([]byte, error) {
|
|
|
|
o, err := b, errShortBytes
|
|
|
|
|
2021-11-23 09:40:57 +01:00
|
|
|
l := len(b)
|
|
|
|
if l < 1 {
|
2021-12-06 02:29:46 +01:00
|
|
|
return o, err
|
2021-11-23 09:40:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sz := int(b[0] & 0x1f)
|
|
|
|
if len(b[1:]) < sz {
|
2021-12-06 02:29:46 +01:00
|
|
|
return o, err
|
2021-11-23 09:40:57 +01:00
|
|
|
}
|
|
|
|
if *d, err = NewFromString(string(b[1 : sz+1])); err == nil {
|
|
|
|
o = b[sz:]
|
|
|
|
}
|
2021-12-06 02:22:32 +01:00
|
|
|
return o, err
|
2021-11-23 09:40:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message
|
|
|
|
func (d Decimal) Msgsize() int {
|
2021-12-06 04:35:43 +01:00
|
|
|
return 32
|
2021-11-23 09:40:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Require ensures that cap(old)-len(old) >= extra.
|
|
|
|
func require(old []byte, extra int) []byte {
|
|
|
|
l := len(old)
|
|
|
|
c := cap(old)
|
|
|
|
r := l + extra
|
|
|
|
if c >= r {
|
|
|
|
return old
|
|
|
|
} else if l == 0 {
|
|
|
|
return make([]byte, 0, extra)
|
|
|
|
}
|
|
|
|
// the new size is the greater
|
|
|
|
// of double the old capacity
|
|
|
|
// and the sum of the old length
|
|
|
|
// and the number of new bytes
|
|
|
|
// necessary.
|
|
|
|
c <<= 1
|
|
|
|
if c < r {
|
|
|
|
c = r
|
|
|
|
}
|
|
|
|
n := make([]byte, l, c)
|
|
|
|
copy(n, old)
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure 'sz' extra bytes in 'b' btw len(b) and cap(b)
|
|
|
|
func ensure(b []byte, sz int) ([]byte, int) {
|
|
|
|
l := len(b)
|
|
|
|
c := cap(b)
|
|
|
|
if c-l < sz {
|
|
|
|
o := make([]byte, (2*c)+sz) // exponential growth
|
|
|
|
n := copy(o, b)
|
|
|
|
return o[:n+sz], n
|
|
|
|
}
|
|
|
|
return b[:l+sz], l
|
|
|
|
}
|