本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
2.3KB

  1. package runes
  2. func Index(s, needle []rune) int {
  3. ls, ln := len(s), len(needle)
  4. switch {
  5. case ln == 0:
  6. return 0
  7. case ln == 1:
  8. return IndexRune(s, needle[0])
  9. case ln == ls:
  10. if Equal(s, needle) {
  11. return 0
  12. }
  13. return -1
  14. case ln > ls:
  15. return -1
  16. }
  17. head:
  18. for i := 0; i < ls && ls-i >= ln; i++ {
  19. for y := 0; y < ln; y++ {
  20. if s[i+y] != needle[y] {
  21. continue head
  22. }
  23. }
  24. return i
  25. }
  26. return -1
  27. }
  28. func LastIndex(s, needle []rune) int {
  29. ls, ln := len(s), len(needle)
  30. switch {
  31. case ln == 0:
  32. if ls == 0 {
  33. return 0
  34. }
  35. return ls
  36. case ln == 1:
  37. return IndexLastRune(s, needle[0])
  38. case ln == ls:
  39. if Equal(s, needle) {
  40. return 0
  41. }
  42. return -1
  43. case ln > ls:
  44. return -1
  45. }
  46. head:
  47. for i := ls - 1; i >= 0 && i >= ln; i-- {
  48. for y := ln - 1; y >= 0; y-- {
  49. if s[i-(ln-y-1)] != needle[y] {
  50. continue head
  51. }
  52. }
  53. return i - ln + 1
  54. }
  55. return -1
  56. }
  57. // IndexAny returns the index of the first instance of any Unicode code point
  58. // from chars in s, or -1 if no Unicode code point from chars is present in s.
  59. func IndexAny(s, chars []rune) int {
  60. if len(chars) > 0 {
  61. for i, c := range s {
  62. for _, m := range chars {
  63. if c == m {
  64. return i
  65. }
  66. }
  67. }
  68. }
  69. return -1
  70. }
  71. func Contains(s, needle []rune) bool {
  72. return Index(s, needle) >= 0
  73. }
  74. func Max(s []rune) (max rune) {
  75. for _, r := range s {
  76. if r > max {
  77. max = r
  78. }
  79. }
  80. return
  81. }
  82. func Min(s []rune) rune {
  83. min := rune(-1)
  84. for _, r := range s {
  85. if min == -1 {
  86. min = r
  87. continue
  88. }
  89. if r < min {
  90. min = r
  91. }
  92. }
  93. return min
  94. }
  95. func IndexRune(s []rune, r rune) int {
  96. for i, c := range s {
  97. if c == r {
  98. return i
  99. }
  100. }
  101. return -1
  102. }
  103. func IndexLastRune(s []rune, r rune) int {
  104. for i := len(s) - 1; i >= 0; i-- {
  105. if s[i] == r {
  106. return i
  107. }
  108. }
  109. return -1
  110. }
  111. func Equal(a, b []rune) bool {
  112. if len(a) == len(b) {
  113. for i := 0; i < len(a); i++ {
  114. if a[i] != b[i] {
  115. return false
  116. }
  117. }
  118. return true
  119. }
  120. return false
  121. }
  122. // HasPrefix tests whether the string s begins with prefix.
  123. func HasPrefix(s, prefix []rune) bool {
  124. return len(s) >= len(prefix) && Equal(s[0:len(prefix)], prefix)
  125. }
  126. // HasSuffix tests whether the string s ends with suffix.
  127. func HasSuffix(s, suffix []rune) bool {
  128. return len(s) >= len(suffix) && Equal(s[len(s)-len(suffix):], suffix)
  129. }
上海开阖软件有限公司 沪ICP备12045867号-1