Skip to content

Commit

Permalink
Access members of FormattingOptions directly instead of via getters…
Browse files Browse the repository at this point in the history
…/setters
  • Loading branch information
EliasHolzmann committed Mar 8, 2024
1 parent bb7cb4b commit cb4ab28
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 197 deletions.
6 changes: 3 additions & 3 deletions library/core/src/fmt/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ where
true => flt2dec::Sign::MinusPlus,
};

if let Some(precision) = fmt.precision() {
if let Some(precision) = fmt.options.precision {
float_to_decimal_common_exact(fmt, num, sign, precision)
} else {
let min_precision = 0;
Expand Down Expand Up @@ -161,7 +161,7 @@ where
true => flt2dec::Sign::MinusPlus,
};

if let Some(precision) = fmt.precision() {
if let Some(precision) = fmt.options.precision {
// 1 integral digit + `precision` fractional digits = `precision + 1` total digits
float_to_exponential_common_exact(fmt, num, sign, precision + 1, upper)
} else {
Expand All @@ -179,7 +179,7 @@ where
true => flt2dec::Sign::MinusPlus,
};

if let Some(precision) = fmt.precision() {
if let Some(precision) = fmt.options.precision {
// this behavior of {:.PREC?} predates exponential formatting for {:?}
float_to_decimal_common_exact(fmt, num, sign, precision)
} else {
Expand Down
74 changes: 36 additions & 38 deletions library/core/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1433,14 +1433,14 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
}

unsafe fn run(fmt: &mut Formatter<'_>, arg: &rt::Placeholder, args: &[rt::Argument<'_>]) -> Result {
fmt.options.fill(arg.fill);
fmt.options.align(arg.align.into());
fmt.options.flags(arg.flags);
fmt.options.fill = arg.fill;
fmt.options.align = arg.align.into();
fmt.options.flags = arg.flags;
// SAFETY: arg and args come from the same Arguments,
// which guarantees the indexes are always within bounds.
unsafe {
fmt.options.width(getcount(args, &arg.width));
fmt.options.precision(getcount(args, &arg.precision));
fmt.options.width = getcount(args, &arg.width);
fmt.options.precision = getcount(args, &arg.precision);
}

// Extract the correct argument
Expand Down Expand Up @@ -1579,7 +1579,7 @@ impl<'a> Formatter<'a> {
}

// The `width` field is more of a `min-width` parameter at this point.
match self.width() {
match self.options.width {
// If there's no minimum length requirements then we can just
// write the bytes.
None => {
Expand Down Expand Up @@ -1647,12 +1647,12 @@ impl<'a> Formatter<'a> {
#[stable(feature = "rust1", since = "1.0.0")]
pub fn pad(&mut self, s: &str) -> Result {
// Make sure there's a fast path up front
if self.width().is_none() && self.precision().is_none() {
if self.options.width.is_none() && self.options.precision.is_none() {
return self.buf.write_str(s);
}
// The `precision` field can be interpreted as a `max-width` for the
// string being formatted.
let s = if let Some(max) = self.precision() {
let s = if let Some(max) = self.options.precision {
// If our string is longer that the precision, then we must have
// truncation. However other flags like `fill`, `width` and `align`
// must act as always.
Expand All @@ -1669,7 +1669,7 @@ impl<'a> Formatter<'a> {
&s
};
// The `width` field is more of a `min-width` parameter at this point.
match self.width() {
match self.options.width {
// If we're under the maximum length, and there's no minimum length
// requirements, then we can just emit the string
None => self.buf.write_str(s),
Expand Down Expand Up @@ -1709,10 +1709,10 @@ impl<'a> Formatter<'a> {
};

for _ in 0..pre_pad {
self.buf.write_char(self.fill())?;
self.buf.write_char(self.options.fill)?;
}

Ok(PostPadding::new(self.fill(), post_pad))
Ok(PostPadding::new(self.options.fill, post_pad))
}

/// Takes the formatted parts and applies the padding.
Expand All @@ -1723,12 +1723,12 @@ impl<'a> Formatter<'a> {
///
/// Any `numfmt::Part::Copy` parts in `formatted` must contain valid UTF-8.
unsafe fn pad_formatted_parts(&mut self, formatted: &numfmt::Formatted<'_>) -> Result {
if let Some(mut width) = self.width() {
if let Some(mut width) = self.options.width {
// for the sign-aware zero padding, we render the sign first and
// behave as if we had no sign from the beginning.
let mut formatted = formatted.clone();
let old_fill = self.fill();
let old_align = self.align();
let old_fill = self.options.fill;
let old_align = self.options.align;
if self.sign_aware_zero_pad() {
// a sign always goes first
let sign = formatted.sign;
Expand All @@ -1737,8 +1737,8 @@ impl<'a> Formatter<'a> {
// remove the sign from the formatted parts
formatted.sign = "";
width = width.saturating_sub(sign.len());
self.options.fill('0');
self.options.align(Some(Alignment::Right));
self.options.fill = '0';
self.options.align = Some(Alignment::Right);
}

// remaining parts go through the ordinary padding process.
Expand All @@ -1755,8 +1755,8 @@ impl<'a> Formatter<'a> {
}
post_padding.write(self)
};
self.options.fill(old_fill);
self.options.align(old_align);
self.options.fill = old_fill;
self.options.align = old_align;
ret
} else {
// this is the common case and we take a shortcut
Expand Down Expand Up @@ -1873,7 +1873,7 @@ impl<'a> Formatter<'a> {
or `sign_aware_zero_pad` methods instead"
)]
pub fn flags(&self) -> u32 {
self.options.get_flags()
self.options.flags
}

/// Character used as 'fill' whenever there is alignment.
Expand Down Expand Up @@ -1906,7 +1906,7 @@ impl<'a> Formatter<'a> {
#[must_use]
#[stable(feature = "fmt_flags", since = "1.5.0")]
pub fn fill(&self) -> char {
self.options.get_fill()
self.options.fill
}

/// Flag indicating what form of alignment was requested.
Expand Down Expand Up @@ -1941,7 +1941,7 @@ impl<'a> Formatter<'a> {
#[must_use]
#[stable(feature = "fmt_flags_align", since = "1.28.0")]
pub fn align(&self) -> Option<Alignment> {
self.options.get_align()
self.options.align
}

/// Optionally specified integer width that the output should be.
Expand Down Expand Up @@ -1971,7 +1971,7 @@ impl<'a> Formatter<'a> {
#[must_use]
#[stable(feature = "fmt_flags", since = "1.5.0")]
pub fn width(&self) -> Option<usize> {
self.options.get_width()
self.options.width
}

/// Optionally specified precision for numeric types. Alternatively, the
Expand Down Expand Up @@ -2002,7 +2002,7 @@ impl<'a> Formatter<'a> {
#[must_use]
#[stable(feature = "fmt_flags", since = "1.5.0")]
pub fn precision(&self) -> Option<usize> {
self.options.get_precision()
self.options.precision
}

/// Determines if the `+` flag was specified.
Expand Down Expand Up @@ -2034,7 +2034,7 @@ impl<'a> Formatter<'a> {
#[must_use]
#[stable(feature = "fmt_flags", since = "1.5.0")]
pub fn sign_plus(&self) -> bool {
self.options.get_sign() == Some(Sign::Plus)
self.options.flags & (1 << rt::Flag::SignPlus as u32) != 0
}

/// Determines if the `-` flag was specified.
Expand Down Expand Up @@ -2063,7 +2063,7 @@ impl<'a> Formatter<'a> {
#[must_use]
#[stable(feature = "fmt_flags", since = "1.5.0")]
pub fn sign_minus(&self) -> bool {
self.options.get_sign() == Some(Sign::Minus)
self.options.flags & (1 << rt::Flag::SignMinus as u32) != 0
}

/// Determines if the `#` flag was specified.
Expand Down Expand Up @@ -2091,7 +2091,7 @@ impl<'a> Formatter<'a> {
#[must_use]
#[stable(feature = "fmt_flags", since = "1.5.0")]
pub fn alternate(&self) -> bool {
self.options.get_alternate()
self.options.flags & (1 << rt::Flag::Alternate as u32) != 0
}

/// Determines if the `0` flag was specified.
Expand All @@ -2117,7 +2117,7 @@ impl<'a> Formatter<'a> {
#[must_use]
#[stable(feature = "fmt_flags", since = "1.5.0")]
pub fn sign_aware_zero_pad(&self) -> bool {
self.options.get_sign_aware_zero_pad()
self.options.flags & (1 << rt::Flag::SignAwareZeroPad as u32) != 0
}

// FIXME: Decide what public API we want for these two flags.
Expand Down Expand Up @@ -2674,7 +2674,7 @@ impl Debug for char {
#[stable(feature = "rust1", since = "1.0.0")]
impl Display for char {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
if f.width().is_none() && f.precision().is_none() {
if f.options.width.is_none() && f.options.precision.is_none() {
f.write_char(*self)
} else {
f.pad(self.encode_utf8(&mut [0; 4]))
Expand All @@ -2698,28 +2698,26 @@ impl<T: ?Sized> Pointer for *const T {
///
/// [problematic]: https://github.com/rust-lang/rust/issues/95489
pub(crate) fn pointer_fmt_inner(ptr_addr: usize, f: &mut Formatter<'_>) -> Result {
let old_width = f.width();
let old_alternate = f.alternate();
let old_zero_pad = f.sign_aware_zero_pad();
let old_width = f.options.width;
let old_flags = f.options.flags;

// The alternate flag is already treated by LowerHex as being special-
// 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.alternate() {
f.options.sign_aware_zero_pad(true);
f.options.flags |= 1 << (rt::Flag::SignAwareZeroPad as u32);

if f.width().is_none() {
f.options.width(Some((usize::BITS / 4) as usize + 2));
if f.options.width.is_none() {
f.options.width = Some((usize::BITS / 4) as usize + 2);
}
}
f.options.alternate(true);
f.options.flags |= 1 << (rt::Flag::Alternate as u32);

let ret = LowerHex::fmt(&ptr_addr, f);

f.options.width(old_width);
f.options.alternate(old_alternate);
f.options.sign_aware_zero_pad(old_zero_pad);
f.options.width = old_width;
f.options.flags = old_flags;

ret
}
Expand Down

0 comments on commit cb4ab28

Please sign in to comment.