@@ -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]
@@ -1084,21 +1090,28 @@ impl PyString {
1084
1090
}
1085
1091
1086
1092
#[ pymethod]
1087
- fn expandtabs ( & self , tab_stop : OptionalArg < usize > ) -> String {
1088
- let tab_stop = tab_stop . into_option ( ) . unwrap_or ( 8 as usize ) ;
1093
+ fn expandtabs ( & self , args : ExpandtabsArgs ) -> String {
1094
+ let tab_stop = args . tabsize . unwrap_or ( 8 ) ;
1089
1095
let mut expanded_str = String :: with_capacity ( self . value . len ( ) ) ;
1090
1096
let mut tab_size = tab_stop;
1091
1097
let mut col_count = 0 as usize ;
1092
1098
for ch in self . value . chars ( ) {
1093
- // 0x0009 is tab
1094
- if ch == 0x0009 as char {
1095
- let num_spaces = tab_size - col_count;
1096
- col_count += num_spaces;
1097
- let expand = " " . repeat ( num_spaces) ;
1098
- expanded_str. push_str ( & expand) ;
1099
- } else {
1100
- expanded_str. push ( ch) ;
1101
- col_count += 1 ;
1099
+ match ch {
1100
+ '\t' => {
1101
+ let num_spaces = tab_size - col_count;
1102
+ col_count += num_spaces;
1103
+ let expand = " " . repeat ( num_spaces) ;
1104
+ expanded_str. push_str ( & expand) ;
1105
+ }
1106
+ '\r' | '\n' => {
1107
+ expanded_str. push ( ch) ;
1108
+ col_count = 0 ;
1109
+ tab_size = 0 ;
1110
+ }
1111
+ _ => {
1112
+ expanded_str. push ( ch) ;
1113
+ col_count += 1 ;
1114
+ }
1102
1115
}
1103
1116
if col_count >= tab_size {
1104
1117
tab_size += tab_stop;
0 commit comments