Mercurial > public > mercurial-scm > hg-stable
annotate rust/hg-core/src/utils.rs @ 46617:a34cd9aa3323
copies-rust: yield both p1 and p2 copies in `ChangedFiles.actions()`
Instead of filtering the relevant parent inside de ChangedFiles method, we now
yield all copies information and let the caller do the filtering. Soon, the
filtering will be replaced by dispatching.
Differential Revision: https://phab.mercurial-scm.org/D9649
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Mon, 21 Dec 2020 10:24:16 +0100 |
parents | a25033eb43b5 |
children | 435d9fc72646 |
rev | line source |
---|---|
42767
4b3b27d567d5
rust-docstrings: add missing module docstrings
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42764
diff
changeset
|
1 // utils module |
4b3b27d567d5
rust-docstrings: add missing module docstrings
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42764
diff
changeset
|
2 // |
4b3b27d567d5
rust-docstrings: add missing module docstrings
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42764
diff
changeset
|
3 // Copyright 2019 Raphaël Gomès <rgomes@octobus.net> |
4b3b27d567d5
rust-docstrings: add missing module docstrings
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42764
diff
changeset
|
4 // |
4b3b27d567d5
rust-docstrings: add missing module docstrings
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42764
diff
changeset
|
5 // This software may be used and distributed according to the terms of the |
4b3b27d567d5
rust-docstrings: add missing module docstrings
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42764
diff
changeset
|
6 // GNU General Public License version 2 or any later version. |
4b3b27d567d5
rust-docstrings: add missing module docstrings
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42764
diff
changeset
|
7 |
4b3b27d567d5
rust-docstrings: add missing module docstrings
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42764
diff
changeset
|
8 //! Contains useful functions, traits, structs, etc. for use in core. |
4b3b27d567d5
rust-docstrings: add missing module docstrings
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42764
diff
changeset
|
9 |
46513
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
46091
diff
changeset
|
10 use crate::errors::{HgError, IoErrorContext}; |
44315
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
11 use crate::utils::hg_path::HgPath; |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
12 use std::{io::Write, ops::Deref}; |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
13 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
14 pub mod files; |
42959
3fe40dd6355d
rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42869
diff
changeset
|
15 pub mod hg_path; |
44312
c18dd48cea4a
rust-pathauditor: add Rust implementation of the `pathauditor`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44079
diff
changeset
|
16 pub mod path_auditor; |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
17 |
44079
191a461d6be6
rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42959
diff
changeset
|
18 /// Useful until rust/issues/56345 is stable |
191a461d6be6
rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42959
diff
changeset
|
19 /// |
191a461d6be6
rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42959
diff
changeset
|
20 /// # Examples |
191a461d6be6
rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42959
diff
changeset
|
21 /// |
191a461d6be6
rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42959
diff
changeset
|
22 /// ``` |
191a461d6be6
rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42959
diff
changeset
|
23 /// use crate::hg::utils::find_slice_in_slice; |
191a461d6be6
rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42959
diff
changeset
|
24 /// |
191a461d6be6
rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42959
diff
changeset
|
25 /// let haystack = b"This is the haystack".to_vec(); |
191a461d6be6
rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42959
diff
changeset
|
26 /// assert_eq!(find_slice_in_slice(&haystack, b"the"), Some(8)); |
191a461d6be6
rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42959
diff
changeset
|
27 /// assert_eq!(find_slice_in_slice(&haystack, b"not here"), None); |
191a461d6be6
rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42959
diff
changeset
|
28 /// ``` |
191a461d6be6
rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42959
diff
changeset
|
29 pub fn find_slice_in_slice<T>(slice: &[T], needle: &[T]) -> Option<usize> |
191a461d6be6
rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42959
diff
changeset
|
30 where |
191a461d6be6
rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42959
diff
changeset
|
31 for<'a> &'a [T]: PartialEq, |
191a461d6be6
rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42959
diff
changeset
|
32 { |
191a461d6be6
rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42959
diff
changeset
|
33 slice |
191a461d6be6
rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42959
diff
changeset
|
34 .windows(needle.len()) |
191a461d6be6
rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42959
diff
changeset
|
35 .position(|window| window == needle) |
191a461d6be6
rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42959
diff
changeset
|
36 } |
191a461d6be6
rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42959
diff
changeset
|
37 |
42610
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
38 /// Replaces the `from` slice with the `to` slice inside the `buf` slice. |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
39 /// |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
40 /// # Examples |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
41 /// |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
42 /// ``` |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
43 /// use crate::hg::utils::replace_slice; |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
44 /// let mut line = b"I hate writing tests!".to_vec(); |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
45 /// replace_slice(&mut line, b"hate", b"love"); |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
46 /// assert_eq!( |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
47 /// line, |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
48 /// b"I love writing tests!".to_vec() |
42851
ce6797ef6eab
rust: apply more formatting fixes
Yuya Nishihara <yuya@tcha.org>
parents:
42815
diff
changeset
|
49 /// ); |
42610
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
50 /// ``` |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
51 pub fn replace_slice<T>(buf: &mut [T], from: &[T], to: &[T]) |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
52 where |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
53 T: Clone + PartialEq, |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
54 { |
42611
2f760da140ee
rust-utils: remove buggy assertion
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42610
diff
changeset
|
55 if buf.len() < from.len() || from.len() != to.len() { |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
56 return; |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
57 } |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
58 for i in 0..=buf.len() - from.len() { |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
59 if buf[i..].starts_with(from) { |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
60 buf[i..(i + from.len())].clone_from_slice(to); |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
61 } |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
62 } |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
63 } |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
64 |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
65 pub trait SliceExt { |
42610
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
66 fn trim_end(&self) -> &Self; |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
67 fn trim_start(&self) -> &Self; |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
68 fn trim(&self) -> &Self; |
42869
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42851
diff
changeset
|
69 fn drop_prefix(&self, needle: &Self) -> Option<&Self>; |
46557
a25033eb43b5
rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents:
46542
diff
changeset
|
70 fn split_2(&self, separator: u8) -> Option<(&[u8], &[u8])>; |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
71 } |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
72 |
44998
26114bd6ec60
rust: do a clippy pass
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44315
diff
changeset
|
73 #[allow(clippy::trivially_copy_pass_by_ref)] |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
74 fn is_not_whitespace(c: &u8) -> bool { |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
75 !(*c as char).is_whitespace() |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
76 } |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
77 |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
78 impl SliceExt for [u8] { |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
79 fn trim_end(&self) -> &[u8] { |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
80 if let Some(last) = self.iter().rposition(is_not_whitespace) { |
44998
26114bd6ec60
rust: do a clippy pass
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44315
diff
changeset
|
81 &self[..=last] |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
82 } else { |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
83 &[] |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
84 } |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
85 } |
42610
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
86 fn trim_start(&self) -> &[u8] { |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
87 if let Some(first) = self.iter().position(is_not_whitespace) { |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
88 &self[first..] |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
89 } else { |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
90 &[] |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
91 } |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
92 } |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
93 |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
94 /// ``` |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
95 /// use hg::utils::SliceExt; |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
96 /// assert_eq!( |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
97 /// b" to trim ".trim(), |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
98 /// b"to trim" |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
99 /// ); |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
100 /// assert_eq!( |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
101 /// b"to trim ".trim(), |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
102 /// b"to trim" |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
103 /// ); |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
104 /// assert_eq!( |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
105 /// b" to trim".trim(), |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
106 /// b"to trim" |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
107 /// ); |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
108 /// ``` |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
109 fn trim(&self) -> &[u8] { |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
110 self.trim_start().trim_end() |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42609
diff
changeset
|
111 } |
42869
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42851
diff
changeset
|
112 |
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42851
diff
changeset
|
113 fn drop_prefix(&self, needle: &Self) -> Option<&Self> { |
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42851
diff
changeset
|
114 if self.starts_with(needle) { |
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42851
diff
changeset
|
115 Some(&self[needle.len()..]) |
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42851
diff
changeset
|
116 } else { |
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42851
diff
changeset
|
117 None |
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42851
diff
changeset
|
118 } |
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42851
diff
changeset
|
119 } |
46557
a25033eb43b5
rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents:
46542
diff
changeset
|
120 |
a25033eb43b5
rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents:
46542
diff
changeset
|
121 fn split_2(&self, separator: u8) -> Option<(&[u8], &[u8])> { |
a25033eb43b5
rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents:
46542
diff
changeset
|
122 let mut iter = self.splitn(2, |&byte| byte == separator); |
a25033eb43b5
rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents:
46542
diff
changeset
|
123 let a = iter.next()?; |
a25033eb43b5
rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents:
46542
diff
changeset
|
124 let b = iter.next()?; |
a25033eb43b5
rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents:
46542
diff
changeset
|
125 Some((a, b)) |
a25033eb43b5
rhg: add limited support for the `config` sub-command
Simon Sapin <simon.sapin@octobus.net>
parents:
46542
diff
changeset
|
126 } |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
127 } |
44315
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
128 |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
129 pub trait Escaped { |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
130 /// Return bytes escaped for display to the user |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
131 fn escaped_bytes(&self) -> Vec<u8>; |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
132 } |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
133 |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
134 impl Escaped for u8 { |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
135 fn escaped_bytes(&self) -> Vec<u8> { |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
136 let mut acc = vec![]; |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
137 match self { |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
138 c @ b'\'' | c @ b'\\' => { |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
139 acc.push(b'\\'); |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
140 acc.push(*c); |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
141 } |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
142 b'\t' => { |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
143 acc.extend(br"\\t"); |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
144 } |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
145 b'\n' => { |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
146 acc.extend(br"\\n"); |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
147 } |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
148 b'\r' => { |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
149 acc.extend(br"\\r"); |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
150 } |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
151 c if (*c < b' ' || *c >= 127) => { |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
152 write!(acc, "\\x{:x}", self).unwrap(); |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
153 } |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
154 c => { |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
155 acc.push(*c); |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
156 } |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
157 } |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
158 acc |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
159 } |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
160 } |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
161 |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
162 impl<'a, T: Escaped> Escaped for &'a [T] { |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
163 fn escaped_bytes(&self) -> Vec<u8> { |
44998
26114bd6ec60
rust: do a clippy pass
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44315
diff
changeset
|
164 self.iter().flat_map(Escaped::escaped_bytes).collect() |
44315
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
165 } |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
166 } |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
167 |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
168 impl<T: Escaped> Escaped for Vec<T> { |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
169 fn escaped_bytes(&self) -> Vec<u8> { |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
170 self.deref().escaped_bytes() |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
171 } |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
172 } |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
173 |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
174 impl<'a> Escaped for &'a HgPath { |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
175 fn escaped_bytes(&self) -> Vec<u8> { |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
176 self.as_bytes().escaped_bytes() |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
177 } |
aa0fc32ece9e
rust-utils: add `Escaped` trait
Rapha?l Gom?s <rgomes@octobus.net>
parents:
44312
diff
changeset
|
178 } |
46091
9eb07ab3f2d4
rhg: use persistent nodemap when available
Simon Sapin <simon-commits@exyr.org>
parents:
44998
diff
changeset
|
179 |
9eb07ab3f2d4
rhg: use persistent nodemap when available
Simon Sapin <simon-commits@exyr.org>
parents:
44998
diff
changeset
|
180 // TODO: use the str method when we require Rust 1.45 |
9eb07ab3f2d4
rhg: use persistent nodemap when available
Simon Sapin <simon-commits@exyr.org>
parents:
44998
diff
changeset
|
181 pub(crate) fn strip_suffix<'a>(s: &'a str, suffix: &str) -> Option<&'a str> { |
9eb07ab3f2d4
rhg: use persistent nodemap when available
Simon Sapin <simon-commits@exyr.org>
parents:
44998
diff
changeset
|
182 if s.ends_with(suffix) { |
9eb07ab3f2d4
rhg: use persistent nodemap when available
Simon Sapin <simon-commits@exyr.org>
parents:
44998
diff
changeset
|
183 Some(&s[..s.len() - suffix.len()]) |
9eb07ab3f2d4
rhg: use persistent nodemap when available
Simon Sapin <simon-commits@exyr.org>
parents:
44998
diff
changeset
|
184 } else { |
9eb07ab3f2d4
rhg: use persistent nodemap when available
Simon Sapin <simon-commits@exyr.org>
parents:
44998
diff
changeset
|
185 None |
9eb07ab3f2d4
rhg: use persistent nodemap when available
Simon Sapin <simon-commits@exyr.org>
parents:
44998
diff
changeset
|
186 } |
9eb07ab3f2d4
rhg: use persistent nodemap when available
Simon Sapin <simon-commits@exyr.org>
parents:
44998
diff
changeset
|
187 } |
46513
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
46091
diff
changeset
|
188 |
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
46091
diff
changeset
|
189 pub fn current_dir() -> Result<std::path::PathBuf, HgError> { |
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
46091
diff
changeset
|
190 std::env::current_dir().map_err(|error| HgError::IoError { |
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
46091
diff
changeset
|
191 error, |
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
46091
diff
changeset
|
192 context: IoErrorContext::CurrentDir, |
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
46091
diff
changeset
|
193 }) |
ca3f73cc3cf4
rhg: Simplify CommandError based on its use
Simon Sapin <simon.sapin@octobus.net>
parents:
46091
diff
changeset
|
194 } |
46542
2845892dd489
rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46513
diff
changeset
|
195 |
2845892dd489
rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46513
diff
changeset
|
196 pub fn current_exe() -> Result<std::path::PathBuf, HgError> { |
2845892dd489
rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46513
diff
changeset
|
197 std::env::current_exe().map_err(|error| HgError::IoError { |
2845892dd489
rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46513
diff
changeset
|
198 error, |
2845892dd489
rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46513
diff
changeset
|
199 context: IoErrorContext::CurrentExe, |
2845892dd489
rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46513
diff
changeset
|
200 }) |
2845892dd489
rust: Parse system and user configuration
Simon Sapin <simon.sapin@octobus.net>
parents:
46513
diff
changeset
|
201 } |