Skip to content

Commit

Permalink
Use the bitflags! macro to implement FlagV1
Browse files Browse the repository at this point in the history
  • Loading branch information
tamird committed Apr 22, 2015
1 parent 3dbfa74 commit c1e0bce
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#![stable(feature = "rust1", since = "1.0.0")]

#[macro_use] #[no_link] extern crate rustc_bitflags;

use prelude::*;

use cell::{Cell, RefCell, Ref, RefMut, BorrowState};
Expand Down Expand Up @@ -198,9 +200,16 @@ impl<'a> ArgumentV1<'a> {
}

// flags available in the v1 format of format_args
#[derive(Copy, Clone)]
#[allow(dead_code)] // SignMinus isn't currently used
enum FlagV1 { SignPlus, SignMinus, Alternate, SignAwareZeroPad, }
bitflags! {
#[derive(Copy, Clone)]
#[allow(dead_code)] // SignMinus isn't currently used
flags FlagV1 : u32 {
const SignPlus = 1 << 0,
const SignMinus = 1 << 1,
const Alternate = 1 << 2,
const SignAwareZeroPad = 1 << 3,
}
}

impl<'a> Arguments<'a> {
/// When using the format_args!() macro, this function is used to generate the
Expand Down Expand Up @@ -472,12 +481,12 @@ impl<'a> Formatter<'a> {
let mut sign = None;
if !is_positive {
sign = Some('-'); width += 1;
} else if self.flags & (1 << (FlagV1::SignPlus as u32)) != 0 {
} else if self.flags.contains(FlagV1::SignPlus) {
sign = Some('+'); width += 1;
}

let mut prefixed = false;
if self.flags & (1 << (FlagV1::Alternate as u32)) != 0 {
if self.flags.contains(FlagV1::Alternate) {
prefixed = true; width += prefix.char_len();
}

Expand Down Expand Up @@ -507,7 +516,7 @@ impl<'a> Formatter<'a> {
}
// The sign and prefix goes before the padding if the fill character
// is zero
Some(min) if self.flags & (1 << (FlagV1::SignAwareZeroPad as u32)) != 0 => {
Some(min) if self.flags.contains(FlagV1::SignAwareZeroPad) => {
self.fill = '0';
try!(write_prefix(self));
self.with_padding(min - width, Alignment::Right, |f| {
Expand Down Expand Up @@ -873,8 +882,8 @@ impl<T> Pointer for *const T {
// it denotes whether to prefix with 0x. We use it to work out whether
// or not to zero extend, and then unconditionally set it to get the
// prefix.
if f.flags & 1 << (FlagV1::Alternate as u32) > 0 {
f.flags |= 1 << (FlagV1::SignAwareZeroPad as u32);
if f.flags.contains(FlagV1::Alternate) {
f.flags.insert(FlagV1::SignAwareZeroPad);

if let None = f.width {
// The formats need two extra bytes, for the 0x
Expand All @@ -885,7 +894,7 @@ impl<T> Pointer for *const T {
}
}
}
f.flags |= 1 << (FlagV1::Alternate as u32);
f.flags.insert(FlagV1::Alternate);

let ret = LowerHex::fmt(&(*self as usize), f);

Expand Down

0 comments on commit c1e0bce

Please sign in to comment.