1
0

string.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package stringx
  2. import (
  3. "bytes"
  4. "strings"
  5. "unicode"
  6. )
  7. var WhiteSpace = []rune{'\n', '\t', '\f', '\v', ' '}
  8. // String provides for converting the source text into other spell case,like lower,snake,camel
  9. type String struct {
  10. source string
  11. }
  12. // From converts the input text to String and returns it
  13. func From(data string) String {
  14. return String{source: data}
  15. }
  16. // IsEmptyOrSpace returns true if the length of the string value is 0 after call strings.TrimSpace, or else returns false
  17. func (s String) IsEmptyOrSpace() bool {
  18. if len(s.source) == 0 {
  19. return true
  20. }
  21. if strings.TrimSpace(s.source) == "" {
  22. return true
  23. }
  24. return false
  25. }
  26. // Lower calls the strings.ToLower
  27. func (s String) Lower() string {
  28. return strings.ToLower(s.source)
  29. }
  30. // Upper calls the strings.ToUpper
  31. func (s String) Upper() string {
  32. return strings.ToUpper(s.source)
  33. }
  34. // ReplaceAll calls the strings.ReplaceAll
  35. func (s String) ReplaceAll(old, new string) string {
  36. return strings.Replace(s.source, old, new, -1)
  37. }
  38. // Source returns the source string value
  39. func (s String) Source() string {
  40. return s.source
  41. }
  42. // Title calls the strings.Title
  43. func (s String) Title() string {
  44. if s.IsEmptyOrSpace() {
  45. return s.source
  46. }
  47. return strings.Title(s.source)
  48. }
  49. // ToCamel converts the input text into camel case
  50. func (s String) ToCamel() string {
  51. list := s.splitBy(func(r rune) bool {
  52. return r == '_'
  53. }, true)
  54. var target []string
  55. for _, item := range list {
  56. target = append(target, From(item).Title())
  57. }
  58. return strings.Join(target, "")
  59. }
  60. // ToSnake converts the input text into snake case
  61. func (s String) ToSnake() string {
  62. list := s.splitBy(unicode.IsUpper, false)
  63. var target []string
  64. for _, item := range list {
  65. target = append(target, From(item).Lower())
  66. }
  67. return strings.Join(target, "_")
  68. }
  69. // Untitle return the original string if rune is not letter at index 0
  70. func (s String) Untitle() string {
  71. if s.IsEmptyOrSpace() {
  72. return s.source
  73. }
  74. r := rune(s.source[0])
  75. if !unicode.IsUpper(r) && !unicode.IsLower(r) {
  76. return s.source
  77. }
  78. return string(unicode.ToLower(r)) + s.source[1:]
  79. }
  80. // it will not ignore spaces
  81. func (s String) splitBy(fn func(r rune) bool, remove bool) []string {
  82. if s.IsEmptyOrSpace() {
  83. return nil
  84. }
  85. var list []string
  86. buffer := new(bytes.Buffer)
  87. for _, r := range s.source {
  88. if fn(r) {
  89. if buffer.Len() != 0 {
  90. list = append(list, buffer.String())
  91. buffer.Reset()
  92. }
  93. if !remove {
  94. buffer.WriteRune(r)
  95. }
  96. continue
  97. }
  98. buffer.WriteRune(r)
  99. }
  100. if buffer.Len() != 0 {
  101. list = append(list, buffer.String())
  102. }
  103. return list
  104. }
  105. func ContainsAny(s string, runes ...rune) bool {
  106. if len(runes) == 0 {
  107. return true
  108. }
  109. tmp := make(map[rune]struct{}, len(runes))
  110. for _, r := range runes {
  111. tmp[r] = struct{}{}
  112. }
  113. for _, r := range s {
  114. if _, ok := tmp[r]; ok {
  115. return true
  116. }
  117. }
  118. return false
  119. }
  120. func ContainsWhiteSpace(s string) bool {
  121. return ContainsAny(s, WhiteSpace...)
  122. }