strings.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package stringx
  2. import (
  3. "errors"
  4. "github.com/tal-tech/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) string {
  37. var i int
  38. for j := range s {
  39. if i == n {
  40. return s[:j]
  41. }
  42. i++
  43. }
  44. return s
  45. }
  46. // HasEmpty checks if there are empty strings in args.
  47. func HasEmpty(args ...string) bool {
  48. for _, arg := range args {
  49. if len(arg) == 0 {
  50. return true
  51. }
  52. }
  53. return false
  54. }
  55. // NotEmpty checks if all strings are not empty in args.
  56. func NotEmpty(args ...string) bool {
  57. return !HasEmpty(args...)
  58. }
  59. // Remove removes given strs from strings.
  60. func Remove(strings []string, strs ...string) []string {
  61. out := append([]string(nil), strings...)
  62. for _, str := range strs {
  63. var n int
  64. for _, v := range out {
  65. if v != str {
  66. out[n] = v
  67. n++
  68. }
  69. }
  70. out = out[:n]
  71. }
  72. return out
  73. }
  74. // Reverse reverses s.
  75. func Reverse(s string) string {
  76. runes := []rune(s)
  77. for from, to := 0, len(runes)-1; from < to; from, to = from+1, to-1 {
  78. runes[from], runes[to] = runes[to], runes[from]
  79. }
  80. return string(runes)
  81. }
  82. // Substr returns runes between start and stop [start, stop) regardless of the chars are ascii or utf8.
  83. func Substr(str string, start, stop int) (string, error) {
  84. rs := []rune(str)
  85. length := len(rs)
  86. if start < 0 || start > length {
  87. return "", ErrInvalidStartPosition
  88. }
  89. if stop < 0 || stop > length {
  90. return "", ErrInvalidStopPosition
  91. }
  92. return string(rs[start:stop]), nil
  93. }
  94. // TakeOne returns valid string if not empty or later one.
  95. func TakeOne(valid, or string) string {
  96. if len(valid) > 0 {
  97. return valid
  98. }
  99. return or
  100. }
  101. // TakeWithPriority returns the first not empty result from fns.
  102. func TakeWithPriority(fns ...func() string) string {
  103. for _, fn := range fns {
  104. val := fn()
  105. if len(val) > 0 {
  106. return val
  107. }
  108. }
  109. return ""
  110. }
  111. // Union merges the strings in first and second.
  112. func Union(first, second []string) []string {
  113. set := make(map[string]lang.PlaceholderType)
  114. for _, each := range first {
  115. set[each] = lang.Placeholder
  116. }
  117. for _, each := range second {
  118. set[each] = lang.Placeholder
  119. }
  120. merged := make([]string, 0, len(set))
  121. for k := range set {
  122. merged = append(merged, k)
  123. }
  124. return merged
  125. }