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}
44
45/// Returns a raw string representation if the string contains quotes or backslashes.
46/// Otherwise returns None (caller should use regular quoting or bare string).
47///
48/// Raw strings avoid escaping by using `r#'...'#` syntax with enough `#` characters
49/// to ensure the closing delimiter is unambiguous.
50///
51/// Note: Nushell requires at least one `#` in raw strings (i.e., `r#'...'#` not `r'...'`).
52pub fn as_raw_string(s: &str) -> Option<String> {
53    // Only use raw strings if they would avoid escaping
54    if !s.contains('"') && !s.contains('\\') {
55        return None;
56    }
57
58    // Find minimum # count needed for delimiter
59    // Nushell requires at least one #, so start at 1
60    // Need to avoid `'#...#` patterns in content that would close early
61    let mut hash_count = 1;
62    loop {
63        let closing = format!("'{}", "#".repeat(hash_count));
64        if !s.contains(&closing) {
65            break;
66        }
67        hash_count += 1;
68    }
69
70    let hashes = "#".repeat(hash_count);
71    Some(format!("r{hashes}'{s}'{hashes}"))
72}