Source File
strings_unsafe.go
Belonging Package
google.golang.org/protobuf/internal/strs
// 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.
//go:build !purego && !appengine
// +build !purego,!appengine
package strs
import (
)
type (
stringHeader struct {
Data unsafe.Pointer
Len int
}
sliceHeader struct {
Data unsafe.Pointer
Len int
Cap int
}
)
// UnsafeString returns an unsafe string reference of b.
// The caller must treat the input slice as immutable.
//
// WARNING: Use carefully. The returned result must not leak to the end user
// unless the input slice is provably immutable.
func ( []byte) ( string) {
:= (*sliceHeader)(unsafe.Pointer(&))
:= (*stringHeader)(unsafe.Pointer(&))
.Data = .Data
.Len = .Len
return
}
// UnsafeBytes returns an unsafe bytes slice reference of s.
// The caller must treat returned slice as immutable.
//
// WARNING: Use carefully. The returned result must not leak to the end user.
func ( string) ( []byte) {
:= (*stringHeader)(unsafe.Pointer(&))
:= (*sliceHeader)(unsafe.Pointer(&))
.Data = .Data
.Len = .Len
.Cap = .Len
return
}
// Builder builds a set of strings with shared lifetime.
// This differs from strings.Builder, which is for building a single string.
type Builder struct {
buf []byte
}
// AppendFullName is equivalent to protoreflect.FullName.Append,
// but optimized for large batches where each name has a shared lifetime.
func ( *Builder) ( protoreflect.FullName, protoreflect.Name) protoreflect.FullName {
:= len() + len(".") + len()
if len() == 0 {
-= len(".")
}
.grow()
.buf = append(.buf, ...)
.buf = append(.buf, '.')
.buf = append(.buf, ...)
return protoreflect.FullName(.last())
}
// MakeString is equivalent to string(b), but optimized for large batches
// with a shared lifetime.
func ( *Builder) ( []byte) string {
.grow(len())
.buf = append(.buf, ...)
return .last(len())
}
func ( *Builder) ( int) {
if cap(.buf)-len(.buf) >= {
return
}
// Unlike strings.Builder, we do not need to copy over the contents
// of the old buffer since our builder provides no API for
// retrieving previously created strings.
.buf = make([]byte, 2*(cap(.buf)+))
}
func ( *Builder) ( int) string {
return UnsafeString(.buf[len(.buf)-:])
}
The pages are generated with Golds v0.4.9. (GOOS=linux GOARCH=amd64)