Mercurial > hg
annotate rust/hg-core/src/filepatterns.rs @ 44303:d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Within the next few patches we will be using this new API.
Differential Revision: https://phab.mercurial-scm.org/D7908
author | Raphaël Gomès <rgomes@octobus.net> |
---|---|
date | Thu, 16 Jan 2020 10:28:40 +0100 |
parents | 5ac243a92e37 |
children | 2fe89bec8011 |
rev | line source |
---|---|
42751
4b3b27d567d5
rust-docstrings: add missing module docstrings
Raphaël Gomès <rgomes@octobus.net>
parents:
42636
diff
changeset
|
1 // filepatterns.rs |
4b3b27d567d5
rust-docstrings: add missing module docstrings
Raphaël Gomès <rgomes@octobus.net>
parents:
42636
diff
changeset
|
2 // |
4b3b27d567d5
rust-docstrings: add missing module docstrings
Raphaël Gomès <rgomes@octobus.net>
parents:
42636
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:
42636
diff
changeset
|
4 // |
4b3b27d567d5
rust-docstrings: add missing module docstrings
Raphaël Gomès <rgomes@octobus.net>
parents:
42636
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:
42636
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:
42636
diff
changeset
|
7 |
4b3b27d567d5
rust-docstrings: add missing module docstrings
Raphaël Gomès <rgomes@octobus.net>
parents:
42636
diff
changeset
|
8 //! Handling of Mercurial-specific patterns. |
4b3b27d567d5
rust-docstrings: add missing module docstrings
Raphaël Gomès <rgomes@octobus.net>
parents:
42636
diff
changeset
|
9 |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
10 use crate::{utils::SliceExt, FastHashMap, PatternError}; |
42609
326fdce22fb2
rust: switch hg-core and hg-cpython to rust 2018 edition
Raphaël Gomès <rgomes@octobus.net>
parents:
42483
diff
changeset
|
11 use lazy_static::lazy_static; |
42636
12addcc7956c
rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents:
42635
diff
changeset
|
12 use regex::bytes::{NoExpand, Regex}; |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
13 use std::fs::File; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
14 use std::io::Read; |
42957
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents:
42865
diff
changeset
|
15 use std::path::{Path, PathBuf}; |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
16 use std::vec::Vec; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
17 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
18 lazy_static! { |
42483
a4a468b00d44
rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents:
42438
diff
changeset
|
19 static ref RE_ESCAPE: Vec<Vec<u8>> = { |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
20 let mut v: Vec<Vec<u8>> = (0..=255).map(|byte| vec![byte]).collect(); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
21 let to_escape = b"()[]{}?*+-|^$\\.&~# \t\n\r\x0b\x0c"; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
22 for byte in to_escape { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
23 v[*byte as usize].insert(0, b'\\'); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
24 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
25 v |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
26 }; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
27 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
28 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
29 /// These are matched in order |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
30 const GLOB_REPLACEMENTS: &[(&[u8], &[u8])] = |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
31 &[(b"*/", b"(?:.*/)?"), (b"*", b".*"), (b"", b"[^/]*")]; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
32 |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
33 /// Appended to the regexp of globs |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
34 const GLOB_SUFFIX: &[u8; 7] = b"(?:/|$)"; |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
35 |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
36 #[derive(Debug, Copy, Clone, PartialEq, Eq)] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
37 pub enum PatternSyntax { |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
38 /// A regular expression |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
39 Regexp, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
40 /// Glob that matches at the front of the path |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
41 RootGlob, |
42841
ce6797ef6eab
rust: apply more formatting fixes
Yuya Nishihara <yuya@tcha.org>
parents:
42751
diff
changeset
|
42 /// Glob that matches at any suffix of the path (still anchored at |
ce6797ef6eab
rust: apply more formatting fixes
Yuya Nishihara <yuya@tcha.org>
parents:
42751
diff
changeset
|
43 /// slashes) |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
44 Glob, |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
45 /// a path relative to repository root, which is matched recursively |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
46 Path, |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
47 /// A path relative to cwd |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
48 RelPath, |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
49 /// an unrooted glob (*.rs matches Rust files in all dirs) |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
50 RelGlob, |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
51 /// A regexp that needn't match the start of a name |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
52 RelRegexp, |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
53 /// A path relative to repository root, which is matched non-recursively |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
54 /// (will not match subdirectories) |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
55 RootFiles, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
56 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
57 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
58 /// Transforms a glob pattern into a regex |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
59 fn glob_to_re(pat: &[u8]) -> Vec<u8> { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
60 let mut input = pat; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
61 let mut res: Vec<u8> = vec![]; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
62 let mut group_depth = 0; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
63 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
64 while let Some((c, rest)) = input.split_first() { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
65 input = rest; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
66 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
67 match c { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
68 b'*' => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
69 for (source, repl) in GLOB_REPLACEMENTS { |
42863
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42841
diff
changeset
|
70 if let Some(rest) = input.drop_prefix(source) { |
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42841
diff
changeset
|
71 input = rest; |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
72 res.extend(*repl); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
73 break; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
74 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
75 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
76 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
77 b'?' => res.extend(b"."), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
78 b'[' => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
79 match input.iter().skip(1).position(|b| *b == b']') { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
80 None => res.extend(b"\\["), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
81 Some(end) => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
82 // Account for the one we skipped |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
83 let end = end + 1; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
84 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
85 res.extend(b"["); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
86 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
87 for (i, b) in input[..end].iter().enumerate() { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
88 if *b == b'!' && i == 0 { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
89 res.extend(b"^") |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
90 } else if *b == b'^' && i == 0 { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
91 res.extend(b"\\^") |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
92 } else if *b == b'\\' { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
93 res.extend(b"\\\\") |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
94 } else { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
95 res.push(*b) |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
96 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
97 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
98 res.extend(b"]"); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
99 input = &input[end + 1..]; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
100 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
101 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
102 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
103 b'{' => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
104 group_depth += 1; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
105 res.extend(b"(?:") |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
106 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
107 b'}' if group_depth > 0 => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
108 group_depth -= 1; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
109 res.extend(b")"); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
110 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
111 b',' if group_depth > 0 => res.extend(b"|"), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
112 b'\\' => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
113 let c = { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
114 if let Some((c, rest)) = input.split_first() { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
115 input = rest; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
116 c |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
117 } else { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
118 c |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
119 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
120 }; |
42483
a4a468b00d44
rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents:
42438
diff
changeset
|
121 res.extend(&RE_ESCAPE[*c as usize]) |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
122 } |
42483
a4a468b00d44
rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents:
42438
diff
changeset
|
123 _ => res.extend(&RE_ESCAPE[*c as usize]), |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
124 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
125 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
126 res |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
127 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
128 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
129 fn escape_pattern(pattern: &[u8]) -> Vec<u8> { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
130 pattern |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
131 .iter() |
42483
a4a468b00d44
rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents:
42438
diff
changeset
|
132 .flat_map(|c| RE_ESCAPE[*c as usize].clone()) |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
133 .collect() |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
134 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
135 |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
136 pub fn parse_pattern_syntax( |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
137 kind: &[u8], |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
138 ) -> Result<PatternSyntax, PatternError> { |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
139 match kind { |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
140 b"re:" => Ok(PatternSyntax::Regexp), |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
141 b"path:" => Ok(PatternSyntax::Path), |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
142 b"relpath:" => Ok(PatternSyntax::RelPath), |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
143 b"rootfilesin:" => Ok(PatternSyntax::RootFiles), |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
144 b"relglob:" => Ok(PatternSyntax::RelGlob), |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
145 b"relre:" => Ok(PatternSyntax::RelRegexp), |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
146 b"glob:" => Ok(PatternSyntax::Glob), |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
147 b"rootglob:" => Ok(PatternSyntax::RootGlob), |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
148 _ => Err(PatternError::UnsupportedSyntax( |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
149 String::from_utf8_lossy(kind).to_string(), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
150 )), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
151 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
152 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
153 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
154 /// Builds the regex that corresponds to the given pattern. |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
155 /// If within a `syntax: regexp` context, returns the pattern, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
156 /// otherwise, returns the corresponding regex. |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
157 fn _build_single_regex(entry: &IgnorePattern) -> Vec<u8> { |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
158 let IgnorePattern { |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
159 syntax, pattern, .. |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
160 } = entry; |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
161 if pattern.is_empty() { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
162 return vec![]; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
163 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
164 match syntax { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
165 PatternSyntax::Regexp => pattern.to_owned(), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
166 PatternSyntax::RelRegexp => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
167 if pattern[0] == b'^' { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
168 return pattern.to_owned(); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
169 } |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
170 [&b".*"[..], pattern].concat() |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
171 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
172 PatternSyntax::Path | PatternSyntax::RelPath => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
173 if pattern == b"." { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
174 return vec![]; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
175 } |
42865
69195b6f8f97
rustfilepatterns: shorter code for concatenating slices
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42864
diff
changeset
|
176 [escape_pattern(pattern).as_slice(), b"(?:/|$)"].concat() |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
177 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
178 PatternSyntax::RootFiles => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
179 let mut res = if pattern == b"." { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
180 vec![] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
181 } else { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
182 // Pattern is a directory name. |
42865
69195b6f8f97
rustfilepatterns: shorter code for concatenating slices
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42864
diff
changeset
|
183 [escape_pattern(pattern).as_slice(), b"/"].concat() |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
184 }; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
185 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
186 // Anything after the pattern must be a non-directory. |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
187 res.extend(b"[^/]+$"); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
188 res |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
189 } |
42864
72890d8f9860
match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42863
diff
changeset
|
190 PatternSyntax::RelGlob => { |
72890d8f9860
match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42863
diff
changeset
|
191 let glob_re = glob_to_re(pattern); |
72890d8f9860
match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42863
diff
changeset
|
192 if let Some(rest) = glob_re.drop_prefix(b"[^/]*") { |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
193 [b".*", rest, GLOB_SUFFIX].concat() |
42864
72890d8f9860
match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42863
diff
changeset
|
194 } else { |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
195 [b"(?:|.*/)", glob_re.as_slice(), GLOB_SUFFIX].concat() |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
196 } |
42864
72890d8f9860
match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42863
diff
changeset
|
197 } |
72890d8f9860
match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42863
diff
changeset
|
198 PatternSyntax::Glob | PatternSyntax::RootGlob => { |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
199 [glob_to_re(pattern).as_slice(), GLOB_SUFFIX].concat() |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
200 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
201 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
202 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
203 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
204 const GLOB_SPECIAL_CHARACTERS: [u8; 7] = |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
205 [b'*', b'?', b'[', b']', b'{', b'}', b'\\']; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
206 |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
207 /// TODO support other platforms |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
208 #[cfg(unix)] |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
209 pub fn normalize_path_bytes(bytes: &[u8]) -> Vec<u8> { |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
210 if bytes.is_empty() { |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
211 return b".".to_vec(); |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
212 } |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
213 let sep = b'/'; |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
214 |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
215 let mut initial_slashes = bytes.iter().take_while(|b| **b == sep).count(); |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
216 if initial_slashes > 2 { |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
217 // POSIX allows one or two initial slashes, but treats three or more |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
218 // as single slash. |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
219 initial_slashes = 1; |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
220 } |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
221 let components = bytes |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
222 .split(|b| *b == sep) |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
223 .filter(|c| !(c.is_empty() || c == b".")) |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
224 .fold(vec![], |mut acc, component| { |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
225 if component != b".." |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
226 || (initial_slashes == 0 && acc.is_empty()) |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
227 || (!acc.is_empty() && acc[acc.len() - 1] == b"..") |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
228 { |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
229 acc.push(component) |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
230 } else if !acc.is_empty() { |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
231 acc.pop(); |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
232 } |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
233 acc |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
234 }); |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
235 let mut new_bytes = components.join(&sep); |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
236 |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
237 if initial_slashes > 0 { |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
238 let mut buf: Vec<_> = (0..initial_slashes).map(|_| sep).collect(); |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
239 buf.extend(new_bytes); |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
240 new_bytes = buf; |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
241 } |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
242 if new_bytes.is_empty() { |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
243 b".".to_vec() |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
244 } else { |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
245 new_bytes |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
246 } |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
247 } |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
248 |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
249 /// Wrapper function to `_build_single_regex` that short-circuits 'exact' globs |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
250 /// that don't need to be transformed into a regex. |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
251 pub fn build_single_regex( |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
252 entry: &IgnorePattern, |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
253 ) -> Result<Vec<u8>, PatternError> { |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
254 let IgnorePattern { |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
255 pattern, syntax, .. |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
256 } = entry; |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
257 let pattern = match syntax { |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
258 PatternSyntax::RootGlob |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
259 | PatternSyntax::Path |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
260 | PatternSyntax::RelGlob |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
261 | PatternSyntax::RootFiles => normalize_path_bytes(&pattern), |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
262 _ => pattern.to_owned(), |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
263 }; |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
264 if *syntax == PatternSyntax::RootGlob |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
265 && !pattern.iter().any(|b| GLOB_SPECIAL_CHARACTERS.contains(b)) |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
266 { |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
267 let mut escaped = escape_pattern(&pattern); |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
268 escaped.extend(GLOB_SUFFIX); |
42438
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
269 Ok(escaped) |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
270 } else { |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
271 let mut entry = entry.clone(); |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
272 entry.pattern = pattern; |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
273 Ok(_build_single_regex(&entry)) |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
274 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
275 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
276 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
277 lazy_static! { |
43826
5ac243a92e37
rust-performance: introduce FastHashMap type alias for HashMap
Raphaël Gomès <rgomes@octobus.net>
parents:
42957
diff
changeset
|
278 static ref SYNTAXES: FastHashMap<&'static [u8], &'static [u8]> = { |
5ac243a92e37
rust-performance: introduce FastHashMap type alias for HashMap
Raphaël Gomès <rgomes@octobus.net>
parents:
42957
diff
changeset
|
279 let mut m = FastHashMap::default(); |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
280 |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
281 m.insert(b"re".as_ref(), b"relre:".as_ref()); |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
282 m.insert(b"regexp".as_ref(), b"relre:".as_ref()); |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
283 m.insert(b"glob".as_ref(), b"relglob:".as_ref()); |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
284 m.insert(b"rootglob".as_ref(), b"rootglob:".as_ref()); |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
285 m.insert(b"include".as_ref(), b"include:".as_ref()); |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
286 m.insert(b"subinclude".as_ref(), b"subinclude:".as_ref()); |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
287 m |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
288 }; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
289 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
290 |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
291 #[derive(Debug)] |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
292 pub enum PatternFileWarning { |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
293 /// (file path, syntax bytes) |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
294 InvalidSyntax(PathBuf, Vec<u8>), |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
295 /// File path |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
296 NoSuchFile(PathBuf), |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
297 } |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
298 |
42957
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents:
42865
diff
changeset
|
299 pub fn parse_pattern_file_contents<P: AsRef<Path>>( |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
300 lines: &[u8], |
42957
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents:
42865
diff
changeset
|
301 file_path: P, |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
302 warn: bool, |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
303 ) -> Result<(Vec<IgnorePattern>, Vec<PatternFileWarning>), PatternError> { |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
304 let comment_regex = Regex::new(r"((?:^|[^\\])(?:\\\\)*)#.*").unwrap(); |
42636
12addcc7956c
rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents:
42635
diff
changeset
|
305 let comment_escape_regex = Regex::new(r"\\#").unwrap(); |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
306 let mut inputs: Vec<IgnorePattern> = vec![]; |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
307 let mut warnings: Vec<PatternFileWarning> = vec![]; |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
308 |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
309 let mut current_syntax = b"relre:".as_ref(); |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
310 |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
311 for (line_number, mut line) in lines.split(|c| *c == b'\n').enumerate() { |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
312 let line_number = line_number + 1; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
313 |
42636
12addcc7956c
rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents:
42635
diff
changeset
|
314 let line_buf; |
42635
30f8e786868c
rust-filepatterns: use literal b'#' instead of cast
Yuya Nishihara <yuya@tcha.org>
parents:
42634
diff
changeset
|
315 if line.contains(&b'#') { |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
316 if let Some(cap) = comment_regex.captures(line) { |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
317 line = &line[..cap.get(1).unwrap().end()] |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
318 } |
42636
12addcc7956c
rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents:
42635
diff
changeset
|
319 line_buf = comment_escape_regex.replace_all(line, NoExpand(b"#")); |
12addcc7956c
rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents:
42635
diff
changeset
|
320 line = &line_buf; |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
321 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
322 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
323 let mut line = line.trim_end(); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
324 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
325 if line.is_empty() { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
326 continue; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
327 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
328 |
42863
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42841
diff
changeset
|
329 if let Some(syntax) = line.drop_prefix(b"syntax:") { |
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42841
diff
changeset
|
330 let syntax = syntax.trim(); |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
331 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
332 if let Some(rel_syntax) = SYNTAXES.get(syntax) { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
333 current_syntax = rel_syntax; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
334 } else if warn { |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
335 warnings.push(PatternFileWarning::InvalidSyntax( |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
336 file_path.as_ref().to_owned(), |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
337 syntax.to_owned(), |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
338 )); |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
339 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
340 continue; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
341 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
342 |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
343 let mut line_syntax: &[u8] = ¤t_syntax; |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
344 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
345 for (s, rels) in SYNTAXES.iter() { |
42863
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42841
diff
changeset
|
346 if let Some(rest) = line.drop_prefix(rels) { |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
347 line_syntax = rels; |
42863
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42841
diff
changeset
|
348 line = rest; |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
349 break; |
42863
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42841
diff
changeset
|
350 } |
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42841
diff
changeset
|
351 if let Some(rest) = line.drop_prefix(&[s, &b":"[..]].concat()) { |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
352 line_syntax = rels; |
42863
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42841
diff
changeset
|
353 line = rest; |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
354 break; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
355 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
356 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
357 |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
358 inputs.push(IgnorePattern::new( |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
359 parse_pattern_syntax(&line_syntax).map_err(|e| match e { |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
360 PatternError::UnsupportedSyntax(syntax) => { |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
361 PatternError::UnsupportedSyntaxInFile( |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
362 syntax, |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
363 file_path.as_ref().to_string_lossy().into(), |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
364 line_number, |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
365 ) |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
366 } |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
367 _ => e, |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
368 })?, |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
369 &line, |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
370 &file_path, |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
371 )); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
372 } |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
373 Ok((inputs, warnings)) |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
374 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
375 |
42957
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents:
42865
diff
changeset
|
376 pub fn read_pattern_file<P: AsRef<Path>>( |
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents:
42865
diff
changeset
|
377 file_path: P, |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
378 warn: bool, |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
379 ) -> Result<(Vec<IgnorePattern>, Vec<PatternFileWarning>), PatternError> { |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
380 let mut f = match File::open(file_path.as_ref()) { |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
381 Ok(f) => Ok(f), |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
382 Err(e) => match e.kind() { |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
383 std::io::ErrorKind::NotFound => { |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
384 return Ok(( |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
385 vec![], |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
386 vec![PatternFileWarning::NoSuchFile( |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
387 file_path.as_ref().to_owned(), |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
388 )], |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
389 )) |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
390 } |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
391 _ => Err(e), |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
392 }, |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
393 }?; |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
394 let mut contents = Vec::new(); |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
395 |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
396 f.read_to_end(&mut contents)?; |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
397 |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
398 Ok(parse_pattern_file_contents(&contents, file_path, warn)?) |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
399 } |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
400 |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
401 /// Represents an entry in an "ignore" file. |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
402 #[derive(Debug, Eq, PartialEq, Clone)] |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
403 pub struct IgnorePattern { |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
404 pub syntax: PatternSyntax, |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
405 pub pattern: Vec<u8>, |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
406 pub source: PathBuf, |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
407 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
408 |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
409 impl IgnorePattern { |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
410 pub fn new( |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
411 syntax: PatternSyntax, |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
412 pattern: &[u8], |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
413 source: impl AsRef<Path>, |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
414 ) -> Self { |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
415 Self { |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
416 syntax, |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
417 pattern: pattern.to_owned(), |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
418 source: source.as_ref().to_owned(), |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
419 } |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
420 } |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
421 } |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
422 |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
423 pub type PatternResult<T> = Result<T, PatternError>; |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
424 |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
425 #[cfg(test)] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
426 mod tests { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
427 use super::*; |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
428 use pretty_assertions::assert_eq; |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
429 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
430 #[test] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
431 fn escape_pattern_test() { |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
432 let untouched = |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
433 br#"!"%',/0123456789:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ_`abcdefghijklmnopqrstuvwxyz"#; |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
434 assert_eq!(escape_pattern(untouched), untouched.to_vec()); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
435 // All escape codes |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
436 assert_eq!( |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
437 escape_pattern(br#"()[]{}?*+-|^$\\.&~# \t\n\r\v\f"#), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
438 br#"\(\)\[\]\{\}\?\*\+\-\|\^\$\\\\\.\&\~\#\ \\t\\n\\r\\v\\f"# |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
439 .to_vec() |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
440 ); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
441 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
442 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
443 #[test] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
444 fn glob_test() { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
445 assert_eq!(glob_to_re(br#"?"#), br#"."#); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
446 assert_eq!(glob_to_re(br#"*"#), br#"[^/]*"#); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
447 assert_eq!(glob_to_re(br#"**"#), br#".*"#); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
448 assert_eq!(glob_to_re(br#"**/a"#), br#"(?:.*/)?a"#); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
449 assert_eq!(glob_to_re(br#"a/**/b"#), br#"a/(?:.*/)?b"#); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
450 assert_eq!(glob_to_re(br#"[a*?!^][^b][!c]"#), br#"[a*?!^][\^b][^c]"#); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
451 assert_eq!(glob_to_re(br#"{a,b}"#), br#"(?:a|b)"#); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
452 assert_eq!(glob_to_re(br#".\*\?"#), br#"\.\*\?"#); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
453 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
454 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
455 #[test] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
456 fn test_parse_pattern_file_contents() { |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
457 let lines = b"syntax: glob\n*.elc"; |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
458 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
459 assert_eq!( |
42957
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents:
42865
diff
changeset
|
460 parse_pattern_file_contents(lines, Path::new("file_path"), false) |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
461 .unwrap() |
42957
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents:
42865
diff
changeset
|
462 .0, |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
463 vec![IgnorePattern::new( |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
464 PatternSyntax::RelGlob, |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
465 b"*.elc", |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
466 Path::new("file_path") |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
467 )], |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
468 ); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
469 |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
470 let lines = b"syntax: include\nsyntax: glob"; |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
471 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
472 assert_eq!( |
42957
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents:
42865
diff
changeset
|
473 parse_pattern_file_contents(lines, Path::new("file_path"), false) |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
474 .unwrap() |
42957
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents:
42865
diff
changeset
|
475 .0, |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
476 vec![] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
477 ); |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
478 let lines = b"glob:**.o"; |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
479 assert_eq!( |
42957
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents:
42865
diff
changeset
|
480 parse_pattern_file_contents(lines, Path::new("file_path"), false) |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
481 .unwrap() |
42957
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Raphaël Gomès <rgomes@octobus.net>
parents:
42865
diff
changeset
|
482 .0, |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
483 vec![IgnorePattern::new( |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
484 PatternSyntax::RelGlob, |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
485 b"**.o", |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
486 Path::new("file_path") |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
487 )] |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
488 ); |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
489 } |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
490 |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
491 #[test] |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
492 fn test_build_single_regex() { |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
493 assert_eq!( |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
494 build_single_regex(&IgnorePattern::new( |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
495 PatternSyntax::RelGlob, |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
496 b"rust/target/", |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
497 Path::new("") |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
498 )) |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
499 .unwrap(), |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
500 br"(?:|.*/)rust/target(?:/|$)".to_vec(), |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
501 ); |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
502 } |
42438
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
503 |
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
504 #[test] |
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
505 fn test_build_single_regex_shortcut() { |
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
506 assert_eq!( |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
507 build_single_regex(&IgnorePattern::new( |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
508 PatternSyntax::RootGlob, |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
509 b"", |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
510 Path::new("") |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
511 )) |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
512 .unwrap(), |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
513 br"\.(?:/|$)".to_vec(), |
42438
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
514 ); |
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
515 assert_eq!( |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
516 build_single_regex(&IgnorePattern::new( |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
517 PatternSyntax::RootGlob, |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
518 b"whatever", |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
519 Path::new("") |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
520 )) |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
521 .unwrap(), |
42438
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
522 br"whatever(?:/|$)".to_vec(), |
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
523 ); |
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
524 assert_eq!( |
44303
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
525 build_single_regex(&IgnorePattern::new( |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
526 PatternSyntax::RootGlob, |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
527 b"*.o", |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
528 Path::new("") |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
529 )) |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
530 .unwrap(), |
d42eea9a0494
rust-filepatterns: improve API and robustness for pattern files parsing
Raphaël Gomès <rgomes@octobus.net>
parents:
43826
diff
changeset
|
531 br"[^/]*\.o(?:/|$)".to_vec(), |
42438
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
532 ); |
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
533 } |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
534 } |