您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

132 行
3.9 KiB

  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2015 Ian Coleman
  5. * Copyright (c) 2018 Ma_124, <github.com/Ma124>
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, Subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or Substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. */
  25. package strcase
  26. import (
  27. "strings"
  28. )
  29. // ToSnake converts a string to snake_case
  30. func ToSnake(s string) string {
  31. return ToDelimited(s, '_')
  32. }
  33. func ToSnakeWithIgnore(s string, ignore uint8) string {
  34. return ToScreamingDelimited(s, '_', ignore, false)
  35. }
  36. // ToScreamingSnake converts a string to SCREAMING_SNAKE_CASE
  37. func ToScreamingSnake(s string) string {
  38. return ToScreamingDelimited(s, '_', 0, true)
  39. }
  40. // ToKebab converts a string to kebab-case
  41. func ToKebab(s string) string {
  42. return ToDelimited(s, '-')
  43. }
  44. // ToScreamingKebab converts a string to SCREAMING-KEBAB-CASE
  45. func ToScreamingKebab(s string) string {
  46. return ToScreamingDelimited(s, '-', 0, true)
  47. }
  48. // ToDelimited converts a string to delimited.snake.case
  49. // (in this case `delimiter = '.'`)
  50. func ToDelimited(s string, delimiter uint8) string {
  51. return ToScreamingDelimited(s, delimiter, 0, false)
  52. }
  53. // ToScreamingDelimited converts a string to SCREAMING.DELIMITED.SNAKE.CASE
  54. // (in this case `delimiter = '.'; screaming = true`)
  55. // or delimited.snake.case
  56. // (in this case `delimiter = '.'; screaming = false`)
  57. func ToScreamingDelimited(s string, delimiter uint8, ignore uint8, screaming bool) string {
  58. n := strings.Builder{}
  59. n.Grow(len(s) + 2) // nominal 2 bytes of extra space for inserted delimiters
  60. start := true
  61. spaces := 0
  62. for i, v := range []byte(s) {
  63. if v == ' ' {
  64. spaces++
  65. continue
  66. } else if start {
  67. start = false
  68. spaces = 0
  69. } else {
  70. for ; spaces > 0; spaces-- {
  71. if ignore == ' ' {
  72. n.WriteByte(' ')
  73. } else {
  74. n.WriteByte(delimiter)
  75. }
  76. }
  77. }
  78. vIsCap := v >= 'A' && v <= 'Z'
  79. vIsLow := v >= 'a' && v <= 'z'
  80. if vIsLow && screaming {
  81. v += 'A'
  82. v -= 'a'
  83. } else if vIsCap && !screaming {
  84. v += 'a'
  85. v -= 'A'
  86. }
  87. // treat acronyms as words, eg for JSONData -> JSON is a whole word
  88. if i+1 < len(s) {
  89. next := s[i+1]
  90. vIsNum := v >= '0' && v <= '9'
  91. nextIsCap := next >= 'A' && next <= 'Z'
  92. nextIsLow := next >= 'a' && next <= 'z'
  93. nextIsNum := next >= '0' && next <= '9'
  94. // add underscore if next letter case type is changed
  95. if (vIsCap && (nextIsLow || nextIsNum)) || (vIsLow && (nextIsCap || nextIsNum)) || (vIsNum && (nextIsCap || nextIsLow)) {
  96. if prevIgnore := ignore > 0 && i > 0 && s[i-1] == ignore; !prevIgnore {
  97. if vIsCap && nextIsLow {
  98. if prevIsCap := i > 0 && s[i-1] >= 'A' && s[i-1] <= 'Z'; prevIsCap {
  99. n.WriteByte(delimiter)
  100. }
  101. }
  102. n.WriteByte(v)
  103. if vIsLow || vIsNum || nextIsNum {
  104. n.WriteByte(delimiter)
  105. }
  106. continue
  107. }
  108. }
  109. }
  110. if (v == ' ' || v == '_' || v == '-') && uint8(v) != ignore {
  111. // replace space/underscore/hyphen with delimiter
  112. n.WriteByte(delimiter)
  113. } else {
  114. n.WriteByte(v)
  115. }
  116. }
  117. return n.String()
  118. }