Source File
decode_string.go
Belonging Package
google.golang.org/protobuf/internal/encoding/text
package text
import (
)
func ( *Decoder) () (string, error) {
:= .in
if len() == 0 {
return "", ErrUnexpectedEOF
}
:= [0]
= [1:]
:= indexNeedEscapeInBytes()
, := [:], [::] // set cap to prevent mutations
for len() > 0 {
switch , := utf8.DecodeRune(); {
case == utf8.RuneError && == 1:
return "", .newSyntaxError("invalid UTF-8 detected")
case == 0 || == '\n':
return "", .newSyntaxError("invalid character %q in string", )
case == rune():
= [1:]
.consume(len(.in) - len())
return string(), nil
case == '\\':
if len() < 2 {
return "", ErrUnexpectedEOF
}
switch := [1]; {
case '"', '\'', '\\', '?':
, = [2:], append(, )
case 'a':
, = [2:], append(, '\a')
case 'b':
, = [2:], append(, '\b')
case 'n':
, = [2:], append(, '\n')
case 'r':
, = [2:], append(, '\r')
case 't':
, = [2:], append(, '\t')
case 'v':
, = [2:], append(, '\v')
case 'f':
, = [2:], append(, '\f')
:= 6
if == 'U' {
= 10
}
if len() < {
return "", ErrUnexpectedEOF
}
, := strconv.ParseUint(string([2:]), 16, 32)
if utf8.MaxRune < || != nil {
return "", .newSyntaxError("invalid Unicode escape code %q in string", [:])
}
= [:]
:= rune()
if utf16.IsSurrogate() {
if len() < 6 {
return "", ErrUnexpectedEOF
}
, := strconv.ParseUint(string([2:6]), 16, 16)
= utf16.DecodeRune(, rune())
if [0] != '\\' || [1] != 'u' || == unicode.ReplacementChar || != nil {
return "", .newSyntaxError("invalid Unicode escape code %q in string", [:6])
}
= [6:]
}
= append(, string()...)
default:
return "", .newSyntaxError("invalid escape code %q in string", [:2])
}
default:
:= indexNeedEscapeInBytes([:])
, = [+:], append(, [:+]...)
}
}
return "", ErrUnexpectedEOF
}
func ( []byte) int { return indexNeedEscapeInString(strs.UnsafeString()) }
func ( string) (string, error) {
:= NewDecoder([]byte())
return .parseString()
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |