Skip to content

Commit

Permalink
Minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
dylni committed Nov 21, 2022
1 parent 49b46b8 commit 93771f0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
//! However, the following invariants will always be upheld:
//!
//! - The encoding will be compatible with UTF-8. In particular, splitting an
//! encoded byte sequence by a UTF-8encoded character always produces other
//! valid byte sequences. They can be re-encoded without error using
//! encoded byte sequence by a UTF-8–encoded character always produces
//! other valid byte sequences. They can be re-encoded without error using
//! [`RawOsString::into_os_string`] and similar methods.
//!
//! - All characters valid in platform strings are representable. [`OsStr`] and
Expand Down
8 changes: 4 additions & 4 deletions src/windows/wtf8/code_points.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ where
{
iter: Peekable<I>,
surrogate: bool,
utf8: bool,
still_utf8: bool,
}

impl<I> CodePoints<I>
Expand All @@ -29,12 +29,12 @@ where
Self {
iter: string.into_iter().peekable(),
surrogate: false,
utf8: true,
still_utf8: true,
}
}

pub(super) fn is_still_utf8(&self) -> bool {
self.utf8
self.still_utf8
}

fn consume_next(&mut self, code_point: &mut u32) -> Result<()> {
Expand Down Expand Up @@ -107,7 +107,7 @@ where
} else if code_point & 0xFE0 == 0x360 {
if code_point & 0x10 == 0 {
self.surrogate = true;
self.utf8 = false;
self.still_utf8 = false;
} else if prev_surrogate {
// Decoding a broken surrogate pair would be lossy.
invalid = true;
Expand Down
26 changes: 16 additions & 10 deletions src/windows/wtf8/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ use crate::util;
const SURROGATE_LENGTH: usize = 3;

pub(crate) fn ends_with(string: &[u8], mut suffix: &[u8]) -> bool {
let index = match string.len().checked_sub(suffix.len()) {
Some(index) => index,
None => return false,
let index = if let Some(index) = string.len().checked_sub(suffix.len()) {
index
} else {
return false;
};
if let Some(&byte) = string.get(index) {
if util::is_continuation(byte) {
let index = expect_encoded!(index.checked_sub(1));
let mut wide_surrogate = match suffix.get(..SURROGATE_LENGTH) {
Some(surrogate) => super::encode_wide(surrogate),
None => return false,
};
let mut wide_surrogate =
if let Some(surrogate) = suffix.get(..SURROGATE_LENGTH) {
super::encode_wide(surrogate)
} else {
return false;
};
let surrogate_wchar = wide_surrogate
.next()
.expect("failed decoding non-empty suffix");
Expand All @@ -35,9 +38,12 @@ pub(crate) fn ends_with(string: &[u8], mut suffix: &[u8]) -> bool {
pub(crate) fn starts_with(string: &[u8], mut prefix: &[u8]) -> bool {
if let Some(&byte) = string.get(prefix.len()) {
if util::is_continuation(byte) {
let index = match prefix.len().checked_sub(SURROGATE_LENGTH) {
Some(index) => index,
None => return false,
let index = if let Some(index) =
prefix.len().checked_sub(SURROGATE_LENGTH)
{
index
} else {
return false;
};
let (substring, surrogate) = prefix.split_at(index);
let mut wide_surrogate = super::encode_wide(surrogate);
Expand Down

0 comments on commit 93771f0

Please sign in to comment.