@@ -66,26 +66,76 @@ func Usleep(t int64) {
6666
6767// strpos()
6868func 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()
7385func 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()
80104func 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()
85122func 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}
0 commit comments