@@ -122,6 +122,43 @@ func fieldsN(s string, n int) []string {
122
122
}
123
123
124
124
func init () {
125
+ StringType .Dict ["endswith" ] = MustNewMethod ("endswith" , func (self Object , args Tuple ) (Object , error ) {
126
+ selfStr := string (self .(String ))
127
+ suffix := []string {}
128
+ if len (args ) > 0 {
129
+ if s , ok := args [0 ].(String ); ok {
130
+ suffix = append (suffix , string (s ))
131
+ } else if s , ok := args [0 ].(Tuple ); ok {
132
+ for _ , t := range s {
133
+ if v , ok := t .(String ); ok {
134
+ suffix = append (suffix , string (v ))
135
+ }
136
+ }
137
+ } else {
138
+ return nil , ExceptionNewf (TypeError , "endswith first arg must be str, unicode, or tuple, not %s" , args [0 ].Type ())
139
+ }
140
+ } else {
141
+ return nil , ExceptionNewf (TypeError , "endswith() takes at least 1 argument (0 given)" )
142
+ }
143
+ for _ , s := range suffix {
144
+ if strings .HasSuffix (selfStr , s ) {
145
+ return Bool (true ), nil
146
+ }
147
+ }
148
+ return Bool (false ), nil
149
+ }, 0 , "endswith(suffix[, start[, end]]) -> bool" )
150
+
151
+ StringType .Dict ["find" ] = MustNewMethod ("find" , func (self Object , args Tuple ) (Object , error ) {
152
+ return self .(String ).find (args )
153
+ }, 0 , `find(...)
154
+ S.find(sub[, start[, end]]) -> int
155
+
156
+ Return the lowest index in S where substring sub is found,
157
+ such that sub is contained within S[start:end]. Optional
158
+ arguments start and end are interpreted as in slice notation.
159
+
160
+ Return -1 on failure.` )
161
+
125
162
StringType .Dict ["replace" ] = MustNewMethod ("replace" , func (self Object , args Tuple ) (Object , error ) {
126
163
return self .(String ).Replace (args )
127
164
}, 0 , `replace(self, old, new, count=-1) -> return a copy with all occurrences of substring old replaced by new.
@@ -169,32 +206,6 @@ replaced.`)
169
206
return Bool (false ), nil
170
207
}, 0 , "startswith(prefix[, start[, end]]) -> bool" )
171
208
172
- StringType .Dict ["endswith" ] = MustNewMethod ("endswith" , func (self Object , args Tuple ) (Object , error ) {
173
- selfStr := string (self .(String ))
174
- suffix := []string {}
175
- if len (args ) > 0 {
176
- if s , ok := args [0 ].(String ); ok {
177
- suffix = append (suffix , string (s ))
178
- } else if s , ok := args [0 ].(Tuple ); ok {
179
- for _ , t := range s {
180
- if v , ok := t .(String ); ok {
181
- suffix = append (suffix , string (v ))
182
- }
183
- }
184
- } else {
185
- return nil , ExceptionNewf (TypeError , "endswith first arg must be str, unicode, or tuple, not %s" , args [0 ].Type ())
186
- }
187
- } else {
188
- return nil , ExceptionNewf (TypeError , "endswith() takes at least 1 argument (0 given)" )
189
- }
190
- for _ , s := range suffix {
191
- if strings .HasSuffix (selfStr , s ) {
192
- return Bool (true ), nil
193
- }
194
- }
195
- return Bool (false ), nil
196
- }, 0 , "endswith(suffix[, start[, end]]) -> bool" )
197
-
198
209
}
199
210
200
211
// Type of this object
@@ -578,6 +589,32 @@ func (s String) M__contains__(item Object) (Object, error) {
578
589
return NewBool (strings .Contains (string (s ), string (needle ))), nil
579
590
}
580
591
592
+ func (s String ) find (args Tuple ) (Object , error ) {
593
+ var (
594
+ pysub Object
595
+ pybeg Object = Int (0 )
596
+ pyend Object = Int (len (s ))
597
+ pyfmt = "s|ii:find"
598
+ )
599
+ err := ParseTuple (args , pyfmt , & pysub , & pybeg , & pyend )
600
+ if err != nil {
601
+ return nil , err
602
+ }
603
+
604
+ var (
605
+ beg = int (pybeg .(Int ))
606
+ end = int (pyend .(Int ))
607
+ off = s .slice (0 , beg , s .len ()).len ()
608
+ str = string (s .slice (beg , end , s .len ()))
609
+ sub = string (pysub .(String ))
610
+ idx = strings .Index (str , sub )
611
+ )
612
+ if idx < 0 {
613
+ return Int (idx ), nil
614
+ }
615
+ return Int (off + String (str [:idx ]).len ()), nil
616
+ }
617
+
581
618
func (s String ) Split (args Tuple , kwargs StringDict ) (Object , error ) {
582
619
var (
583
620
pyval Object = None
0 commit comments