Skip to content

Commit

Permalink
Merge pull request #66 from tdakkota/feat/add-more-int-decoders
Browse files Browse the repository at this point in the history
feat: add 8/16-bit integers decoder
  • Loading branch information
ernado committed Oct 6, 2022
2 parents eda9b6b + 7d8ac4a commit e2ecde9
Show file tree
Hide file tree
Showing 8 changed files with 587 additions and 129 deletions.
5 changes: 3 additions & 2 deletions dec_float_test.go
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"io"
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -25,11 +26,11 @@ func decodeStr(t *testing.T, s string, f func(d *Decoder)) {
},
{
Name: "Decode",
Fn: func() *Decoder { return Decode(bytes.NewBufferString(s), 0) },
Fn: func() *Decoder { return Decode(strings.NewReader(s), 0) },
},
{
Name: "DecodeSingleByteBuf",
Fn: func() *Decoder { return Decode(bytes.NewBufferString(s), 1) },
Fn: func() *Decoder { return Decode(strings.NewReader(s), 1) },
},
} {
t.Run(d.Name, func(t *testing.T) {
Expand Down
265 changes: 243 additions & 22 deletions dec_int.go
Expand Up @@ -8,9 +8,14 @@ import (
"github.com/go-faster/errors"
)

var intDigits [256]int8
var (
intDigits [256]int8
errOverflow = strconv.ErrRange
)

const (
uint8SafeToMultiply10 = uint8(0xff)/10 - 1
uint16SafeToMultiply10 = uint16(0xffff)/10 - 1
uint32SafeToMultiply10 = uint32(0xffffffff)/10 - 1
uint64SafeToMultiple10 = uint64(0xffffffffffffffff)/10 - 1
)
Expand All @@ -24,35 +29,251 @@ func init() {
}
}

func (d *Decoder) int(size int) (int, error) {
switch size {
case 8:
v, err := d.Int8()
return int(v), err
case 16:
v, err := d.Int16()
return int(v), err
case 32:
v, err := d.Int32()
return int(v), err
default:
v, err := d.Int64()
return int(v), err
}
}

// Int reads int.
func (d *Decoder) Int() (int, error) {
return d.int(strconv.IntSize)
}

func (d *Decoder) uint(size int) (uint, error) {
if size == 32 {
switch size {
case 8:
v, err := d.UInt8()
return uint(v), err
case 16:
v, err := d.UInt16()
return uint(v), err
case 32:
v, err := d.UInt32()
return uint(v), err
default:
v, err := d.UInt64()
return uint(v), err
}
v, err := d.UInt64()
return uint(v), err
}

// UInt read uint.
// UInt reads uint.
func (d *Decoder) UInt() (uint, error) {
return d.uint(strconv.IntSize)
}

func (d *Decoder) int(size int) (int, error) {
if size == 32 {
v, err := d.Int32()
return int(v), err
// Int8 reads int8.
func (d *Decoder) Int8() (int8, error) {
c, err := d.byte()
if err != nil {
return 0, errors.Wrap(err, "byte")
}
if c == '-' {
val, err := d.readUInt8()
if err != nil {
return 0, err
}
if val > math.MaxInt8+1 {
return 0, errOverflow
}
return -int8(val), nil
}
d.unread()
val, err := d.readUInt8()
if err != nil {
return 0, err
}
v, err := d.Int64()
return int(v), err
if val > math.MaxInt8 {
return 0, errOverflow
}
return int8(val), nil
}

// Int reads integer.
func (d *Decoder) Int() (int, error) {
return d.int(strconv.IntSize)
// UInt8 reads uint8.
func (d *Decoder) UInt8() (uint8, error) {
return d.readUInt8()
}

func (d *Decoder) readUInt8() (uint8, error) {
c, err := d.byte()
if err != nil {
return 0, errors.Wrap(err, "byte")
}
ind := intDigits[c]
if ind == 0 {
return 0, nil
}
if ind == invalidCharForNumber {
return 0, errors.Wrap(err, "bad token")
}
value := uint8(ind)
if d.tail-d.head > 10 {
i := d.head
ind2 := intDigits[d.buf[i]]
if ind2 == invalidCharForNumber {
d.head = i
return value, nil
}
i++
ind3 := intDigits[d.buf[i]]
if ind3 == invalidCharForNumber {
d.head = i
return value*10 + uint8(ind2), nil
}
i++
ind4 := intDigits[d.buf[i]]
value = value*100 + uint8(ind2)*10 + uint8(ind3)
d.head = i
if ind4 == invalidCharForNumber {
return value, nil
}
}
for {
buf := d.buf[d.head:d.tail]
for i, c := range buf {
ind = intDigits[c]
if ind == invalidCharForNumber {
d.head += i
return value, nil
}
if value > uint8SafeToMultiply10 {
value2 := (value << 3) + (value << 1) + uint8(ind)
if value2 < value {
return 0, errOverflow
}
value = value2
continue
}
value = (value << 3) + (value << 1) + uint8(ind)
}
err := d.read()
if err == io.EOF {
return value, nil
}
if err != nil {
return 0, err
}
}
}

// Int16 reads int16.
func (d *Decoder) Int16() (int16, error) {
c, err := d.byte()
if err != nil {
return 0, errors.Wrap(err, "byte")
}
if c == '-' {
val, err := d.readUInt16()
if err != nil {
return 0, err
}
if val > math.MaxInt16+1 {
return 0, errOverflow
}
return -int16(val), nil
}
d.unread()
val, err := d.readUInt16()
if err != nil {
return 0, err
}
if val > math.MaxInt16 {
return 0, errOverflow
}
return int16(val), nil
}

// UInt16 reads uint16.
func (d *Decoder) UInt16() (uint16, error) {
return d.readUInt16()
}

func (d *Decoder) readUInt16() (uint16, error) {
c, err := d.byte()
if err != nil {
return 0, errors.Wrap(err, "byte")
}
ind := intDigits[c]
if ind == 0 {
return 0, nil
}
if ind == invalidCharForNumber {
return 0, errors.Wrap(err, "bad token")
}
value := uint16(ind)
if d.tail-d.head > 10 {
i := d.head
ind2 := intDigits[d.buf[i]]
if ind2 == invalidCharForNumber {
d.head = i
return value, nil
}
i++
ind3 := intDigits[d.buf[i]]
if ind3 == invalidCharForNumber {
d.head = i
return value*10 + uint16(ind2), nil
}
i++
ind4 := intDigits[d.buf[i]]
if ind4 == invalidCharForNumber {
d.head = i
return value*100 + uint16(ind2)*10 + uint16(ind3), nil
}
i++
ind5 := intDigits[d.buf[i]]
if ind5 == invalidCharForNumber {
d.head = i
return value*1000 + uint16(ind2)*100 + uint16(ind3)*10 + uint16(ind4), nil
}
i++
ind6 := intDigits[d.buf[i]]
value = value*10000 + uint16(ind2)*1000 + uint16(ind3)*100 + uint16(ind4)*10 + uint16(ind5)
d.head = i
if ind6 == invalidCharForNumber {
return value, nil
}
}
for {
buf := d.buf[d.head:d.tail]
for i, c := range buf {
ind = intDigits[c]
if ind == invalidCharForNumber {
d.head += i
return value, nil
}
if value > uint16SafeToMultiply10 {
value2 := (value << 3) + (value << 1) + uint16(ind)
if value2 < value {
return 0, errOverflow
}
value = value2
continue
}
value = (value << 3) + (value << 1) + uint16(ind)
}
err := d.read()
if err == io.EOF {
return value, nil
}
if err != nil {
return 0, err
}
}
}

// Int32 reads int32 value.
// Int32 reads int32.
func (d *Decoder) Int32() (int32, error) {
c, err := d.byte()
if err != nil {
Expand All @@ -64,7 +285,7 @@ func (d *Decoder) Int32() (int32, error) {
return 0, err
}
if val > math.MaxInt32+1 {
return 0, errors.New("overflow")
return 0, errOverflow
}
return -int32(val), nil
}
Expand All @@ -74,12 +295,12 @@ func (d *Decoder) Int32() (int32, error) {
return 0, err
}
if val > math.MaxInt32 {
return 0, errors.New("overflow")
return 0, errOverflow
}
return int32(val), nil
}

// UInt32 read uint32
// UInt32 reads uint32.
func (d *Decoder) UInt32() (uint32, error) {
return d.readUInt32()
}
Expand Down Expand Up @@ -159,7 +380,7 @@ func (d *Decoder) readUInt32() (uint32, error) {
if value > uint32SafeToMultiply10 {
value2 := (value << 3) + (value << 1) + uint32(ind)
if value2 < value {
return 0, errors.New("overflow")
return 0, errOverflow
}
value = value2
continue
Expand All @@ -176,7 +397,7 @@ func (d *Decoder) readUInt32() (uint32, error) {
}
}

// Int64 read int64
// Int64 reads int64.
func (d *Decoder) Int64() (int64, error) {
c, err := d.byte()
if err != nil {
Expand Down Expand Up @@ -206,7 +427,7 @@ func (d *Decoder) Int64() (int64, error) {
return int64(val), nil
}

// UInt64 read uint64
// UInt64 reads uint64.
func (d *Decoder) UInt64() (uint64, error) {
c, err := d.byte()
if err != nil {
Expand Down Expand Up @@ -286,7 +507,7 @@ func (d *Decoder) readUInt64(c byte) (uint64, error) {
if value > uint64SafeToMultiple10 {
value2 := (value << 3) + (value << 1) + uint64(ind)
if value2 < value {
return 0, errors.New("overflow")
return 0, errOverflow
}
value = value2
continue
Expand Down
27 changes: 27 additions & 0 deletions dec_int_fuzz_test.go
@@ -0,0 +1,27 @@
package jx

import (
"strconv"
"testing"

"github.com/stretchr/testify/require"
"golang.org/x/exp/constraints"
)

func fuzzCallback[Int constraints.Signed](decoder func(*Decoder) (Int, error)) func(*testing.T, Int) {
return func(t *testing.T, expected Int) {
a := require.New(t)
buf := make([]byte, 0, 32)
buf = strconv.AppendInt(buf, int64(expected), 10)

d := DecodeBytes(buf)
got, err := decoder(d)
a.NoError(err)
a.Equal(expected, got)
}
}

func FuzzDecoderInt8(f *testing.F) { f.Fuzz(fuzzCallback[int8]((*Decoder).Int8)) }
func FuzzDecoderInt16(f *testing.F) { f.Fuzz(fuzzCallback[int16]((*Decoder).Int16)) }
func FuzzDecoderInt32(f *testing.F) { f.Fuzz(fuzzCallback[int32]((*Decoder).Int32)) }
func FuzzDecoderInt64(f *testing.F) { f.Fuzz(fuzzCallback[int64]((*Decoder).Int64)) }

0 comments on commit e2ecde9

Please sign in to comment.