case.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package util
  2. import (
  3. "strings"
  4. "unicode"
  5. )
  6. // IsUpperCase returns true if the rune in A-Z
  7. func IsUpperCase(r rune) bool {
  8. if r >= 'A' && r <= 'Z' {
  9. return true
  10. }
  11. return false
  12. }
  13. // IsLowerCase returns true if the rune in a-z
  14. func IsLowerCase(r rune) bool {
  15. if r >= 'a' && r <= 'z' {
  16. return true
  17. }
  18. return false
  19. }
  20. // ToSnakeCase returns a copy string by converting camel case into snake case
  21. func ToSnakeCase(s string) string {
  22. var out []rune
  23. for index, r := range s {
  24. if index == 0 {
  25. out = append(out, ToLowerCase(r))
  26. continue
  27. }
  28. if IsUpperCase(r) && index != 0 {
  29. if IsLowerCase(rune(s[index-1])) {
  30. out = append(out, '_', ToLowerCase(r))
  31. continue
  32. }
  33. if index < len(s)-1 && IsLowerCase(rune(s[index+1])) {
  34. out = append(out, '_', ToLowerCase(r))
  35. continue
  36. }
  37. out = append(out, ToLowerCase(r))
  38. continue
  39. }
  40. out = append(out, r)
  41. }
  42. return string(out)
  43. }
  44. // ToCamelCase returns a copy string by converting snake case into camel case
  45. func ToCamelCase(s string) string {
  46. s = ToLower(s)
  47. out := []rune{}
  48. for index, r := range s {
  49. if r == '_' {
  50. continue
  51. }
  52. if index == 0 {
  53. out = append(out, ToUpperCase(r))
  54. continue
  55. }
  56. if index > 0 && s[index-1] == '_' {
  57. out = append(out, ToUpperCase(r))
  58. continue
  59. }
  60. out = append(out, r)
  61. }
  62. return string(out)
  63. }
  64. // ToLowerCase converts rune into lower case
  65. func ToLowerCase(r rune) rune {
  66. dx := 'A' - 'a'
  67. if IsUpperCase(r) {
  68. return r - dx
  69. }
  70. return r
  71. }
  72. // ToUpperCase converts rune into upper case
  73. func ToUpperCase(r rune) rune {
  74. dx := 'A' - 'a'
  75. if IsLowerCase(r) {
  76. return r + dx
  77. }
  78. return r
  79. }
  80. // ToLower returns a copy string by converting it into lower
  81. func ToLower(s string) string {
  82. var out []rune
  83. for _, r := range s {
  84. out = append(out, ToLowerCase(r))
  85. }
  86. return string(out)
  87. }
  88. // ToUpper returns a copy string by converting it into upper
  89. func ToUpper(s string) string {
  90. var out []rune
  91. for _, r := range s {
  92. out = append(out, ToUpperCase(r))
  93. }
  94. return string(out)
  95. }
  96. // UpperFirst converts s[0] into upper case
  97. func UpperFirst(s string) string {
  98. if len(s) == 0 {
  99. return s
  100. }
  101. return ToUpper(s[:1]) + s[1:]
  102. }
  103. // UnExport converts the first letter into lower case
  104. func UnExport(text string) bool {
  105. var flag bool
  106. str := strings.Map(func(r rune) rune {
  107. if flag {
  108. return r
  109. }
  110. if unicode.IsLetter(r) {
  111. flag = true
  112. return unicode.ToLower(r)
  113. }
  114. return r
  115. }, text)
  116. return str == text
  117. }