mirror of
https://github.com/json-iterator/go.git
synced 2025-01-05 12:50:34 +02:00
fix negative number to uint
This commit is contained in:
parent
ac8dd56dfb
commit
712ddb1942
@ -1,8 +1,6 @@
|
||||
package jsoniter
|
||||
|
||||
import (
|
||||
"unsafe"
|
||||
)
|
||||
import "unsafe"
|
||||
|
||||
type numberLazyAny struct {
|
||||
baseAny
|
||||
|
@ -21,6 +21,9 @@ func init() {
|
||||
}
|
||||
|
||||
func (iter *Iterator) ReadUint() uint {
|
||||
if iter.buf[iter.head] == '-' && len(iter.buf) > 1 {
|
||||
iter.buf = iter.buf[1:]
|
||||
}
|
||||
return uint(iter.ReadUint64())
|
||||
}
|
||||
|
||||
@ -104,6 +107,9 @@ func (iter *Iterator) ReadInt32() (ret int32) {
|
||||
}
|
||||
|
||||
func (iter *Iterator) ReadUint32() (ret uint32) {
|
||||
if iter.buf[iter.head] == '-' && len(iter.buf) > 1 {
|
||||
iter.buf = iter.buf[1:]
|
||||
}
|
||||
return iter.readUint32(iter.nextToken())
|
||||
}
|
||||
|
||||
@ -215,6 +221,9 @@ func (iter *Iterator) ReadInt64() (ret int64) {
|
||||
}
|
||||
|
||||
func (iter *Iterator) ReadUint64() uint64 {
|
||||
if iter.buf[iter.head] == '-' && len(iter.buf) > 1 {
|
||||
iter.buf = iter.buf[1:]
|
||||
}
|
||||
return iter.readUint64(iter.nextToken())
|
||||
}
|
||||
|
||||
|
@ -12,7 +12,7 @@ var intConvertMap = map[string]int{
|
||||
"321.1": 321,
|
||||
"-321.1": -321,
|
||||
`"1.1"`: 1,
|
||||
`"-1.1"`: -1,
|
||||
`"-321.1"`: -321,
|
||||
"0.0": 0,
|
||||
"0": 0,
|
||||
`"0"`: 0,
|
||||
@ -58,13 +58,15 @@ func Test_read_any_to_int(t *testing.T) {
|
||||
}
|
||||
|
||||
var uintConvertMap = map[string]int{
|
||||
|
||||
"321.1": 321,
|
||||
`"1.1"`: 1,
|
||||
`"-1.1"`: 1,
|
||||
`"-123.1"`: 123,
|
||||
"0.0": 0,
|
||||
"0": 0,
|
||||
`"0"`: 0,
|
||||
`"0.0"`: 0,
|
||||
`"00.0"`: 0,
|
||||
"true": 1,
|
||||
"false": 0,
|
||||
`"true"`: 0,
|
||||
@ -75,12 +77,13 @@ var uintConvertMap = map[string]int{
|
||||
`""`: 0,
|
||||
"+": 0,
|
||||
"-": 0,
|
||||
".": 0,
|
||||
"[]": 0,
|
||||
"[1,2]": 1,
|
||||
"{}": 0,
|
||||
// TODO need to solve
|
||||
//"-1.1": 1,
|
||||
//"-321.1": 321,
|
||||
"{1,2}": 0,
|
||||
"-1.1": 1,
|
||||
"-321.1": 321,
|
||||
}
|
||||
|
||||
func Test_read_any_to_uint(t *testing.T) {
|
||||
@ -98,7 +101,7 @@ func Test_read_any_to_uint(t *testing.T) {
|
||||
|
||||
for k, v := range uintConvertMap {
|
||||
any := Get([]byte(k))
|
||||
should.Equal(uint32(v), any.ToUint32(), fmt.Sprintf("origin val %v", k))
|
||||
should.Equal(uint(v), any.ToUint(), fmt.Sprintf("origin val %v", k))
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user