Fix parsing bugs + boolean impl

This commit is contained in:
Thomas Pelletier
2021-02-06 08:54:40 -05:00
parent a466f0ca79
commit 540c2a7b59
3 changed files with 68 additions and 36 deletions
+22 -3
View File
@@ -23,6 +23,25 @@ var scanFollowsFalse = scanFollows([]byte{'f', 'a', 'l', 's', 'e'})
var scanFollowsArrayTableBegin = scanFollows([]byte{arrayOrTableBegin, arrayOrTableBegin})
var scanFollowsArrayTableEnd = scanFollows([]byte{arrayOrTableEnd, arrayOrTableEnd})
func scanNewline(b []byte) ([]byte, []byte, error) {
if len(b) == 0 {
return nil, nil, fmt.Errorf("not enough bytes for new line")
}
if b[0] == '\n' {
return b[:1], b[1:], nil
}
if b[0] == '\r' {
if len(b) < 2 {
return nil, nil, fmt.Errorf("not enough bytes for windows newline")
}
if b[1] == '\n' {
return b[:2], b[2:], nil
}
return nil, nil, unexpectedCharacter{r: '\n', b: b[2:]}
}
return nil, nil, unexpectedCharacter{b: b}
}
const (
dot = '.'
equal = '='
@@ -94,7 +113,7 @@ func scan(b []byte) ([]byte, []byte, error) {
func scanUnquotedKey(b []byte) ([]byte, []byte, error) {
//unquoted-key = 1*( ALPHA / DIGIT / %x2D / %x5F ) ; A-Z / a-z / 0-9 / - / _
for i := 1; i < len(b); i++ {
for i := 0; i < len(b); i++ {
if !isUnquotedKeyChar(b[i]) {
return b[:i], b[i:], nil
}
@@ -153,7 +172,7 @@ func scanWindowsNewline(b []byte) ([]byte, []byte, error) {
}
func scanWhitespace(b []byte) ([]byte, []byte) {
for i := 1; i < len(b); i++ {
for i := 0; i < len(b); i++ {
switch b[i] {
case ' ', '\t':
continue
@@ -176,7 +195,7 @@ func scanComment(b []byte) ([]byte, []byte, error) {
for i := 1; i < len(b); i++ {
switch b[i] {
case '\n':
return b[:i+1], b[i+1:], nil
return b[:i], b[i:], nil
}
}
return b, nil, nil