strings.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package stringx
  2. import (
  3. "errors"
  4. "github.com/zeromicro/go-zero/core/lang"
  5. )
  6. var (
  7. // ErrInvalidStartPosition is an error that indicates the start position is invalid.
  8. ErrInvalidStartPosition = errors.New("start position is invalid")
  9. // ErrInvalidStopPosition is an error that indicates the stop position is invalid.
  10. ErrInvalidStopPosition = errors.New("stop position is invalid")
  11. )
  12. // Contains checks if str is in list.
  13. func Contains(list []string, str string) bool {
  14. for _, each := range list {
  15. if each == str {
  16. return true
  17. }
  18. }
  19. return false
  20. }
  21. // Filter filters chars from s with given filter function.
  22. func Filter(s string, filter func(r rune) bool) string {
  23. var n int
  24. chars := []rune(s)
  25. for i, x := range chars {
  26. if n < i {
  27. chars[n] = x
  28. }
  29. if !filter(x) {
  30. n++
  31. }
  32. }
  33. return string(chars[:n])
  34. }
  35. // FirstN returns first n runes from s.
  36. func FirstN(s string, n int, ellipsis ...string) string {
  37. var i int
  38. for j := range s {
  39. if i == n {
  40. ret := s[:j]
  41. for _, each := range ellipsis {
  42. ret += each
  43. }
  44. return ret
  45. }
  46. i++
  47. }
  48. return s
  49. }
  50. // HasEmpty checks if there are empty strings in args.
  51. func HasEmpty(args ...string) bool {
  52. for _, arg := range args {
  53. if len(arg) == 0 {
  54. return true
  55. }
  56. }
  57. return false
  58. }
  59. // NotEmpty checks if all strings are not empty in args.
  60. func NotEmpty(args ...string) bool {
  61. return !HasEmpty(args...)
  62. }
  63. // Remove removes given strs from strings.
  64. func Remove(strings []string, strs ...string) []string {
  65. out := append([]string(nil), strings...)
  66. for _, str := range strs {
  67. var n int
  68. for _, v := range out {
  69. if v != str {
  70. out[n] = v
  71. n++
  72. }
  73. }
  74. out = out[:n]
  75. }
  76. return out
  77. }
  78. // Reverse reverses s.
  79. func Reverse(s string) string {
  80. runes := []rune(s)
  81. for from, to := 0, len(runes)-1; from < to; from, to = from+1, to-1 {
  82. runes[from], runes[to] = runes[to], runes[from]
  83. }
  84. return string(runes)
  85. }
  86. // Substr returns runes between start and stop [start, stop)
  87. // regardless of the chars are ascii or utf8.
  88. func Substr(str string, start, stop int) (string, error) {
  89. rs := []rune(str)
  90. length := len(rs)
  91. if start < 0 || start > length {
  92. return "", ErrInvalidStartPosition
  93. }
  94. if stop < 0 || stop > length {
  95. return "", ErrInvalidStopPosition
  96. }
  97. return string(rs[start:stop]), nil
  98. }
  99. // TakeOne returns valid string if not empty or later one.
  100. func TakeOne(valid, or string) string {
  101. if len(valid) > 0 {
  102. return valid
  103. }
  104. return or
  105. }
  106. // TakeWithPriority returns the first not empty result from fns.
  107. func TakeWithPriority(fns ...func() string) string {
  108. for _, fn := range fns {
  109. val := fn()
  110. if len(val) > 0 {
  111. return val
  112. }
  113. }
  114. return ""
  115. }
  116. // Union merges the strings in first and second.
  117. func Union(first, second []string) []string {
  118. set := make(map[string]lang.PlaceholderType)
  119. for _, each := range first {
  120. set[each] = lang.Placeholder
  121. }
  122. for _, each := range second {
  123. set[each] = lang.Placeholder
  124. }
  125. merged := make([]string, 0, len(set))
  126. for k := range set {
  127. merged = append(merged, k)
  128. }
  129. return merged
  130. }