Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
dylni committed Nov 11, 2023
1 parent 74ed1b1 commit 44e265a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 23 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["dylni"]
edition = "2021"
rust-version = "1.74.0"
description = """
Convert between byte sequences and platform-native strings
Losslessly manipulate platform-native strings
"""
readme = "README.md"
repository = "https://github.com/dylni/os_str_bytes"
Expand All @@ -25,6 +25,7 @@ memchr = { version = "2.4", optional = true }
[dev-dependencies]
fastrand = "2.0"
lazy_static = "1.4"
tempfile = "3.2"

[features]
default = ["memchr", "raw_os_str"]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ The minimum supported Rust toolchain version depends on the platform:
<tr>
<td>HermitCore</td>
<td><code>*-*-hermit</code></td>
<td>nightly (<a href="https://github.com/hermitcore/rusty-hermit/blob/86232e295ff5c50db6c283a47cff3f38a0d1b012/rust-toolchain.toml#L3"><code>rust-toolchain.toml</code></a>)</td>
<td>nightly (<a href="https://github.com/hermit-os/hermit-rs/blob/5f148e3f97d24e1a142d68b649c31579d8f499ba/rust-toolchain.toml#L2"><code>rust-toolchain.toml</code></a>)</td>
</tr>
<tr>
<td>SOLID</td>
Expand Down
25 changes: 8 additions & 17 deletions src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,17 @@ fn is_boundary(string: &OsStr, index: usize) -> bool {

if !util::is_continuation(byte) {
let bytes = &string[index..];
let valid = str::from_utf8(&bytes[..bytes.len().min(MAX_UTF8_LENGTH)])
.err()
.map(|x| x.valid_up_to() != 0)
.unwrap_or(true);
if valid {
if !str::from_utf8(&bytes[..bytes.len().min(MAX_UTF8_LENGTH)])
.is_err_and(|x| x.valid_up_to() == 0)
{
return true;
}
}
let mut start = index;
for _ in 0..MAX_UTF8_LENGTH {
if let Some(index) = start.checked_sub(1) {
start = index;
} else {
return false;
}
if !util::is_continuation(string[start]) {
break;
}
}
str::from_utf8(&string[start..index]).is_ok()
(0..index)
.rev()
.take(MAX_UTF8_LENGTH)
.find(|&x| !util::is_continuation(string[x]))
.is_some_and(|x| str::from_utf8(&string[x..index]).is_ok())
}

#[track_caller]
Expand Down
12 changes: 8 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,23 +147,27 @@
//! # #[cfg(any())]
//! use std::env;
//! use std::fs;
//! # use std::path::Path;
//!
//! use os_str_bytes::OsStrBytesExt;
//!
//! # mod env {
//! # use std::env;
//! # use std::ffi::OsString;
//! # use std::iter;
//! #
//! # use tempfile::NamedTempFile;
//! #
//! # pub(super) fn args_os() -> impl Iterator<Item = OsString> {
//! # let mut file = env::temp_dir();
//! # file.push("os_str_bytes\u{E9}.txt");
//! # vec![OsString::new(), file.into_os_string()].into_iter()
//! # let file = NamedTempFile::with_prefix("os_str_bytes_").unwrap();
//! # iter::from_fn(move || Some(file.path().as_os_str().to_owned()))
//! # .take(2)
//! # }
//! # }
//! #
//! for file in env::args_os().skip(1) {
//! if !file.starts_with('-') {
//! let string = "Hello, world!";
//! # assert!(Path::new(&file).exists());
//! fs::write(&file, string)?;
//! assert_eq!(string, fs::read_to_string(file)?);
//! }
Expand Down

0 comments on commit 44e265a

Please sign in to comment.