Skip to content

Commit ede39ae

Browse files
committed
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>
1 parent ea0dc92 commit ede39ae

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

src/libcollections/fmt.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,8 @@
408408
//! ## Precision
409409
//!
410410
//! For non-numeric types, this can be considered a "maximum width". If the resulting string is
411-
//! longer than this width, then it is truncated down to this many characters and only those are
412-
//! emitted.
411+
//! longer than this width, then it is truncated down to this many characters and that truncated
412+
//! value is emitted with proper `fill`, `alignment` and `width` if those parameters are set.
413413
//!
414414
//! For integral types, this is ignored.
415415
//!
@@ -469,13 +469,15 @@
469469
//! ```
470470
//! println!("{}, `{name:.*}` has 3 fractional digits", "Hello", 3, name=1234.56);
471471
//! println!("{}, `{name:.*}` has 3 characters", "Hello", 3, name="1234.56");
472+
//! println!("{}, `{name:>8.*}` has 3 right-aligned characters", "Hello", 3, name="1234.56");
472473
//! ```
473474
//!
474475
//! print two significantly different things:
475476
//!
476477
//! ```text
477478
//! Hello, `1234.560` has 3 fractional digits
478479
//! Hello, `123` has 3 characters
480+
//! Hello, ` 123` has 3 right-aligned characters
479481
//! ```
480482
//!
481483
//! # Escaping

src/libcore/fmt/mod.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -983,15 +983,19 @@ impl<'a> Formatter<'a> {
983983
return self.buf.write_str(s);
984984
}
985985
// The `precision` field can be interpreted as a `max-width` for the
986-
// string being formatted
987-
if let Some(max) = self.precision {
988-
// If there's a maximum width and our string is longer than
989-
// that, then we must always have truncation. This is the only
990-
// case where the maximum length will matter.
986+
// string being formatted.
987+
let s = if let Some(max) = self.precision {
988+
// If our string is longer that the precision, then we must have
989+
// truncation. However other flags like `fill`, `width` and `align`
990+
// must act as always.
991991
if let Some((i, _)) = s.char_indices().skip(max).next() {
992-
return self.buf.write_str(&s[..i])
992+
&s[..i]
993+
} else {
994+
&s
993995
}
994-
}
996+
} else {
997+
&s
998+
};
995999
// The `width` field is more of a `min-width` parameter at this point.
9961000
match self.width {
9971001
// If we're under the maximum length, and there's no minimum length

src/test/run-pass/ifmt.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub fn main() {
125125
t!(format!("{:<4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
126126
t!(format!("{:>4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
127127
t!(format!("{:^4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
128-
t!(format!("{:>10.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
128+
t!(format!("{:>10.4}", "aaaaaaaaaaaaaaaaaa"), " aaaa");
129129
t!(format!("{:2.4}", "aaaaa"), "aaaa");
130130
t!(format!("{:2.4}", "aaaa"), "aaaa");
131131
t!(format!("{:2.4}", "aaa"), "aaa");
@@ -140,6 +140,7 @@ pub fn main() {
140140
t!(format!("{:a$}", "a", a=4), "a ");
141141
t!(format!("{:-#}", "a"), "a");
142142
t!(format!("{:+#}", "a"), "a");
143+
t!(format!("{:/^10.8}", "1234567890"), "/12345678/");
143144

144145
// Some float stuff
145146
t!(format!("{:}", 1.0f32), "1");

0 commit comments

Comments
 (0)