Skip to main content

alef_e2e/
escape.rs

1//! Language-specific string escaping for e2e test code generation.
2
3/// Escape a string for embedding in a Python string literal.
4pub fn escape_python(s: &str) -> String {
5    s.replace('\\', "\\\\")
6        .replace('"', "\\\"")
7        .replace('\n', "\\n")
8        .replace('\r', "\\r")
9        .replace('\t', "\\t")
10}
11
12/// Escape a string for embedding in a Rust string literal.
13pub fn escape_rust(s: &str) -> String {
14    s.replace('\\', "\\\\")
15        .replace('"', "\\\"")
16        .replace('\n', "\\n")
17        .replace('\r', "\\r")
18        .replace('\t', "\\t")
19}
20
21/// Compute the number of # needed for a Rust raw string literal.
22pub fn raw_string_hashes(s: &str) -> usize {
23    let mut max_hashes = 0;
24    let mut current = 0;
25    let mut after_quote = false;
26    for ch in s.chars() {
27        if ch == '"' {
28            after_quote = true;
29            current = 0;
30        } else if ch == '#' && after_quote {
31            current += 1;
32            max_hashes = max_hashes.max(current);
33        } else {
34            after_quote = false;
35            current = 0;
36        }
37    }
38    max_hashes + 1
39}
40
41/// Format a string as a Rust raw string literal (r#"..."#).
42pub fn rust_raw_string(s: &str) -> String {
43    let hashes = raw_string_hashes(s);
44    let h: String = "#".repeat(hashes);
45    format!("r{h}\"{s}\"{h}")
46}
47
48/// Escape a string for embedding in a JavaScript/TypeScript double-quoted string literal.
49///
50/// `$` does not need escaping in double-quoted strings (only in template literals).
51/// Escaping it would produce `\$` which Biome flags as `noUselessEscapeInString`.
52pub fn escape_js(s: &str) -> String {
53    s.replace('\\', "\\\\")
54        .replace('"', "\\\"")
55        .replace('\n', "\\n")
56        .replace('\r', "\\r")
57        .replace('\t', "\\t")
58}
59
60/// Escape a string for embedding in a JavaScript/TypeScript template literal (backtick string).
61///
62/// Template literals interpolate `${...}` and use backtick delimiters, so both
63/// `` ` `` and `$` must be escaped to prevent unintended interpolation.
64pub fn escape_js_template(s: &str) -> String {
65    s.replace('\\', "\\\\").replace('`', "\\`").replace('$', "\\$")
66}
67
68/// Returns `true` if the string must use a Go interpreted (double-quoted) literal
69/// rather than a raw (backtick) literal.
70///
71/// Go raw string literals cannot contain backtick characters or NUL bytes, and
72/// `\r` inside a raw string is passed through as a literal CR which gofmt rejects.
73fn go_needs_quoted(s: &str) -> bool {
74    s.contains('`') || s.bytes().any(|b| b == 0 || b == b'\r')
75}
76
77/// Format a string as a Go string literal (backtick or quoted).
78///
79/// Prefers backtick raw literals for readability, but falls back to double-quoted
80/// interpreted literals when the string contains characters that raw literals
81/// cannot represent: backtick `` ` ``, NUL (`\x00`), or carriage return (`\r`).
82pub fn go_string_literal(s: &str) -> String {
83    if go_needs_quoted(s) {
84        format!("\"{}\"", escape_go(s))
85    } else {
86        format!("`{s}`")
87    }
88}
89
90/// Escape a string for embedding in a Go double-quoted string.
91///
92/// Handles all characters that cannot appear literally in a Go interpreted string:
93/// `\\`, `"`, `\n`, `\r`, `\t`, and NUL (`\x00`). Other non-printable bytes are
94/// emitted as `\xNN` hex escape sequences.
95pub fn escape_go(s: &str) -> String {
96    let mut out = String::with_capacity(s.len());
97    for b in s.bytes() {
98        match b {
99            b'\\' => out.push_str("\\\\"),
100            b'"' => out.push_str("\\\""),
101            b'\n' => out.push_str("\\n"),
102            b'\r' => out.push_str("\\r"),
103            b'\t' => out.push_str("\\t"),
104            0 => out.push_str("\\x00"),
105            // Other control characters or non-ASCII bytes: hex escape.
106            b if b < 0x20 || b == 0x7f => {
107                out.push_str(&format!("\\x{b:02x}"));
108            }
109            _ => out.push(b as char),
110        }
111    }
112    out
113}
114
115/// Escape a string for embedding in a Java string literal.
116pub fn escape_java(s: &str) -> String {
117    s.replace('\\', "\\\\")
118        .replace('"', "\\\"")
119        .replace('\n', "\\n")
120        .replace('\r', "\\r")
121        .replace('\t', "\\t")
122}
123
124/// Escape a string for embedding in a C# string literal.
125pub fn escape_csharp(s: &str) -> String {
126    s.replace('\\', "\\\\")
127        .replace('"', "\\\"")
128        .replace('\n', "\\n")
129        .replace('\r', "\\r")
130        .replace('\t', "\\t")
131}
132
133/// Escape a string for embedding in a PHP string literal.
134pub fn escape_php(s: &str) -> String {
135    s.replace('\\', "\\\\")
136        .replace('"', "\\\"")
137        .replace('$', "\\$")
138        .replace('\n', "\\n")
139        .replace('\r', "\\r")
140        .replace('\t', "\\t")
141}
142
143/// Escape a string for embedding in a double-quoted Ruby string literal.
144pub fn escape_ruby(s: &str) -> String {
145    s.replace('\\', "\\\\")
146        .replace('"', "\\\"")
147        .replace('#', "\\#")
148        .replace('\n', "\\n")
149        .replace('\r', "\\r")
150        .replace('\t', "\\t")
151}
152
153/// Escape a string for embedding in a single-quoted Ruby string literal.
154/// Single-quoted Ruby strings only interpret `\\` and `\'`.
155pub fn escape_ruby_single(s: &str) -> String {
156    s.replace('\\', "\\\\").replace('\'', "\\'")
157}
158
159/// Returns true if the string needs double quotes (contains control characters
160/// that require escape sequences only available in double-quoted strings).
161pub fn ruby_needs_double_quotes(s: &str) -> bool {
162    s.contains('\n') || s.contains('\r') || s.contains('\t') || s.contains('\0')
163}
164
165/// Format a string as a Ruby literal, preferring single quotes.
166pub fn ruby_string_literal(s: &str) -> String {
167    if ruby_needs_double_quotes(s) {
168        format!("\"{}\"", escape_ruby(s))
169    } else {
170        format!("'{}'", escape_ruby_single(s))
171    }
172}
173
174/// Escape a string for embedding in an Elixir string literal.
175pub fn escape_elixir(s: &str) -> String {
176    s.replace('\\', "\\\\")
177        .replace('"', "\\\"")
178        .replace('#', "\\#")
179        .replace('\n', "\\n")
180        .replace('\r', "\\r")
181        .replace('\t', "\\t")
182}
183
184/// Escape a string for embedding in an R string literal.
185pub fn escape_r(s: &str) -> String {
186    s.replace('\\', "\\\\")
187        .replace('"', "\\\"")
188        .replace('\n', "\\n")
189        .replace('\r', "\\r")
190        .replace('\t', "\\t")
191}
192
193/// Escape a string for embedding in a C string literal.
194pub fn escape_c(s: &str) -> String {
195    s.replace('\\', "\\\\")
196        .replace('"', "\\\"")
197        .replace('\n', "\\n")
198        .replace('\r', "\\r")
199        .replace('\t', "\\t")
200}
201
202/// Sanitize an identifier for use as a test function name.
203/// Replaces non-alphanumeric characters with underscores, strips leading digits.
204pub fn sanitize_ident(s: &str) -> String {
205    let mut result = String::with_capacity(s.len());
206    for ch in s.chars() {
207        if ch.is_ascii_alphanumeric() || ch == '_' {
208            result.push(ch);
209        } else {
210            result.push('_');
211        }
212    }
213    // Strip leading digits
214    let trimmed = result.trim_start_matches(|c: char| c.is_ascii_digit());
215    if trimmed.is_empty() {
216        "_".to_string()
217    } else {
218        trimmed.to_string()
219    }
220}
221
222/// Convert a category name to a sanitized filename component.
223pub fn sanitize_filename(s: &str) -> String {
224    s.chars()
225        .map(|c| if c.is_ascii_alphanumeric() || c == '_' { c } else { '_' })
226        .collect::<String>()
227        .to_lowercase()
228}
229
230/// Expand fixture template expressions in a string value.
231///
232/// Supported templates:
233/// - `{{ repeat 'X' N times }}` — expands to the character X repeated N times
234///
235/// If no templates are found, the original string is returned unchanged.
236pub fn expand_fixture_templates(s: &str) -> String {
237    const PREFIX: &str = "{{ repeat '";
238    const SUFFIX: &str = " times }}";
239
240    let mut result = String::with_capacity(s.len());
241    let mut remaining = s;
242
243    while let Some(start) = remaining.find(PREFIX) {
244        result.push_str(&remaining[..start]);
245        let after_prefix = &remaining[start + PREFIX.len()..];
246
247        // Expect character(s) followed by `' N times }}`
248        if let Some(quote_pos) = after_prefix.find("' ") {
249            let ch = &after_prefix[..quote_pos];
250            let after_quote = &after_prefix[quote_pos + 2..];
251
252            if let Some(end) = after_quote.find(SUFFIX) {
253                let count_str = after_quote[..end].trim();
254                if let Ok(count) = count_str.parse::<usize>() {
255                    result.push_str(&ch.repeat(count));
256                    remaining = &after_quote[end + SUFFIX.len()..];
257                    continue;
258                }
259            }
260        }
261
262        // Template didn't match — emit the prefix literally and continue
263        result.push_str(PREFIX);
264        remaining = after_prefix;
265    }
266    result.push_str(remaining);
267    result
268}
269
270/// Escape a string for embedding in a POSIX single-quoted shell string literal.
271///
272/// Wraps the string in single quotes and escapes embedded single quotes as `'\''`.
273/// Single-quoted shell strings treat every character literally except `'`, so
274/// no other escaping is needed.
275pub fn escape_shell(s: &str) -> String {
276    s.replace('\'', r"'\''")
277}
278
279/// Escape a string for embedding in a Gleam string literal.
280pub fn escape_gleam(s: &str) -> String {
281    s.replace('\\', "\\\\")
282        .replace('"', "\\\"")
283        .replace('\n', "\\n")
284        .replace('\r', "\\r")
285        .replace('\t', "\\t")
286}
287
288/// Escape a string for embedding in a Zig string literal.
289pub fn escape_zig(s: &str) -> String {
290    s.replace('\\', "\\\\")
291        .replace('"', "\\\"")
292        .replace('\n', "\\n")
293        .replace('\r', "\\r")
294        .replace('\t', "\\t")
295}
296
297#[cfg(test)]
298mod tests {
299    use super::*;
300
301    /// Go raw string literals (backticks) cannot contain NUL bytes — gofmt rejects them.
302    /// Strings with NUL must fall back to a double-quoted interpreted literal with `\x00`.
303    #[test]
304    fn go_string_literal_nul_bytes_use_quoted_form() {
305        let s = "Hello\x00World";
306        let lit = go_string_literal(s);
307        // Must not contain a raw NUL byte
308        assert!(
309            !lit.as_bytes().contains(&0u8),
310            "go_string_literal emitted a NUL byte — gofmt would reject this: {lit:?}"
311        );
312        // Must be a double-quoted string, not a backtick raw string
313        assert!(
314            lit.starts_with('"'),
315            "expected double-quoted string for NUL input, got: {lit:?}"
316        );
317        // The NUL must be represented as \\x00
318        assert!(
319            lit.contains("\\x00"),
320            "expected \\x00 escape sequence for NUL byte, got: {lit:?}"
321        );
322    }
323
324    /// Strings with carriage return must also use the double-quoted form
325    /// because Go raw strings cannot represent `\r`.
326    #[test]
327    fn go_string_literal_carriage_return_uses_quoted_form() {
328        let s = "line1\r\nline2";
329        let lit = go_string_literal(s);
330        assert!(
331            !lit.as_bytes().contains(&b'\r'),
332            "go_string_literal emitted a literal CR — gofmt would reject this: {lit:?}"
333        );
334        assert!(
335            lit.starts_with('"'),
336            "expected double-quoted string for CR input, got: {lit:?}"
337        );
338    }
339
340    /// Strings with only printable chars and no backtick should still use the
341    /// readable backtick form.
342    #[test]
343    fn go_string_literal_plain_string_uses_backtick() {
344        let s = "Hello World\nwith newline";
345        let lit = go_string_literal(s);
346        assert!(
347            lit.starts_with('`'),
348            "expected backtick form for plain string, got: {lit:?}"
349        );
350    }
351
352    /// Strings that contain a backtick must fall back to double-quoted form.
353    #[test]
354    fn go_string_literal_backtick_in_string_uses_quoted_form() {
355        let s = "has `backtick`";
356        let lit = go_string_literal(s);
357        assert!(
358            lit.starts_with('"'),
359            "expected double-quoted form when string contains backtick, got: {lit:?}"
360        );
361    }
362}