// Copyright 2019 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.
// Package strs provides string manipulation functionality specific to protobuf.
package strsimport ()// EnforceUTF8 reports whether to enforce strict UTF-8 validation.func ( protoreflect.FieldDescriptor) bool {ifflags.ProtoLegacy {if , := .(interface{ () bool }); {return .() } }return .Syntax() == protoreflect.Proto3}// GoCamelCase camel-cases a protobuf name for use as a Go identifier.//// If there is an interior underscore followed by a lower case letter,// drop the underscore and convert the letter to upper case.func ( string) string {// Invariant: if the next letter is lower case, it must be converted // to upper case. // That is, we process a word at a time, where words are marked by _ or // upper case letter. Digits are treated as words.var []bytefor := 0; < len(); ++ { := []switch {case == '.' && +1 < len() && isASCIILower([+1]):// Skip over '.' in ".{{lowercase}}".case == '.': = append(, '_') // convert '.' to '_'case == '_' && ( == 0 || [-1] == '.'):// Convert initial '_' to ensure we start with a capital letter. // Do the same for '_' after '.' to match historic behavior. = append(, 'X') // convert '_' to 'X'case == '_' && +1 < len() && isASCIILower([+1]):// Skip over '_' in "_{{lowercase}}".caseisASCIIDigit(): = append(, )default:// Assume we have a letter now - if not, it's a bogus identifier. // The next word is a sequence of characters that must start upper case.ifisASCIILower() { -= 'a' - 'A'// convert lowercase to uppercase } = append(, )// Accept lower case sequence that follows.for ; +1 < len() && isASCIILower([+1]); ++ { = append(, [+1]) } } }returnstring()}// GoSanitized converts a string to a valid Go identifier.func ( string) string {// Sanitize the input to the set of valid characters, // which must be '_' or be in the Unicode L or N categories. = strings.Map(func( rune) rune {ifunicode.IsLetter() || unicode.IsDigit() {return }return'_' }, )// Prepend '_' in the event of a Go keyword conflict or if // the identifier is invalid (does not start in the Unicode L category). , := utf8.DecodeRuneInString()iftoken.Lookup().IsKeyword() || !unicode.IsLetter() {return"_" + }return}// JSONCamelCase converts a snake_case identifier to a camelCase identifier,// according to the protobuf JSON specification.func ( string) string {var []bytevarboolfor := 0; < len(); ++ { // proto identifiers are always ASCII := []if != '_' {if && isASCIILower() { -= 'a' - 'A'// convert to uppercase } = append(, ) } = == '_' }returnstring()}// JSONSnakeCase converts a camelCase identifier to a snake_case identifier,// according to the protobuf JSON specification.func ( string) string {var []bytefor := 0; < len(); ++ { // proto identifiers are always ASCII := []ifisASCIIUpper() { = append(, '_') += 'a' - 'A'// convert to lowercase } = append(, ) }returnstring()}// MapEntryName derives the name of the map entry message given the field name.// See protoc v3.8.0: src/google/protobuf/descriptor.cc:254-276,6057func ( string) string {var []byte := truefor , := range {switch {case == '_': = truecase : = append(, byte(unicode.ToUpper())) = falsedefault: = append(, byte()) } } = append(, "Entry"...)returnstring()}// EnumValueName derives the camel-cased enum value name.// See protoc v3.8.0: src/google/protobuf/descriptor.cc:297-313func ( string) string {var []byte := truefor , := range {switch {case == '_': = truecase : = append(, byte(unicode.ToUpper())) = falsedefault: = append(, byte(unicode.ToLower())) = false } }returnstring()}// TrimEnumPrefix trims the enum name prefix from an enum value name,// where the prefix is all lowercase without underscores.// See protoc v3.8.0: src/google/protobuf/descriptor.cc:330-375func (, string) string { := // original inputforlen() > 0 && len() > 0 {if [0] == '_' { = [1:]continue }ifunicode.ToLower(rune([0])) != rune([0]) {return// no prefix match } , = [1:], [1:] }iflen() > 0 {return// no prefix match } = strings.TrimLeft(, "_")iflen() == 0 {return// avoid returning empty string }return}func ( byte) bool {return'a' <= && <= 'z'}func ( byte) bool {return'A' <= && <= 'Z'}func ( byte) bool {return'0' <= && <= '9'}
The pages are generated with Goldsv0.4.9. (GOOS=linux GOARCH=amd64)