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

Change how the 0 flag works in format! #40241

Merged
merged 2 commits into from
Mar 20, 2017
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions src/libcollections/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@
//! like `{:08}` would yield `00000001` for the integer `1`, while the
//! same format would yield `-0000001` for the integer `-1`. Notice that
//! the negative version has one fewer zero than the positive version.
//! Note that padding zeroes are always placed after the sign (if any)
//! and before the digits. When used together with the `#` flag, a similar
//! rule applies: padding zeroes are inserted after the prefix but before
//! the digits.
//!
//! ## Width
//!
Expand Down
6 changes: 5 additions & 1 deletion src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,7 @@ impl<'a> Formatter<'a> {
// is zero
Some(min) if self.sign_aware_zero_pad() => {
self.fill = '0';
self.align = rt::v1::Alignment::Right;
write_prefix(self)?;
self.with_padding(min - width, rt::v1::Alignment::Right, |f| {
f.buf.write_str(buf)
Expand Down Expand Up @@ -1153,8 +1154,9 @@ impl<'a> Formatter<'a> {
// for the sign-aware zero padding, we render the sign first and
// behave as if we had no sign from the beginning.
let mut formatted = formatted.clone();
let mut align = self.align;
let old_fill = self.fill;
let old_align = self.align;
let mut align = old_align;
if self.sign_aware_zero_pad() {
// a sign always goes first
let sign = unsafe { str::from_utf8_unchecked(formatted.sign) };
Expand All @@ -1165,6 +1167,7 @@ impl<'a> Formatter<'a> {
width = if width < sign.len() { 0 } else { width - sign.len() };
align = rt::v1::Alignment::Right;
self.fill = '0';
self.align = rt::v1::Alignment::Right;
}

// remaining parts go through the ordinary padding process.
Expand All @@ -1177,6 +1180,7 @@ impl<'a> Formatter<'a> {
})
};
self.fill = old_fill;
self.align = old_align;
ret
} else {
// this is the common case and we take a shortcut
Expand Down
28 changes: 28 additions & 0 deletions src/test/run-pass/ifmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,34 @@ pub fn main() {
t!(format!("{:?}", -0.0), "-0");
t!(format!("{:?}", 0.0), "0");

// sign aware zero padding
t!(format!("{:<3}", 1), "1 ");
t!(format!("{:>3}", 1), " 1");
t!(format!("{:^3}", 1), " 1 ");
t!(format!("{:03}", 1), "001");
t!(format!("{:<03}", 1), "001");
t!(format!("{:>03}", 1), "001");
t!(format!("{:^03}", 1), "001");
t!(format!("{:+03}", 1), "+01");
t!(format!("{:<+03}", 1), "+01");
t!(format!("{:>+03}", 1), "+01");
t!(format!("{:^+03}", 1), "+01");
t!(format!("{:#05x}", 1), "0x001");
t!(format!("{:<#05x}", 1), "0x001");
t!(format!("{:>#05x}", 1), "0x001");
t!(format!("{:^#05x}", 1), "0x001");
t!(format!("{:05}", 1.2), "001.2");
t!(format!("{:<05}", 1.2), "001.2");
t!(format!("{:>05}", 1.2), "001.2");
t!(format!("{:^05}", 1.2), "001.2");
t!(format!("{:05}", -1.2), "-01.2");
t!(format!("{:<05}", -1.2), "-01.2");
t!(format!("{:>05}", -1.2), "-01.2");
t!(format!("{:^05}", -1.2), "-01.2");
t!(format!("{:+05}", 1.2), "+01.2");
t!(format!("{:<+05}", 1.2), "+01.2");
t!(format!("{:>+05}", 1.2), "+01.2");
t!(format!("{:^+05}", 1.2), "+01.2");

// Ergonomic format_args!
t!(format!("{0:x} {0:X}", 15), "f F");
Expand Down