@@ -58,69 +58,57 @@ pub enum EscapeError {
5858 NonAsciiCharInByteString ,
5959}
6060
61- /// Takes a contents of a char literal (without quotes), and returns an
62- /// unescaped char or an error
63- pub fn unescape_char ( literal_text : & str ) -> Result < char , ( usize , EscapeError ) > {
64- let mut chars = literal_text. chars ( ) ;
65- unescape_char_or_byte ( & mut chars, Mode :: Char )
66- . map_err ( |err| ( literal_text. len ( ) - chars. as_str ( ) . len ( ) , err) )
67- }
68-
69- /// Takes a contents of a byte literal (without quotes), and returns an
70- /// unescaped byte or an error.
71- pub fn unescape_byte ( literal_text : & str ) -> Result < u8 , ( usize , EscapeError ) > {
72- let mut chars = literal_text. chars ( ) ;
73- unescape_char_or_byte ( & mut chars, Mode :: Byte )
74- . map ( byte_from_char)
75- . map_err ( |err| ( literal_text. len ( ) - chars. as_str ( ) . len ( ) , err) )
76- }
77-
78- /// Takes a contents of a string literal (without quotes) and produces a
61+ /// Takes a contents of a literal (without quotes) and produces a
7962/// sequence of escaped characters or errors.
8063/// Values are returned through invoking of the provided callback.
81- pub fn unescape_str < F > ( literal_text : & str , callback : & mut F )
64+ pub fn unescape_literal < F > ( literal_text : & str , mode : Mode , callback : & mut F )
8265where
8366 F : FnMut ( Range < usize > , Result < char , EscapeError > ) ,
8467{
85- unescape_str_or_byte_str ( literal_text, Mode :: Str , callback)
68+ match mode {
69+ Mode :: Char | Mode :: Byte => {
70+ let mut chars = literal_text. chars ( ) ;
71+ let result = unescape_char_or_byte ( & mut chars, mode) ;
72+ // The Chars iterator moved forward.
73+ callback ( 0 ..( literal_text. len ( ) - chars. as_str ( ) . len ( ) ) , result) ;
74+ }
75+ Mode :: Str | Mode :: ByteStr => unescape_str_or_byte_str ( literal_text, mode, callback) ,
76+ // NOTE: Raw strings do not perform any explicit character escaping, here we
77+ // only translate CRLF to LF and produce errors on bare CR.
78+ Mode :: RawStr | Mode :: RawByteStr => {
79+ unescape_raw_str_or_byte_str ( literal_text, mode, callback)
80+ }
81+ }
8682}
8783
88- /// Takes a contents of a byte string literal (without quotes) and produces a
89- /// sequence of bytes or errors.
84+ /// Takes a contents of a byte, byte string or raw byte string (without quotes)
85+ /// and produces a sequence of bytes or errors.
9086/// Values are returned through invoking of the provided callback.
91- pub fn unescape_byte_str < F > ( literal_text : & str , callback : & mut F )
87+ pub fn unescape_byte_literal < F > ( literal_text : & str , mode : Mode , callback : & mut F )
9288where
9389 F : FnMut ( Range < usize > , Result < u8 , EscapeError > ) ,
9490{
95- unescape_str_or_byte_str ( literal_text, Mode :: ByteStr , & mut |range, char| {
96- callback ( range, char. map ( byte_from_char) )
91+ assert ! ( mode. is_bytes( ) ) ;
92+ unescape_literal ( literal_text, mode, & mut |range, result| {
93+ callback ( range, result. map ( byte_from_char) ) ;
9794 } )
9895}
9996
100- /// Takes a contents of a raw string literal (without quotes) and produces a
101- /// sequence of characters or errors.
102- /// Values are returned through invoking of the provided callback.
103- /// NOTE: Raw strings do not perform any explicit character escaping, here we
104- /// only translate CRLF to LF and produce errors on bare CR.
105- pub fn unescape_raw_str < F > ( literal_text : & str , callback : & mut F )
106- where
107- F : FnMut ( Range < usize > , Result < char , EscapeError > ) ,
108- {
109- unescape_raw_str_or_byte_str ( literal_text, Mode :: Str , callback)
97+ /// Takes a contents of a char literal (without quotes), and returns an
98+ /// unescaped char or an error
99+ pub fn unescape_char ( literal_text : & str ) -> Result < char , ( usize , EscapeError ) > {
100+ let mut chars = literal_text. chars ( ) ;
101+ unescape_char_or_byte ( & mut chars, Mode :: Char )
102+ . map_err ( |err| ( literal_text. len ( ) - chars. as_str ( ) . len ( ) , err) )
110103}
111104
112- /// Takes a contents of a raw byte string literal (without quotes) and produces a
113- /// sequence of bytes or errors.
114- /// Values are returned through invoking of the provided callback.
115- /// NOTE: Raw strings do not perform any explicit character escaping, here we
116- /// only translate CRLF to LF and produce errors on bare CR.
117- pub fn unescape_raw_byte_str < F > ( literal_text : & str , callback : & mut F )
118- where
119- F : FnMut ( Range < usize > , Result < u8 , EscapeError > ) ,
120- {
121- unescape_raw_str_or_byte_str ( literal_text, Mode :: ByteStr , & mut |range, char| {
122- callback ( range, char. map ( byte_from_char) )
123- } )
105+ /// Takes a contents of a byte literal (without quotes), and returns an
106+ /// unescaped byte or an error.
107+ pub fn unescape_byte ( literal_text : & str ) -> Result < u8 , ( usize , EscapeError ) > {
108+ let mut chars = literal_text. chars ( ) ;
109+ unescape_char_or_byte ( & mut chars, Mode :: Byte )
110+ . map ( byte_from_char)
111+ . map_err ( |err| ( literal_text. len ( ) - chars. as_str ( ) . len ( ) , err) )
124112}
125113
126114/// What kind of literal do we parse.
@@ -130,13 +118,15 @@ pub enum Mode {
130118 Str ,
131119 Byte ,
132120 ByteStr ,
121+ RawStr ,
122+ RawByteStr ,
133123}
134124
135125impl Mode {
136126 pub fn in_single_quotes ( self ) -> bool {
137127 match self {
138128 Mode :: Char | Mode :: Byte => true ,
139- Mode :: Str | Mode :: ByteStr => false ,
129+ Mode :: Str | Mode :: ByteStr | Mode :: RawStr | Mode :: RawByteStr => false ,
140130 }
141131 }
142132
@@ -146,8 +136,8 @@ impl Mode {
146136
147137 pub fn is_bytes ( self ) -> bool {
148138 match self {
149- Mode :: Byte | Mode :: ByteStr => true ,
150- Mode :: Char | Mode :: Str => false ,
139+ Mode :: Byte | Mode :: ByteStr | Mode :: RawByteStr => true ,
140+ Mode :: Char | Mode :: Str | Mode :: RawStr => false ,
151141 }
152142 }
153143}
@@ -345,7 +335,7 @@ where
345335
346336fn byte_from_char ( c : char ) -> u8 {
347337 let res = c as u32 ;
348- assert ! ( res <= u8 :: max_value( ) as u32 , "guaranteed because of Mode::Byte(Str) " ) ;
338+ assert ! ( res <= u8 :: max_value( ) as u32 , "guaranteed because of Mode::ByteStr " ) ;
349339 res as u8
350340}
351341
0 commit comments