go-json/decode_bool.go

154 lines
3.6 KiB
Go
Raw Normal View History

2020-04-23 19:39:20 +03:00
package json
import (
"unsafe"
)
type boolDecoder struct {
structName string
fieldName string
}
2020-04-23 19:39:20 +03:00
func newBoolDecoder(structName, fieldName string) *boolDecoder {
return &boolDecoder{structName: structName, fieldName: fieldName}
2020-04-23 19:39:20 +03:00
}
2020-07-30 16:41:53 +03:00
func trueBytes(s *stream) error {
2020-07-31 11:10:03 +03:00
if s.cursor+3 >= s.length {
if !s.read() {
return errInvalidCharacter(s.char(), "bool(true)", s.totalOffset())
}
}
s.cursor++
2020-07-30 16:41:53 +03:00
if s.char() != 'r' {
return errInvalidCharacter(s.char(), "bool(true)", s.totalOffset())
}
2020-07-31 11:10:03 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
if s.char() != 'u' {
return errInvalidCharacter(s.char(), "bool(true)", s.totalOffset())
}
2020-07-31 11:10:03 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
if s.char() != 'e' {
return errInvalidCharacter(s.char(), "bool(true)", s.totalOffset())
}
2020-07-31 11:10:03 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
return nil
}
func falseBytes(s *stream) error {
2020-07-31 11:10:03 +03:00
if s.cursor+4 >= s.length {
2020-08-12 19:21:10 +03:00
if !s.read() {
2020-07-31 11:10:03 +03:00
return errInvalidCharacter(s.char(), "bool(false)", s.totalOffset())
}
}
s.cursor++
2020-07-30 16:41:53 +03:00
if s.char() != 'a' {
return errInvalidCharacter(s.char(), "bool(false)", s.totalOffset())
}
2020-07-31 11:10:03 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
if s.char() != 'l' {
return errInvalidCharacter(s.char(), "bool(false)", s.totalOffset())
}
2020-07-31 11:10:03 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
if s.char() != 's' {
return errInvalidCharacter(s.char(), "bool(false)", s.totalOffset())
}
2020-07-31 11:10:03 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
if s.char() != 'e' {
return errInvalidCharacter(s.char(), "bool(false)", s.totalOffset())
}
2020-07-31 11:10:03 +03:00
s.cursor++
2020-07-30 16:41:53 +03:00
return nil
}
2020-11-19 06:47:42 +03:00
func (d *boolDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
2020-07-30 16:41:53 +03:00
s.skipWhiteSpace()
2020-07-31 11:10:03 +03:00
for {
switch s.char() {
case 't':
if err := trueBytes(s); err != nil {
return err
}
**(**bool)(unsafe.Pointer(&p)) = true
2020-07-31 11:10:03 +03:00
return nil
case 'f':
if err := falseBytes(s); err != nil {
return err
}
**(**bool)(unsafe.Pointer(&p)) = false
2020-07-31 11:10:03 +03:00
return nil
2020-12-24 12:30:11 +03:00
case 'n':
if err := nullBytes(s); err != nil {
return err
}
2021-02-16 19:51:42 +03:00
return nil
2020-07-31 11:10:03 +03:00
case nul:
if s.read() {
continue
}
goto ERROR
2020-07-30 16:41:53 +03:00
}
2020-07-31 11:10:03 +03:00
break
2020-07-30 16:41:53 +03:00
}
2020-07-31 11:10:03 +03:00
ERROR:
2020-07-30 16:41:53 +03:00
return errUnexpectedEndOfJSON("bool", s.totalOffset())
}
2020-11-19 06:47:42 +03:00
func (d *boolDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
2020-05-23 06:51:09 +03:00
buflen := int64(len(buf))
2020-05-06 20:37:29 +03:00
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 {
2020-05-23 06:51:09 +03:00
return 0, errUnexpectedEndOfJSON("bool(true)", cursor)
2020-04-23 19:39:20 +03:00
}
if buf[cursor+1] != 'r' {
2020-05-23 06:51:09 +03:00
return 0, errInvalidCharacter(buf[cursor+1], "bool(true)", cursor)
2020-04-23 19:39:20 +03:00
}
if buf[cursor+2] != 'u' {
2020-05-23 06:51:09 +03:00
return 0, errInvalidCharacter(buf[cursor+2], "bool(true)", cursor)
2020-04-23 19:39:20 +03:00
}
if buf[cursor+3] != 'e' {
2020-05-23 06:51:09 +03:00
return 0, errInvalidCharacter(buf[cursor+3], "bool(true)", cursor)
2020-04-23 19:39:20 +03:00
}
2020-05-06 20:37:29 +03:00
cursor += 4
**(**bool)(unsafe.Pointer(&p)) = true
2020-07-30 16:41:53 +03:00
return cursor, nil
2020-04-23 19:39:20 +03:00
case 'f':
2020-05-06 20:37:29 +03:00
if cursor+4 >= buflen {
2020-05-23 06:51:09 +03:00
return 0, errUnexpectedEndOfJSON("bool(false)", cursor)
2020-04-23 19:39:20 +03:00
}
if buf[cursor+1] != 'a' {
2020-05-23 06:51:09 +03:00
return 0, errInvalidCharacter(buf[cursor+1], "bool(false)", cursor)
2020-04-23 19:39:20 +03:00
}
if buf[cursor+2] != 'l' {
2020-05-23 06:51:09 +03:00
return 0, errInvalidCharacter(buf[cursor+2], "bool(false)", cursor)
2020-04-23 19:39:20 +03:00
}
if buf[cursor+3] != 's' {
2020-05-23 06:51:09 +03:00
return 0, errInvalidCharacter(buf[cursor+3], "bool(false)", cursor)
2020-04-23 19:39:20 +03:00
}
if buf[cursor+4] != 'e' {
2020-05-23 06:51:09 +03:00
return 0, errInvalidCharacter(buf[cursor+4], "bool(false)", cursor)
2020-04-23 19:39:20 +03:00
}
2020-05-06 20:37:29 +03:00
cursor += 5
**(**bool)(unsafe.Pointer(&p)) = false
2020-07-30 16:41:53 +03:00
return cursor, nil
2020-12-24 12:30:11 +03:00
case 'n':
if cursor+3 >= buflen {
return 0, errUnexpectedEndOfJSON("null", cursor)
}
if buf[cursor+1] != 'u' {
return 0, errInvalidCharacter(buf[cursor+1], "null", cursor)
}
if buf[cursor+2] != 'l' {
return 0, errInvalidCharacter(buf[cursor+2], "null", cursor)
}
if buf[cursor+3] != 'l' {
return 0, errInvalidCharacter(buf[cursor+3], "null", cursor)
}
cursor += 4
return cursor, nil
2020-04-23 19:39:20 +03:00
}
2020-07-30 16:41:53 +03:00
return 0, errUnexpectedEndOfJSON("bool", cursor)
2020-04-23 19:39:20 +03:00
}