| 
 | 1 | +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT  | 
 | 2 | +// file at the top-level directory of this distribution and at  | 
 | 3 | +// http://rust-lang.org/COPYRIGHT.  | 
 | 4 | +//  | 
 | 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or  | 
 | 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license  | 
 | 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your  | 
 | 8 | +// option. This file may not be copied, modified, or distributed  | 
 | 9 | +// except according to those terms.  | 
 | 10 | + | 
 | 11 | +#![allow(dead_code)]  | 
 | 12 | + | 
 | 13 | +use fmt::{Formatter, Result, Write};  | 
 | 14 | +use std_unicode::lossy::{Utf8Lossy, Utf8LossyChunk};  | 
 | 15 | + | 
 | 16 | +pub fn debug_fmt_bytestring(slice: &[u8], f: &mut Formatter) -> Result {  | 
 | 17 | +    // Writes out a valid unicode string with the correct escape sequences  | 
 | 18 | +    fn write_str_escaped(f: &mut Formatter, s: &str) -> Result {  | 
 | 19 | +        for c in s.chars().flat_map(|c| c.escape_debug()) {  | 
 | 20 | +            f.write_char(c)?  | 
 | 21 | +        }  | 
 | 22 | +        Ok(())  | 
 | 23 | +    }  | 
 | 24 | + | 
 | 25 | +    f.write_str("\"")?;  | 
 | 26 | +    for Utf8LossyChunk { valid, broken } in Utf8Lossy::from_bytes(slice).chunks() {  | 
 | 27 | +        write_str_escaped(f, valid)?;  | 
 | 28 | +        for b in broken {  | 
 | 29 | +            write!(f, "\\x{:02X}", b)?;  | 
 | 30 | +        }  | 
 | 31 | +    }  | 
 | 32 | +    f.write_str("\"")  | 
 | 33 | +}  | 
 | 34 | + | 
 | 35 | +#[cfg(test)]  | 
 | 36 | +mod tests {  | 
 | 37 | +    use super::*;  | 
 | 38 | +    use fmt::{Formatter, Result, Debug};  | 
 | 39 | + | 
 | 40 | +    #[test]  | 
 | 41 | +    fn smoke() {  | 
 | 42 | +        struct Helper<'a>(&'a [u8]);  | 
 | 43 | + | 
 | 44 | +        impl<'a> Debug for Helper<'a> {  | 
 | 45 | +            fn fmt(&self, f: &mut Formatter) -> Result {  | 
 | 46 | +                debug_fmt_bytestring(self.0, f)  | 
 | 47 | +            }  | 
 | 48 | +        }  | 
 | 49 | + | 
 | 50 | +        let input =      b"\xF0hello,\tworld";  | 
 | 51 | +        let expected = r#""\xF0hello,\tworld""#;  | 
 | 52 | +        let output = format!("{:?}", Helper(input));  | 
 | 53 | + | 
 | 54 | +        assert!(output == expected);  | 
 | 55 | +    }  | 
 | 56 | +}  | 
0 commit comments