1
// Copyright 2018 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
5
//go:build !purego && !appengine && !go1.21
6
// +build !purego,!appengine,!go1.21
13
"google.golang.org/protobuf/reflect/protoreflect"
28
// UnsafeString returns an unsafe string reference of b.
29
// The caller must treat the input slice as immutable.
31
// WARNING: Use carefully. The returned result must not leak to the end user
32
// unless the input slice is provably immutable.
33
func UnsafeString(b []byte) (s string) {
34
src := (*sliceHeader)(unsafe.Pointer(&b))
35
dst := (*stringHeader)(unsafe.Pointer(&s))
41
// UnsafeBytes returns an unsafe bytes slice reference of s.
42
// The caller must treat returned slice as immutable.
44
// WARNING: Use carefully. The returned result must not leak to the end user.
45
func UnsafeBytes(s string) (b []byte) {
46
src := (*stringHeader)(unsafe.Pointer(&s))
47
dst := (*sliceHeader)(unsafe.Pointer(&b))
54
// Builder builds a set of strings with shared lifetime.
55
// This differs from strings.Builder, which is for building a single string.
60
// AppendFullName is equivalent to protoreflect.FullName.Append,
61
// but optimized for large batches where each name has a shared lifetime.
62
func (sb *Builder) AppendFullName(prefix protoreflect.FullName, name protoreflect.Name) protoreflect.FullName {
63
n := len(prefix) + len(".") + len(name)
68
sb.buf = append(sb.buf, prefix...)
69
sb.buf = append(sb.buf, '.')
70
sb.buf = append(sb.buf, name...)
71
return protoreflect.FullName(sb.last(n))
74
// MakeString is equivalent to string(b), but optimized for large batches
75
// with a shared lifetime.
76
func (sb *Builder) MakeString(b []byte) string {
78
sb.buf = append(sb.buf, b...)
79
return sb.last(len(b))
82
func (sb *Builder) grow(n int) {
83
if cap(sb.buf)-len(sb.buf) >= n {
87
// Unlike strings.Builder, we do not need to copy over the contents
88
// of the old buffer since our builder provides no API for
89
// retrieving previously created strings.
90
sb.buf = make([]byte, 0, 2*(cap(sb.buf)+n))
93
func (sb *Builder) last(n int) string {
94
return UnsafeString(sb.buf[len(sb.buf)-n:])