equal
deleted
inserted
replaced
|
1 pub mod files; |
|
2 |
|
3 pub fn replace_slice<T>(buf: &mut [T], from: &[T], to: &[T]) |
|
4 where |
|
5 T: Clone + PartialEq, |
|
6 { |
|
7 if buf.len() < from.len() || from.len() != to.len() { |
|
8 return; |
|
9 } |
|
10 for i in 0..=buf.len() - from.len() { |
|
11 if buf[i..].starts_with(from) { |
|
12 buf[i..(i + from.len())].clone_from_slice(to); |
|
13 } |
|
14 } |
|
15 } |
|
16 |
|
17 pub trait SliceExt { |
|
18 fn trim(&self) -> &Self; |
|
19 fn trim_end(&self) -> &Self; |
|
20 } |
|
21 |
|
22 fn is_not_whitespace(c: &u8) -> bool { |
|
23 !(*c as char).is_whitespace() |
|
24 } |
|
25 |
|
26 impl SliceExt for [u8] { |
|
27 fn trim(&self) -> &[u8] { |
|
28 if let Some(first) = self.iter().position(is_not_whitespace) { |
|
29 if let Some(last) = self.iter().rposition(is_not_whitespace) { |
|
30 &self[first..last + 1] |
|
31 } else { |
|
32 unreachable!(); |
|
33 } |
|
34 } else { |
|
35 &[] |
|
36 } |
|
37 } |
|
38 fn trim_end(&self) -> &[u8] { |
|
39 if let Some(last) = self.iter().rposition(is_not_whitespace) { |
|
40 &self[..last + 1] |
|
41 } else { |
|
42 &[] |
|
43 } |
|
44 } |
|
45 } |