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
use haystack::{Hay, Haystack};
use std::ops::Range;

unsafe impl Hay for str {
    type Index = usize;

    #[inline]
    fn empty<'a>() -> &'a Self {
        ""
    }

    #[inline]
    fn start_index(&self) -> usize {
        0
    }

    #[inline]
    fn end_index(&self) -> usize {
        self.len()
    }

    #[inline]
    unsafe fn slice_unchecked(&self, range: Range<usize>) -> &Self {
        self.get_unchecked(range)
    }

    #[inline]
    unsafe fn next_index(&self, index: Self::Index) -> Self::Index {
        index + self.get_unchecked(index..).chars().next().unwrap().len_utf8()
    }

    #[inline]
    unsafe fn prev_index(&self, index: Self::Index) -> Self::Index {
        index - self.get_unchecked(..index).chars().next_back().unwrap().len_utf8()
    }
}

unsafe impl<'h> Haystack for &'h mut str {
    #[inline]
    fn empty() -> &'h mut str {
        Self::default()
    }

    #[inline]
    unsafe fn slice_unchecked(self, range: Range<usize>) -> Self {
        self.get_unchecked_mut(range)
    }

    #[inline]
    unsafe fn split_around(self, range: Range<usize>) -> [Self; 3] {
        let (haystack, right) = self.split_at_mut(range.end);
        let (left, middle) = haystack.split_at_mut(range.start);
        [left, middle, right]
    }

    #[inline]
    fn restore_range(&self, range: Range<usize>, subrange: Range<usize>) -> Range<usize> {
        (subrange.start + range.start)..(subrange.end + range.start)
    }
}

mod char;
mod func;
mod str;