luaur_common/functions/
escape.rs1use crate::functions::format_append::formatAppend;
2use alloc::string::String;
3
4pub fn escape(s: &str, escape_for_interp_string: bool) -> String {
5 let mut r = String::with_capacity(s.len() + 50);
6
7 for &c in s.as_bytes() {
8 if c >= b' ' && c != b'\\' && c != b'\'' && c != b'\"' && c != b'`' && c != b'{' {
9 r.push(c as char);
10 } else {
11 r.push('\\');
12
13 if escape_for_interp_string && (c == b'`' || c == b'{') {
14 r.push(c as char);
15 continue;
16 }
17
18 match c {
19 7 => r.push('a'), 8 => r.push('b'), 12 => r.push('f'), 10 => r.push('n'), 13 => r.push('r'), 9 => r.push('t'), 11 => r.push('v'), b'\'' => r.push('\''),
27 b'\"' => r.push('\"'),
28 b'\\' => r.push('\\'),
29 _ => formatAppend(&mut r, format_args!("{:03}", c)),
31 }
32 }
33 }
34
35 r
36}