Skip to main content

ct_regex_internal/haystack/ext/
ecow.rs

1use std::ops::Range;
2
3use ecow::{EcoString, EcoVec};
4
5use crate::haystack::{ByteStack, HaystackIter, IntoHaystack, OwnedHaystackable, StrStack};
6
7impl OwnedHaystackable<char> for EcoString {
8    type Hay<'a> = StrStack<'a>;
9
10    fn replace_range<'a>(
11        &mut self,
12        range: Range<usize>,
13        with: <Self::Hay<'a> as HaystackIter<'a>>::Slice
14    ) where Self: 'a {
15        let tail = EcoString::from(self.split_at(range.end).1);
16        self.truncate(range.start);
17        self.push_str(with);
18        self.push_str(&tail);
19    }
20
21    fn as_haystack<'a>(&'a self) -> Self::Hay<'a> {
22        self.into_haystack()
23    }
24
25    fn as_slice<'a>(&'a self) -> <Self::Hay<'a> as HaystackIter<'a>>::Slice {
26        self
27    }
28
29    fn len(&self) -> usize {
30        self.len()
31    }
32}
33
34impl OwnedHaystackable<u8> for EcoVec<u8> {
35    type Hay<'a> = ByteStack<'a>;
36
37    fn replace_range<'a>(
38        &mut self,
39        range: Range<usize>,
40        with: <Self::Hay<'a> as HaystackIter<'a>>::Slice
41    ) where Self: 'a {
42        if range.len() == with.len() {
43            let mut_self = self.make_mut();
44            for (src, target) in range.enumerate() {
45                mut_self[target] = with[src];
46            }
47        } else {
48            let tail = EcoVec::from(self.split_at(range.end).1);
49            self.truncate(range.start);
50            self.extend_from_slice(with);
51            self.extend_from_slice(&tail);
52        }
53    }
54
55    fn as_haystack<'a>(&'a self) -> Self::Hay<'a> {
56        self.into_haystack()
57    }
58
59    fn as_slice<'a>(&'a self) -> <Self::Hay<'a> as HaystackIter<'a>>::Slice {
60        self
61    }
62
63    fn len(&self) -> usize {
64        self.len()
65    }
66}