Use sublice patterns to avoid computing the len

This commit is contained in:
lzutao 2020-03-16 23:43:42 +07:00 committed by GitHub
parent 59f4ba9504
commit ce5e49f86f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -599,24 +599,16 @@ impl Wtf8 {
#[inline]
fn final_lead_surrogate(&self) -> Option<u16> {
let len = self.len();
if len < 3 {
return None;
}
match self.bytes[(len - 3)..] {
[0xED, b2 @ 0xA0..=0xAF, b3] => Some(decode_surrogate(b2, b3)),
match self.bytes {
[.., 0xED, b2 @ 0xA0..=0xAF, b3] => Some(decode_surrogate(*b2, *b3)),
_ => None,
}
}
#[inline]
fn initial_trail_surrogate(&self) -> Option<u16> {
let len = self.len();
if len < 3 {
return None;
}
match self.bytes[..3] {
[0xED, b2 @ 0xB0..=0xBF, b3] => Some(decode_surrogate(b2, b3)),
match self.bytes {
[0xED, b2 @ 0xB0..=0xBF, b3, ..] => Some(decode_surrogate(*b2, *b3)),
_ => None,
}
}