@@ -281,36 +281,35 @@ impl ByteInnerSplitOptions {
281
281
#[ derive( FromArgs ) ]
282
282
pub struct ByteInnerExpandtabsOptions {
283
283
#[ pyarg( positional_or_keyword, optional = true ) ]
284
- tabsize : OptionalArg < PyObjectRef > ,
284
+ tabsize : OptionalArg < PyIntRef > ,
285
285
}
286
286
287
287
impl ByteInnerExpandtabsOptions {
288
288
pub fn get_value ( self ) -> usize {
289
- if let OptionalArg :: Present ( value) = self . tabsize {
290
- if let Some ( v) = objint:: get_value ( & value) . to_usize ( ) {
291
- v
292
- } else {
293
- 0
294
- }
295
- } else {
296
- 8
289
+ match self . tabsize . into_option ( ) {
290
+ Some ( int) => int. as_bigint ( ) . to_usize ( ) . unwrap_or ( 0 ) ,
291
+ None => 8 ,
297
292
}
298
293
}
299
294
}
300
295
301
296
#[ derive( FromArgs ) ]
302
297
pub struct ByteInnerSplitlinesOptions {
303
298
#[ pyarg( positional_or_keyword, optional = true ) ]
304
- keepends : OptionalArg < PyObjectRef > ,
299
+ keepends : OptionalArg < bool > ,
305
300
}
306
301
307
302
impl ByteInnerSplitlinesOptions {
308
- pub fn get_value ( self , vm : & VirtualMachine ) -> PyResult < bool > {
309
- if let OptionalArg :: Present ( value) = self . keepends {
310
- Ok ( bool:: try_from_object ( vm, value) ?)
311
- } else {
312
- Ok ( false )
303
+ pub fn get_value ( self ) -> bool {
304
+ match self . keepends . into_option ( ) {
305
+ Some ( x) => x,
306
+ None => false ,
313
307
}
308
+ // if let OptionalArg::Present(value) = self.keepends {
309
+ // Ok(bool::try_from_object(vm, value)?)
310
+ // } else {
311
+ // Ok(false)
312
+ // }
314
313
}
315
314
}
316
315
@@ -836,24 +835,11 @@ impl PyByteInner {
836
835
}
837
836
}
838
837
839
- pub fn partition (
840
- & self ,
841
- sep : & PyObjectRef ,
842
- reverse : bool ,
843
- vm : & VirtualMachine ,
844
- ) -> PyResult < ( Vec < u8 > , Vec < u8 > ) > {
845
- let sep = match try_as_bytes_like ( & sep) {
846
- Some ( value) => value,
847
- None => {
848
- return Err (
849
- vm. new_type_error ( format ! ( "a bytes-like object is required, not {}" , sep) )
850
- ) ;
851
- }
852
- } ;
838
+ pub fn partition ( & self , sep : & PyByteInner , reverse : bool ) -> PyResult < ( Vec < u8 > , Vec < u8 > ) > {
853
839
let splitted = if reverse {
854
- split_slice_reverse ( & self . elements , & sep, 1 )
840
+ split_slice_reverse ( & self . elements , & sep. elements , 1 )
855
841
} else {
856
- split_slice ( & self . elements , & sep, 1 )
842
+ split_slice ( & self . elements , & sep. elements , 1 )
857
843
} ;
858
844
Ok ( ( splitted[ 0 ] . to_vec ( ) , splitted[ 1 ] . to_vec ( ) ) )
859
845
}
@@ -890,17 +876,13 @@ impl PyByteInner {
890
876
res
891
877
}
892
878
893
- pub fn splitlines (
894
- & self ,
895
- options : ByteInnerSplitlinesOptions ,
896
- vm : & VirtualMachine ,
897
- ) -> PyResult < Vec < & [ u8 ] > > {
898
- let keepends = options. get_value ( vm) ?;
879
+ pub fn splitlines ( & self , options : ByteInnerSplitlinesOptions ) -> Vec < & [ u8 ] > {
880
+ let keepends = options. get_value ( ) ;
899
881
900
882
let mut res = vec ! [ ] ;
901
883
902
884
if self . elements . is_empty ( ) {
903
- return Ok ( vec ! [ ] ) ;
885
+ return vec ! [ ] ;
904
886
}
905
887
906
888
let mut prev_index = 0 ;
@@ -935,7 +917,7 @@ impl PyByteInner {
935
917
}
936
918
}
937
919
938
- Ok ( res)
920
+ res
939
921
}
940
922
941
923
pub fn zfill ( & self , width : PyIntRef ) -> Vec < u8 > {
@@ -960,37 +942,16 @@ impl PyByteInner {
960
942
961
943
pub fn replace (
962
944
& self ,
963
- old : PyObjectRef ,
964
- new : PyObjectRef ,
945
+ old : PyByteInner ,
946
+ new : PyByteInner ,
965
947
count : OptionalArg < PyIntRef > ,
966
- vm : & VirtualMachine ,
967
948
) -> PyResult < Vec < u8 > > {
968
- let old = match try_as_bytes_like ( & old) {
969
- Some ( value) => value,
970
- None => {
971
- return Err (
972
- vm. new_type_error ( format ! ( "a bytes-like object is required, not {}" , old) )
973
- ) ;
974
- }
975
- } ;
976
-
977
- let new = match try_as_bytes_like ( & new) {
978
- Some ( value) => value,
979
- None => {
980
- return Err (
981
- vm. new_type_error ( format ! ( "a bytes-like object is required, not {}" , new) )
982
- ) ;
983
- }
984
- } ;
985
-
986
- let count = if let OptionalArg :: Present ( int) = count {
987
- if let Some ( value) = int. as_bigint ( ) . to_u32 ( ) {
988
- value
989
- } else {
990
- self . elements . len ( ) as u32
991
- }
992
- } else {
993
- self . elements . len ( ) as u32
949
+ let count = match count. into_option ( ) {
950
+ Some ( int) => int
951
+ . as_bigint ( )
952
+ . to_u32 ( )
953
+ . unwrap_or ( self . elements . len ( ) as u32 ) ,
954
+ None => self . elements . len ( ) as u32 ,
994
955
} ;
995
956
996
957
let mut res = vec ! [ ] ;
@@ -1003,8 +964,8 @@ impl PyByteInner {
1003
964
res. extend_from_slice ( & slice[ index..] ) ;
1004
965
break ;
1005
966
}
1006
- if & slice[ index..index + old. len ( ) ] == old. as_slice ( ) {
1007
- res. extend_from_slice ( & new) ;
967
+ if & slice[ index..index + old. len ( ) ] == old. elements . as_slice ( ) {
968
+ res. extend_from_slice ( & new. elements ) ;
1008
969
index += old. len ( ) ;
1009
970
done += 1 ;
1010
971
} else {
0 commit comments