Mercurial > public > mercurial-scm > hg
comparison rust/hg-core/src/utils/mod.rs @ 42437:9609430d3625
rust-filepatterns: use bytes instead of String
In my initial patch, I introduced an unnecessary hard constraint on UTF-8
filenames and patterns which I forgot to remove. Although the performance
penalty for using String might be negligible, we don't want to break
compatibility with non-UTF-8 encodings for no reason.
Moreover, this change allows for a cleaner Rust core API.
This patch introduces a new utils module that is used with this fix.
Finally, PatternError was not put inside the Python module generated by
Rust, which would have raised a NameError.
Differential Revision: https://phab.mercurial-scm.org/D6485
author | Rapha?l Gom?s <rgomes@octobus.net> |
---|---|
date | Thu, 06 Jun 2019 15:30:56 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
42436:dc5bd66a8270 | 42437:9609430d3625 |
---|---|
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 } |