|
8 | 8 | // option. This file may not be copied, modified, or distributed
|
9 | 9 | // except according to those terms.
|
10 | 10 |
|
| 11 | +use ascii; |
11 | 12 | use borrow::{Cow, ToOwned, Borrow};
|
12 | 13 | use boxed::Box;
|
13 | 14 | use clone::Clone;
|
14 | 15 | use convert::{Into, From};
|
15 | 16 | use cmp::{PartialEq, Eq, PartialOrd, Ord, Ordering};
|
16 | 17 | use error::Error;
|
17 |
| -use fmt; |
| 18 | +use fmt::{self, Write}; |
18 | 19 | use io;
|
19 | 20 | use iter::Iterator;
|
20 | 21 | use libc;
|
@@ -268,7 +269,18 @@ impl Deref for CString {
|
268 | 269 | #[stable(feature = "rust1", since = "1.0.0")]
|
269 | 270 | impl fmt::Debug for CString {
|
270 | 271 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
271 |
| - fmt::Debug::fmt(&String::from_utf8_lossy(self.as_bytes()), f) |
| 272 | + fmt::Debug::fmt(&**self, f) |
| 273 | + } |
| 274 | +} |
| 275 | + |
| 276 | +#[stable(feature = "cstr_debug", since = "1.3.0")] |
| 277 | +impl fmt::Debug for CStr { |
| 278 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 279 | + try!(write!(f, "\"")); |
| 280 | + for byte in self.to_bytes().iter().flat_map(|&b| ascii::escape_default(b)) { |
| 281 | + try!(f.write_char(byte as char)); |
| 282 | + } |
| 283 | + write!(f, "\"") |
272 | 284 | }
|
273 | 285 | }
|
274 | 286 |
|
@@ -501,8 +513,8 @@ mod tests {
|
501 | 513 |
|
502 | 514 | #[test]
|
503 | 515 | fn formatted() {
|
504 |
| - let s = CString::new(&b"12"[..]).unwrap(); |
505 |
| - assert_eq!(format!("{:?}", s), "\"12\""); |
| 516 | + let s = CString::new(&b"abc\x01\x02\n\xE2\x80\xA6\xFF"[..]).unwrap(); |
| 517 | + assert_eq!(format!("{:?}", s), r#""abc\x01\x02\n\xe2\x80\xa6\xff""#); |
506 | 518 | }
|
507 | 519 |
|
508 | 520 | #[test]
|
|
0 commit comments