Skip to content

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

License

Notifications You must be signed in to change notification settings

ctamblyn/brace-expand

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 characters.

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]

Note that in Rust source we must escape the backslashes within ordinary string literals an additional time, giving the following:

let output = brace_expand("this is {\\{braced\\},[bracketed\\, nicely]}");

assert_eq!(output, vec!["this is {braced}", "this is [bracketed, nicely]"]);

If you want a literal backslash, that must be escaped by writing it as a double backslash:

this is a backslash: \\

produces:

this is a backslash: \

In Rust source, this would look as follows:

let output = brace_expand("this is a backslash: \\\\");

println!("{}", output[0]);

which produces the following output:

this is a backslash: \

Example

The following code snippet illustrates how inputs can contain multiple lists, and even be nested:

let output = brace_expand("{hello,goodbye} {world,my {friends,colleagues}}");

assert_eq!(
    output,
    vec![
        "hello world",
        "hello my friends",
        "hello my colleagues",
        "goodbye world",
        "goodbye my friends",
        "goodbye my colleagues",
    ]
);

Minimum supported Rust version (MSRV) policy

brace-expand's current minimum supported Rust version (MSRV) is 1.31.1.

brace-expand is guaranteed to compile with that version. It might also compile with older versions, but that could change in a future patch release.

If the MSRV of brace-expand changes, that will be done in a minor version release (e.g. 1.0.x -> 1.1.0).

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published