File tree Expand file tree Collapse file tree 1 file changed +17
-9
lines changed Expand file tree Collapse file tree 1 file changed +17
-9
lines changed Original file line number Diff line number Diff line change @@ -456,30 +456,38 @@ impl fmt::Display for EscapeQuotedString<'_> {
456
456
// | `"A\"B\"A"` | default | `DoubleQuotedString(String::from("A\"B\"A"))` | `"A""B""A"` |
457
457
let quote = self . quote ;
458
458
let mut previous_char = char:: default ( ) ;
459
- let mut peekable_chars = self . string . chars ( ) . peekable ( ) ;
460
- while let Some ( & ch) = peekable_chars. peek ( ) {
459
+ let mut start_idx = 0 ;
460
+ let mut peekable_chars = self . string . char_indices ( ) . peekable ( ) ;
461
+ while let Some ( & ( idx, ch) ) = peekable_chars. peek ( ) {
461
462
match ch {
462
463
char if char == quote => {
463
464
if previous_char == '\\' {
464
- write ! ( f , "{char}" ) ? ;
465
+ // the quote is already escaped with a backslash, skip
465
466
peekable_chars. next ( ) ;
466
467
continue ;
467
468
}
468
469
peekable_chars. next ( ) ;
469
- if peekable_chars. peek ( ) . map ( |c| * c == quote) . unwrap_or ( false ) {
470
- write ! ( f, "{char}{char}" ) ?;
471
- peekable_chars. next ( ) ;
472
- } else {
473
- write ! ( f, "{char}{char}" ) ?;
470
+ match peekable_chars. peek ( ) {
471
+ Some ( ( _, c) ) if * c == quote => {
472
+ // the quote is already escaped with another quote, skip
473
+ peekable_chars. next ( ) ;
474
+ }
475
+ _ => {
476
+ // The quote is not escaped.
477
+ // Including idx in the range, so the quote at idx will be printed twice:
478
+ // in this call to write_str() and in the next one.
479
+ f. write_str ( & self . string [ start_idx..=idx] ) ?;
480
+ start_idx = idx;
481
+ }
474
482
}
475
483
}
476
484
_ => {
477
- write ! ( f, "{ch}" ) ?;
478
485
peekable_chars. next ( ) ;
479
486
}
480
487
}
481
488
previous_char = ch;
482
489
}
490
+ f. write_str ( & self . string [ start_idx..] ) ?;
483
491
Ok ( ( ) )
484
492
}
485
493
}
You can’t perform that action at this time.
0 commit comments