1
// Copyright 2019 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
// Package strs provides string manipulation functionality specific to protobuf.
14
"google.golang.org/protobuf/internal/flags"
15
"google.golang.org/protobuf/reflect/protoreflect"
18
// EnforceUTF8 reports whether to enforce strict UTF-8 validation.
19
func EnforceUTF8(fd protoreflect.FieldDescriptor) bool {
20
if flags.ProtoLegacy || fd.Syntax() == protoreflect.Editions {
21
if fd, ok := fd.(interface{ EnforceUTF8() bool }); ok {
22
return fd.EnforceUTF8()
25
return fd.Syntax() == protoreflect.Proto3
28
// GoCamelCase camel-cases a protobuf name for use as a Go identifier.
30
// If there is an interior underscore followed by a lower case letter,
31
// drop the underscore and convert the letter to upper case.
32
func GoCamelCase(s string) string {
33
// Invariant: if the next letter is lower case, it must be converted
35
// That is, we process a word at a time, where words are marked by _ or
36
// upper case letter. Digits are treated as words.
38
for i := 0; i < len(s); i++ {
41
case c == '.' && i+1 < len(s) && isASCIILower(s[i+1]):
42
// Skip over '.' in ".{{lowercase}}".
44
b = append(b, '_') // convert '.' to '_'
45
case c == '_' && (i == 0 || s[i-1] == '.'):
46
// Convert initial '_' to ensure we start with a capital letter.
47
// Do the same for '_' after '.' to match historic behavior.
48
b = append(b, 'X') // convert '_' to 'X'
49
case c == '_' && i+1 < len(s) && isASCIILower(s[i+1]):
50
// Skip over '_' in "_{{lowercase}}".
54
// Assume we have a letter now - if not, it's a bogus identifier.
55
// The next word is a sequence of characters that must start upper case.
57
c -= 'a' - 'A' // convert lowercase to uppercase
61
// Accept lower case sequence that follows.
62
for ; i+1 < len(s) && isASCIILower(s[i+1]); i++ {
70
// GoSanitized converts a string to a valid Go identifier.
71
func GoSanitized(s string) string {
72
// Sanitize the input to the set of valid characters,
73
// which must be '_' or be in the Unicode L or N categories.
74
s = strings.Map(func(r rune) rune {
75
if unicode.IsLetter(r) || unicode.IsDigit(r) {
81
// Prepend '_' in the event of a Go keyword conflict or if
82
// the identifier is invalid (does not start in the Unicode L category).
83
r, _ := utf8.DecodeRuneInString(s)
84
if token.Lookup(s).IsKeyword() || !unicode.IsLetter(r) {
90
// JSONCamelCase converts a snake_case identifier to a camelCase identifier,
91
// according to the protobuf JSON specification.
92
func JSONCamelCase(s string) string {
94
var wasUnderscore bool
95
for i := 0; i < len(s); i++ { // proto identifiers are always ASCII
98
if wasUnderscore && isASCIILower(c) {
99
c -= 'a' - 'A' // convert to uppercase
103
wasUnderscore = c == '_'
108
// JSONSnakeCase converts a camelCase identifier to a snake_case identifier,
109
// according to the protobuf JSON specification.
110
func JSONSnakeCase(s string) string {
112
for i := 0; i < len(s); i++ { // proto identifiers are always ASCII
116
c += 'a' - 'A' // convert to lowercase
123
// MapEntryName derives the name of the map entry message given the field name.
124
// See protoc v3.8.0: src/google/protobuf/descriptor.cc:254-276,6057
125
func MapEntryName(s string) string {
128
for _, c := range s {
133
b = append(b, byte(unicode.ToUpper(c)))
136
b = append(b, byte(c))
139
b = append(b, "Entry"...)
143
// EnumValueName derives the camel-cased enum value name.
144
// See protoc v3.8.0: src/google/protobuf/descriptor.cc:297-313
145
func EnumValueName(s string) string {
148
for _, c := range s {
153
b = append(b, byte(unicode.ToUpper(c)))
156
b = append(b, byte(unicode.ToLower(c)))
163
// TrimEnumPrefix trims the enum name prefix from an enum value name,
164
// where the prefix is all lowercase without underscores.
165
// See protoc v3.8.0: src/google/protobuf/descriptor.cc:330-375
166
func TrimEnumPrefix(s, prefix string) string {
167
s0 := s // original input
168
for len(s) > 0 && len(prefix) > 0 {
173
if unicode.ToLower(rune(s[0])) != rune(prefix[0]) {
174
return s0 // no prefix match
176
s, prefix = s[1:], prefix[1:]
179
return s0 // no prefix match
181
s = strings.TrimLeft(s, "_")
183
return s0 // avoid returning empty string
188
func isASCIILower(c byte) bool {
189
return 'a' <= c && c <= 'z'
191
func isASCIIUpper(c byte) bool {
192
return 'A' <= c && c <= 'Z'
194
func isASCIIDigit(c byte) bool {
195
return '0' <= c && c <= '9'