gstr_pos.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2018 gf Author(https://github.com/gogf/gf). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gstr
  7. import "strings"
  8. // Pos returns the position of the first occurrence of <needle>
  9. // in <haystack> from <startOffset>, case-sensitively.
  10. // It returns -1, if not found.
  11. func Pos(haystack, needle string, startOffset ...int) int {
  12. length := len(haystack)
  13. offset := 0
  14. if len(startOffset) > 0 {
  15. offset = startOffset[0]
  16. }
  17. if length == 0 || offset > length || -offset > length {
  18. return -1
  19. }
  20. if offset < 0 {
  21. offset += length
  22. }
  23. pos := strings.Index(haystack[offset:], needle)
  24. if pos == -1 {
  25. return -1
  26. }
  27. return pos + offset
  28. }
  29. // PosRune acts like function Pos but considers <haystack> and <needle> as unicode string.
  30. func PosRune(haystack, needle string, startOffset ...int) int {
  31. pos := Pos(haystack, needle, startOffset...)
  32. if pos < 3 {
  33. return pos
  34. }
  35. return len([]rune(haystack[:pos]))
  36. }
  37. // PosI returns the position of the first occurrence of <needle>
  38. // in <haystack> from <startOffset>, case-insensitively.
  39. // It returns -1, if not found.
  40. func PosI(haystack, needle string, startOffset ...int) int {
  41. length := len(haystack)
  42. offset := 0
  43. if len(startOffset) > 0 {
  44. offset = startOffset[0]
  45. }
  46. if length == 0 || offset > length || -offset > length {
  47. return -1
  48. }
  49. if offset < 0 {
  50. offset += length
  51. }
  52. pos := strings.Index(strings.ToLower(haystack[offset:]), strings.ToLower(needle))
  53. if pos == -1 {
  54. return -1
  55. }
  56. return pos + offset
  57. }
  58. // PosIRune acts like function PosI but considers <haystack> and <needle> as unicode string.
  59. func PosIRune(haystack, needle string, startOffset ...int) int {
  60. pos := PosI(haystack, needle, startOffset...)
  61. if pos < 3 {
  62. return pos
  63. }
  64. return len([]rune(haystack[:pos]))
  65. }
  66. // PosR returns the position of the last occurrence of <needle>
  67. // in <haystack> from <startOffset>, case-sensitively.
  68. // It returns -1, if not found.
  69. func PosR(haystack, needle string, startOffset ...int) int {
  70. offset := 0
  71. if len(startOffset) > 0 {
  72. offset = startOffset[0]
  73. }
  74. pos, length := 0, len(haystack)
  75. if length == 0 || offset > length || -offset > length {
  76. return -1
  77. }
  78. if offset < 0 {
  79. haystack = haystack[:offset+length+1]
  80. } else {
  81. haystack = haystack[offset:]
  82. }
  83. pos = strings.LastIndex(haystack, needle)
  84. if offset > 0 && pos != -1 {
  85. pos += offset
  86. }
  87. return pos
  88. }
  89. // PosRRune acts like function PosR but considers <haystack> and <needle> as unicode string.
  90. func PosRRune(haystack, needle string, startOffset ...int) int {
  91. pos := PosR(haystack, needle, startOffset...)
  92. if pos < 3 {
  93. return pos
  94. }
  95. return len([]rune(haystack[:pos]))
  96. }
  97. // PosRI returns the position of the last occurrence of <needle>
  98. // in <haystack> from <startOffset>, case-insensitively.
  99. // It returns -1, if not found.
  100. func PosRI(haystack, needle string, startOffset ...int) int {
  101. offset := 0
  102. if len(startOffset) > 0 {
  103. offset = startOffset[0]
  104. }
  105. pos, length := 0, len(haystack)
  106. if length == 0 || offset > length || -offset > length {
  107. return -1
  108. }
  109. if offset < 0 {
  110. haystack = haystack[:offset+length+1]
  111. } else {
  112. haystack = haystack[offset:]
  113. }
  114. pos = strings.LastIndex(strings.ToLower(haystack), strings.ToLower(needle))
  115. if offset > 0 && pos != -1 {
  116. pos += offset
  117. }
  118. return pos
  119. }
  120. // PosRIRune acts like function PosRI but considers <haystack> and <needle> as unicode string.
  121. func PosRIRune(haystack, needle string, startOffset ...int) int {
  122. pos := PosRI(haystack, needle, startOffset...)
  123. if pos < 3 {
  124. return pos
  125. }
  126. return len([]rune(haystack[:pos]))
  127. }