brace-expansion 0.1.1

Bash-style brace expansion: a{b,c}d -> [abd, acd], {1..3} -> [1,2,3]. A faithful port of the brace-expansion npm package. Zero dependencies.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! # brace-expansion — bash-style brace expansion
//!
//! Expand brace patterns the way `bash` does: `a{b,c}d` becomes `["abd", "acd"]`,
//! `{1..3}` becomes `["1", "2", "3"]`, with nested sets, numeric and alphabetic
//! sequences (with an optional step and zero-padding), and backslash escaping. A
//! faithful Rust port of the [`brace-expansion`](https://www.npmjs.com/package/brace-expansion)
//! npm package (the expander behind `minimatch`/`glob`). Zero dependencies and
//! `#![no_std]`.
//!
//! ```
//! use brace_expansion::expand;
//!
//! assert_eq!(expand("a{b,c}d"), ["abd", "acd"]);
//! assert_eq!(expand("{1..3}"), ["1", "2", "3"]);
//! assert_eq!(expand("{a..c}"), ["a", "b", "c"]);
//! assert_eq!(expand("{01..03}"), ["01", "02", "03"]);
//! assert_eq!(expand("a{b,c{d,e}}f"), ["abf", "acdf", "acef"]);
//! ```

#![no_std]
#![doc(html_root_url = "https://docs.rs/brace-expansion/0.1.0")]
// Index arithmetic mirrors balanced-match's `-1`/`indexOf` logic; every cast is on a
// value already bounded by the input length.
#![allow(
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::cast_possible_wrap
)]

extern crate alloc;

use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;

// Compile-test the README's examples as part of `cargo test`.
#[cfg(doctest)]
#[doc = include_str!("../README.md")]
struct ReadmeDoctests;

/// The default cap on the number of generated results, matching the npm package.
pub const EXPANSION_MAX: usize = 100_000;

/// A bound on brace-nesting recursion depth, so pathologically nested input degrades
/// gracefully instead of overflowing the stack. Kept well below what a small (2 MiB)
/// thread stack tolerates; real patterns nest only a handful of levels.
const MAX_DEPTH: u32 = 256;

// Sentinels used to hide escaped characters during expansion. They are wrapped in
// NUL bytes, which do not appear in realistic brace patterns (an input that
// literally contains these markers would round-trip incorrectly).
const ESC_SLASH: &str = "\u{0}SLASH\u{0}";
const ESC_OPEN: &str = "\u{0}OPEN\u{0}";
const ESC_CLOSE: &str = "\u{0}CLOSE\u{0}";
const ESC_COMMA: &str = "\u{0}COMMA\u{0}";
const ESC_PERIOD: &str = "\u{0}PERIOD\u{0}";

/// Expand a brace pattern, returning every resulting string in order.
///
/// An empty input yields an empty list. The number of results is capped at
/// [`EXPANSION_MAX`]; use [`expand_with_max`] to choose a different limit.
///
/// ```
/// assert_eq!(brace_expansion::expand("{a,b}{c,d}"), ["ac", "ad", "bc", "bd"]);
/// ```
#[must_use]
pub fn expand(pattern: &str) -> Vec<String> {
    expand_with_max(pattern, EXPANSION_MAX)
}

/// Expand a brace pattern, capping the number of results at `max`.
#[must_use]
pub fn expand_with_max(pattern: &str, max: usize) -> Vec<String> {
    if pattern.is_empty() {
        return Vec::new();
    }
    // Bash 4.3 preserves a leading `{}`; mirror that by escaping it.
    let pattern = match pattern.strip_prefix("{}") {
        Some(rest) => format!("\\{{\\}}{rest}"),
        None => pattern.to_string(),
    };

    expand_inner(&escape_braces(&pattern), max, true, 0)
        .iter()
        .map(|s| unescape_braces(s))
        .collect()
}

fn escape_braces(s: &str) -> String {
    // Order matters: collapse escaped backslashes (`\\`) first, then `\{ \} \, \.`.
    s.replace("\\\\", ESC_SLASH)
        .replace("\\{", ESC_OPEN)
        .replace("\\}", ESC_CLOSE)
        .replace("\\,", ESC_COMMA)
        .replace("\\.", ESC_PERIOD)
}

fn unescape_braces(s: &str) -> String {
    s.replace(ESC_SLASH, "\\")
        .replace(ESC_OPEN, "{")
        .replace(ESC_CLOSE, "}")
        .replace(ESC_COMMA, ",")
        .replace(ESC_PERIOD, ".")
}

fn embrace(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 2);
    out.push('{');
    out.push_str(s);
    out.push('}');
    out
}

/// `parseInt`-style numeric value: the integer if it parses, otherwise the first
/// character's code point. Integers beyond `i64` saturate (they only occur in
/// pathological sequences, which the result cap bounds anyway).
fn numeric(s: &str) -> i64 {
    match s.parse::<i64>() {
        Ok(n) => n,
        Err(_) if is_int(s) => {
            if s.starts_with('-') {
                i64::MIN
            } else {
                i64::MAX
            }
        }
        Err(_) => s.chars().next().map_or(0, |c| c as i64),
    }
}

/// Whether `el` is zero-padded (`/^-?0\d/`).
fn is_padded(el: &str) -> bool {
    let body = el.strip_prefix('-').unwrap_or(el).as_bytes();
    body.len() >= 2 && body[0] == b'0' && body[1].is_ascii_digit()
}

fn is_int(s: &str) -> bool {
    let body = s.strip_prefix('-').unwrap_or(s);
    !body.is_empty() && body.bytes().all(|b| b.is_ascii_digit())
}

fn is_single_letter(s: &str) -> bool {
    let mut chars = s.chars();
    matches!((chars.next(), chars.next()), (Some(c), None) if c.is_ascii_alphabetic())
}

fn is_numeric_sequence(body: &str) -> bool {
    let parts: Vec<&str> = body.split("..").collect();
    matches!(parts.len(), 2 | 3) && parts.iter().all(|p| is_int(p))
}

fn is_alpha_sequence(body: &str) -> bool {
    let parts: Vec<&str> = body.split("..").collect();
    match parts.len() {
        2 => is_single_letter(parts[0]) && is_single_letter(parts[1]),
        3 => is_single_letter(parts[0]) && is_single_letter(parts[1]) && is_int(parts[2]),
        _ => false,
    }
}

/// Whether `post` matches `/,(?!,).*\}/`: a comma not followed by a comma, with a
/// `}` somewhere after it.
fn has_dangling_close(post: &str) -> bool {
    let chars: Vec<char> = post.chars().collect();
    for i in 0..chars.len() {
        if chars[i] == ',' && chars.get(i + 1) != Some(&',') && chars[i + 1..].contains(&'}') {
            return true;
        }
    }
    false
}

// ---------------------------------------------------------------------------
// balanced-match: find the first balanced `{ … }`
// ---------------------------------------------------------------------------

/// Find the first balanced `{ … }` and return `(pre, body, post)`.
fn balanced(s: &str) -> Option<(String, String, String)> {
    let chars: Vec<char> = s.chars().collect();
    let (start, end) = range(&chars)?;
    Some((
        chars[..start].iter().collect(),
        chars[start + 1..end].iter().collect(),
        chars[end + 1..].iter().collect(),
    ))
}

fn index_of(s: &[char], c: char, from: i64) -> i64 {
    let start = if from < 0 { 0 } else { from as usize };
    if start >= s.len() {
        return -1;
    }
    match s[start..].iter().position(|&x| x == c) {
        Some(p) => (start + p) as i64,
        None => -1,
    }
}

/// Port of balanced-match's `range` for `a = '{'`, `b = '}'`.
#[allow(clippy::many_single_char_names)] // names mirror the reference implementation
fn range(s: &[char]) -> Option<(usize, usize)> {
    let (a, b) = ('{', '}');
    let mut ai = index_of(s, a, 0);
    let mut bi = index_of(s, b, ai + 1);
    let mut i = ai;
    let mut result: Option<(i64, i64)> = None;

    if ai >= 0 && bi > 0 {
        let mut begs: Vec<i64> = Vec::new();
        let mut left = s.len() as i64;
        let mut right: i64 = -1;
        let mut right_set = false;

        while i >= 0 && result.is_none() {
            if i == ai {
                begs.push(i);
                ai = index_of(s, a, i + 1);
            } else if begs.len() == 1 {
                if let Some(r) = begs.pop() {
                    result = Some((r, bi));
                }
            } else {
                if let Some(beg) = begs.pop() {
                    if beg < left {
                        left = beg;
                        right = bi;
                        right_set = true;
                    }
                }
                bi = index_of(s, b, i + 1);
            }
            i = if ai < bi && ai >= 0 { ai } else { bi };
        }

        if !begs.is_empty() && right_set {
            result = Some((left, right));
        }
    }

    result.map(|(l, r)| (l as usize, r as usize))
}

/// Split `str` on top-level commas, treating nested `{ … }` sets as single members.
fn parse_comma_parts(s: &str, depth: u32) -> Vec<String> {
    if s.is_empty() {
        return vec![String::new()];
    }
    let split_plain = || s.split(',').map(ToString::to_string).collect();
    if depth > MAX_DEPTH {
        return split_plain();
    }
    let Some((pre, body, post)) = balanced(s) else {
        return split_plain();
    };

    let mut p: Vec<String> = pre.split(',').map(ToString::to_string).collect();
    let last = p.len() - 1;
    p[last].push('{');
    p[last].push_str(&body);
    p[last].push('}');

    let mut post_parts = parse_comma_parts(&post, depth + 1);
    if !post.is_empty() && !post_parts.is_empty() {
        let first = post_parts.remove(0);
        p[last].push_str(&first);
        p.extend(post_parts);
    }
    p
}

fn expand_inner(s: &str, max: usize, is_top: bool, depth: u32) -> Vec<String> {
    if depth > MAX_DEPTH {
        return vec![s.to_string()];
    }
    let Some((pre, body, post_str)) = balanced(s) else {
        return vec![s.to_string()];
    };

    let post = if post_str.is_empty() {
        vec![String::new()]
    } else {
        expand_inner(&post_str, max, false, depth + 1)
    };

    let mut expansions: Vec<String> = Vec::new();

    if pre.ends_with('$') {
        for p in post.iter().take(max) {
            expansions.push(format!("{pre}{{{body}}}{p}"));
        }
        return expansions;
    }

    let is_numeric_seq = is_numeric_sequence(&body);
    let is_alpha_seq = is_alpha_sequence(&body);
    let is_sequence = is_numeric_seq || is_alpha_seq;
    let is_options = body.contains(',');

    if !is_sequence && !is_options {
        if has_dangling_close(&post_str) {
            let rebuilt = format!("{pre}{{{body}{ESC_CLOSE}{post_str}");
            return expand_inner(&rebuilt, max, true, depth + 1);
        }
        return vec![s.to_string()];
    }

    let n: Vec<String> = if is_sequence {
        body.split("..").map(ToString::to_string).collect()
    } else {
        let parsed = parse_comma_parts(&body, depth + 1);
        if parsed.len() == 1 {
            let embraced: Vec<String> = expand_inner(&parsed[0], max, false, depth + 1)
                .iter()
                .map(|x| embrace(x))
                .collect();
            if embraced.len() == 1 {
                return post
                    .iter()
                    .map(|p| format!("{pre}{}{p}", embraced[0]))
                    .collect();
            }
            embraced
        } else {
            parsed
        }
    };

    let big_n: Vec<String> = if is_sequence {
        sequence(&n, is_alpha_seq, max)
    } else {
        let mut v = Vec::new();
        for part in &n {
            v.extend(expand_inner(part, max, false, depth + 1));
        }
        v
    };

    for item in &big_n {
        for p in &post {
            if expansions.len() >= max {
                break;
            }
            let expansion = format!("{pre}{item}{p}");
            if !is_top || is_sequence || !expansion.is_empty() {
                expansions.push(expansion);
            }
        }
    }

    expansions
}

/// Generate the members of a numeric or alphabetic sequence (`n` is the `..`-split
/// body, length 2 or 3).
#[allow(clippy::many_single_char_names)] // names mirror the reference implementation
fn sequence(n: &[String], is_alpha: bool, max: usize) -> Vec<String> {
    let x = numeric(&n[0]);
    let y = numeric(&n[1]);
    let width = n[0].chars().count().max(n[1].chars().count());
    let mut incr: i64 = if n.len() == 3 {
        numeric(&n[2]).saturating_abs().max(1)
    } else {
        1
    };
    let reverse = y < x;
    if reverse {
        incr = -incr;
    }
    let pad = n.iter().any(|e| is_padded(e));

    let mut out = Vec::new();
    let mut i = x;
    loop {
        let in_range = if reverse { i >= y } else { i <= y };
        if !in_range || out.len() >= max {
            break;
        }
        if is_alpha {
            match u32::try_from(i).ok().and_then(char::from_u32) {
                Some('\\') | None => out.push(String::new()),
                Some(c) => out.push(c.to_string()),
            }
        } else {
            out.push(pad_number(i, width, pad));
        }
        // Stop rather than overflow when a sequence runs to the i64 boundary.
        match i.checked_add(incr) {
            Some(next) => i = next,
            None => break,
        }
    }
    out
}

fn pad_number(i: i64, width: usize, pad: bool) -> String {
    let mut c = i.to_string();
    if pad {
        let need = width as i64 - c.chars().count() as i64;
        if need > 0 {
            let zeros = "0".repeat(need as usize);
            c = if i < 0 {
                let digits = c.strip_prefix('-').unwrap_or("").to_string();
                format!("-{zeros}{digits}")
            } else {
                format!("{zeros}{c}")
            };
        }
    }
    c
}