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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
use super::{Prefilter, load::load_window_maskless, load_window};
use crate::prefilter::{
UnicodeChar,
backend::{Backend, BitMaskOps},
};
impl<B: Backend> Prefilter<B> {
#[inline(always)]
unsafe fn match_unicode_char_prefix(
start: usize,
len: usize,
haystack: &[u8],
char_len: usize,
chars: [u8; 4],
) -> B::Mask {
debug_assert!(char_len <= 4 && char_len > 1);
match char_len {
2 => unsafe {
B::eq(
load_window_maskless::<B>(haystack, start, len),
B::splat(chars[0]),
)
},
3 => unsafe {
let occ_1 = B::eq(
load_window_maskless::<B>(haystack, start + 1, len),
B::splat(chars[1]),
);
let occ_2 = B::eq(
load_window_maskless::<B>(haystack, start, len),
B::splat(chars[0]),
);
occ_1.and(occ_2)
},
4 => unsafe {
let occ_1 = B::eq(
load_window_maskless::<B>(haystack, start + 2, len),
B::splat(chars[2]),
);
let occ_2 = B::eq(
load_window_maskless::<B>(haystack, start + 1, len),
B::splat(chars[1]),
);
let occ_3 = B::eq(
load_window_maskless::<B>(haystack, start, len),
B::splat(chars[0]),
);
occ_1.and(occ_2).and(occ_3)
},
_ => unreachable!(),
}
}
#[inline(always)]
pub(super) unsafe fn unicode_char_mask(
start: usize,
len: usize,
haystack: &[u8],
needle_char: &UnicodeChar,
) -> B::Mask {
let char_len = needle_char.len;
debug_assert!(char_len <= 4 && char_len > 0);
if start + char_len > len {
return B::Mask::zero();
}
let (chunk, chunk_mask) = unsafe { load_window::<B>(haystack, start + char_len - 1, len) };
let mut mask =
unsafe { B::eq(chunk, B::splat(needle_char.chars[char_len - 1])) }.and(chunk_mask);
if mask.is_zero() {
// check the case flipped version
mask = unsafe { B::eq(chunk, B::splat(needle_char.flipped_chars[char_len - 1])) }
.and(chunk_mask);
// check that the rest of the bytes in the flipped case char match
if !mask.is_zero() && char_len > 1 {
mask = mask.and(unsafe {
Self::match_unicode_char_prefix(
start,
len,
haystack,
char_len,
needle_char.flipped_chars,
)
});
}
} else if char_len > 1 {
mask = mask.and(unsafe {
Self::match_unicode_char_prefix(start, len, haystack, char_len, needle_char.chars)
});
}
mask
}
#[inline(always)]
pub unsafe fn match_haystack_unicode(&self, haystack: &[u8]) -> (bool, usize, usize) {
let len = haystack.len();
if len == 0 {
return (false, 0, 0);
}
let mut can_skip_chunks = true;
let mut match_start_pos = 0usize;
let mut needle_iter = self.needle_unicode.iter();
let mut needle_char = needle_iter.next().unwrap();
let mut last_needle_char_bytes = unsafe {
(
B::splat(needle_char.chars[needle_char.len - 1]),
B::splat(needle_char.flipped_chars[needle_char.len - 1]),
)
};
let mut start = 0usize;
while start + needle_char.len <= len {
let (chunk, mut chunk_mask) =
unsafe { load_window::<B>(haystack, start + needle_char.len - 1, len) };
loop {
// check the last byte first since it's the most discriminating
// since the prefix bytes identify the script/block, not the char
// and nearby chars are likely to be all in the same script/block
let mut mask = unsafe { B::eq(chunk, last_needle_char_bytes.0) }.and(chunk_mask);
if mask.is_zero() {
// check the case flipped version
mask = unsafe { B::eq(chunk, last_needle_char_bytes.1) }.and(chunk_mask);
if mask.is_zero() {
break;
}
// check that the rest of the bytes in the flipped case char match
if needle_char.len > 1 {
mask = mask.and(unsafe {
Self::match_unicode_char_prefix(
start,
len,
haystack,
needle_char.len,
needle_char.flipped_chars,
)
});
if mask.is_zero() {
break;
}
}
}
// check that the rest of the bytes in the char match
if needle_char.len > 1 {
mask = mask.and(unsafe {
Self::match_unicode_char_prefix(
start,
len,
haystack,
needle_char.len,
needle_char.chars,
)
});
if mask.is_zero() {
break;
}
}
chunk_mask = chunk_mask.clear_through_lowest(mask);
if can_skip_chunks {
match_start_pos = start + mask.trailing_zeros();
can_skip_chunks = false;
}
if let Some(next_needle_char) = needle_iter.next() {
needle_char = next_needle_char;
last_needle_char_bytes = unsafe {
(
B::splat(needle_char.chars[needle_char.len - 1]),
B::splat(needle_char.flipped_chars[needle_char.len - 1]),
)
};
} else if start + needle_char.len - 1 + B::LANES >= len {
return (
true,
match_start_pos,
start + B::LANES - mask.leading_zeros() + needle_char.len - 1,
);
} else {
let end_pos = start
+ unsafe {
Self::find_last_unicode_char_pos(needle_char, &haystack[start..])
};
return (true, match_start_pos, end_pos);
}
}
start += B::LANES;
}
(false, match_start_pos, len)
}
#[inline(always)]
unsafe fn find_last_unicode_char_pos(needle_char: &UnicodeChar, haystack: &[u8]) -> usize {
let len = haystack.len();
let char_len = needle_char.len;
debug_assert!(char_len <= 4 && char_len > 0);
debug_assert!(len >= char_len);
let last_bytes = unsafe {
(
B::splat(needle_char.chars[char_len - 1]),
B::splat(needle_char.flipped_chars[char_len - 1]),
)
};
let mut start = len.saturating_sub(B::LANES + char_len - 1);
loop {
let (chunk, chunk_mask) =
unsafe { load_window::<B>(haystack, start + char_len - 1, len) };
// we check both the original and flipped case bytes simultaneously since either
// could have matched. this can result in a false positive, but that's fine for
// this stage since this just controls bounds, and bounds being too large hurt
// performance, not correctness.
let mut mask = unsafe { B::eq(chunk, last_bytes.0).or(B::eq(chunk, last_bytes.1)) }
.and(chunk_mask);
if !mask.is_zero() && char_len > 1 {
mask = mask.and(unsafe {
Self::match_unicode_char_prefix(
start,
len,
haystack,
char_len,
needle_char.chars,
)
.or(Self::match_unicode_char_prefix(
start,
len,
haystack,
char_len,
needle_char.flipped_chars,
))
});
}
if !mask.is_zero() {
return start + B::LANES - mask.leading_zeros() + char_len - 1;
}
if start == 0 {
break;
}
start = start.saturating_sub(B::LANES);
}
len
}
}