diff --git a/encoding/prototext/decode.go b/encoding/prototext/decode.go index 5e9975da..9dea257d 100644 --- a/encoding/prototext/decode.go +++ b/encoding/prototext/decode.go @@ -6,7 +6,6 @@ package prototext import ( "fmt" - "io" "strings" "unicode/utf8" @@ -131,7 +130,7 @@ func (d decoder) unmarshalMessage(m pref.Message, checkDelims bool) error { // Continue below. case text.EOF: if checkDelims { - return io.ErrUnexpectedEOF + return text.ErrUnexpectedEOF } return nil default: diff --git a/internal/encoding/text/decode.go b/internal/encoding/text/decode.go index 074e849d..eb10ea10 100644 --- a/internal/encoding/text/decode.go +++ b/internal/encoding/text/decode.go @@ -45,6 +45,9 @@ func NewDecoder(b []byte) *Decoder { return &Decoder{orig: b, in: b} } +// ErrUnexpectedEOF means that EOF was encountered in the middle of the input. +var ErrUnexpectedEOF = errors.New("%v", io.ErrUnexpectedEOF) + // call specifies which Decoder method was invoked. type call uint8 @@ -70,7 +73,7 @@ func (d *Decoder) Read() (Token, error) { return d.lastToken, d.lastErr } - tok, err := d.parseNext(d.lastToken.Kind()) + tok, err := d.parseNext(d.lastToken.kind) if err != nil { return Token{}, err } @@ -114,7 +117,7 @@ func (d *Decoder) parseNext(lastKind Kind) (Token, error) { case Name: // Next token can be MessageOpen, ListOpen or Scalar. if isEOF { - return Token{}, io.ErrUnexpectedEOF + return Token{}, ErrUnexpectedEOF } switch ch := d.in[0]; ch { case '{', '<': @@ -148,7 +151,7 @@ func (d *Decoder) parseNext(lastKind Kind) (Token, error) { case MessageOpen: // Next token can be MessageClose, comma, semicolon or Name. if isEOF { - return Token{}, io.ErrUnexpectedEOF + return Token{}, ErrUnexpectedEOF } switch ch := d.in[0]; ch { case closeCh: @@ -167,7 +170,7 @@ func (d *Decoder) parseNext(lastKind Kind) (Token, error) { case ListOpen: // Next token can be ListClose or comma. if isEOF { - return Token{}, io.ErrUnexpectedEOF + return Token{}, ErrUnexpectedEOF } switch ch := d.in[0]; ch { case ']': @@ -183,7 +186,7 @@ func (d *Decoder) parseNext(lastKind Kind) (Token, error) { case MessageOpen: // Next token can be MessageClose or Name. if isEOF { - return Token{}, io.ErrUnexpectedEOF + return Token{}, ErrUnexpectedEOF } _, closeCh := d.currentOpenKind() switch ch := d.in[0]; ch { @@ -217,7 +220,7 @@ func (d *Decoder) parseNext(lastKind Kind) (Token, error) { case MessageOpen: // Next token can be MessageClose, comma, semicolon or Name. if isEOF { - return Token{}, io.ErrUnexpectedEOF + return Token{}, ErrUnexpectedEOF } switch ch := d.in[0]; ch { case closeCh: @@ -236,7 +239,7 @@ func (d *Decoder) parseNext(lastKind Kind) (Token, error) { case ListOpen: // Next token can be ListClose or comma if isEOF { - return Token{}, io.ErrUnexpectedEOF + return Token{}, ErrUnexpectedEOF } switch ch := d.in[0]; ch { case closeCh: @@ -252,7 +255,7 @@ func (d *Decoder) parseNext(lastKind Kind) (Token, error) { case ListOpen: // Next token can be ListClose, MessageStart or Scalar. if isEOF { - return Token{}, io.ErrUnexpectedEOF + return Token{}, ErrUnexpectedEOF } switch ch := d.in[0]; ch { case ']': @@ -286,7 +289,7 @@ func (d *Decoder) parseNext(lastKind Kind) (Token, error) { case MessageOpen: // Next token can be MessageClose, comma, semicolon or Name. if isEOF { - return Token{}, io.ErrUnexpectedEOF + return Token{}, ErrUnexpectedEOF } switch ch := d.in[0]; ch { case closeCh: @@ -319,7 +322,7 @@ func (d *Decoder) parseNext(lastKind Kind) (Token, error) { case MessageOpen: // Next token can be MessageClose or Name. if isEOF { - return Token{}, io.ErrUnexpectedEOF + return Token{}, ErrUnexpectedEOF } switch ch := d.in[0]; ch { case closeCh: @@ -340,7 +343,7 @@ func (d *Decoder) parseNext(lastKind Kind) (Token, error) { } // Next token can be MessageOpen or Scalar. if isEOF { - return Token{}, io.ErrUnexpectedEOF + return Token{}, ErrUnexpectedEOF } switch ch := d.in[0]; ch { case '{', '<': @@ -432,7 +435,7 @@ func (d *Decoder) parseTypeName() (Token, error) { // Caller already checks for [ as first character. s := consume(d.in[1:], 0) if len(s) == 0 { - return Token{}, io.ErrUnexpectedEOF + return Token{}, ErrUnexpectedEOF } var name []byte @@ -470,7 +473,7 @@ func (d *Decoder) parseTypeName() (Token, error) { } if !closed { - return Token{}, io.ErrUnexpectedEOF + return Token{}, ErrUnexpectedEOF } // First character cannot be '.'. Last character cannot be '.' or '/'. diff --git a/internal/encoding/text/decode_string.go b/internal/encoding/text/decode_string.go index 020c8c70..d4d34902 100644 --- a/internal/encoding/text/decode_string.go +++ b/internal/encoding/text/decode_string.go @@ -6,7 +6,6 @@ package text import ( "bytes" - "io" "strconv" "strings" "unicode" @@ -51,7 +50,7 @@ func (d *Decoder) parseStringValue() (Token, error) { func (d *Decoder) parseString() (string, error) { in := d.in if len(in) == 0 { - return "", io.ErrUnexpectedEOF + return "", ErrUnexpectedEOF } quote := in[0] in = in[1:] @@ -69,7 +68,7 @@ func (d *Decoder) parseString() (string, error) { return string(out), nil case r == '\\': if len(in) < 2 { - return "", io.ErrUnexpectedEOF + return "", ErrUnexpectedEOF } switch r := in[1]; r { case '"', '\'', '\\', '?': @@ -117,7 +116,7 @@ func (d *Decoder) parseString() (string, error) { n = 10 } if len(in) < n { - return "", io.ErrUnexpectedEOF + return "", ErrUnexpectedEOF } v, err := strconv.ParseUint(string(in[2:n]), 16, 32) if utf8.MaxRune < v || err != nil { @@ -128,7 +127,7 @@ func (d *Decoder) parseString() (string, error) { r := rune(v) if utf16.IsSurrogate(r) { if len(in) < 6 { - return "", io.ErrUnexpectedEOF + return "", ErrUnexpectedEOF } v, err := strconv.ParseUint(string(in[2:6]), 16, 16) r = utf16.DecodeRune(r, rune(v)) @@ -146,7 +145,7 @@ func (d *Decoder) parseString() (string, error) { in, out = in[n+i:], append(out, in[:n+i]...) } } - return "", io.ErrUnexpectedEOF + return "", ErrUnexpectedEOF } // indexNeedEscapeInString returns the index of the character that needs diff --git a/internal/encoding/text/decode_test.go b/internal/encoding/text/decode_test.go index 6ad31ce9..4749f8c5 100644 --- a/internal/encoding/text/decode_test.go +++ b/internal/encoding/text/decode_test.go @@ -6,7 +6,6 @@ package text_test import ( "fmt" - "io" "math" "strings" "testing" @@ -17,7 +16,7 @@ import ( "google.golang.org/protobuf/internal/flags" ) -var eofErr = io.ErrUnexpectedEOF.Error() +var eofErr = text.ErrUnexpectedEOF.Error() type R struct { // K is expected Kind of the returned Token object from calling Decoder.Read. diff --git a/internal/encoding/text/decode_token.go b/internal/encoding/text/decode_token.go index b7b4e2ff..83d2b0d5 100644 --- a/internal/encoding/text/decode_token.go +++ b/internal/encoding/text/decode_token.go @@ -1,6 +1,7 @@ // 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 text import ( @@ -16,6 +17,7 @@ import ( // Kind represents a token kind expressible in the textproto format. type Kind uint8 +// Kind values. const ( Invalid Kind = iota EOF @@ -65,6 +67,7 @@ func (t Kind) String() string { // NameKind represents different types of field names. type NameKind uint8 +// NameKind values. const ( IdentName NameKind = iota + 1 TypeName diff --git a/internal/encoding/text/encode.go b/internal/encoding/text/encode.go index a5939786..c4ba1c59 100644 --- a/internal/encoding/text/encode.go +++ b/internal/encoding/text/encode.go @@ -93,7 +93,7 @@ func (e *Encoder) EndMessage() { e.out = append(e.out, e.delims[1]) } -// Writname writes out the field name and the separator ':'. +// WriteName writes out the field name and the separator ':'. func (e *Encoder) WriteName(s string) { e.prepareNext(name) e.out = append(e.out, s...)