-
Notifications
You must be signed in to change notification settings - Fork 0
/
v5.rs
367 lines (301 loc) · 10.5 KB
/
v5.rs
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
pub trait Pattern<H: SearchPtrs>: Sized {
type Searcher: Searcher<H>;
fn into_searcher(self, haystack: H) -> Self::Searcher;
fn is_prefix_of(self, haystack: H) -> bool;
fn is_suffix_of(self, haystack: H) -> bool
where Self::Searcher: ReverseSearcher<H>;
fn is_contained_in(self, haystack: H) -> bool {
self.into_searcher(haystack).next_match().is_some()
}
}
// Defined associated types and functions
// for dealing with positions in a slice-like type
// with pointer-like cursors
// Logically, Haystack <= Cursor <= Back
pub trait SearchPtrs {
// For storing the bounds of the haystack.
// Usually a combination of Memory address in form of a raw pointer or usize
type Haystack: Copy;
// Begin or End of a Match.
// Two of these can be used to define a range of elements
// as found by a Searcher.
// Can be absolute, or relative to Haystack.
// Usually a Memory address in form of a raw pointer or usize
type Cursor: Copy;
unsafe fn offset_from_start(hs: Self::Haystack, begin: Self::Cursor) -> usize;
unsafe fn range_to_self(hs: Self::Haystack,
start: Self::Cursor,
end: Self::Cursor) -> Self;
unsafe fn cursor_at_front(hs: Self::Haystack) -> Self::Cursor;
unsafe fn cursor_at_back(hs: Self::Haystack) -> Self::Cursor;
}
pub unsafe trait Searcher<H: SearchPtrs> {
fn haystack(&self) -> H::Haystack;
fn next_match(&mut self) -> Option<(H::Cursor, H::Cursor)>;
fn next_reject(&mut self) -> Option<(H::Cursor, H::Cursor)>;
}
pub unsafe trait ReverseSearcher<H: SearchPtrs>: Searcher<H> {
fn next_match_back(&mut self) -> Option<(H::Cursor, H::Cursor)>;
fn next_reject_back(&mut self) -> Option<(H::Cursor, H::Cursor)>;
}
pub trait DoubleEndedSearcher<H: SearchPtrs>: ReverseSearcher<H> {}
pub mod string {
use super::*;
impl<'a> SearchPtrs for &'a str {
type Haystack = (*const u8, *const u8);
type Cursor = *const u8;
unsafe fn offset_from_start(haystack: Self::Haystack,
begin: Self::Cursor) -> usize {
begin as usize - haystack.0 as usize
}
unsafe fn range_to_self(_: Self::Haystack,
start: Self::Cursor,
end: Self::Cursor) -> Self {
let slice = ::std::slice::from_raw_parts(start,
end as usize - start as usize);
::std::str::from_utf8_unchecked(slice)
}
unsafe fn cursor_at_front(hs: Self::Haystack) -> Self::Cursor {
hs.0
}
unsafe fn cursor_at_back(hs: Self::Haystack) -> Self::Cursor {
hs.1
}
}
pub struct Ascii(pub u8);
pub struct AsciiSearcher<'a> {
haystack: (*const u8, *const u8),
start: *const u8,
end: *const u8,
ascii: u8,
_marker: ::std::marker::PhantomData<&'a str>
}
unsafe impl<'a> Searcher<&'a str> for AsciiSearcher<'a> {
fn haystack(&self) -> (*const u8, *const u8) {
self.haystack
}
fn next_match(&mut self) -> Option<(*const u8, *const u8)> {
while self.start != self.end {
unsafe {
let p = self.start;
self.start = self.start.offset(1);
if *p == self.ascii {
return Some((p, self.start));
}
}
}
None
}
fn next_reject(&mut self) -> Option<(*const u8, *const u8)> {
while self.start != self.end {
unsafe {
let p = self.start;
self.start = self.start.offset(1);
if *p != self.ascii {
return Some((p, self.start));
}
}
}
None
}
}
impl<'a> Pattern<&'a str> for Ascii {
type Searcher = AsciiSearcher<'a>;
fn into_searcher(self, haystack: &'a str) -> Self::Searcher {
let begin = haystack.as_ptr();
let end = unsafe {
haystack.as_ptr().offset(haystack.len() as isize)
};
AsciiSearcher {
haystack: (begin, end),
start: begin,
end: end,
ascii: self.0,
_marker: ::std::marker::PhantomData,
}
}
fn is_prefix_of(self, haystack: &'a str) -> bool {
haystack.as_bytes()
.get(0)
.map(|&b| b == self.0)
.unwrap_or(false)
}
fn is_suffix_of(self, haystack: &'a str) -> bool
where Self::Searcher: ReverseSearcher<&'a str> {
haystack.as_bytes()
.get(haystack.len() - 1)
.map(|&b| b == self.0)
.unwrap_or(false)
}
}
}
pub mod slice {
use super::*;
impl<'a> SearchPtrs for &'a mut [u8] {
// Store address bounds as usize since aliasing interaction is unclear
type Haystack = (*mut u8, *mut u8);
type Cursor = *mut u8;
unsafe fn offset_from_start(haystack: Self::Haystack,
begin: Self::Cursor) -> usize {
begin as usize - haystack.0 as usize
}
unsafe fn range_to_self(_: Self::Haystack,
start: Self::Cursor,
end: Self::Cursor) -> Self {
::std::slice::from_raw_parts_mut(start,
end as usize - start as usize)
}
unsafe fn cursor_at_front(hs: Self::Haystack) -> Self::Cursor {
hs.0 as *mut u8
}
unsafe fn cursor_at_back(hs: Self::Haystack) -> Self::Cursor {
hs.1 as *mut u8
}
}
pub struct Ascii(pub u8);
pub struct AsciiSearcher<'a> {
haystack: (*mut u8, *mut u8),
start: *mut u8,
end: *mut u8,
ascii: u8,
_marker: ::std::marker::PhantomData<&'a mut [u8]>
}
unsafe impl<'a> Searcher<&'a mut [u8]> for AsciiSearcher<'a> {
fn haystack(&self) -> (*mut u8, *mut u8) {
self.haystack
}
fn next_match(&mut self) -> Option<(*mut u8, *mut u8)> {
while self.start != self.end {
unsafe {
let p = self.start;
self.start = self.start.offset(1);
if *p == self.ascii {
return Some((p, self.start));
}
}
}
None
}
fn next_reject(&mut self) -> Option<(*mut u8, *mut u8)> {
while self.start != self.end {
unsafe {
let p = self.start;
self.start = self.start.offset(1);
if *p != self.ascii {
return Some((p, self.start));
}
}
}
None
}
}
impl<'a> Pattern<&'a mut [u8]> for Ascii {
type Searcher = AsciiSearcher<'a>;
fn into_searcher(self, haystack: &'a mut [u8]) -> Self::Searcher {
let begin = haystack.as_mut_ptr();
let end = unsafe {
haystack.as_mut_ptr().offset(haystack.len() as isize)
};
AsciiSearcher {
haystack: (begin, end),
start: begin,
end: end,
ascii: self.0,
_marker: ::std::marker::PhantomData,
}
}
fn is_prefix_of(self, haystack: &'a mut [u8]) -> bool {
haystack
.get(0)
.map(|&b| b == self.0)
.unwrap_or(false)
}
fn is_suffix_of(self, haystack: &'a mut [u8]) -> bool
where Self::Searcher: ReverseSearcher<&'a mut [u8]> {
haystack
.get(haystack.len() - 1)
.map(|&b| b == self.0)
.unwrap_or(false)
}
}
}
pub mod os_string {
//use super::*;
}
pub mod api_consumer {
use super::*;
pub fn match_indices<H, P>(haystack: H, pattern: P) -> Vec<(usize, H)>
where H: SearchPtrs,
P: Pattern<H>,
{
let mut searcher = pattern.into_searcher(haystack);
let mut ret = vec![];
while let Some((begin, end)) = searcher.next_match() {
let haystack = searcher.haystack();
unsafe {
let offset = H::offset_from_start(haystack, begin);
let slice = H::range_to_self(haystack, begin, end);
ret.push((offset, slice));
}
}
ret
}
#[test]
fn test_match_indices() {
assert_eq!(match_indices("banana", string::Ascii(b'a')),
vec![(1, "a"), (3, "a"), (5, "a")]);
let mut slice = &mut {*b"banana"}[..];
{
let match_indices = match_indices(&mut*slice, slice::Ascii(b'a'));
assert_eq!(match_indices.iter().map(|x| x.0).collect::<Vec<_>>(),
vec![1, 3, 5]);
for m in match_indices {
m.1[0] = b'i';
}
}
assert_eq!(slice, b"binini");
}
pub fn split<H, P>(haystack: H, pattern: P) -> Vec<H>
where H: SearchPtrs,
P: Pattern<H>,
{
let mut searcher = pattern.into_searcher(haystack);
let mut ret = vec![];
let haystack = searcher.haystack();
let mut last_end = Some(unsafe {
H::cursor_at_front(haystack)
});
while let Some((begin, end)) = searcher.next_match() {
if let Some(last_end) = last_end {
unsafe {
let slice = H::range_to_self(haystack, last_end, begin);
ret.push(slice);
}
}
last_end = Some(end);
}
if let Some(last_end) = last_end {
unsafe {
let end = H::cursor_at_back(haystack);
let slice = H::range_to_self(haystack, last_end, end);
ret.push(slice);
}
}
ret
}
#[test]
fn test_split() {
assert_eq!(split("hangman", string::Ascii(b'a')),
vec!["h", "ngm", "n"]);
let mut slice = &mut {*b"hangman"}[..];
{
let split = split(&mut*slice, slice::Ascii(b'a'));
for m in split {
for byte in m {
*byte = b'-';
}
}
}
assert_eq!(slice, b"-a---a-");
}
}