@@ -1534,23 +1534,6 @@ pub enum CharEscape {
1534
1534
AsciiControl ( u8 ) ,
1535
1535
}
1536
1536
1537
- impl CharEscape {
1538
- #[ inline]
1539
- fn from_escape_table ( escape : u8 , byte : u8 ) -> CharEscape {
1540
- match escape {
1541
- self :: BB => CharEscape :: Backspace ,
1542
- self :: TT => CharEscape :: Tab ,
1543
- self :: NN => CharEscape :: LineFeed ,
1544
- self :: FF => CharEscape :: FormFeed ,
1545
- self :: RR => CharEscape :: CarriageReturn ,
1546
- self :: QU => CharEscape :: Quote ,
1547
- self :: BS => CharEscape :: ReverseSolidus ,
1548
- self :: UU => CharEscape :: AsciiControl ( byte) ,
1549
- _ => unreachable ! ( ) ,
1550
- }
1551
- }
1552
- }
1553
-
1554
1537
/// This trait abstracts away serializing the JSON control characters, which allows the user to
1555
1538
/// optionally pretty print the JSON output.
1556
1539
pub trait Formatter {
@@ -1784,30 +1767,33 @@ pub trait Formatter {
1784
1767
{
1785
1768
use self :: CharEscape :: * ;
1786
1769
1787
- let s = match char_escape {
1788
- Quote => b"\\ \" " ,
1789
- ReverseSolidus => b"\\ \\ " ,
1790
- Solidus => b"\\ /" ,
1791
- Backspace => b"\\ b" ,
1792
- FormFeed => b"\\ f" ,
1793
- LineFeed => b"\\ n" ,
1794
- CarriageReturn => b"\\ r" ,
1795
- Tab => b"\\ t" ,
1770
+ let escape_char = match char_escape {
1771
+ Quote => b'"' ,
1772
+ ReverseSolidus => b'\\' ,
1773
+ Solidus => b'/' ,
1774
+ Backspace => b'b' ,
1775
+ FormFeed => b'f' ,
1776
+ LineFeed => b'n' ,
1777
+ CarriageReturn => b'r' ,
1778
+ Tab => b't' ,
1779
+ AsciiControl ( _) => b'u' ,
1780
+ } ;
1781
+
1782
+ match char_escape {
1796
1783
AsciiControl ( byte) => {
1797
1784
static HEX_DIGITS : [ u8 ; 16 ] = * b"0123456789abcdef" ;
1798
1785
let bytes = & [
1799
1786
b'\\' ,
1800
- b'u' ,
1787
+ escape_char ,
1801
1788
b'0' ,
1802
1789
b'0' ,
1803
1790
HEX_DIGITS [ ( byte >> 4 ) as usize ] ,
1804
1791
HEX_DIGITS [ ( byte & 0xF ) as usize ] ,
1805
1792
] ;
1806
- return writer. write_all ( bytes) ;
1793
+ writer. write_all ( bytes)
1807
1794
}
1808
- } ;
1809
-
1810
- writer. write_all ( s)
1795
+ _ => writer. write_all ( & [ b'\\' , escape_char] ) ,
1796
+ }
1811
1797
}
1812
1798
1813
1799
/// Writes the representation of a byte array. Formatters can choose whether
@@ -2120,7 +2106,18 @@ where
2120
2106
tri ! ( formatter. write_string_fragment( writer, string_run) ) ;
2121
2107
}
2122
2108
2123
- let char_escape = CharEscape :: from_escape_table ( escape, byte) ;
2109
+ let char_escape = match escape {
2110
+ self :: BB => CharEscape :: Backspace ,
2111
+ self :: TT => CharEscape :: Tab ,
2112
+ self :: NN => CharEscape :: LineFeed ,
2113
+ self :: FF => CharEscape :: FormFeed ,
2114
+ self :: RR => CharEscape :: CarriageReturn ,
2115
+ self :: QU => CharEscape :: Quote ,
2116
+ self :: BS => CharEscape :: ReverseSolidus ,
2117
+ self :: UU => CharEscape :: AsciiControl ( byte) ,
2118
+ // safety: the escape table does not contain any other type of character.
2119
+ _ => unsafe { core:: hint:: unreachable_unchecked ( ) } ,
2120
+ } ;
2124
2121
tri ! ( formatter. write_char_escape( writer, char_escape) ) ;
2125
2122
}
2126
2123
0 commit comments