Merge sys_common::bytestring into os_str_bytes

This commit is contained in:
Christiaan Dirkx 2021-05-06 18:21:58 +02:00
parent 3824017f8e
commit 059008f033
5 changed files with 28 additions and 50 deletions

View file

@ -1,26 +0,0 @@
#![allow(dead_code)]
#[cfg(test)]
mod tests;
use crate::fmt::{Formatter, Result, Write};
use core::str::lossy::{Utf8Lossy, Utf8LossyChunk};
pub fn debug_fmt_bytestring(slice: &[u8], f: &mut Formatter<'_>) -> Result {
// Writes out a valid unicode string with the correct escape sequences
fn write_str_escaped(f: &mut Formatter<'_>, s: &str) -> Result {
for c in s.chars().flat_map(|c| c.escape_debug()) {
f.write_char(c)?
}
Ok(())
}
f.write_str("\"")?;
for Utf8LossyChunk { valid, broken } in Utf8Lossy::from_bytes(slice).chunks() {
write_str_escaped(f, valid)?;
for b in broken {
write!(f, "\\x{:02X}", b)?;
}
}
f.write_str("\"")
}

View file

@ -1,19 +0,0 @@
use super::*;
use crate::fmt::{Debug, Formatter, Result};
#[test]
fn smoke() {
struct Helper<'a>(&'a [u8]);
impl Debug for Helper<'_> {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
debug_fmt_bytestring(self.0, f)
}
}
let input = b"\xF0hello,\tworld";
let expected = r#""\xF0hello,\tworld""#;
let output = format!("{:?}", Helper(input));
assert!(output == expected);
}

View file

@ -21,7 +21,6 @@
mod tests;
pub mod backtrace;
pub mod bytestring;
pub mod condvar;
pub mod fs;
pub mod io;

View file

@ -2,16 +2,18 @@
//! systems: just a `Vec<u8>`/`[u8]`.
use crate::borrow::Cow;
use crate::fmt;
use crate::fmt::Write;
use crate::mem;
use crate::rc::Rc;
use crate::str;
use crate::sync::Arc;
use crate::sys_common::bytestring::debug_fmt_bytestring;
use crate::sys_common::{AsInner, IntoInner};
use core::str::lossy::Utf8Lossy;
use core::str::lossy::{Utf8Lossy, Utf8LossyChunk};
#[cfg(test)]
mod tests;
#[derive(Hash)]
#[repr(transparent)]
@ -26,7 +28,19 @@ pub struct Slice {
impl fmt::Debug for Slice {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
debug_fmt_bytestring(&self.inner, formatter)
// Writes out a valid unicode string with the correct escape sequences
formatter.write_str("\"")?;
for Utf8LossyChunk { valid, broken } in Utf8Lossy::from_bytes(&self.inner).chunks() {
for c in valid.chars().flat_map(|c| c.escape_debug()) {
formatter.write_char(c)?
}
for b in broken {
write!(formatter, "\\x{:02X}", b)?;
}
}
formatter.write_str("\"")
}
}

View file

@ -0,0 +1,10 @@
use super::*;
#[test]
fn slice_debug_output() {
let input = Slice::from_u8_slice(b"\xF0hello,\tworld");
let expected = r#""\xF0hello,\tworld""#;
let output = format!("{:?}", input);
assert_eq!(output, expected);
}