Commit ba108c93 authored by Francesco Renzi's avatar Francesco Renzi Committed by Ian Lance Taylor

encoding/json: add Decoder.InputOffset for offset access

This makes Decoder.offset public while renaming it to
Decoder.InputOffset to match encoding/xml Decoder API

Code changes made by Adam Stankiewicz [sheerun@sher.pl]

Fixes #29688

Change-Id: I86dbfd2b2da80160846e92bfa580c53d8d45e2db
Reviewed-on: https://go-review.googlesource.com/c/go/+/200677
Run-TryBot: Johan Brandhorst <johan.brandhorst@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent ac8966aa
...@@ -56,7 +56,7 @@ func (dec *Decoder) Decode(v interface{}) error { ...@@ -56,7 +56,7 @@ func (dec *Decoder) Decode(v interface{}) error {
} }
if !dec.tokenValueAllowed() { if !dec.tokenValueAllowed() {
return &SyntaxError{msg: "not at beginning of value", Offset: dec.offset()} return &SyntaxError{msg: "not at beginning of value", Offset: dec.InputOffset()}
} }
// Read whole value into buffer. // Read whole value into buffer.
...@@ -314,7 +314,7 @@ func (dec *Decoder) tokenPrepareForDecode() error { ...@@ -314,7 +314,7 @@ func (dec *Decoder) tokenPrepareForDecode() error {
return err return err
} }
if c != ',' { if c != ',' {
return &SyntaxError{"expected comma after array element", dec.offset()} return &SyntaxError{"expected comma after array element", dec.InputOffset()}
} }
dec.scanp++ dec.scanp++
dec.tokenState = tokenArrayValue dec.tokenState = tokenArrayValue
...@@ -324,7 +324,7 @@ func (dec *Decoder) tokenPrepareForDecode() error { ...@@ -324,7 +324,7 @@ func (dec *Decoder) tokenPrepareForDecode() error {
return err return err
} }
if c != ':' { if c != ':' {
return &SyntaxError{"expected colon after object key", dec.offset()} return &SyntaxError{"expected colon after object key", dec.InputOffset()}
} }
dec.scanp++ dec.scanp++
dec.tokenState = tokenObjectValue dec.tokenState = tokenObjectValue
...@@ -477,7 +477,7 @@ func (dec *Decoder) tokenError(c byte) (Token, error) { ...@@ -477,7 +477,7 @@ func (dec *Decoder) tokenError(c byte) (Token, error) {
case tokenObjectComma: case tokenObjectComma:
context = " after object key:value pair" context = " after object key:value pair"
} }
return nil, &SyntaxError{"invalid character " + quoteChar(c) + context, dec.offset()} return nil, &SyntaxError{"invalid character " + quoteChar(c) + context, dec.InputOffset()}
} }
// More reports whether there is another element in the // More reports whether there is another element in the
...@@ -506,6 +506,9 @@ func (dec *Decoder) peek() (byte, error) { ...@@ -506,6 +506,9 @@ func (dec *Decoder) peek() (byte, error) {
} }
} }
func (dec *Decoder) offset() int64 { // InputOffset returns the input stream byte offset of the current decoder position.
// The offset gives the location of the end of the most recently returned token
// and the beginning of the next token.
func (dec *Decoder) InputOffset() int64 {
return dec.scanned + int64(dec.scanp) return dec.scanned + int64(dec.scanp)
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment