litrs/
escape.rs

1use crate::{ParseError, err::{perr, ParseErrorKind::*}, parse::hex_digit_value};
2
3
4/// Must start with `\`
5pub(crate) fn unescape<E: Escapee>(input: &str, offset: usize) -> Result<(E, usize), ParseError> {
6    let first = input.as_bytes().get(1)
7        .ok_or(perr(offset, UnterminatedEscape))?;
8    let out = match first {
9        // Quote escapes
10        b'\'' => (E::from_byte(b'\''), 2),
11        b'"' => (E::from_byte(b'"'), 2),
12
13        // Ascii escapes
14        b'n' => (E::from_byte(b'\n'), 2),
15        b'r' => (E::from_byte(b'\r'), 2),
16        b't' => (E::from_byte(b'\t'), 2),
17        b'\\' => (E::from_byte(b'\\'), 2),
18        b'0' => (E::from_byte(b'\0'), 2),
19        b'x' => {
20            let hex_string = input.get(2..4)
21                .ok_or(perr(offset..offset + input.len(), UnterminatedEscape))?
22                .as_bytes();
23            let first = hex_digit_value(hex_string[0])
24                .ok_or(perr(offset..offset + 4, InvalidXEscape))?;
25            let second = hex_digit_value(hex_string[1])
26                .ok_or(perr(offset..offset + 4, InvalidXEscape))?;
27            let value = second + 16 * first;
28
29            if E::SUPPORTS_UNICODE && value > 0x7F {
30                return Err(perr(offset..offset + 4, NonAsciiXEscape));
31            }
32
33            (E::from_byte(value), 4)
34        },
35
36        // Unicode escape
37        b'u' => {
38            if !E::SUPPORTS_UNICODE {
39                return Err(perr(offset..offset + 2, UnicodeEscapeInByteLiteral));
40            }
41
42            if input.as_bytes().get(2) != Some(&b'{') {
43                return Err(perr(offset..offset + 2, UnicodeEscapeWithoutBrace));
44            }
45
46            let closing_pos = input.bytes().position(|b| b == b'}')
47                .ok_or(perr(offset..offset + input.len(), UnterminatedUnicodeEscape))?;
48
49            let inner = &input[3..closing_pos];
50            if inner.as_bytes().first() == Some(&b'_') {
51                return Err(perr(4, InvalidStartOfUnicodeEscape));
52            }
53
54            let mut v: u32 = 0;
55            let mut digit_count = 0;
56            for (i, b) in inner.bytes().enumerate() {
57                if b == b'_'{
58                    continue;
59                }
60
61                let digit = hex_digit_value(b)
62                    .ok_or(perr(offset + 3 + i, NonHexDigitInUnicodeEscape))?;
63
64                if digit_count == 6 {
65                    return Err(perr(offset + 3 + i, TooManyDigitInUnicodeEscape));
66                }
67                digit_count += 1;
68                v = 16 * v + digit as u32;
69            }
70
71            let c = std::char::from_u32(v)
72                .ok_or(perr(offset..closing_pos + 1, InvalidUnicodeEscapeChar))?;
73
74            (E::from_char(c), closing_pos + 1)
75        }
76
77        _ => return Err(perr(offset..offset + 2, UnknownEscape)),
78    };
79
80    Ok(out)
81}
82
83pub(crate) trait Escapee: Into<char> {
84    const SUPPORTS_UNICODE: bool;
85    fn from_byte(b: u8) -> Self;
86    fn from_char(c: char) -> Self;
87}
88
89impl Escapee for u8 {
90    const SUPPORTS_UNICODE: bool = false;
91    fn from_byte(b: u8) -> Self {
92        b
93    }
94    fn from_char(_: char) -> Self {
95        panic!("bug: `<u8 as Escapee>::from_char` was called");
96    }
97}
98
99impl Escapee for char {
100    const SUPPORTS_UNICODE: bool = true;
101    fn from_byte(b: u8) -> Self {
102        b.into()
103    }
104    fn from_char(c: char) -> Self {
105        c
106    }
107}
108
109/// Checks whether the character is skipped after a string continue start
110/// (unescaped backlash followed by `\n`).
111pub(crate) fn is_string_continue_skipable_whitespace(b: u8) -> bool {
112    b == b' ' || b == b'\t' || b == b'\n' || b == b'\r'
113}
114
115/// Unescapes a whole string or byte string.
116pub(crate) fn unescape_string<E: Escapee>(
117    input: &str,
118    offset: usize,
119) -> Result<Option<String>, ParseError> {
120    let mut i = offset;
121    let mut end_last_escape = offset;
122    let mut value = String::new();
123    while i < input.len() - 1 {
124        match input.as_bytes()[i] {
125            // Handle "string continue".
126            b'\\' if input.as_bytes()[i + 1] == b'\n' => {
127                value.push_str(&input[end_last_escape..i]);
128
129                // Find the first non-whitespace character.
130                let end_escape = input[i + 2..].bytes()
131                    .position(|b| !is_string_continue_skipable_whitespace(b))
132                    .ok_or(perr(None, UnterminatedString))?;
133
134                i += 2 + end_escape;
135                end_last_escape = i;
136            }
137            b'\\' => {
138                let (c, len) = unescape::<E>(&input[i..input.len() - 1], i)?;
139                value.push_str(&input[end_last_escape..i]);
140                value.push(c.into());
141                i += len;
142                end_last_escape = i;
143            }
144            b'\r' => {
145                if input.as_bytes()[i + 1] == b'\n' {
146                    value.push_str(&input[end_last_escape..i]);
147                    value.push('\n');
148                    i += 2;
149                    end_last_escape = i;
150                } else {
151                    return Err(perr(i, IsolatedCr))
152                }
153            }
154            b'"' => return Err(perr(i + 1..input.len(), UnexpectedChar)),
155            b if !E::SUPPORTS_UNICODE && !b.is_ascii()
156                => return Err(perr(i, NonAsciiInByteLiteral)),
157            _ => i += 1,
158        }
159    }
160
161    if input.as_bytes()[input.len() - 1] != b'"' || input.len() == offset {
162        return Err(perr(None, UnterminatedString));
163    }
164
165    // `value` is only empty if there was no escape in the input string
166    // (with the special case of the input being empty). This means the
167    // string value basically equals the input, so we store `None`.
168    let value = if value.is_empty() {
169        None
170    } else {
171        // There was an escape in the string, so we need to push the
172        // remaining unescaped part of the string still.
173        value.push_str(&input[end_last_escape..input.len() - 1]);
174        Some(value)
175    };
176
177    Ok(value)
178}
179
180/// Reads and checks a raw (byte) string literal, converting `\r\n` sequences to
181/// just `\n` sequences. Returns an optional new string (if the input contained
182/// any `\r\n`) and the number of hashes used by the literal.
183pub(crate) fn scan_raw_string<E: Escapee>(
184    input: &str,
185    offset: usize,
186) -> Result<(Option<String>, u32), ParseError> {
187    // Raw string literal
188    let num_hashes = input[offset..].bytes().position(|b| b != b'#')
189        .ok_or(perr(None, InvalidLiteral))?;
190
191    if input.as_bytes().get(offset + num_hashes) != Some(&b'"') {
192        return Err(perr(None, InvalidLiteral));
193    }
194    let start_inner = offset + num_hashes + 1;
195    let hashes = &input[offset..num_hashes + offset];
196
197    let mut closing_quote_pos = None;
198    let mut i = start_inner;
199    let mut end_last_escape = start_inner;
200    let mut value = String::new();
201    while i < input.len() {
202        let b = input.as_bytes()[i];
203        if b == b'"' && input[i + 1..].starts_with(hashes) {
204            closing_quote_pos = Some(i);
205            break;
206        }
207
208        if b == b'\r' {
209            // Convert `\r\n` into `\n`. This is currently not well documented
210            // in the Rust reference, but is done even for raw strings. That's
211            // because rustc simply converts all line endings when reading
212            // source files.
213            if input.as_bytes().get(i + 1) == Some(&b'\n') {
214                value.push_str(&input[end_last_escape..i]);
215                value.push('\n');
216                i += 2;
217                end_last_escape = i;
218                continue;
219            } else if E::SUPPORTS_UNICODE {
220                // If no \n follows the \r and we are scanning a raw string
221                // (not raw byte string), we error.
222                return Err(perr(i, IsolatedCr))
223            }
224        }
225
226        if !E::SUPPORTS_UNICODE {
227            if !b.is_ascii() {
228                return Err(perr(i, NonAsciiInByteLiteral));
229            }
230        }
231
232        i += 1;
233    }
234
235    let closing_quote_pos = closing_quote_pos
236        .ok_or(perr(None, UnterminatedRawString))?;
237
238    if closing_quote_pos + num_hashes != input.len() - 1 {
239        return Err(perr(closing_quote_pos + num_hashes + 1..input.len(), UnexpectedChar));
240    }
241
242    // `value` is only empty if there was no \r\n in the input string (with the
243    // special case of the input being empty). This means the string value
244    // equals the input, so we store `None`.
245    let value = if value.is_empty() {
246        None
247    } else {
248        // There was an \r\n in the string, so we need to push the remaining
249        // unescaped part of the string still.
250        value.push_str(&input[end_last_escape..closing_quote_pos]);
251        Some(value)
252    };
253
254    Ok((value, num_hashes as u32))
255}