Skip to content

Commit 704640e

Browse files
committed
Complete str*pos funcs
1 parent 3cb12b0 commit 704640e

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed

php2go.go

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,76 @@ func Usleep(t int64) {
6666

6767
// strpos()
6868
func Strpos(haystack, needle string, offset int) int {
69-
return strings.Index(haystack, needle)
69+
length := len(haystack)
70+
if length == 0 || offset > length || -offset > length {
71+
return -1
72+
}
73+
74+
if offset < 0 {
75+
offset += length
76+
}
77+
pos := strings.Index(haystack[offset:], needle)
78+
if pos == -1 {
79+
return -1
80+
}
81+
return pos + offset
7082
}
7183

7284
// stripos()
7385
func Stripos(haystack, needle string, offset int) int {
74-
haystack = strings.ToLower(haystack)
86+
length := len(haystack)
87+
if length == 0 || offset > length || -offset > length {
88+
return -1
89+
}
90+
91+
haystack = strings.ToLower(haystack[offset:])
7592
needle = strings.ToLower(needle)
76-
return strings.Index(haystack, needle)
93+
if offset < 0 {
94+
offset += length
95+
}
96+
pos := strings.Index(haystack, needle)
97+
if pos == -1 {
98+
return -1
99+
}
100+
return pos + offset
77101
}
78102

79103
// strrpos()
80104
func Strrpos(haystack, needle string, offset int) int {
81-
return strings.LastIndex(haystack, needle)
105+
pos, length := 0, len(haystack)
106+
if length == 0 || offset > length || -offset > length {
107+
return -1
108+
}
109+
110+
if offset < 0 {
111+
pos = strings.LastIndex(haystack[:offset+length+1], needle)
112+
} else {
113+
pos = strings.LastIndex(haystack[offset:], needle)
114+
if pos != -1 {
115+
pos += offset
116+
}
117+
}
118+
return pos
82119
}
83120

84121
// strripos()
85122
func Strripos(haystack, needle string, offset int) int {
123+
pos, length := 0, len(haystack)
124+
if length == 0 || offset > length || -offset > length {
125+
return -1
126+
}
127+
86128
haystack = strings.ToLower(haystack)
87129
needle = strings.ToLower(needle)
88-
return strings.LastIndex(haystack, needle)
130+
if offset < 0 {
131+
pos = strings.LastIndex(haystack[:offset+length+1], needle)
132+
} else {
133+
pos = strings.LastIndex(haystack[offset:], needle)
134+
if pos != -1 {
135+
pos += offset
136+
}
137+
}
138+
return pos
89139
}
90140

91141
// str_replace()
@@ -374,7 +424,7 @@ func Strtr(haystack string, params ...interface{}) string {
374424
for i = 0; i < trlen; i++ {
375425
xlat[from[i]] = to[i]
376426
}
377-
for i = 0; i < len(haystack); i ++ {
427+
for i = 0; i < len(haystack); i++ {
378428
str[i] = xlat[haystack[i]]
379429
}
380430
return string(str)
@@ -881,7 +931,7 @@ func ArrayChunk(s []interface{}, size int) [][]interface{} {
881931
end = length
882932
}
883933
n = append(n, s[i*size:end])
884-
i ++
934+
i++
885935
}
886936
return n
887937
}

php2go_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,18 @@ func TestString(t *testing.T) {
5656
tMbStrlen := MbStrlen("G简体")
5757
equal(t, 3, tMbStrlen)
5858

59-
tstrpos := Strpos("hello world", "w", 0)
59+
tstrpos := Strpos("hello wworld", "w", -6)
6060
equal(t, 6, tstrpos)
6161

62+
tstripos := Stripos("hello Wworld", "w", 8)
63+
equal(t, -1, tstripos)
64+
65+
tstrrpos := Strrpos("hello wworld", "w", -6)
66+
equal(t, 6, tstrrpos)
67+
68+
tstrripos := Strripos("hello wWorld", "w", 0)
69+
equal(t, 7, tstrripos)
70+
6271
timplode := Implode(",", []string{"a", "b", "c"})
6372
equal(t, "a,b,c", timplode)
6473

0 commit comments

Comments
 (0)