gitea源码

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package util
  4. import (
  5. "strings"
  6. "unsafe"
  7. )
  8. func isSnakeCaseUpper(c byte) bool {
  9. return 'A' <= c && c <= 'Z'
  10. }
  11. func isSnakeCaseLowerOrNumber(c byte) bool {
  12. return 'a' <= c && c <= 'z' || '0' <= c && c <= '9'
  13. }
  14. // ToSnakeCase convert the input string to snake_case format.
  15. //
  16. // Some samples.
  17. //
  18. // "FirstName" => "first_name"
  19. // "HTTPServer" => "http_server"
  20. // "NoHTTPS" => "no_https"
  21. // "GO_PATH" => "go_path"
  22. // "GO PATH" => "go_path" // space is converted to underscore.
  23. // "GO-PATH" => "go_path" // hyphen is converted to underscore.
  24. func ToSnakeCase(input string) string {
  25. if len(input) == 0 {
  26. return ""
  27. }
  28. var res []byte
  29. if len(input) == 1 {
  30. c := input[0]
  31. if isSnakeCaseUpper(c) {
  32. res = []byte{c + 'a' - 'A'}
  33. } else if isSnakeCaseLowerOrNumber(c) {
  34. res = []byte{c}
  35. } else {
  36. res = []byte{'_'}
  37. }
  38. } else {
  39. res = make([]byte, 0, len(input)*4/3)
  40. pos := 0
  41. needSep := false
  42. for pos < len(input) {
  43. c := input[pos]
  44. if c >= 0x80 {
  45. res = append(res, c)
  46. pos++
  47. continue
  48. }
  49. isUpper := isSnakeCaseUpper(c)
  50. if isUpper || isSnakeCaseLowerOrNumber(c) {
  51. end := pos + 1
  52. if isUpper {
  53. // skip the following upper letters
  54. for end < len(input) && isSnakeCaseUpper(input[end]) {
  55. end++
  56. }
  57. if end-pos > 1 && end < len(input) && isSnakeCaseLowerOrNumber(input[end]) {
  58. end--
  59. }
  60. }
  61. // skip the following lower or number letters
  62. for end < len(input) && (isSnakeCaseLowerOrNumber(input[end]) || input[end] >= 0x80) {
  63. end++
  64. }
  65. if needSep {
  66. res = append(res, '_')
  67. }
  68. res = append(res, input[pos:end]...)
  69. pos = end
  70. needSep = true
  71. } else {
  72. res = append(res, '_')
  73. pos++
  74. needSep = false
  75. }
  76. }
  77. for i := 0; i < len(res); i++ {
  78. if isSnakeCaseUpper(res[i]) {
  79. res[i] += 'a' - 'A'
  80. }
  81. }
  82. }
  83. return UnsafeBytesToString(res)
  84. }
  85. // UnsafeBytesToString uses Go's unsafe package to convert a byte slice to a string.
  86. func UnsafeBytesToString(b []byte) string {
  87. return unsafe.String(unsafe.SliceData(b), len(b))
  88. }
  89. // UnsafeStringToBytes uses Go's unsafe package to convert a string to a byte slice.
  90. func UnsafeStringToBytes(s string) []byte {
  91. return unsafe.Slice(unsafe.StringData(s), len(s))
  92. }
  93. // SplitTrimSpace splits the string at given separator and trims leading and trailing space
  94. func SplitTrimSpace(input, sep string) []string {
  95. input = strings.TrimSpace(input)
  96. var stringList []string
  97. for s := range strings.SplitSeq(input, sep) {
  98. if s = strings.TrimSpace(s); s != "" {
  99. stringList = append(stringList, s)
  100. }
  101. }
  102. return stringList
  103. }
  104. func asciiLower(b byte) byte {
  105. if 'A' <= b && b <= 'Z' {
  106. return b + ('a' - 'A')
  107. }
  108. return b
  109. }
  110. // AsciiEqualFold is from Golang https://cs.opensource.google/go/go/+/refs/tags/go1.24.4:src/net/http/internal/ascii/print.go
  111. // ASCII only. In most cases for protocols, we should only use this but not [strings.EqualFold]
  112. func AsciiEqualFold(s, t string) bool { //nolint:revive // PascalCase
  113. if len(s) != len(t) {
  114. return false
  115. }
  116. for i := 0; i < len(s); i++ {
  117. if asciiLower(s[i]) != asciiLower(t[i]) {
  118. return false
  119. }
  120. }
  121. return true
  122. }