strings.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.
  4. // Package strs provides string manipulation functionality specific to protobuf.
  5. package strs
  6. import (
  7. "strings"
  8. "unicode"
  9. "google.golang.org/protobuf/internal/flags"
  10. "google.golang.org/protobuf/reflect/protoreflect"
  11. )
  12. // EnforceUTF8 reports whether to enforce strict UTF-8 validation.
  13. func EnforceUTF8(fd protoreflect.FieldDescriptor) bool {
  14. if flags.ProtoLegacy {
  15. if fd, ok := fd.(interface{ EnforceUTF8() bool }); ok {
  16. return fd.EnforceUTF8()
  17. }
  18. }
  19. return fd.Syntax() == protoreflect.Proto3
  20. }
  21. // JSONCamelCase converts a snake_case identifier to a camelCase identifier,
  22. // according to the protobuf JSON specification.
  23. func JSONCamelCase(s string) string {
  24. var b []byte
  25. var wasUnderscore bool
  26. for i := 0; i < len(s); i++ { // proto identifiers are always ASCII
  27. c := s[i]
  28. if c != '_' {
  29. isLower := 'a' <= c && c <= 'z'
  30. if wasUnderscore && isLower {
  31. c -= 'a' - 'A' // convert to uppercase
  32. }
  33. b = append(b, c)
  34. }
  35. wasUnderscore = c == '_'
  36. }
  37. return string(b)
  38. }
  39. // JSONSnakeCase converts a camelCase identifier to a snake_case identifier,
  40. // according to the protobuf JSON specification.
  41. func JSONSnakeCase(s string) string {
  42. var b []byte
  43. for i := 0; i < len(s); i++ { // proto identifiers are always ASCII
  44. c := s[i]
  45. isUpper := 'A' <= c && c <= 'Z'
  46. if isUpper {
  47. b = append(b, '_')
  48. c += 'a' - 'A' // convert to lowercase
  49. }
  50. b = append(b, c)
  51. }
  52. return string(b)
  53. }
  54. // MapEntryName derives the name of the map entry message given the field name.
  55. // See protoc v3.8.0: src/google/protobuf/descriptor.cc:254-276,6057
  56. func MapEntryName(s string) string {
  57. var b []byte
  58. upperNext := true
  59. for _, c := range s {
  60. switch {
  61. case c == '_':
  62. upperNext = true
  63. case upperNext:
  64. b = append(b, byte(unicode.ToUpper(c)))
  65. upperNext = false
  66. default:
  67. b = append(b, byte(c))
  68. }
  69. }
  70. b = append(b, "Entry"...)
  71. return string(b)
  72. }
  73. // EnumValueName derives the camel-cased enum value name.
  74. // See protoc v3.8.0: src/google/protobuf/descriptor.cc:297-313
  75. func EnumValueName(s string) string {
  76. var b []byte
  77. upperNext := true
  78. for _, c := range s {
  79. switch {
  80. case c == '_':
  81. upperNext = true
  82. case upperNext:
  83. b = append(b, byte(unicode.ToUpper(c)))
  84. upperNext = false
  85. default:
  86. b = append(b, byte(unicode.ToLower(c)))
  87. upperNext = false
  88. }
  89. }
  90. return string(b)
  91. }
  92. // TrimEnumPrefix trims the enum name prefix from an enum value name,
  93. // where the prefix is all lowercase without underscores.
  94. // See protoc v3.8.0: src/google/protobuf/descriptor.cc:330-375
  95. func TrimEnumPrefix(s, prefix string) string {
  96. s0 := s // original input
  97. for len(s) > 0 && len(prefix) > 0 {
  98. if s[0] == '_' {
  99. s = s[1:]
  100. continue
  101. }
  102. if unicode.ToLower(rune(s[0])) != rune(prefix[0]) {
  103. return s0 // no prefix match
  104. }
  105. s, prefix = s[1:], prefix[1:]
  106. }
  107. if len(prefix) > 0 {
  108. return s0 // no prefix match
  109. }
  110. s = strings.TrimLeft(s, "_")
  111. if len(s) == 0 {
  112. return s0 // avoid returning empty string
  113. }
  114. return s
  115. }