diff options
author | Luke Shumaker <lukeshu@datawire.io> | 2022-08-17 00:48:11 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@datawire.io> | 2022-08-17 00:48:11 -0600 |
commit | 6b11bd03dd572a4c1b5bb2a72da5a556c7998c56 (patch) | |
tree | 2f5dbc5dbfd88c6b2f689fcad2be9950064e7538 /decode.go | |
parent | 111cdaffb625f7db9b8c5a27e386999944346e0d (diff) |
decode: Don't be greedy unless we're looking for EOF
Diffstat (limited to 'decode.go')
-rw-r--r-- | decode.go | 33 |
1 files changed, 17 insertions, 16 deletions
@@ -130,15 +130,25 @@ func DecodeThenEOF(r interface { if err := dec.Decode(ptr); err != nil { return err } - if c, _, err := r.ReadRune(); err == nil { - return &DecodeError{ - Err: &DecodeSyntaxError{ - Err: fmt.Errorf("invalid character %q after top-level value", c), - Offset: dec.InputOffset(), - }, + for { + c, s, t, _ := dec.io.ReadRuneType() + switch t { + case RuneTypeSpace: + // do nothing + case RuneTypeEOF: + if s > 0 { + return &DecodeError{ + Err: &DecodeSyntaxError{ + Err: fmt.Errorf("invalid character %q after top-level value", c), + Offset: dec.InputOffset(), + }, + } + } + return nil + default: + panic("should not happen") } } - return nil } func (dec *Decoder) Decode(ptr any) (err error) { @@ -167,15 +177,6 @@ func (dec *Decoder) Decode(ptr any) (err error) { } }() dec.decode(ptrVal.Elem(), false) - for { - _, s, t, _ := dec.io.ReadRuneType() - if t == RuneTypeEOF || t == RuneTypeError { - if s > 0 { - dec.io.UnreadRune() - } - break - } - } return nil } |