You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
1.3 KiB
Go
69 lines
1.3 KiB
Go
2 years ago
|
package stt
|
||
|
|
||
|
import "fmt"
|
||
|
|
||
|
// ReadNil reads a json object as nil and
|
||
|
// returns whether it's a nil or not
|
||
|
func (iter *Iterator) ReadNil() (ret bool) {
|
||
|
c := iter.nextToken()
|
||
|
if c == 'n' {
|
||
|
iter.skipBytes('u', 'l', 'l') // null
|
||
|
return true
|
||
|
}
|
||
|
iter.unreadByte()
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
// ReadBool reads t/true/f/false/1/0 as BoolValue
|
||
|
func (iter *Iterator) ReadBool() (ret bool) {
|
||
|
c := iter.nextToken()
|
||
|
if c == 't' {
|
||
|
iter.skipBytes('r', 'u', 'e')
|
||
|
return true
|
||
|
}
|
||
|
if c == 'f' {
|
||
|
iter.skipBytes('a', 'l', 's', 'e')
|
||
|
return false
|
||
|
}
|
||
|
if c == '0' {
|
||
|
return false
|
||
|
}
|
||
|
if c == '1' {
|
||
|
return true
|
||
|
}
|
||
|
iter.ReportError("ReadBool", "expect t/true/1 or f/false/0, but found "+string([]byte{c}))
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// Skip skips an object and positions to relatively the next object
|
||
|
func (iter *Iterator) Skip() {
|
||
|
iter.skipString()
|
||
|
}
|
||
|
|
||
|
func (iter *Iterator) skipString() {
|
||
|
if !iter.trySkipString() {
|
||
|
iter.unreadByte()
|
||
|
iter.ReadString()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (iter *Iterator) trySkipString() bool {
|
||
|
for i := iter.head; i < iter.tail; i++ {
|
||
|
c := iter.buf[i]
|
||
|
if c == '/' {
|
||
|
iter.head = i
|
||
|
return true // valid
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
func (iter *Iterator) skipBytes(bytes ...byte) {
|
||
|
for _, b := range bytes {
|
||
|
if iter.readByte() != b {
|
||
|
iter.ReportError("skipBytes", fmt.Sprintf("expect %s", string(bytes)))
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
}
|