#braces #expansion #shell

brace-expand

Rust crate which performs brace expansion of strings, as in shells like Bash etc

1 unstable release

0.1.0 Oct 7, 2021

#1543 in Algorithms

MIT license

11KB
133 lines

Brace expansion crate for Rust

Test results Crates.io Documentation

This library performs brace expansion of strings, similar in spirit (though different in several details) to that used in shells like Bash etc.

What the algorithm does

Given the input:

{hello,goodbye,wonderful} world

this algorithm produces the following collection of strings:

hello world
goodbye world
wonderful world

Note that unlike shell brace expansion, the result is a collection of separate strings rather than a single string. Also, whitespace characters are not treated specially by the algorithm; they are on the same footing as printing characers.

Curly braces { and } are used to mark the start and end of an expansion list, and commas separate the items in each list. Literal curly braces and commas must be escaped with single backslashes:

this is {\{braced\},[bracketed\, nicely]}

produces:

this is {braced}
this is [bracketed, nicely]

If you want a literal backslash, that too must be escaped:

this is a backslash: \\

produces:

this is a backslash: \

Note that the escaping backslashes are removed from the output.

Inputs can contain multiple expansion lists, and these can be nested. For example:

{hello,goodbye} {world,my {friends,colleagues}}

produces:

hello world
goodbye world
hello my friends
hello my colleagues
goodbye my friends
goodbye my colleagues

Example

use brace_expand::brace_expand;

fn main() {
    let output = brace_expand("this {is,is not} a pipe"); 

    assert_eq!(output, vec!["this is a pipe", "this is not a pipe"]);
}

No runtime deps