Merge pull request #3070 from water-ghosts/printf-0

printf: Support leading zeroes with %0n formatting
This commit is contained in:
Sylvestre Ledru 2022-02-05 21:02:16 +01:00 committed by GitHub
commit 2ffc5f9e13
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View file

@ -60,6 +60,7 @@ pub struct Sub {
field_char: char, field_char: char,
field_type: FieldType, field_type: FieldType,
orig: String, orig: String,
prefix_char: char,
} }
impl Sub { impl Sub {
pub fn new( pub fn new(
@ -67,6 +68,7 @@ impl Sub {
second_field: CanAsterisk<Option<u32>>, second_field: CanAsterisk<Option<u32>>,
field_char: char, field_char: char,
orig: String, orig: String,
prefix_char: char,
) -> Self { ) -> Self {
// for more dry printing, field characters are grouped // for more dry printing, field characters are grouped
// in initialization of token. // in initialization of token.
@ -90,6 +92,7 @@ impl Sub {
field_char, field_char,
field_type, field_type,
orig, orig,
prefix_char,
} }
} }
} }
@ -126,6 +129,11 @@ impl SubParser {
fn build_token(parser: Self) -> Box<dyn token::Token> { fn build_token(parser: Self) -> Box<dyn token::Token> {
// not a self method so as to allow move of sub-parser vals. // not a self method so as to allow move of sub-parser vals.
// return new Sub struct as token // return new Sub struct as token
let prefix_char = match &parser.min_width_tmp {
Some(width) if width.starts_with('0') => '0',
_ => ' ',
};
let t: Box<dyn token::Token> = Box::new(Sub::new( let t: Box<dyn token::Token> = Box::new(Sub::new(
if parser.min_width_is_asterisk { if parser.min_width_is_asterisk {
CanAsterisk::Asterisk CanAsterisk::Asterisk
@ -139,6 +147,7 @@ impl SubParser {
}, },
parser.field_char.unwrap(), parser.field_char.unwrap(),
parser.text_so_far, parser.text_so_far,
prefix_char,
)); ));
t t
} }
@ -394,7 +403,7 @@ impl token::Token for Sub {
final_str.push_str(&pre_min_width); final_str.push_str(&pre_min_width);
} }
for _ in 0..diff { for _ in 0..diff {
final_str.push(' '); final_str.push(self.prefix_char);
} }
if pad_before { if pad_before {
final_str.push_str(&pre_min_width); final_str.push_str(&pre_min_width);

View file

@ -437,3 +437,11 @@ fn stop_after_additional_escape() {
.succeeds() .succeeds()
.stdout_only("ABC"); .stdout_only("ABC");
} }
#[test]
fn sub_float_leading_zeroes() {
new_ucmd!()
.args(&["%010f", "1"])
.succeeds()
.stdout_only("001.000000");
}