Skip to content

Commit 7ebfd85

Browse files
committed
Auto merge of #28615 - sfackler:formatter-methods, r=alexcrichton
cc #27726 r? @alexcrichton
2 parents 78ce46f + e5ee13f commit 7ebfd85

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

src/libcore/fmt/mod.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -865,12 +865,12 @@ impl<'a> Formatter<'a> {
865865
let mut sign = None;
866866
if !is_positive {
867867
sign = Some('-'); width += 1;
868-
} else if self.flags & (1 << (FlagV1::SignPlus as u32)) != 0 {
868+
} else if self.sign_plus() {
869869
sign = Some('+'); width += 1;
870870
}
871871

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

@@ -900,7 +900,7 @@ impl<'a> Formatter<'a> {
900900
}
901901
// The sign and prefix goes before the padding if the fill character
902902
// is zero
903-
Some(min) if self.flags & (1 << (FlagV1::SignAwareZeroPad as u32)) != 0 => {
903+
Some(min) if self.sign_aware_zero_pad() => {
904904
self.fill = '0';
905905
try!(write_prefix(self));
906906
self.with_padding(min - width, Alignment::Right, |f| {
@@ -1013,7 +1013,7 @@ impl<'a> Formatter<'a> {
10131013
let mut formatted = formatted.clone();
10141014
let mut align = self.align;
10151015
let old_fill = self.fill;
1016-
if self.flags & (1 << (FlagV1::SignAwareZeroPad as u32)) != 0 {
1016+
if self.sign_aware_zero_pad() {
10171017
// a sign always goes first
10181018
let sign = unsafe { str::from_utf8_unchecked(formatted.sign) };
10191019
try!(self.buf.write_str(sign));
@@ -1117,6 +1117,28 @@ impl<'a> Formatter<'a> {
11171117
issue = "27726")]
11181118
pub fn precision(&self) -> Option<usize> { self.precision }
11191119

1120+
/// Determines if the `+` flag was specified.
1121+
#[unstable(feature = "fmt_flags", reason = "method was just created",
1122+
issue = "27726")]
1123+
pub fn sign_plus(&self) -> bool { self.flags & (1 << FlagV1::SignPlus as u32) != 0 }
1124+
1125+
/// Determines if the `-` flag was specified.
1126+
#[unstable(feature = "fmt_flags", reason = "method was just created",
1127+
issue = "27726")]
1128+
pub fn sign_minus(&self) -> bool { self.flags & (1 << FlagV1::SignMinus as u32) != 0 }
1129+
1130+
/// Determines if the `#` flag was specified.
1131+
#[unstable(feature = "fmt_flags", reason = "method was just created",
1132+
issue = "27726")]
1133+
pub fn alternate(&self) -> bool { self.flags & (1 << FlagV1::Alternate as u32) != 0 }
1134+
1135+
/// Determines if the `0` flag was specified.
1136+
#[unstable(feature = "fmt_flags", reason = "method was just created",
1137+
issue = "27726")]
1138+
pub fn sign_aware_zero_pad(&self) -> bool {
1139+
self.flags & (1 << FlagV1::SignAwareZeroPad as u32) != 0
1140+
}
1141+
11201142
/// Creates a `DebugStruct` builder designed to assist with creation of
11211143
/// `fmt::Debug` implementations for structs.
11221144
///
@@ -1361,7 +1383,7 @@ impl<T> Pointer for *const T {
13611383
// it denotes whether to prefix with 0x. We use it to work out whether
13621384
// or not to zero extend, and then unconditionally set it to get the
13631385
// prefix.
1364-
if f.flags & 1 << (FlagV1::Alternate as u32) > 0 {
1386+
if f.alternate() {
13651387
f.flags |= 1 << (FlagV1::SignAwareZeroPad as u32);
13661388

13671389
if let None = f.width {
@@ -1410,7 +1432,7 @@ impl<'a, T> Pointer for &'a mut T {
14101432
fn float_to_decimal_common<T>(fmt: &mut Formatter, num: &T, negative_zero: bool) -> Result
14111433
where T: flt2dec::DecodableFloat
14121434
{
1413-
let force_sign = fmt.flags & (1 << (FlagV1::SignPlus as u32)) != 0;
1435+
let force_sign = fmt.sign_plus();
14141436
let sign = match (force_sign, negative_zero) {
14151437
(false, false) => flt2dec::Sign::Minus,
14161438
(false, true) => flt2dec::Sign::MinusRaw,
@@ -1434,7 +1456,7 @@ fn float_to_decimal_common<T>(fmt: &mut Formatter, num: &T, negative_zero: bool)
14341456
fn float_to_exponential_common<T>(fmt: &mut Formatter, num: &T, upper: bool) -> Result
14351457
where T: flt2dec::DecodableFloat
14361458
{
1437-
let force_sign = fmt.flags & (1 << (FlagV1::SignPlus as u32)) != 0;
1459+
let force_sign = fmt.sign_plus();
14381460
let sign = match force_sign {
14391461
false => flt2dec::Sign::Minus,
14401462
true => flt2dec::Sign::MinusPlus,

0 commit comments

Comments
 (0)