mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-03 21:47:36 +00:00
feat: reinterpret precision
field for strings
This commit changes the behavior of formatting string arguments with both width and precision fields set. Documentation says that the `width` field is the "minimum width" that the format should take up. If the value's string does not fill up this many characters, then the padding specified by fill/alignment will be used to take up the required space. This is true for all formatted types except string, which is truncated down to `precision` number of chars and then all of `fill`, `align` and `width` fields are completely ignored. For example: `format!("{:/^10.8}", "1234567890);` emits "12345678". In the contrast Python version works as the expected: ```python >>> '{:/^10.8}'.format('1234567890') '/12345678/' ``` This commit gives back the `Python` behavior by changing the `precision` field meaning to the truncation and nothing more. The result string *will* be prepended/appended up to the `width` field with the proper `fill` char. However, this is the breaking change. Also updated `std::fmt` docs about string precision. Signed-off-by: Evgeny Safronov <division494@gmail.com>
This commit is contained in:
parent
ea0dc92972
commit
ede39aeb33
@ -408,8 +408,8 @@
|
|||||||
//! ## Precision
|
//! ## Precision
|
||||||
//!
|
//!
|
||||||
//! For non-numeric types, this can be considered a "maximum width". If the resulting string is
|
//! For non-numeric types, this can be considered a "maximum width". If the resulting string is
|
||||||
//! longer than this width, then it is truncated down to this many characters and only those are
|
//! longer than this width, then it is truncated down to this many characters and that truncated
|
||||||
//! emitted.
|
//! value is emitted with proper `fill`, `alignment` and `width` if those parameters are set.
|
||||||
//!
|
//!
|
||||||
//! For integral types, this is ignored.
|
//! For integral types, this is ignored.
|
||||||
//!
|
//!
|
||||||
@ -469,6 +469,7 @@
|
|||||||
//! ```
|
//! ```
|
||||||
//! println!("{}, `{name:.*}` has 3 fractional digits", "Hello", 3, name=1234.56);
|
//! println!("{}, `{name:.*}` has 3 fractional digits", "Hello", 3, name=1234.56);
|
||||||
//! println!("{}, `{name:.*}` has 3 characters", "Hello", 3, name="1234.56");
|
//! println!("{}, `{name:.*}` has 3 characters", "Hello", 3, name="1234.56");
|
||||||
|
//! println!("{}, `{name:>8.*}` has 3 right-aligned characters", "Hello", 3, name="1234.56");
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! print two significantly different things:
|
//! print two significantly different things:
|
||||||
@ -476,6 +477,7 @@
|
|||||||
//! ```text
|
//! ```text
|
||||||
//! Hello, `1234.560` has 3 fractional digits
|
//! Hello, `1234.560` has 3 fractional digits
|
||||||
//! Hello, `123` has 3 characters
|
//! Hello, `123` has 3 characters
|
||||||
|
//! Hello, ` 123` has 3 right-aligned characters
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! # Escaping
|
//! # Escaping
|
||||||
|
@ -983,15 +983,19 @@ impl<'a> Formatter<'a> {
|
|||||||
return self.buf.write_str(s);
|
return self.buf.write_str(s);
|
||||||
}
|
}
|
||||||
// The `precision` field can be interpreted as a `max-width` for the
|
// The `precision` field can be interpreted as a `max-width` for the
|
||||||
// string being formatted
|
// string being formatted.
|
||||||
if let Some(max) = self.precision {
|
let s = if let Some(max) = self.precision {
|
||||||
// If there's a maximum width and our string is longer than
|
// If our string is longer that the precision, then we must have
|
||||||
// that, then we must always have truncation. This is the only
|
// truncation. However other flags like `fill`, `width` and `align`
|
||||||
// case where the maximum length will matter.
|
// must act as always.
|
||||||
if let Some((i, _)) = s.char_indices().skip(max).next() {
|
if let Some((i, _)) = s.char_indices().skip(max).next() {
|
||||||
return self.buf.write_str(&s[..i])
|
&s[..i]
|
||||||
}
|
} else {
|
||||||
|
&s
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
&s
|
||||||
|
};
|
||||||
// The `width` field is more of a `min-width` parameter at this point.
|
// The `width` field is more of a `min-width` parameter at this point.
|
||||||
match self.width {
|
match self.width {
|
||||||
// If we're under the maximum length, and there's no minimum length
|
// If we're under the maximum length, and there's no minimum length
|
||||||
|
@ -140,6 +140,7 @@ pub fn main() {
|
|||||||
t!(format!("{:a$}", "a", a=4), "a ");
|
t!(format!("{:a$}", "a", a=4), "a ");
|
||||||
t!(format!("{:-#}", "a"), "a");
|
t!(format!("{:-#}", "a"), "a");
|
||||||
t!(format!("{:+#}", "a"), "a");
|
t!(format!("{:+#}", "a"), "a");
|
||||||
|
t!(format!("{:/^10.8}", "1234567890"), "/12345678/");
|
||||||
|
|
||||||
// Some float stuff
|
// Some float stuff
|
||||||
t!(format!("{:}", 1.0f32), "1");
|
t!(format!("{:}", 1.0f32), "1");
|
||||||
|
Loading…
Reference in New Issue
Block a user