Skip to content

Fix bug in padding unicode, #17105. #17145

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

Merged
merged 1 commit into from
Sep 12, 2014
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: 2 additions & 2 deletions src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ impl<'a> Formatter<'a> {

let mut prefixed = false;
if self.flags & (1 << (FlagAlternate as uint)) != 0 {
prefixed = true; width += prefix.len();
prefixed = true; width += prefix.char_len();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test for this change as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a recommendation as to how to test that? There isn't a trait for which # will print unicode, so I'd have to have access to the internal function to test it, which isn't possible from here, I don't think.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pad_integral function is a public function, and you could implement Show for a custom newtype which invokes pad_integral with a unicode prefix.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the custom Show function.

One question, when it runs format!("{:#4}", C) with C being declared, struct C;, is that printing an object of type C or is it printing the type C without creating an object?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you have struct C; it's both a value and a type, so the expression C constructs a value of type C

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After looking back at the tutorial, I noticed this is just a special case of Point { x: 1, y: 2 }, being used to create a Point.

}

// Writes the sign if it exists, and then the prefix if it was requested
Expand Down Expand Up @@ -562,7 +562,7 @@ impl<'a> Formatter<'a> {
// If we're under both the maximum and the minimum width, then fill
// up the minimum width with the specified string + some alignment.
Some(width) => {
self.with_padding(width - s.len(), rt::AlignLeft, |me| {
self.with_padding(width - s.char_len(), rt::AlignLeft, |me| {
me.buf.write(s.as_bytes())
})
}
Expand Down
8 changes: 8 additions & 0 deletions src/test/run-pass/ifmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use std::str;

struct A;
struct B;
struct C;

impl fmt::Signed for A {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand All @@ -36,6 +37,11 @@ impl fmt::Signed for B {
f.write("adios".as_bytes())
}
}
impl fmt::Show for C {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.pad_integral(true, "☃", "123".as_bytes())
}
}

macro_rules! t(($a:expr, $b:expr) => { assert_eq!($a.as_slice(), $b) })

Expand Down Expand Up @@ -81,13 +87,15 @@ pub fn main() {
t!(format!("{} {0}", "a"), "a a");
t!(format!("{foo_bar}", foo_bar=1i), "1");
t!(format!("{:d}", 5i + 5i), "10");
t!(format!("{:#4}", C), "☃123");

let a: &fmt::Show = &1i;
t!(format!("{}", a), "1");

// Formatting strings and their arguments
t!(format!("{:s}", "a"), "a");
t!(format!("{:4s}", "a"), "a ");
t!(format!("{:4s}", "☃"), "☃ ");
t!(format!("{:>4s}", "a"), " a");
t!(format!("{:<4s}", "a"), "a ");
t!(format!("{:^5s}", "a"), " a ");
Expand Down