Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std: Respect formatting flags for str-like OsStr #43830

Merged
merged 1 commit into from
Aug 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/libstd/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3953,4 +3953,10 @@ mod tests {
assert_eq!(path, path_buf);
assert!(path_buf.into_os_string().capacity() >= 15);
}

#[test]
fn display_format_flags() {
assert_eq!(format!("a{:#<5}b", Path::new("").display()), "a#####b");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for adding these tests!

assert_eq!(format!("a{:#<5}b", Path::new("a").display()), "aa####b");
}
}
10 changes: 7 additions & 3 deletions src/libstd/sys_common/wtf8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,10 +452,14 @@ impl fmt::Display for Wtf8 {
pos = surrogate_pos + 3;
},
None => {
formatter.write_str(unsafe {
let s = unsafe {
str::from_utf8_unchecked(&wtf8_bytes[pos..])
})?;
return Ok(());
};
if pos == 0 {
return s.fmt(formatter)
} else {
return formatter.write_str(s)
}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/libstd_unicode/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@

#![feature(core_char_ext)]
#![feature(str_internals)]
#![feature(core_intrinsics)]
#![feature(decode_utf8)]
#![feature(fused)]
#![feature(fn_traits)]
Expand Down
18 changes: 16 additions & 2 deletions src/libstd_unicode/lossy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use core::str as core_str;
use core::fmt;
use core::fmt::Write;
use char;
use core::intrinsics;
use core::mem;


/// Lossy UTF-8 string.
Expand All @@ -27,7 +27,7 @@ impl Utf8Lossy {
}

pub fn from_bytes(bytes: &[u8]) -> &Utf8Lossy {
unsafe { intrinsics::transmute(bytes) }
unsafe { mem::transmute(bytes) }
}

pub fn chunks(&self) -> Utf8LossyChunksIter {
Expand Down Expand Up @@ -153,7 +153,21 @@ impl<'a> Iterator for Utf8LossyChunksIter<'a> {

impl fmt::Display for Utf8Lossy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// If we're the empty string then our iterator won't actually yield
// anything, so perform the formatting manually
if self.bytes.len() == 0 {
return "".fmt(f)
}

for Utf8LossyChunk { valid, broken } in self.chunks() {
// If we successfully decoded the whole chunk as a valid string then
// we can return a direct formatting of the string which will also
// respect various formatting flags if possible.
if valid.len() == self.bytes.len() {
assert!(broken.is_empty());
return valid.fmt(f)
}

f.write_str(valid)?;
if !broken.is_empty() {
f.write_char(char::REPLACEMENT_CHARACTER)?;
Expand Down