about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorEvgeny Safronov <division494@gmail.com>2016-06-29 10:40:25 +0300
committerEvgeny Safronov <division494@gmail.com>2016-07-06 00:01:14 +0300
commitede39aeb331bf6efb3739d22a60c1844e9c2c3d6 (patch)
treebcd62c0edd6afb3fbd803e28c5f1cf627d97ede4 /src/test
parentea0dc9297283daff6486807f43e190b4eb561412 (diff)
downloadrust-ede39aeb331bf6efb3739d22a60c1844e9c2c3d6.tar.gz
rust-ede39aeb331bf6efb3739d22a60c1844e9c2c3d6.zip
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>
Diffstat (limited to 'src/test')
-rw-r--r--src/test/run-pass/ifmt.rs3
1 files changed, 2 insertions, 1 deletions
diff --git a/src/test/run-pass/ifmt.rs b/src/test/run-pass/ifmt.rs
index 27cafeacc20..1cc52c93034 100644
--- a/src/test/run-pass/ifmt.rs
+++ b/src/test/run-pass/ifmt.rs
@@ -125,7 +125,7 @@ pub fn main() {
     t!(format!("{:<4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
     t!(format!("{:>4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
     t!(format!("{:^4.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
-    t!(format!("{:>10.4}", "aaaaaaaaaaaaaaaaaa"), "aaaa");
+    t!(format!("{:>10.4}", "aaaaaaaaaaaaaaaaaa"), "      aaaa");
     t!(format!("{:2.4}", "aaaaa"), "aaaa");
     t!(format!("{:2.4}", "aaaa"), "aaaa");
     t!(format!("{:2.4}", "aaa"), "aaa");
@@ -140,6 +140,7 @@ pub fn main() {
     t!(format!("{:a$}", "a", a=4), "a   ");
     t!(format!("{:-#}", "a"), "a");
     t!(format!("{:+#}", "a"), "a");
+    t!(format!("{:/^10.8}", "1234567890"), "/12345678/");
 
     // Some float stuff
     t!(format!("{:}", 1.0f32), "1");