go-json/decode_bool.go

54 lines
1.3 KiB
Go
Raw Normal View History

2020-04-23 19:39:20 +03:00
package json
import (
"errors"
"unsafe"
)
type boolDecoder struct{}
func newBoolDecoder() *boolDecoder {
return &boolDecoder{}
}
2020-05-06 20:37:29 +03:00
func (d *boolDecoder) decode(buf []byte, cursor int, p uintptr) (int, error) {
buflen := len(buf)
cursor = skipWhiteSpace(buf, cursor)
2020-04-23 19:39:20 +03:00
switch buf[cursor] {
case 't':
2020-05-06 20:37:29 +03:00
if cursor+3 >= buflen {
return 0, errors.New("unexpected error. invalid bool character")
2020-04-23 19:39:20 +03:00
}
if buf[cursor+1] != 'r' {
2020-05-06 20:37:29 +03:00
return 0, errors.New("unexpected error. invalid bool character")
2020-04-23 19:39:20 +03:00
}
if buf[cursor+2] != 'u' {
2020-05-06 20:37:29 +03:00
return 0, errors.New("unexpected error. invalid bool character")
2020-04-23 19:39:20 +03:00
}
if buf[cursor+3] != 'e' {
2020-05-06 20:37:29 +03:00
return 0, errors.New("unexpected error. invalid bool character")
2020-04-23 19:39:20 +03:00
}
2020-05-06 20:37:29 +03:00
cursor += 4
2020-04-23 19:39:20 +03:00
*(*bool)(unsafe.Pointer(p)) = true
case 'f':
2020-05-06 20:37:29 +03:00
if cursor+4 >= buflen {
return 0, errors.New("unexpected error. invalid bool character")
2020-04-23 19:39:20 +03:00
}
if buf[cursor+1] != 'a' {
2020-05-06 20:37:29 +03:00
return 0, errors.New("unexpected error. invalid bool character")
2020-04-23 19:39:20 +03:00
}
if buf[cursor+2] != 'l' {
2020-05-06 20:37:29 +03:00
return 0, errors.New("unexpected error. invalid bool character")
2020-04-23 19:39:20 +03:00
}
if buf[cursor+3] != 's' {
2020-05-06 20:37:29 +03:00
return 0, errors.New("unexpected error. invalid bool character")
2020-04-23 19:39:20 +03:00
}
if buf[cursor+4] != 'e' {
2020-05-06 20:37:29 +03:00
return 0, errors.New("unexpected error. invalid bool character")
2020-04-23 19:39:20 +03:00
}
2020-05-06 20:37:29 +03:00
cursor += 5
2020-04-23 19:39:20 +03:00
*(*bool)(unsafe.Pointer(p)) = false
}
2020-05-06 20:37:29 +03:00
return cursor, nil
2020-04-23 19:39:20 +03:00
}