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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
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!(),
}
}
/// Occurrence mask for one case variant of a needle char: lanes where
/// `last_byte` matches, verified against the char's remaining UTF-8
/// bytes
#[inline(always)]
unsafe fn char_variant_mask(
(chunk, chunk_mask): (B::Chunk, B::Mask),
last_byte: B::Chunk,
start: usize,
len: usize,
haystack: &[u8],
char_len: usize,
chars: [u8; 4],
) -> B::Mask {
let mut mask = unsafe { B::eq(chunk, last_byte) }.and(chunk_mask);
if !mask.is_zero() && char_len > 1 {
mask = mask.and(unsafe {
Self::match_unicode_char_prefix(start, len, haystack, char_len, chars)
});
}
mask
}
#[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) };
// check the regular needle
let mut mask = unsafe {
Self::char_variant_mask(
(chunk, chunk_mask),
B::splat(needle_char.chars[char_len - 1]),
start,
len,
haystack,
char_len,
needle_char.chars,
)
};
// check the case flipped version
mask = mask.or(unsafe {
Self::char_variant_mask(
(chunk, chunk_mask),
B::splat(needle_char.flipped_chars[char_len - 1]),
start,
len,
haystack,
char_len,
needle_char.flipped_chars,
)
});
mask
}
#[cfg_attr(not(target_arch = "wasm32"), inline(always))]
#[cfg_attr(target_arch = "wasm32", inline(never))]
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 {
// keep the subsequence mask (`available`) separate from the load's
// width-dependent bounds (`valid`) so a width change can reload
// without dropping consumed lanes
let mut char_len = needle_char.len;
let (mut chunk, mut valid) =
unsafe { load_window::<B>(haystack, start + char_len - 1, len) };
let mut available = B::Mask::all();
loop {
let chunk_mask = available.and(valid);
// check the regular needle first
let mut mask = unsafe {
Self::char_variant_mask(
(chunk, chunk_mask),
last_needle_char_bytes.0,
start,
len,
haystack,
needle_char.len,
needle_char.chars,
)
};
// check the case flipped version
mask = mask.or(unsafe {
Self::char_variant_mask(
(chunk, chunk_mask),
last_needle_char_bytes.1,
start,
len,
haystack,
needle_char.len,
needle_char.flipped_chars,
)
});
if mask.is_zero() {
break;
}
available = available.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]),
)
};
// reload the window when the char width changes to realign the lanes
if needle_char.len != char_len {
if start + needle_char.len > len {
break;
}
char_len = needle_char.len;
(chunk, valid) =
unsafe { load_window::<B>(haystack, start + char_len - 1, len) };
}
} 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
}
}