author | Raphaël Gomès <rgomes@octobus.net> |
Tue, 09 Jul 2019 11:49:49 +0200 | |
changeset 42764 | 7cae6bc29ff9 |
parent 42611 | 2f760da140ee |
child 42767 | 4b3b27d567d5 |
permissions | -rw-r--r-- |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
1 |
pub mod files; |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
2 |
|
42764
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42611
diff
changeset
|
3 |
use std::convert::AsMut; |
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42611
diff
changeset
|
4 |
|
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42611
diff
changeset
|
5 |
/// Takes a slice and copies it into an array. |
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42611
diff
changeset
|
6 |
/// |
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42611
diff
changeset
|
7 |
/// # Panics |
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42611
diff
changeset
|
8 |
/// |
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42611
diff
changeset
|
9 |
/// Will panic if the slice and target array don't have the same length. |
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42611
diff
changeset
|
10 |
pub fn copy_into_array<A, T>(slice: &[T]) -> A |
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42611
diff
changeset
|
11 |
where |
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42611
diff
changeset
|
12 |
A: Sized + Default + AsMut<[T]>, |
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42611
diff
changeset
|
13 |
T: Copy, |
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42611
diff
changeset
|
14 |
{ |
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42611
diff
changeset
|
15 |
let mut a = Default::default(); |
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42611
diff
changeset
|
16 |
<A as AsMut<[T]>>::as_mut(&mut a).copy_from_slice(slice); |
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42611
diff
changeset
|
17 |
a |
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42611
diff
changeset
|
18 |
} |
7cae6bc29ff9
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop
Raphaël Gomès <rgomes@octobus.net>
parents:
42611
diff
changeset
|
19 |
|
42610
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
20 |
/// 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
|
21 |
/// |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
22 |
/// # Examples |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
23 |
/// |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
24 |
/// ``` |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
25 |
/// 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
|
26 |
/// 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
|
27 |
/// 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
|
28 |
/// assert_eq!( |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
29 |
/// line, |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
30 |
/// b"I love 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
|
31 |
///); |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
32 |
/// |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
33 |
/// ``` |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
34 |
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
|
35 |
where |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
36 |
T: Clone + PartialEq, |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
37 |
{ |
42611
2f760da140ee
rust-utils: remove buggy assertion
Raphaël Gomès <rgomes@octobus.net>
parents:
42610
diff
changeset
|
38 |
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
|
39 |
return; |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
40 |
} |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
41 |
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
|
42 |
if buf[i..].starts_with(from) { |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
43 |
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
|
44 |
} |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
45 |
} |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
46 |
} |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
47 |
|
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
48 |
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
|
49 |
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
|
50 |
fn trim_start(&self) -> &Self; |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
51 |
fn trim(&self) -> &Self; |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
52 |
} |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
53 |
|
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
54 |
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
|
55 |
!(*c as char).is_whitespace() |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
56 |
} |
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 |
impl SliceExt for [u8] { |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
59 |
fn trim_end(&self) -> &[u8] { |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
60 |
if let Some(last) = self.iter().rposition(is_not_whitespace) { |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
61 |
&self[..last + 1] |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
62 |
} else { |
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 |
} |
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_start(&self) -> &[u8] { |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
67 |
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
|
68 |
&self[first..] |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
69 |
} else { |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
70 |
&[] |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
71 |
} |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
72 |
} |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
73 |
|
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
74 |
/// ``` |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
75 |
/// 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
|
76 |
/// assert_eq!( |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
77 |
/// 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
|
78 |
/// b"to trim" |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
79 |
/// ); |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
80 |
/// assert_eq!( |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
81 |
/// 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
|
82 |
/// b"to trim" |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
83 |
/// ); |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
84 |
/// assert_eq!( |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
85 |
/// 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
|
86 |
/// b"to trim" |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
87 |
/// ); |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
88 |
/// ``` |
5672bb73f61e
rust-utils: add docstrings and doctests for utils.rs
Raphaël Gomès <rgomes@octobus.net>
parents:
42609
diff
changeset
|
89 |
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
|
90 |
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
|
91 |
} |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
92 |
} |