Skip to main content

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// • A NUL, which must be escaped rather than written raw
9// • Case-insensitive sign-insensitive float "keywords" inf, infinity and nan.
10static NEEDS_QUOTING_REGEX: LazyLock<Regex> = LazyLock::new(|| {
11    Regex::new(r#"[\[\]:`\{\}#'";\(\)\|\$,\.\d\s\x00!?=]|(?i)^[+\-]?(inf(inity)?|nan)$"#)
12        .expect("internal error: NEEDS_QUOTING_REGEX didn't compile")
13});
14
15pub fn needs_quoting(string: &str) -> bool {
16    if string.is_empty() {
17        return true;
18    }
19    // These are case-sensitive keywords
20    match string {
21        // `true`/`false`/`null` are active keywords in JSON and NUON
22        // `&&` is denied by the nu parser for diagnostics reasons
23        // (https://github.com/nushell/nushell/pull/7241)
24        "true" | "false" | "null" | "&&" => return true,
25        _ => (),
26    };
27    // All other cases are handled here
28    NEEDS_QUOTING_REGEX.is_match(string).unwrap_or(false)
29}
30
31pub fn escape_quote_string(string: &str) -> String {
32    let mut output = String::with_capacity(string.len() + 2);
33    output.push('"');
34
35    for c in string.chars() {
36        match c {
37            '"' | '\\' => {
38                output.push('\\');
39                output.push(c);
40            }
41            // A raw NUL is rejected by the reader, so the writer must escape it rather
42            // than emit a byte its own reader refuses.
43            '\0' => output.push_str("\\0"),
44            _ => output.push(c),
45        }
46    }
47
48    output.push('"');
49    output
50}
51
52/// Returns a raw string representation if the string contains quotes or backslashes.
53/// Otherwise returns None (caller should use regular quoting or bare string).
54///
55/// A string containing a NUL also returns None, even when it has a quote or a backslash: a raw
56/// string reproduces its content byte for byte, so it cannot carry the `\0` escape and a NUL
57/// inside one would be written verbatim.
58///
59/// Raw strings avoid escaping by using `r#'...'#` syntax with enough `#` characters
60/// to ensure the closing delimiter is unambiguous.
61///
62/// Note: Nushell requires at least one `#` in raw strings (i.e., `r#'...'#` not `r'...'`).
63pub fn as_raw_string(s: &str) -> Option<String> {
64    // Only use raw strings if they would avoid escaping. The NUL check comes first so the
65    // scan stays single-pass: a raw string cannot represent `\0` at all, so a NUL-bearing
66    // value has to fall through to `escape_quote_string`, which writes the escape.
67    if s.contains('\0') || !s.contains(['"', '\\']) {
68        return None;
69    }
70
71    // Find minimum # count needed for delimiter.
72    // Nushell requires at least one #, so start at 1.
73    // Need to avoid both:
74    // - `'#...#` patterns in content that would close early
75    // - leading `###...` content, because the opening quote plus the first
76    //   `###` would also be parsed as a closing delimiter
77    let mut hash_count = 1;
78    loop {
79        let hashes = "#".repeat(hash_count);
80        let closing = format!("'{}", hashes);
81
82        if !s.starts_with(&hashes) && !s.contains(&closing) {
83            return Some(format!("r{hashes}'{s}'{hashes}"));
84        }
85
86        hash_count += 1;
87    }
88}
89
90#[cfg(test)]
91mod tests {
92    use super::{as_raw_string, escape_quote_string, needs_quoting};
93
94    #[test]
95    fn escape_quote_string_escapes_nul_as_backslash_zero() {
96        // The reader is specified to reject a raw NUL, so the writer must not emit one.
97        assert_eq!(escape_quote_string("a\0b"), r#""a\0b""#);
98        assert_eq!(escape_quote_string("\0"), r#""\0""#);
99        assert!(
100            !escape_quote_string("\0").contains('\0'),
101            "the escaped form must not contain a raw NUL"
102        );
103    }
104
105    #[test]
106    fn escape_quote_string_still_escapes_quotes_and_backslashes() {
107        // The NUL arm must not have displaced the existing escapes.
108        assert_eq!(escape_quote_string(r#"a"b"#), r#""a\"b""#);
109        assert_eq!(escape_quote_string(r"a\b"), r#""a\\b""#);
110        assert_eq!(escape_quote_string("a\0b\"c"), r#""a\0b\"c""#);
111    }
112
113    #[test]
114    fn needs_quoting_forces_a_nul_to_be_quoted() {
115        // Without this, a NUL-bearing string is written as a bare word with a raw NUL
116        // inside it, which no conforming reader will accept.
117        assert!(needs_quoting("a\0b"));
118        assert!(needs_quoting("\0"));
119        // ...and nothing else changed: ordinary strings still take the fast path.
120        assert!(!needs_quoting("hello"));
121        assert!(!needs_quoting("plain_name"));
122    }
123
124    #[test]
125    fn raw_string_uses_single_hash_when_safe() {
126        assert_eq!(
127            as_raw_string(r#"hello \"world\""#),
128            Some(r#"r#'hello \"world\"'#"#.to_string())
129        );
130    }
131
132    #[test]
133    fn raw_string_is_refused_when_the_value_has_a_nul() {
134        // A raw string cannot represent the `\0` escape, so one would come back out
135        // as a raw NUL. Refusing it hands the value to `escape_quote_string` instead.
136        for value in ["a\0\"b", "\0\"", "a\\b\0"] {
137            assert_eq!(
138                as_raw_string(value),
139                None,
140                "a NUL-bearing value must not take the raw form: {value:?}"
141            );
142        }
143
144        // And nothing else changed: a NUL-free value that needs escaping still gets it.
145        assert_eq!(
146            as_raw_string(r#"hello \"world\""#),
147            Some(r#"r#'hello \"world\"'#"#.to_string())
148        );
149    }
150
151    #[test]
152    fn raw_string_uses_more_hashes_for_quote_hash_sequence() {
153        assert_eq!(
154            as_raw_string(r#"contains '# and "quote""#),
155            Some(r##"r##'contains '# and "quote"'##"##.to_string())
156        );
157    }
158
159    #[test]
160    fn raw_string_uses_more_hashes_when_content_starts_with_hash() {
161        let input = "# example.toml\nname = \"my-app\"\nversion = \"1.0.0\"\n";
162
163        assert_eq!(
164            as_raw_string(input),
165            Some(
166                r##"r##'# example.toml
167name = "my-app"
168version = "1.0.0"
169'##"##
170                    .to_string()
171            )
172        );
173    }
174
175    #[test]
176    fn raw_string_scales_hash_count_for_longer_sequences() {
177        assert_eq!(
178            as_raw_string(r#"contains '## and "quote""#),
179            Some(r###"r###'contains '## and "quote"'###"###.to_string())
180        );
181    }
182}