mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-01-14 09:44:39 +00:00
d2ece139c6
All unmarshaling error messages now contain line number and column information, except for the following errors: - `unexpected EOF` - `no support for proto1 MessageSets` - `required fields X not set` Changes to internal/encoding/json: - Moved encoding funcs in string.go and number.go into encode.go. - Separated out encoding kind constants from decoding ones. - Renamed file string.go to decode_string.go. - Renamed file number.go to decode_number.go. - Renamed Type struct to Kind. - Renamed Value struct to Token. - Token accessor methods no longer return error. Name, Bool, ParsedString will panic if called on the wrong kind. Float, Int, Uint has ok bool result to check against. - Changed Peek to return Token and error. Changes to encoding/protojson: - Updated internal/encoding/json API calls. - Added line info on most unmarshaling error messages and kept description simple and consistent. Change-Id: Ie50456694f2214c5c4fafd2c9b9239680da0deec Reviewed-on: https://go-review.googlesource.com/c/protobuf/+/218978 Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
92 lines
2.6 KiB
Go
92 lines
2.6 KiB
Go
// Copyright 2018 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package json
|
|
|
|
import (
|
|
"strconv"
|
|
"unicode"
|
|
"unicode/utf16"
|
|
"unicode/utf8"
|
|
|
|
"google.golang.org/protobuf/internal/strs"
|
|
)
|
|
|
|
func (d *Decoder) parseString(in []byte) (string, int, error) {
|
|
in0 := in
|
|
if len(in) == 0 {
|
|
return "", 0, ErrUnexpectedEOF
|
|
}
|
|
if in[0] != '"' {
|
|
return "", 0, d.newSyntaxError(d.currPos(), "invalid character %q at start of string", in[0])
|
|
}
|
|
in = in[1:]
|
|
i := indexNeedEscapeInBytes(in)
|
|
in, out := in[i:], in[:i:i] // set cap to prevent mutations
|
|
for len(in) > 0 {
|
|
switch r, n := utf8.DecodeRune(in); {
|
|
case r == utf8.RuneError && n == 1:
|
|
return "", 0, d.newSyntaxError(d.currPos(), "invalid UTF-8 in string")
|
|
case r < ' ':
|
|
return "", 0, d.newSyntaxError(d.currPos(), "invalid character %q in string", r)
|
|
case r == '"':
|
|
in = in[1:]
|
|
n := len(in0) - len(in)
|
|
return string(out), n, nil
|
|
case r == '\\':
|
|
if len(in) < 2 {
|
|
return "", 0, ErrUnexpectedEOF
|
|
}
|
|
switch r := in[1]; r {
|
|
case '"', '\\', '/':
|
|
in, out = in[2:], append(out, r)
|
|
case 'b':
|
|
in, out = in[2:], append(out, '\b')
|
|
case 'f':
|
|
in, out = in[2:], append(out, '\f')
|
|
case 'n':
|
|
in, out = in[2:], append(out, '\n')
|
|
case 'r':
|
|
in, out = in[2:], append(out, '\r')
|
|
case 't':
|
|
in, out = in[2:], append(out, '\t')
|
|
case 'u':
|
|
if len(in) < 6 {
|
|
return "", 0, ErrUnexpectedEOF
|
|
}
|
|
v, err := strconv.ParseUint(string(in[2:6]), 16, 16)
|
|
if err != nil {
|
|
return "", 0, d.newSyntaxError(d.currPos(), "invalid escape code %q in string", in[:6])
|
|
}
|
|
in = in[6:]
|
|
|
|
r := rune(v)
|
|
if utf16.IsSurrogate(r) {
|
|
if len(in) < 6 {
|
|
return "", 0, ErrUnexpectedEOF
|
|
}
|
|
v, err := strconv.ParseUint(string(in[2:6]), 16, 16)
|
|
r = utf16.DecodeRune(r, rune(v))
|
|
if in[0] != '\\' || in[1] != 'u' ||
|
|
r == unicode.ReplacementChar || err != nil {
|
|
return "", 0, d.newSyntaxError(d.currPos(), "invalid escape code %q in string", in[:6])
|
|
}
|
|
in = in[6:]
|
|
}
|
|
out = append(out, string(r)...)
|
|
default:
|
|
return "", 0, d.newSyntaxError(d.currPos(), "invalid escape code %q in string", in[:2])
|
|
}
|
|
default:
|
|
i := indexNeedEscapeInBytes(in[n:])
|
|
in, out = in[n+i:], append(out, in[:n+i]...)
|
|
}
|
|
}
|
|
return "", 0, ErrUnexpectedEOF
|
|
}
|
|
|
|
// indexNeedEscapeInBytes returns the index of the character that needs
|
|
// escaping. If no characters need escaping, this returns the input length.
|
|
func indexNeedEscapeInBytes(b []byte) int { return indexNeedEscapeInString(strs.UnsafeString(b)) }
|