-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
decode,interp: Add arbitrary large integer support (BigInt)
Was already handled in fq in various places as gojq uses them Update msgpack to support negative integers that can't represented as int64 Rename read try* number functions to make them more explicit
- Loading branch information
Showing
15 changed files
with
1,882 additions
and
1,209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package num | ||
|
||
import "math/big" | ||
|
||
var BigIntOne = big.NewInt(1) | ||
|
||
func BigIntSetBytesSigned(n *big.Int, buf []byte) *big.Int { | ||
n.SetBytes(buf) | ||
if len(buf) > 0 && buf[0]&0x80 > 0 { | ||
n.Sub(n, new(big.Int).Lsh(BigIntOne, uint(len(buf))*8)) | ||
} | ||
return n | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package num_test | ||
|
||
import ( | ||
"fmt" | ||
"math/big" | ||
"testing" | ||
|
||
"github.com/wader/fq/internal/num" | ||
) | ||
|
||
func TestBigIntSetBytesSigned(t *testing.T) { | ||
testCases := []struct { | ||
buf []byte | ||
s string | ||
}{ | ||
{[]byte{1}, "1"}, | ||
{[]byte{0b1111_1111}, "-1"}, | ||
{[]byte{0b1000_0000}, "-128"}, | ||
{[]byte{0b0111_1111}, "127"}, | ||
{[]byte{0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, "9223372036854775807"}, | ||
{[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, "-1"}, | ||
{[]byte{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, "-9223372036854775808"}, | ||
{[]byte{0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, "2361183241434822606847"}, | ||
{[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, "-1"}, | ||
{[]byte{0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, "-2361183241434822606848"}, | ||
} | ||
for _, tC := range testCases { | ||
t.Run(fmt.Sprintf("%v %s", tC.buf, tC.s), func(t *testing.T) { | ||
var n big.Int | ||
expected := tC.s | ||
actual := num.BigIntSetBytesSigned(&n, tC.buf).String() | ||
if expected != actual { | ||
t.Errorf("expected %s, got %s", expected, actual) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.