@@ -816,63 +816,35 @@ impl PyString {
816
816
}
817
817
818
818
#[ inline]
819
- fn _find < F > (
820
- & self ,
821
- sub : PyStringRef ,
822
- start : OptionalArg < Option < isize > > ,
823
- end : OptionalArg < Option < isize > > ,
824
- find : F ,
825
- ) -> Option < usize >
819
+ fn _find < F > ( & self , args : FindArgs , find : F ) -> Option < usize >
826
820
where
827
821
F : Fn ( & str , & str ) -> Option < usize > ,
828
822
{
829
- let range = adjust_indices ( start , end , self . value . len ( ) ) ;
823
+ let ( sub , range) = args . get_value ( self . len ( ) ) ;
830
824
self . value . py_find ( & sub. value , range, find)
831
825
}
832
826
833
827
#[ pymethod]
834
- fn find (
835
- & self ,
836
- sub : PyStringRef ,
837
- start : OptionalArg < Option < isize > > ,
838
- end : OptionalArg < Option < isize > > ,
839
- ) -> isize {
840
- self . _find ( sub, start, end, |r, s| r. find ( s) )
828
+ fn find ( & self , args : FindArgs ) -> isize {
829
+ self . _find ( args, |r, s| r. find ( s) )
841
830
. map_or ( -1 , |v| v as isize )
842
831
}
843
832
844
833
#[ pymethod]
845
- fn rfind (
846
- & self ,
847
- sub : PyStringRef ,
848
- start : OptionalArg < Option < isize > > ,
849
- end : OptionalArg < Option < isize > > ,
850
- ) -> isize {
851
- self . _find ( sub, start, end, |r, s| r. rfind ( s) )
834
+ fn rfind ( & self , args : FindArgs ) -> isize {
835
+ self . _find ( args, |r, s| r. rfind ( s) )
852
836
. map_or ( -1 , |v| v as isize )
853
837
}
854
838
855
839
#[ pymethod]
856
- fn index (
857
- & self ,
858
- sub : PyStringRef ,
859
- start : OptionalArg < Option < isize > > ,
860
- end : OptionalArg < Option < isize > > ,
861
- vm : & VirtualMachine ,
862
- ) -> PyResult < usize > {
863
- self . _find ( sub, start, end, |r, s| r. find ( s) )
840
+ fn index ( & self , args : FindArgs , vm : & VirtualMachine ) -> PyResult < usize > {
841
+ self . _find ( args, |r, s| r. find ( s) )
864
842
. ok_or_else ( || vm. new_value_error ( "substring not found" . to_owned ( ) ) )
865
843
}
866
844
867
845
#[ pymethod]
868
- fn rindex (
869
- & self ,
870
- sub : PyStringRef ,
871
- start : OptionalArg < Option < isize > > ,
872
- end : OptionalArg < Option < isize > > ,
873
- vm : & VirtualMachine ,
874
- ) -> PyResult < usize > {
875
- self . _find ( sub, start, end, |r, s| r. rfind ( s) )
846
+ fn rindex ( & self , args : FindArgs , vm : & VirtualMachine ) -> PyResult < usize > {
847
+ self . _find ( args, |r, s| r. rfind ( s) )
876
848
. ok_or_else ( || vm. new_value_error ( "substring not found" . to_owned ( ) ) )
877
849
}
878
850
@@ -947,15 +919,10 @@ impl PyString {
947
919
}
948
920
949
921
#[ pymethod]
950
- fn count (
951
- & self ,
952
- sub : PyStringRef ,
953
- start : OptionalArg < Option < isize > > ,
954
- end : OptionalArg < Option < isize > > ,
955
- ) -> usize {
956
- let range = adjust_indices ( start, end, self . value . len ( ) ) ;
922
+ fn count ( & self , args : FindArgs ) -> usize {
923
+ let ( needle, range) = args. get_value ( self . len ( ) ) ;
957
924
self . value
958
- . py_count ( & sub . value , range, |h, n| h. matches ( n) . count ( ) )
925
+ . py_count ( & needle . value , range, |h, n| h. matches ( n) . count ( ) )
959
926
}
960
927
961
928
#[ pymethod]
@@ -1256,6 +1223,23 @@ impl TryFromObject for std::ffi::CString {
1256
1223
1257
1224
type SplitArgs = pystr:: SplitArgs < PyStringRef , str , char > ;
1258
1225
1226
+ #[ derive( FromArgs ) ]
1227
+ pub struct FindArgs {
1228
+ #[ pyarg( positional_only, optional = false ) ]
1229
+ sub : PyStringRef ,
1230
+ #[ pyarg( positional_only, default = "None" ) ]
1231
+ start : Option < PyIntRef > ,
1232
+ #[ pyarg( positional_only, default = "None" ) ]
1233
+ end : Option < PyIntRef > ,
1234
+ }
1235
+
1236
+ impl FindArgs {
1237
+ fn get_value ( self , len : usize ) -> ( PyStringRef , std:: ops:: Range < usize > ) {
1238
+ let range = adjust_indices ( self . start , self . end , len) ;
1239
+ ( self . sub , range)
1240
+ }
1241
+ }
1242
+
1259
1243
pub fn init ( ctx : & PyContext ) {
1260
1244
PyString :: extend_class ( ctx, & ctx. types . str_type ) ;
1261
1245
0 commit comments