Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for zero padding in numeric ranges #6

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ pub fn expand(node: &crate::parser::Node) -> Result<Vec<String>, ExpansionError>
start: _,
end: _,
} => {
// Get the numeric string length to be used later for zero padding
let zero_pad = if from.chars().nth(0) == Some('0') || to.chars().nth(0) == Some('0') {
if from.len() >= to.len() {
from.len()
} else {
to.len()
}
} else {
0
};
let from = if let Ok(from) = from.parse::<usize>() {
from
} else {
Expand All @@ -189,7 +199,7 @@ pub fn expand(node: &crate::parser::Node) -> Result<Vec<String>, ExpansionError>
let range = from..=to;
let mut inner = vec![];
for i in range {
inner.push(i.to_string());
inner.push(format!("{:0>width$}", i, width = zero_pad));
}
Ok(inner)
}
Expand Down Expand Up @@ -417,4 +427,39 @@ mod tests {
])
)
}
#[test]
fn test_expand_range_no_padding_bracoxidize() {
assert_eq!(
bracoxidize("A{1..10}"),
Ok(vec![
"A1".to_owned(),
"A2".to_owned(),
"A3".to_owned(),
"A4".to_owned(),
"A5".to_owned(),
"A6".to_owned(),
"A7".to_owned(),
"A8".to_owned(),
"A9".to_owned(),
"A10".to_owned(),
])
)
}
#[test]
fn test_expand_range_zero_padding_bracoxidize() {
assert_eq!(
bracoxidize("A{4..06}{01..003}"),
Ok(vec![
"A04001".to_owned(),
"A04002".to_owned(),
"A04003".to_owned(),
"A05001".to_owned(),
"A05002".to_owned(),
"A05003".to_owned(),
"A06001".to_owned(),
"A06002".to_owned(),
"A06003".to_owned(),
])
)
}
}