about summary refs log tree commit diff
path: root/tvix/nix-compat/src/derivation/string_escape.rs
blob: 0e1dbe516f7313c0c8a20f9114889a499ccc26e8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const STRING_ESCAPER: [(char, &str); 5] = [
    ('\\', "\\\\"),
    ('\n', "\\n"),
    ('\r', "\\r"),
    ('\t', "\\t"),
    ('\"', "\\\""),
];

pub fn escape_string(s: &str) -> String {
    let mut s_replaced = s.to_string();

    for escape_sequence in STRING_ESCAPER {
        s_replaced = s_replaced.replace(escape_sequence.0, escape_sequence.1);
    }

    format!("\"{}\"", s_replaced)
}