@@ -184,6 +184,12 @@ struct SplitLineArgs {
184
184
keepends : OptionalArg < bool > ,
185
185
}
186
186
187
+ #[ derive( FromArgs ) ]
188
+ struct ExpandtabsArgs {
189
+ #[ pyarg( positional_or_keyword, optional = true ) ]
190
+ tabsize : OptionalArg < usize > ,
191
+ }
192
+
187
193
#[ pyimpl( flags( BASETYPE ) ) ]
188
194
impl PyString {
189
195
#[ pyslot]
@@ -1079,21 +1085,28 @@ impl PyString {
1079
1085
}
1080
1086
1081
1087
#[ pymethod]
1082
- fn expandtabs ( & self , tab_stop : OptionalArg < usize > ) -> String {
1083
- let tab_stop = tab_stop . into_option ( ) . unwrap_or ( 8 as usize ) ;
1088
+ fn expandtabs ( & self , args : ExpandtabsArgs ) -> String {
1089
+ let tab_stop = args . tabsize . unwrap_or ( 8 ) ;
1084
1090
let mut expanded_str = String :: with_capacity ( self . value . len ( ) ) ;
1085
1091
let mut tab_size = tab_stop;
1086
1092
let mut col_count = 0 as usize ;
1087
1093
for ch in self . value . chars ( ) {
1088
- // 0x0009 is tab
1089
- if ch == 0x0009 as char {
1090
- let num_spaces = tab_size - col_count;
1091
- col_count += num_spaces;
1092
- let expand = " " . repeat ( num_spaces) ;
1093
- expanded_str. push_str ( & expand) ;
1094
- } else {
1095
- expanded_str. push ( ch) ;
1096
- col_count += 1 ;
1094
+ match ch {
1095
+ '\t' => {
1096
+ let num_spaces = tab_size - col_count;
1097
+ col_count += num_spaces;
1098
+ let expand = " " . repeat ( num_spaces) ;
1099
+ expanded_str. push_str ( & expand) ;
1100
+ }
1101
+ '\r' | '\n' => {
1102
+ expanded_str. push ( ch) ;
1103
+ col_count = 0 ;
1104
+ tab_size = 0 ;
1105
+ }
1106
+ _ => {
1107
+ expanded_str. push ( ch) ;
1108
+ col_count += 1 ;
1109
+ }
1097
1110
}
1098
1111
if col_count >= tab_size {
1099
1112
tab_size += tab_stop;
0 commit comments