Skip to content

Commit

Permalink
hujson: support single and multi-line comments
Browse files Browse the repository at this point in the history
  • Loading branch information
crawshaw committed Sep 29, 2019
1 parent e2a9696 commit 3254e3b
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 61 deletions.
8 changes: 4 additions & 4 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ func (d *decodeState) skip() {
s, data, i := &d.scan, d.data, d.off
depth := len(s.parseState)
for {
op := s.step(s, data[i])
op := s.step(s, data[i], peekAfter(data, i))
i++
if len(s.parseState) < depth {
d.off = i
Expand All @@ -334,7 +334,7 @@ func (d *decodeState) skip() {
// scanNext processes the byte at d.data[d.off].
func (d *decodeState) scanNext() {
if d.off < len(d.data) {
d.opcode = d.scan.step(&d.scan, d.data[d.off])
d.opcode = d.scan.step(&d.scan, d.data[d.off], peekAfter(d.data, d.off))
d.off++
} else {
d.opcode = d.scan.eof()
Expand All @@ -347,7 +347,7 @@ func (d *decodeState) scanNext() {
func (d *decodeState) scanWhile(op int) {
s, data, i := &d.scan, d.data, d.off
for i < len(data) {
newOp := s.step(s, data[i])
newOp := s.step(s, data[i], peekAfter(data, i))
i++
if newOp != op {
d.opcode = newOp
Expand Down Expand Up @@ -399,7 +399,7 @@ Switch:
i += len("ull")
}
if i < len(data) {
d.opcode = stateEndValue(&d.scan, data[i])
d.opcode = stateEndValue(&d.scan, data[i], peekAfter(data, i))
} else {
d.opcode = scanEnd
}
Expand Down
46 changes: 46 additions & 0 deletions hujson_decode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package hujson

import (
"bytes"
"reflect"
"testing"
)

var hujsonDecodeTests = []unmarshalTest{
{ptr: new(int), in: "// comment\n7", out: 7},
{ptr: new(T), in: `// leading comment
{"X": "xval"}
// trailing comment`, out: T{X: "xval"}},
{ptr: new(T), in: `{
"X": "xval" // trailing-line comment
}`, out: T{X: "xval"}},
{ptr: new(T), in: `{
"Y": 7,
/* multi-line comment
"Y": 8,
*/
"X": /* comment between field name and value */ "x"
}`, disallowUnknownFields: false, out: T{Y: 7, X: "x"}},
}

func TestHuDecode(t *testing.T) {
for _, tt := range hujsonDecodeTests {
t.Run(tt.in, func(t *testing.T) {
in := []byte(tt.in)
v := reflect.New(reflect.TypeOf(tt.ptr).Elem())
dec := NewDecoder(bytes.NewReader(in))
if tt.disallowUnknownFields {
dec.DisallowUnknownFields()
}
if err := dec.Decode(v.Interface()); !equalError(err, tt.err) {
t.Errorf("err=%v, want %v", err, tt.err)
return
} else if err != nil {
return
}
if !reflect.DeepEqual(v.Elem().Interface(), tt.out) {
t.Errorf("mismatch\nhave: %#+v\nwant: %#+v", v.Elem().Interface(), tt.out)
}
})
}
}
6 changes: 3 additions & 3 deletions indent.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func compact(dst *bytes.Buffer, src []byte, escape bool) error {
dst.WriteByte(hex[src[i+2]&0xF])
start = i + 3
}
v := scan.step(&scan, c)
v := scan.step(&scan, c, peekAfter(src, i))
if v >= scanSkipSpace {
if v == scanError {
break
Expand Down Expand Up @@ -82,9 +82,9 @@ func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error {
scan.reset()
needIndent := false
depth := 0
for _, c := range src {
for i, c := range src {
scan.bytes++
v := scan.step(&scan, c)
v := scan.step(&scan, c, peekAfter(src, i))
if v == scanSkipSpace {
continue
}
Expand Down
Loading

0 comments on commit 3254e3b

Please sign in to comment.