nu_utils/
quoting.rs

1use fancy_regex::Regex;
2use std::sync::LazyLock;
3
4// This hits, in order:
5// • Any character of []:`{}#'";()|$,.!?
6// • Any digit (\d)
7// • Any whitespace (\s)
8// • Case-insensitive sign-insensitive float "keywords" inf, infinity and nan.
9static NEEDS_QUOTING_REGEX: LazyLock<Regex> = LazyLock::new(|| {
10    Regex::new(r#"[\[\]:`\{\}#'";\(\)\|\$,\.\d\s!?]|(?i)^[+\-]?(inf(inity)?|nan)$"#)
11        .expect("internal error: NEEDS_QUOTING_REGEX didn't compile")
12});
13
14pub fn needs_quoting(string: &str) -> bool {
15    if string.is_empty() {
16        return true;
17    }
18    // These are case-sensitive keywords
19    match string {
20        // `true`/`false`/`null` are active keywords in JSON and NUON
21        // `&&` is denied by the nu parser for diagnostics reasons
22        // (https://github.com/nushell/nushell/pull/7241)
23        "true" | "false" | "null" | "&&" => return true,
24        _ => (),
25    };
26    // All other cases are handled here
27    NEEDS_QUOTING_REGEX.is_match(string).unwrap_or(false)
28}
29
30pub fn escape_quote_string(string: &str) -> String {
31    let mut output = String::with_capacity(string.len() + 2);
32    output.push('"');
33
34    for c in string.chars() {
35        if c == '"' || c == '\\' {
36            output.push('\\');
37        }
38        output.push(c);
39    }
40
41    output.push('"');
42    output
43}