Skip to content

Add accessors for Formatter flags #28615

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 26, 2015
Merged
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
36 changes: 29 additions & 7 deletions src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -865,12 +865,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.sign_plus() {
sign = Some('+'); width += 1;
}

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

Expand Down Expand Up @@ -900,7 +900,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.sign_aware_zero_pad() => {
self.fill = '0';
try!(write_prefix(self));
self.with_padding(min - width, Alignment::Right, |f| {
Expand Down Expand Up @@ -1013,7 +1013,7 @@ impl<'a> Formatter<'a> {
let mut formatted = formatted.clone();
let mut align = self.align;
let old_fill = self.fill;
if self.flags & (1 << (FlagV1::SignAwareZeroPad as u32)) != 0 {
if self.sign_aware_zero_pad() {
// a sign always goes first
let sign = unsafe { str::from_utf8_unchecked(formatted.sign) };
try!(self.buf.write_str(sign));
Expand Down Expand Up @@ -1117,6 +1117,28 @@ impl<'a> Formatter<'a> {
issue = "27726")]
pub fn precision(&self) -> Option<usize> { self.precision }

/// Determines if the `+` flag was specified.
#[unstable(feature = "fmt_flags", reason = "method was just created",
issue = "27726")]
pub fn sign_plus(&self) -> bool { self.flags & (1 << FlagV1::SignPlus as u32) != 0 }

/// Determines if the `-` flag was specified.
#[unstable(feature = "fmt_flags", reason = "method was just created",
issue = "27726")]
pub fn sign_minus(&self) -> bool { self.flags & (1 << FlagV1::SignMinus as u32) != 0 }

/// Determines if the `#` flag was specified.
#[unstable(feature = "fmt_flags", reason = "method was just created",
issue = "27726")]
pub fn alternate(&self) -> bool { self.flags & (1 << FlagV1::Alternate as u32) != 0 }

/// Determines if the `0` flag was specified.
#[unstable(feature = "fmt_flags", reason = "method was just created",
issue = "27726")]
pub fn sign_aware_zero_pad(&self) -> bool {
self.flags & (1 << FlagV1::SignAwareZeroPad as u32) != 0
}

/// Creates a `DebugStruct` builder designed to assist with creation of
/// `fmt::Debug` implementations for structs.
///
Expand Down Expand Up @@ -1361,7 +1383,7 @@ 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 {
if f.alternate() {
f.flags |= 1 << (FlagV1::SignAwareZeroPad as u32);

if let None = f.width {
Expand Down Expand Up @@ -1410,7 +1432,7 @@ impl<'a, T> Pointer for &'a mut T {
fn float_to_decimal_common<T>(fmt: &mut Formatter, num: &T, negative_zero: bool) -> Result
where T: flt2dec::DecodableFloat
{
let force_sign = fmt.flags & (1 << (FlagV1::SignPlus as u32)) != 0;
let force_sign = fmt.sign_plus();
let sign = match (force_sign, negative_zero) {
(false, false) => flt2dec::Sign::Minus,
(false, true) => flt2dec::Sign::MinusRaw,
Expand All @@ -1434,7 +1456,7 @@ fn float_to_decimal_common<T>(fmt: &mut Formatter, num: &T, negative_zero: bool)
fn float_to_exponential_common<T>(fmt: &mut Formatter, num: &T, upper: bool) -> Result
where T: flt2dec::DecodableFloat
{
let force_sign = fmt.flags & (1 << (FlagV1::SignPlus as u32)) != 0;
let force_sign = fmt.sign_plus();
let sign = match force_sign {
false => flt2dec::Sign::Minus,
true => flt2dec::Sign::MinusPlus,
Expand Down