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

Experiment with standard library Utf8Chunks #1289

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@
#![no_std]
#![doc(html_root_url = "https://docs.rs/cxx/1.0.110")]
#![cfg_attr(doc_cfg, feature(doc_cfg))]
#![feature(utf8_chunks)] // FIXME
#![deny(
improper_ctypes,
improper_ctypes_definitions,
Expand Down
51 changes: 12 additions & 39 deletions src/lossy.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,22 @@
use core::char;
use core::fmt::{self, Write as _};
use core::str;
use core::str::Utf8Chunks;

pub(crate) fn display(mut bytes: &[u8], f: &mut fmt::Formatter) -> fmt::Result {
loop {
match str::from_utf8(bytes) {
Ok(valid) => return f.write_str(valid),
Err(utf8_error) => {
let valid_up_to = utf8_error.valid_up_to();
let valid = unsafe { str::from_utf8_unchecked(&bytes[..valid_up_to]) };
f.write_str(valid)?;
f.write_char(char::REPLACEMENT_CHARACTER)?;
if let Some(error_len) = utf8_error.error_len() {
bytes = &bytes[valid_up_to + error_len..];
} else {
return Ok(());
}
}
pub(crate) fn display(bytes: &[u8], f: &mut fmt::Formatter) -> fmt::Result {
for chunk in Utf8Chunks::new(bytes) {
f.write_str(chunk.valid())?;
if !chunk.invalid().is_empty() {
f.write_char(char::REPLACEMENT_CHARACTER)?;
}
}
Ok(())
}

pub(crate) fn debug(mut bytes: &[u8], f: &mut fmt::Formatter) -> fmt::Result {
pub(crate) fn debug(bytes: &[u8], f: &mut fmt::Formatter) -> fmt::Result {
f.write_char('"')?;

while !bytes.is_empty() {
let from_utf8_result = str::from_utf8(bytes);
let valid = match from_utf8_result {
Ok(valid) => valid,
Err(utf8_error) => {
let valid_up_to = utf8_error.valid_up_to();
unsafe { str::from_utf8_unchecked(&bytes[..valid_up_to]) }
}
};
for chunk in Utf8Chunks::new(bytes) {
let valid = chunk.valid();

let mut written = 0;
for (i, ch) in valid.char_indices() {
Expand All @@ -47,19 +31,8 @@ pub(crate) fn debug(mut bytes: &[u8], f: &mut fmt::Formatter) -> fmt::Result {
}
f.write_str(&valid[written..])?;

match from_utf8_result {
Ok(_valid) => break,
Err(utf8_error) => {
let end_of_broken = if let Some(error_len) = utf8_error.error_len() {
valid.len() + error_len
} else {
bytes.len()
};
for b in &bytes[valid.len()..end_of_broken] {
write!(f, "\\x{:02x}", b)?;
}
bytes = &bytes[end_of_broken..];
}
for b in chunk.invalid() {
write!(f, "\\x{:02x}", b)?;
}
}

Expand Down
Loading