...
1
2
3
4
5
6
7
8 package strs
9
10 import (
11 "unsafe"
12
13 "google.golang.org/protobuf/reflect/protoreflect"
14 )
15
16
17
18
19
20
21 func UnsafeString(b []byte) string {
22 return unsafe.String(unsafe.SliceData(b), len(b))
23 }
24
25
26
27
28
29 func UnsafeBytes(s string) []byte {
30 return unsafe.Slice(unsafe.StringData(s), len(s))
31 }
32
33
34
35 type Builder struct {
36 buf []byte
37 }
38
39
40
41 func (sb *Builder) AppendFullName(prefix protoreflect.FullName, name protoreflect.Name) protoreflect.FullName {
42 n := len(prefix) + len(".") + len(name)
43 if len(prefix) == 0 {
44 n -= len(".")
45 }
46 sb.grow(n)
47 sb.buf = append(sb.buf, prefix...)
48 sb.buf = append(sb.buf, '.')
49 sb.buf = append(sb.buf, name...)
50 return protoreflect.FullName(sb.last(n))
51 }
52
53
54
55 func (sb *Builder) MakeString(b []byte) string {
56 sb.grow(len(b))
57 sb.buf = append(sb.buf, b...)
58 return sb.last(len(b))
59 }
60
61 func (sb *Builder) grow(n int) {
62 if cap(sb.buf)-len(sb.buf) >= n {
63 return
64 }
65
66
67
68
69 sb.buf = make([]byte, 0, 2*(cap(sb.buf)+n))
70 }
71
72 func (sb *Builder) last(n int) string {
73 return UnsafeString(sb.buf[len(sb.buf)-n:])
74 }
75
View as plain text