Minor improvements

This commit is contained in:
dylni
2022-11-20 21:12:41 -05:00
parent 1c483c5b2f
commit 4e25e30302
3 changed files with 22 additions and 16 deletions
+2 -2
View File
@@ -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
+4 -4
View File
@@ -15,7 +15,7 @@ where
{
iter: Peekable<I>,
surrogate: bool,
utf8: bool,
still_utf8: bool,
}
impl<I> CodePoints<I>
@@ -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<()> {
@@ -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;
+16 -10
View File
@@ -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");
@@ -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);