strings.go 2.7 KB

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