Skip to content

Commit 96b04ea

Browse files
authored
Unrolled build for #152865
Rollup merge of #152865 - asder8215:path_display, r=joboet Fixed ByteStr not padding within its Display trait when no specific alignment is mentioned Fixes #152804. `Path`'s `Display` uses `ByteStr`'s `Display`, which is where the problem was occurring. The issue was coming from `ByteStr` implementation of `fmt()` in this particular area: ```rust let Some(align) = f.align() else { return fmt_nopad(self, f); }; let nchars: usize = self .utf8_chunks() .map(|chunk| { chunk.valid().chars().count() + if chunk.invalid().is_empty() { 0 } else { 1 } }) .sum(); let padding = f.width().unwrap_or(0).saturating_sub(nchars); let fill = f.fill(); let (lpad, rpad) = match align { fmt::Alignment::Left => (0, padding), fmt::Alignment::Right => (padding, 0), fmt::Alignment::Center => { let half = padding / 2; (half, half + padding % 2) } }; ``` The docs for the align implies that `Alignment::Left`, `Alignment::Right`, `Alignment::Center` comes from `:<`, `:>`, and `:^` respectively with `align()` returning `None` if neither of those symbols are used in the formatted string. However, while padding is taken care of in the aligned cases, we could still have padding for things that don't use alignment like: ```rust assert_eq!(format!("{:10}", Path::new("/foo/bar").display()), "/foo/bar "); ``` We shouldn't write to `f` and return from there when there's no alignment; we should also include any potential padding/filling bytes here. r? @joboet
2 parents d8b2222 + d6ff921 commit 96b04ea

2 files changed

Lines changed: 37 additions & 18 deletions

File tree

library/core/src/bstr/mod.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -172,39 +172,38 @@ impl fmt::Debug for ByteStr {
172172
#[unstable(feature = "bstr", issue = "134915")]
173173
impl fmt::Display for ByteStr {
174174
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
175-
fn fmt_nopad(this: &ByteStr, f: &mut fmt::Formatter<'_>) -> fmt::Result {
176-
for chunk in this.utf8_chunks() {
177-
f.write_str(chunk.valid())?;
178-
if !chunk.invalid().is_empty() {
179-
f.write_str("\u{FFFD}")?;
180-
}
181-
}
182-
Ok(())
183-
}
184-
185-
let Some(align) = f.align() else {
186-
return fmt_nopad(self, f);
187-
};
188175
let nchars: usize = self
189176
.utf8_chunks()
190177
.map(|chunk| {
191178
chunk.valid().chars().count() + if chunk.invalid().is_empty() { 0 } else { 1 }
192179
})
193180
.sum();
181+
194182
let padding = f.width().unwrap_or(0).saturating_sub(nchars);
195183
let fill = f.fill();
196-
let (lpad, rpad) = match align {
197-
fmt::Alignment::Left => (0, padding),
198-
fmt::Alignment::Right => (padding, 0),
199-
fmt::Alignment::Center => {
184+
185+
let (lpad, rpad) = match f.align() {
186+
Some(fmt::Alignment::Right) => (padding, 0),
187+
Some(fmt::Alignment::Center) => {
200188
let half = padding / 2;
201189
(half, half + padding % 2)
202190
}
191+
// Either alignment is not specified or it's left aligned
192+
// which behaves the same with padding
193+
_ => (0, padding),
203194
};
195+
204196
for _ in 0..lpad {
205197
write!(f, "{fill}")?;
206198
}
207-
fmt_nopad(self, f)?;
199+
200+
for chunk in self.utf8_chunks() {
201+
f.write_str(chunk.valid())?;
202+
if !chunk.invalid().is_empty() {
203+
f.write_str("\u{FFFD}")?;
204+
}
205+
}
206+
208207
for _ in 0..rpad {
209208
write!(f, "{fill}")?;
210209
}

library/std/tests/path.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2291,6 +2291,26 @@ fn display_format_flags() {
22912291
assert_eq!(format!("a{:#<5}b", Path::new("a").display()), "aa####b");
22922292
}
22932293

2294+
#[test]
2295+
fn display_path_with_padding_no_align() {
2296+
assert_eq!(format!("{:10}", Path::new("/foo/bar").display()), "/foo/bar ");
2297+
}
2298+
2299+
#[test]
2300+
fn display_path_with_padding_align_left() {
2301+
assert_eq!(format!("{:<10}", Path::new("/foo/bar").display()), "/foo/bar ");
2302+
}
2303+
2304+
#[test]
2305+
fn display_path_with_padding_align_right() {
2306+
assert_eq!(format!("{:>10}", Path::new("/foo/bar").display()), " /foo/bar");
2307+
}
2308+
2309+
#[test]
2310+
fn display_path_with_padding_align_center() {
2311+
assert_eq!(format!("{:^10}", Path::new("/foo/bar").display()), " /foo/bar ");
2312+
}
2313+
22942314
#[test]
22952315
fn into_rc() {
22962316
let orig = "hello/world";

0 commit comments

Comments
 (0)