1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//! Library for parsing escape characters

/// Escape [ASCII escapes](https://doc.rust-lang.org/reference/tokens.html#ascii-escapes) in `input`
///
/// Turns sequences that look like escape characters into actual escape characters, i.e. a
/// backslash followed by an 'n' turns into a proper newline character.
/// The only difference between ASCII escapes and Byte escapes is that the maximum value for a hex
/// escape in `escape_ascii` is 0x7F.
pub fn escape_ascii(input: &str) -> Result<String, std::string::FromUtf8Error> {
    if input.len() < 1 {
        return Ok(String::new());
    }

    let mut v = Vec::from(input);
    let mut i = 0;
    while i < v.len() - 1 {
        if v[i] == '\\' as u8 {
            if is_simple_escape(v[i + 1] as char) {
                v.remove(i);
                v[i] = char_to_escape_sequence(v[i] as char) as u8;
            } else if is_complex_escape(v[i + 1] as char) {
                if v[i + 1] == 'x' as u8 {
                    v.remove(i);
                    v.remove(i);
                    let sixteens = ascii_to_hex(v.remove(i));
                    let ones = ascii_to_hex(*v.get(i).unwrap());
                    v[i] = (sixteens << 4) | ones;
                }
            }
        }
        i += 1;
    }
    String::from_utf8(v)
}

/// Escape [Byte escapes](https://doc.rust-lang.org/reference/tokens.html#byte-escapes) in `input`
///
/// Turns sequences that look like escape characters into actual escape characters, i.e. a
/// backslash followed by an 'n' turns into a proper newline character.
///
/// The only difference between Byte escapes and ASCII escapes is that the maximum value for a hex
/// escape in `escape_bytes` is 0xFF.
pub fn escape_bytes(input: &str) -> Result<String, std::string::FromUtf8Error> {
    escape_ascii(input)
}

/// Escape [Unicode escapes](https://doc.rust-lang.org/reference/tokens.html#unicode-escapes) in
/// `input`
pub fn escape_unicode(_input: &str) -> Result<String, std::string::FromUtf8Error> {
    unimplemented!("`escape_unicode` is not yet implemented");
}

/// Escape [Quote escapes](https://doc.rust-lang.org/reference/tokens.html#quote-escapes) in
/// `input`
pub fn escape_quotes(_input: &str) -> Result<String, std::string::FromUtf8Error> {
    unimplemented!("`escape_quotes` is not yet implemented");
}

fn char_to_escape_sequence(chr: char) -> char {
    match chr {
        'n' => '\n',
        't' => '\t',
        'r' => '\r',
        '\\' => '\\',
        '0' => '\0',
        _ => chr,
    }
}

fn is_simple_escape(chr: char) -> bool {
    match chr {
        'n' | 't' | 'r' | '\\' | '0' => true,
        _ => false,
    }
}

fn is_complex_escape(chr: char) -> bool {
    match chr {
        'x' | 'u' => true,
        _ => false,
    }
}

fn ascii_to_hex(x: u8) -> u8 {
    match x as char {
        '0' => 0,
        '1' => 1,
        '2' => 2,
        '3' => 3,
        '4' => 4,
        '5' => 5,
        '6' => 6,
        '7' => 7,
        '8' => 8,
        '9' => 9,
        'a' | 'A' => 10,
        'b' | 'B' => 11,
        'c' | 'C' => 12,
        'd' | 'D' => 13,
        'e' | 'E' => 14,
        'f' | 'F' => 15,
        _ => panic!("expected hex value"),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    mod test_process_escapes {
        use super::*;

        #[test]
        fn test_newline() {
            assert_eq!(
                String::from("hello\nworld"),
                escape_ascii(r#"hello\nworld"#).unwrap()
            );
        }

        #[test]
        fn test_carriage_return() {
            assert_eq!(
                String::from("hello\rworld"),
                escape_ascii(r#"hello\rworld"#).unwrap()
            );
        }

        #[test]
        fn test_tab() {
            assert_eq!(
                String::from("hello\tworld"),
                escape_ascii(r#"hello\tworld"#).unwrap()
            );
        }

        #[test]
        fn test_backslash() {
            assert_eq!(
                String::from("hello\\world"),
                escape_ascii(r#"hello\\world"#).unwrap()
            );
        }

        #[test]
        fn test_null() {
            assert_eq!(
                String::from("hello\0world"),
                escape_ascii(r#"hello\0world"#).unwrap()
            );
        }

        #[test]
        fn test_ascii_byte() {
            assert_eq!(
                String::from("hello\x20world"),
                escape_ascii(r#"hello\x20world"#).unwrap()
            );
        }

        #[test]
        fn test_newline_bytes() {
            assert_eq!(
                String::from("hello\nworld"),
                escape_bytes(r#"hello\nworld"#).unwrap()
            );
        }

        #[test]
        fn test_carriage_return_bytes() {
            assert_eq!(
                String::from("hello\rworld"),
                escape_bytes(r#"hello\rworld"#).unwrap()
            );
        }

        #[test]
        fn test_tab_bytes() {
            assert_eq!(
                String::from("hello\tworld"),
                escape_bytes(r#"hello\tworld"#).unwrap()
            );
        }

        #[test]
        fn test_backslash_bytes() {
            assert_eq!(
                String::from("hello\\world"),
                escape_bytes(r#"hello\\world"#).unwrap()
            );
        }

        #[test]
        fn test_null_bytes() {
            assert_eq!(
                String::from("hello\0world"),
                escape_bytes(r#"hello\0world"#).unwrap()
            );
        }
        #[test]
        fn test_non_ascii_byte() {
            assert_eq!(
                String::from("hello\x7fworld"),
                escape_bytes(r#"hello\x7fworld"#).unwrap()
            );
        }

        #[test]
        fn test_unicode_u7fff() {
            assert_eq!(
                String::from("Hello\u{7fff}world"),
                escape_unicode(r#"Hello\u{7fff}world"#).unwrap()
            );
        }

        #[test]
        fn test_unicode_crab_emoji() {
            assert_eq!(
                String::from("Hello🦀world"),
                escape_unicode(r#"Hello\u{1f980}world"#).unwrap()
            );
        }

        #[test]
        fn test_escape_at_end() {
            assert_eq!(
                String::from("Hello world\n"),
                escape_ascii(r#"Hello world\n"#).unwrap()
            );
        }

        #[test]
        fn test_complex_escape_at_end() {
            assert_eq!(
                String::from("Hello world\x20"),
                escape_ascii(r#"Hello world\x20"#).unwrap()
            );
        }

        #[test]
        fn test_trailing_backslash() {
            assert_eq!(
                String::from("Hello world\\"),
                escape_ascii(r#"Hello world\"#).unwrap()
            );
        }
    }

    mod test_char_to_escape_sequence {
        use super::*;
        #[test]
        fn test_escape_n() {
            assert_eq!('\n', char_to_escape_sequence('n'));
        }

        #[test]
        fn test_escape_t() {
            assert_eq!('\t', char_to_escape_sequence('t'));
        }

        #[test]
        fn test_escape_r() {
            assert_eq!('\r', char_to_escape_sequence('r'));
        }

        #[test]
        fn test_escape_backslash() {
            assert_eq!('\\', char_to_escape_sequence('\\'));
        }

        #[test]
        fn test_escape_0() {
            assert_eq!('\0', char_to_escape_sequence('0'));
        }

        #[test]
        fn test_esacpe_x() {
            assert_eq!('\x7f', char_to_escape_sequence(0x7f as char));
        }
    }

    mod is_simple_escape_tests {
        use super::*;

        #[test]
        fn test_escape_n() {
            assert!(is_simple_escape('n'));
        }

        #[test]
        fn test_escape_t() {
            assert!(is_simple_escape('t'));
        }

        #[test]
        fn test_escape_r() {
            assert!(is_simple_escape('r'));
        }

        #[test]
        fn test_escape_backslash() {
            assert!(is_simple_escape('\\'));
        }

        #[test]
        fn test_escape_0() {
            assert!(is_simple_escape('0'));
        }
    }
}