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