@@ -40,14 +40,6 @@ impl PyRange {
40
40
}
41
41
}
42
42
43
- #[ inline]
44
- pub fn contains ( & self , value : & BigInt ) -> bool {
45
- match self . offset ( value) {
46
- Some ( ref offset) => offset. is_multiple_of ( & self . step ) ,
47
- None => false ,
48
- }
49
- }
50
-
51
43
#[ inline]
52
44
pub fn index_of ( & self , value : & BigInt ) -> Option < BigInt > {
53
45
match self . offset ( value) {
@@ -112,16 +104,16 @@ pub fn init(context: &PyContext) {
112
104
113
105
extend_class ! ( context, range_type, {
114
106
"__bool__" => context. new_rustfunc( PyRangeRef :: bool ) ,
115
- "__contains__" => context. new_rustfunc( range_contains ) ,
107
+ "__contains__" => context. new_rustfunc( PyRangeRef :: contains ) ,
116
108
"__doc__" => context. new_str( range_doc. to_string( ) ) ,
117
109
"__getitem__" => context. new_rustfunc( PyRangeRef :: getitem) ,
118
110
"__iter__" => context. new_rustfunc( PyRangeRef :: iter) ,
119
111
"__len__" => context. new_rustfunc( PyRangeRef :: len) ,
120
112
"__new__" => context. new_rustfunc( range_new) ,
121
113
"__repr__" => context. new_rustfunc( PyRangeRef :: repr) ,
122
114
"__reversed__" => context. new_rustfunc( PyRangeRef :: reversed) ,
123
- "count" => context. new_rustfunc( range_count ) ,
124
- "index" => context. new_rustfunc( range_index ) ,
115
+ "count" => context. new_rustfunc( PyRangeRef :: count ) ,
116
+ "index" => context. new_rustfunc( PyRangeRef :: index ) ,
125
117
"start" => context. new_property( PyRangeRef :: start) ,
126
118
"stop" => context. new_property( PyRangeRef :: stop) ,
127
119
"step" => context. new_property( PyRangeRef :: step) ,
@@ -231,6 +223,54 @@ impl PyRangeRef {
231
223
self . stop != self . start
232
224
}
233
225
226
+ fn contains ( self , needle : PyObjectRef , vm : & VirtualMachine ) -> bool {
227
+ if let Ok ( int) = needle. downcast :: < PyInt > ( ) {
228
+ match self . offset ( & int. value ) {
229
+ Some ( ref offset) => offset. is_multiple_of ( & self . step ) ,
230
+ None => false ,
231
+ }
232
+ } else {
233
+ false
234
+ }
235
+ }
236
+
237
+ fn index ( self , needle : PyObjectRef , vm : & VirtualMachine ) -> PyResult < PyInt > {
238
+ arg_check ! (
239
+ vm,
240
+ args,
241
+ required = [ ( zelf, Some ( vm. ctx. range_type( ) ) ) , ( needle, None ) ]
242
+ ) ;
243
+
244
+ let range = get_value ( zelf) ;
245
+
246
+ if objtype:: isinstance ( needle, & vm. ctx . int_type ( ) ) {
247
+ let needle = objint:: get_value ( needle) ;
248
+
249
+ match range. index_of ( & needle) {
250
+ Some ( idx) => Ok ( vm. ctx . new_int ( idx) ) ,
251
+ None => Err ( vm. new_value_error ( format ! ( "{} is not in range" , needle) ) ) ,
252
+ }
253
+ } else {
254
+ Err ( vm. new_value_error ( "sequence.index(x): x not in sequence" . to_string ( ) ) )
255
+ }
256
+ }
257
+
258
+ fn range_count ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
259
+ arg_check ! (
260
+ vm,
261
+ args,
262
+ required = [ ( zelf, Some ( vm. ctx. range_type( ) ) ) , ( item, None ) ]
263
+ ) ;
264
+
265
+ let range = get_value ( zelf) ;
266
+
267
+ if objtype:: isinstance ( item, & vm. ctx . int_type ( ) ) {
268
+ Ok ( vm. ctx . new_int ( range. count ( & objint:: get_value ( item) ) ) )
269
+ } else {
270
+ Ok ( vm. ctx . new_int ( 0 ) )
271
+ }
272
+ }
273
+
234
274
fn getitem ( self , subscript : Either < PyIntRef , PySliceRef > , vm : & VirtualMachine ) -> PyResult {
235
275
match subscript {
236
276
Either :: A ( index) => {
@@ -290,58 +330,3 @@ fn range_new(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
290
330
291
331
Ok ( range. into_object ( ) )
292
332
}
293
-
294
- fn range_contains ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
295
- arg_check ! (
296
- vm,
297
- args,
298
- required = [ ( zelf, Some ( vm. ctx. range_type( ) ) ) , ( needle, None ) ]
299
- ) ;
300
-
301
- let range = get_value ( zelf) ;
302
-
303
- let result = if objtype:: isinstance ( needle, & vm. ctx . int_type ( ) ) {
304
- range. contains ( & objint:: get_value ( needle) )
305
- } else {
306
- false
307
- } ;
308
-
309
- Ok ( vm. ctx . new_bool ( result) )
310
- }
311
-
312
- fn range_index ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
313
- arg_check ! (
314
- vm,
315
- args,
316
- required = [ ( zelf, Some ( vm. ctx. range_type( ) ) ) , ( needle, None ) ]
317
- ) ;
318
-
319
- let range = get_value ( zelf) ;
320
-
321
- if objtype:: isinstance ( needle, & vm. ctx . int_type ( ) ) {
322
- let needle = objint:: get_value ( needle) ;
323
-
324
- match range. index_of ( & needle) {
325
- Some ( idx) => Ok ( vm. ctx . new_int ( idx) ) ,
326
- None => Err ( vm. new_value_error ( format ! ( "{} is not in range" , needle) ) ) ,
327
- }
328
- } else {
329
- Err ( vm. new_value_error ( "sequence.index(x): x not in sequence" . to_string ( ) ) )
330
- }
331
- }
332
-
333
- fn range_count ( vm : & VirtualMachine , args : PyFuncArgs ) -> PyResult {
334
- arg_check ! (
335
- vm,
336
- args,
337
- required = [ ( zelf, Some ( vm. ctx. range_type( ) ) ) , ( item, None ) ]
338
- ) ;
339
-
340
- let range = get_value ( zelf) ;
341
-
342
- if objtype:: isinstance ( item, & vm. ctx . int_type ( ) ) {
343
- Ok ( vm. ctx . new_int ( range. count ( & objint:: get_value ( item) ) ) )
344
- } else {
345
- Ok ( vm. ctx . new_int ( 0 ) )
346
- }
347
- }
0 commit comments