Skip to content

Commit 8255507

Browse files
authored
mrg: zero padding support pr #6
Add support for zero padding in numeric ranges
2 parents 5b10c6d + b606872 commit 8255507

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

src/lib.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,16 @@ pub fn expand(node: &crate::parser::Node) -> Result<Vec<String>, ExpansionError>
175175
start: _,
176176
end: _,
177177
} => {
178+
// Get the numeric string length to be used later for zero padding
179+
let zero_pad = if from.chars().nth(0) == Some('0') || to.chars().nth(0) == Some('0') {
180+
if from.len() >= to.len() {
181+
from.len()
182+
} else {
183+
to.len()
184+
}
185+
} else {
186+
0
187+
};
178188
let from = if let Ok(from) = from.parse::<usize>() {
179189
from
180190
} else {
@@ -189,7 +199,7 @@ pub fn expand(node: &crate::parser::Node) -> Result<Vec<String>, ExpansionError>
189199
let range = from..=to;
190200
let mut inner = vec![];
191201
for i in range {
192-
inner.push(i.to_string());
202+
inner.push(format!("{:0>width$}", i, width = zero_pad));
193203
}
194204
Ok(inner)
195205
}
@@ -417,4 +427,39 @@ mod tests {
417427
])
418428
)
419429
}
430+
#[test]
431+
fn test_expand_range_no_padding_bracoxidize() {
432+
assert_eq!(
433+
bracoxidize("A{1..10}"),
434+
Ok(vec![
435+
"A1".to_owned(),
436+
"A2".to_owned(),
437+
"A3".to_owned(),
438+
"A4".to_owned(),
439+
"A5".to_owned(),
440+
"A6".to_owned(),
441+
"A7".to_owned(),
442+
"A8".to_owned(),
443+
"A9".to_owned(),
444+
"A10".to_owned(),
445+
])
446+
)
447+
}
448+
#[test]
449+
fn test_expand_range_zero_padding_bracoxidize() {
450+
assert_eq!(
451+
bracoxidize("A{4..06}{01..003}"),
452+
Ok(vec![
453+
"A04001".to_owned(),
454+
"A04002".to_owned(),
455+
"A04003".to_owned(),
456+
"A05001".to_owned(),
457+
"A05002".to_owned(),
458+
"A05003".to_owned(),
459+
"A06001".to_owned(),
460+
"A06002".to_owned(),
461+
"A06003".to_owned(),
462+
])
463+
)
464+
}
420465
}

0 commit comments

Comments
 (0)