annotate rust/hg-core/src/utils.rs @ 44142:63db6657d280

rust-nodemap: building blocks for nodetree structures This is similar to `nodetreenode` in `revlog.c`. We give it a higher level feeling for ease of handling in Rust context and provide tools for tests and debugging. The encoding choice is dictated by our ultimate goal in this series, that is to make an append-only persistent version of `nodetree`: the 0th Block must be adressed from other Blocks. Differential Revision: https://phab.mercurial-scm.org/D7787
author Georges Racinet <georges.racinet@octobus.net>
date Wed, 22 Jan 2020 16:23:29 +0100
parents 191a461d6be6
children c18dd48cea4a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
42751
4b3b27d567d5 rust-docstrings: add missing module docstrings
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42748
diff changeset
1 // utils module
4b3b27d567d5 rust-docstrings: add missing module docstrings
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42748
diff changeset
2 //
4b3b27d567d5 rust-docstrings: add missing module docstrings
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42748
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: 42748
diff changeset
4 //
4b3b27d567d5 rust-docstrings: add missing module docstrings
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42748
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: 42748
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: 42748
diff changeset
7
4b3b27d567d5 rust-docstrings: add missing module docstrings
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42748
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: 42748
diff changeset
9
42437
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
10 pub mod files;
42956
3fe40dd6355d rust-hgpath: add HgPath and HgPathBuf structs to encapsulate handling of paths
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42863
diff changeset
11 pub mod hg_path;
42437
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
12
44079
191a461d6be6 rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42956
diff changeset
13 /// 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: 42956
diff changeset
14 ///
191a461d6be6 rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42956
diff changeset
15 /// # Examples
191a461d6be6 rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42956
diff changeset
16 ///
191a461d6be6 rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42956
diff changeset
17 /// ```
191a461d6be6 rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42956
diff changeset
18 /// 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: 42956
diff changeset
19 ///
191a461d6be6 rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42956
diff changeset
20 /// 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: 42956
diff changeset
21 /// 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: 42956
diff changeset
22 /// 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: 42956
diff changeset
23 /// ```
191a461d6be6 rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42956
diff changeset
24 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: 42956
diff changeset
25 where
191a461d6be6 rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42956
diff changeset
26 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: 42956
diff changeset
27 {
191a461d6be6 rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42956
diff changeset
28 slice
191a461d6be6 rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42956
diff changeset
29 .windows(needle.len())
191a461d6be6 rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42956
diff changeset
30 .position(|window| window == needle)
191a461d6be6 rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42956
diff changeset
31 }
191a461d6be6 rust-utils: add util to find a slice in another slice
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42956
diff changeset
32
42610
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42609
diff changeset
33 /// 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
34 ///
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42609
diff changeset
35 /// # Examples
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42609
diff changeset
36 ///
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42609
diff changeset
37 /// ```
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42609
diff changeset
38 /// 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
39 /// 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
40 /// 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
41 /// assert_eq!(
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42609
diff changeset
42 /// line,
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42609
diff changeset
43 /// b"I love writing tests!".to_vec()
42841
ce6797ef6eab rust: apply more formatting fixes
Yuya Nishihara <yuya@tcha.org>
parents: 42799
diff changeset
44 /// );
42610
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42609
diff changeset
45 /// ```
42437
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
46 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
47 where
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
48 T: Clone + PartialEq,
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
49 {
42611
2f760da140ee rust-utils: remove buggy assertion
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42610
diff changeset
50 if buf.len() < from.len() || from.len() != to.len() {
42437
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
51 return;
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 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
54 if buf[i..].starts_with(from) {
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
55 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
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 }
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
59
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
60 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
61 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
62 fn trim_start(&self) -> &Self;
42437
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
63 fn trim(&self) -> &Self;
42863
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42841
diff changeset
64 fn drop_prefix(&self, needle: &Self) -> Option<&Self>;
42437
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
65 }
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
66
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
67 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
68 !(*c as char).is_whitespace()
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
69 }
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
70
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
71 impl SliceExt for [u8] {
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
72 fn trim_end(&self) -> &[u8] {
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
73 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
74 &self[..last + 1]
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
75 } else {
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 }
42610
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42609
diff changeset
79 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
80 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
81 &self[first..]
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42609
diff changeset
82 } else {
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 }
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42609
diff changeset
85 }
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42609
diff changeset
86
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 /// 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
89 /// assert_eq!(
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42609
diff changeset
90 /// 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
91 /// b"to trim"
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 /// assert_eq!(
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42609
diff changeset
94 /// 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
95 /// b"to trim"
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42609
diff changeset
96 /// );
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42609
diff changeset
97 /// assert_eq!(
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".trim(),
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42609
diff changeset
99 /// b"to trim"
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42609
diff changeset
100 /// );
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42609
diff changeset
101 /// ```
5672bb73f61e rust-utils: add docstrings and doctests for utils.rs
Rapha?l Gom?s <rgomes@octobus.net>
parents: 42609
diff changeset
102 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
103 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
104 }
42863
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42841
diff changeset
105
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42841
diff changeset
106 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: 42841
diff changeset
107 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: 42841
diff changeset
108 Some(&self[needle.len()..])
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42841
diff changeset
109 } else {
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42841
diff changeset
110 None
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42841
diff changeset
111 }
62eabdf91f85 rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents: 42841
diff changeset
112 }
42437
9609430d3625 rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
113 }