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