blob: 10b8fafb649c92c32e1b63d98ab81b590669608f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
use crate::util::is_continuation;
use super::encode_wide;
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,
};
if let Some(&byte) = string.get(index) {
if is_continuation(byte) {
let index = index.checked_sub(1).expect("invalid string");
let mut wide_surrogate = match suffix.get(..SURROGATE_LENGTH) {
Some(surrogate) => encode_wide(surrogate),
None => return false,
};
let surrogate_wchar = wide_surrogate
.next()
.expect("failed decoding non-empty suffix");
if wide_surrogate.next().is_some()
|| encode_wide(&string[index..])
.take_while(Result::is_ok)
.nth(1)
!= Some(surrogate_wchar)
{
return false;
}
suffix = &suffix[SURROGATE_LENGTH..];
}
}
string.ends_with(suffix)
}
pub(crate) fn starts_with(string: &[u8], mut prefix: &[u8]) -> bool {
if let Some(&byte) = string.get(prefix.len()) {
if is_continuation(byte) {
let index = match prefix.len().checked_sub(SURROGATE_LENGTH) {
Some(index) => index,
None => return false,
};
let (substring, surrogate) = prefix.split_at(index);
let mut wide_surrogate = encode_wide(surrogate);
let surrogate_wchar = wide_surrogate
.next()
.expect("failed decoding non-empty prefix");
if surrogate_wchar.is_err()
|| wide_surrogate.next().is_some()
|| encode_wide(&string[index..])
.next()
.expect("failed decoding non-empty substring")
!= surrogate_wchar
{
return false;
}
prefix = substring;
}
}
string.starts_with(prefix)
}
|