Skip to content

Center alignment for fmt #16885

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 4, 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
23 changes: 16 additions & 7 deletions src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,19 +461,28 @@ impl<'a> Formatter<'a> {
use char::Char;
let align = match self.align {
rt::AlignUnknown => default,
rt::AlignLeft | rt::AlignRight => self.align
_ => self.align
};
if align == rt::AlignLeft {
try!(f(self));
}

let (pre_pad, post_pad) = match align {
rt::AlignLeft => (0u, padding),
rt::AlignRight | rt::AlignUnknown => (padding, 0u),
rt::AlignCenter => (padding / 2, (padding + 1) / 2),
};

let mut fill = [0u8, ..4];
let len = self.fill.encode_utf8(fill).unwrap_or(0);
for _ in range(0, padding) {

for _ in range(0, pre_pad) {
try!(self.buf.write(fill.slice_to(len)));
}
if align == rt::AlignRight {
try!(f(self));

try!(f(self));

for _ in range(0, post_pad) {
try!(self.buf.write(fill.slice_to(len)));
}

Ok(())
}

Expand Down
2 changes: 2 additions & 0 deletions src/libcore/fmt/rt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pub enum Alignment {
AlignLeft,
/// Indication that contents should be right-aligned.
AlignRight,
/// Indication that contents should be center-aligned.
AlignCenter,
/// No alignment was requested.
AlignUnknown,
}
Expand Down
6 changes: 5 additions & 1 deletion src/libfmt_macros/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ pub enum Alignment {
AlignLeft,
/// The value will be aligned to the right.
AlignRight,
/// The value will be aligned in the center.
AlignCenter,
/// The value will take on a default alignment.
AlignUnknown,
}
Expand Down Expand Up @@ -279,7 +281,7 @@ impl<'a> Parser<'a> {
match self.cur.clone().next() {
Some((_, c)) => {
match self.cur.clone().skip(1).next() {
Some((_, '>')) | Some((_, '<')) => {
Some((_, '>')) | Some((_, '<')) | Some((_, '^')) => {
spec.fill = Some(c);
self.cur.next();
}
Expand All @@ -293,6 +295,8 @@ impl<'a> Parser<'a> {
spec.align = AlignLeft;
} else if self.consume('>') {
spec.align = AlignRight;
} else if self.consume('^') {
spec.align = AlignCenter;
}
// Sign flags
if self.consume('+') {
Expand Down
3 changes: 2 additions & 1 deletion src/libstd/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ argument := integer | identifier

format_spec := [[fill]align][sign]['#'][0][width]['.' precision][type]
fill := character
align := '<' | '>'
align := '<' | '^' | '>'
sign := '+' | '-'
width := count
precision := count | '*'
Expand All @@ -357,6 +357,7 @@ parameter. This indicates that if the value being formatted is smaller than
are specified by `fill`, and the alignment can be one of two options:

* `<` - the argument is left-aligned in `width` columns
* `^` - the argument is center-aligned in `width` columns
* `>` - the argument is right-aligned in `width` columns

### Sign/#/0
Expand Down
3 changes: 3 additions & 0 deletions src/libsyntax/ext/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,9 @@ impl<'a, 'b> Context<'a, 'b> {
parse::AlignRight => {
self.ecx.path_global(sp, self.rtpath("AlignRight"))
}
parse::AlignCenter => {
self.ecx.path_global(sp, self.rtpath("AlignCenter"))
}
parse::AlignUnknown => {
self.ecx.path_global(sp, self.rtpath("AlignUnknown"))
}
Expand Down
5 changes: 5 additions & 0 deletions src/test/run-pass/ifmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,16 @@ pub fn main() {
t!(format!("{:4s}", "a"), "a ");
t!(format!("{:>4s}", "a"), " a");
t!(format!("{:<4s}", "a"), "a ");
t!(format!("{:^5s}", "a"), " a ");
t!(format!("{:^5s}", "aa"), " aa ");
t!(format!("{:^4s}", "a"), " a ");
t!(format!("{:^4s}", "aa"), " aa ");
t!(format!("{:.4s}", "a"), "a");
t!(format!("{:4.4s}", "a"), "a ");
t!(format!("{:4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
t!(format!("{:<4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
t!(format!("{:>4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
t!(format!("{:^4.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
t!(format!("{:>10.4s}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
t!(format!("{:2.4s}", "aaaaa"), "aaaa");
t!(format!("{:2.4s}", "aaaa"), "aaaa");
Expand Down