Source File
encode.go
Belonging Package
google.golang.org/protobuf/internal/encoding/text
package text
import (
)
type encType uint8
const (
_ encType = (1 << iota) / 2
name
scalar
messageOpen
messageClose
)
func ( string, [2]byte, bool) (*Encoder, error) {
:= &Encoder{}
if len() > 0 {
if strings.Trim(, " \t") != "" {
return nil, errors.New("indent may only be composed of space and tab characters")
}
.indent =
.newline = "\n"
}
switch {
case [2]byte{0, 0}:
.delims = [2]byte{'{', '}'}
case [2]byte{'{', '}'}, [2]byte{'<', '>'}:
.delims =
default:
return nil, errors.New("delimiters may only be \"{}\" or \"<>\"")
}
.outputASCII =
return , nil
}
func ( *Encoder) () {
.prepareNext(messageOpen)
.out = append(.out, .delims[0])
}
func ( *Encoder) () {
.prepareNext(messageClose)
.out = append(.out, .delims[1])
}
func ( *Encoder) ( bool) {
if {
.WriteLiteral("true")
} else {
.WriteLiteral("false")
}
}
func ( *Encoder) ( string) {
.prepareNext(scalar)
.out = appendString(.out, , .outputASCII)
}
func ( []byte, string, bool) []byte {
= append(, '"')
:= indexNeedEscapeInString()
, = [:], append(, [:]...)
for len() > 0 {
switch , := utf8.DecodeRuneInString(); {
= rune([0])
fallthrough
case < ' ' || == '"' || == '\\':
= append(, '\\')
switch {
case '"', '\\':
= append(, byte())
case '\n':
= append(, 'n')
case '\r':
= append(, 'r')
case '\t':
= append(, 't')
default:
= append(, 'x')
= append(, "00"[1+(bits.Len32(uint32())-1)/4:]...)
= strconv.AppendUint(, uint64(), 16)
}
= [:]
case && >= utf8.RuneSelf:
= append(, '\\')
if <= math.MaxUint16 {
= append(, 'u')
= append(, "0000"[1+(bits.Len32(uint32())-1)/4:]...)
= strconv.AppendUint(, uint64(), 16)
} else {
= append(, 'U')
= append(, "00000000"[1+(bits.Len32(uint32())-1)/4:]...)
= strconv.AppendUint(, uint64(), 16)
}
= [:]
default:
:= indexNeedEscapeInString([:])
, = [+:], append(, [:+]...)
}
}
= append(, '"')
return
}
func ( *Encoder) ( float64, int) {
.prepareNext(scalar)
.out = appendFloat(.out, , )
}
func ( []byte, float64, int) []byte {
switch {
case math.IsNaN():
return append(, "nan"...)
case math.IsInf(, +1):
return append(, "inf"...)
case math.IsInf(, -1):
return append(, "-inf"...)
default:
return strconv.AppendFloat(, , 'g', -1, )
}
}
func ( *Encoder) ( uint64) {
.prepareNext(scalar)
.out = append(.out, strconv.FormatUint(, 10)...)
}
if .lastType&(scalar|messageClose) != 0 && == name {
if detrand.Bool() {
.out = append(.out, ' ')
}
case .lastType == messageOpen && != messageClose:
.indents = append(.indents, .indent...)
.out = append(.out, '\n')
.out = append(.out, .indents...)
case .lastType&(scalar|messageClose) != 0:
if == messageClose {
.indents = .indents[:len(.indents)-len(.indent)]
}
.out = append(.out, '\n')
.out = append(.out, .indents...)
}
}
func ( *Encoder) () encoderState {
return .encoderState
}
func ( *Encoder) ( encoderState) {
.encoderState =
![]() |
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. |