Skip to content

Commit 476f4eb

Browse files
committed
Optimize str*pos funcs
1 parent 704640e commit 476f4eb

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

php2go.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,11 @@ func Stripos(haystack, needle string, offset int) int {
8888
return -1
8989
}
9090

91-
haystack = strings.ToLower(haystack[offset:])
92-
needle = strings.ToLower(needle)
91+
haystack = haystack[offset:]
9392
if offset < 0 {
9493
offset += length
9594
}
96-
pos := strings.Index(haystack, needle)
95+
pos := strings.Index(strings.ToLower(haystack), strings.ToLower(needle))
9796
if pos == -1 {
9897
return -1
9998
}
@@ -108,12 +107,13 @@ func Strrpos(haystack, needle string, offset int) int {
108107
}
109108

110109
if offset < 0 {
111-
pos = strings.LastIndex(haystack[:offset+length+1], needle)
110+
haystack = haystack[:offset+length+1]
112111
} else {
113-
pos = strings.LastIndex(haystack[offset:], needle)
114-
if pos != -1 {
115-
pos += offset
116-
}
112+
haystack = haystack[offset:]
113+
}
114+
pos = strings.LastIndex(haystack, needle)
115+
if offset > 0 && pos != -1 {
116+
pos += offset
117117
}
118118
return pos
119119
}
@@ -125,15 +125,14 @@ func Strripos(haystack, needle string, offset int) int {
125125
return -1
126126
}
127127

128-
haystack = strings.ToLower(haystack)
129-
needle = strings.ToLower(needle)
130128
if offset < 0 {
131-
pos = strings.LastIndex(haystack[:offset+length+1], needle)
129+
haystack = haystack[:offset+length+1]
132130
} else {
133-
pos = strings.LastIndex(haystack[offset:], needle)
134-
if pos != -1 {
135-
pos += offset
136-
}
131+
haystack = haystack[offset:]
132+
}
133+
pos = strings.LastIndex(strings.ToLower(haystack), strings.ToLower(needle))
134+
if offset > 0 && pos != -1 {
135+
pos += offset
137136
}
138137
return pos
139138
}

0 commit comments

Comments
 (0)