Mercurial > hg
annotate rust/hg-core/src/filepatterns.rs @ 42609:326fdce22fb2
rust: switch hg-core and hg-cpython to rust 2018 edition
Many interesting changes have happened in Rust since the Oxidation Plan was
introduced, like the 2018 edition and procedural macros:
- Opting in to the 2018 edition is a clear benefit in terms of future
proofing, new (nice to have) syntactical sugar notwithstanding. It
also has a new non-lexical, non-AST based borrow checker that has
fewer bugs(!) and allows us to write correct code that in some cases
would have been rejected by the old one.
- Procedural macros allow us to use the PyO3 crate which maintainers have
expressed the clear goal of compiling on stable, which would help in
code maintainability compared to rust-cpython.
In this patch are the following changes:
- Removing most `extern crate` uses
- Updating `use` clauses (`crate` keyword, nested `use`)
- Removing `mod.rs` in favor of an aptly named module file
Like discussed in the mailing list (
https://www.mercurial-scm.org/pipermail/mercurial-devel/2019-July/132316.html
), until Rust integration in Mercurial is considered to be out of the
experimental phase, the maximum version of Rust allowed is whatever the latest
version Debian packages.
Differential Revision: https://phab.mercurial-scm.org/D6597
author | Raphaël Gomès <rgomes@octobus.net> |
---|---|
date | Tue, 02 Jul 2019 17:15:03 +0200 |
parents | a4a468b00d44 |
children | 0247601869ba |
rev | line source |
---|---|
42609
326fdce22fb2
rust: switch hg-core and hg-cpython to rust 2018 edition
Raphaël Gomès <rgomes@octobus.net>
parents:
42483
diff
changeset
|
1 use crate::{ |
326fdce22fb2
rust: switch hg-core and hg-cpython to rust 2018 edition
Raphaël Gomès <rgomes@octobus.net>
parents:
42483
diff
changeset
|
2 utils::{files::get_path_from_bytes, replace_slice, SliceExt}, |
326fdce22fb2
rust: switch hg-core and hg-cpython to rust 2018 edition
Raphaël Gomès <rgomes@octobus.net>
parents:
42483
diff
changeset
|
3 LineNumber, PatternError, PatternFileError, |
326fdce22fb2
rust: switch hg-core and hg-cpython to rust 2018 edition
Raphaël Gomès <rgomes@octobus.net>
parents:
42483
diff
changeset
|
4 }; |
326fdce22fb2
rust: switch hg-core and hg-cpython to rust 2018 edition
Raphaël Gomès <rgomes@octobus.net>
parents:
42483
diff
changeset
|
5 use lazy_static::lazy_static; |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
6 use regex::bytes::Regex; |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
7 use std::collections::HashMap; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
8 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
|
9 use std::io::Read; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
10 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
|
11 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
12 lazy_static! { |
42483
a4a468b00d44
rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents:
42438
diff
changeset
|
13 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
|
14 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
|
15 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
|
16 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
|
17 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
|
18 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
19 v |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
20 }; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
21 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
22 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
23 /// 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
|
24 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
|
25 &[(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
|
26 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
27 #[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
|
28 pub enum PatternSyntax { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
29 Regexp, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
30 /// 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
|
31 RootGlob, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
32 /// Glob that matches at any suffix of the path (still anchored at slashes) |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
33 Glob, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
34 Path, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
35 RelPath, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
36 RelGlob, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
37 RelRegexp, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
38 RootFiles, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
39 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
40 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
41 /// 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
|
42 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
|
43 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
|
44 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
|
45 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
|
46 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
47 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
|
48 input = rest; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
49 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
50 match c { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
51 b'*' => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
52 for (source, repl) in GLOB_REPLACEMENTS { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
53 if input.starts_with(source) { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
54 input = &input[source.len()..]; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
55 res.extend(*repl); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
56 break; |
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 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
59 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
60 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
|
61 b'[' => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
62 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
|
63 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
|
64 Some(end) => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
65 // 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
|
66 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
|
67 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
68 res.extend(b"["); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
69 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
70 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
|
71 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
|
72 res.extend(b"^") |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
73 } 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
|
74 res.extend(b"\\^") |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
75 } 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
|
76 res.extend(b"\\\\") |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
77 } else { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
78 res.push(*b) |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
79 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
80 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
81 res.extend(b"]"); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
82 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
|
83 } |
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 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
86 b'{' => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
87 group_depth += 1; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
88 res.extend(b"(?:") |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
89 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
90 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
|
91 group_depth -= 1; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
92 res.extend(b")"); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
93 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
94 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
|
95 b'\\' => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
96 let c = { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
97 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
|
98 input = rest; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
99 c |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
100 } else { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
101 c |
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 }; |
42483
a4a468b00d44
rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents:
42438
diff
changeset
|
104 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
|
105 } |
42483
a4a468b00d44
rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents:
42438
diff
changeset
|
106 _ => 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
|
107 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
108 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
109 res |
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 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
112 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
|
113 pattern |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
114 .iter() |
42483
a4a468b00d44
rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents:
42438
diff
changeset
|
115 .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
|
116 .collect() |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
117 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
118 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
119 fn parse_pattern_syntax(kind: &[u8]) -> Result<PatternSyntax, PatternError> { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
120 match kind { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
121 b"re" => Ok(PatternSyntax::Regexp), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
122 b"path" => Ok(PatternSyntax::Path), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
123 b"relpath" => Ok(PatternSyntax::RelPath), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
124 b"rootfilesin" => Ok(PatternSyntax::RootFiles), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
125 b"relglob" => Ok(PatternSyntax::RelGlob), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
126 b"relre" => Ok(PatternSyntax::RelRegexp), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
127 b"glob" => Ok(PatternSyntax::Glob), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
128 b"rootglob" => Ok(PatternSyntax::RootGlob), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
129 _ => Err(PatternError::UnsupportedSyntax( |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
130 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
|
131 )), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
132 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
133 } |
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 /// 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
|
136 /// 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
|
137 /// otherwise, returns the corresponding regex. |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
138 fn _build_single_regex( |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
139 syntax: PatternSyntax, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
140 pattern: &[u8], |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
141 globsuffix: &[u8], |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
142 ) -> Vec<u8> { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
143 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
|
144 return vec![]; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
145 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
146 match syntax { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
147 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
|
148 PatternSyntax::RelRegexp => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
149 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
|
150 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
|
151 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
152 let mut res = b".*".to_vec(); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
153 res.extend(pattern); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
154 res |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
155 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
156 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
|
157 if pattern == b"." { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
158 return vec![]; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
159 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
160 let mut pattern = escape_pattern(pattern); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
161 pattern.extend(b"(?:/|$)"); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
162 pattern |
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 PatternSyntax::RootFiles => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
165 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
|
166 vec![] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
167 } else { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
168 // Pattern is a directory name. |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
169 let mut as_vec: Vec<u8> = escape_pattern(pattern); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
170 as_vec.push(b'/'); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
171 as_vec |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
172 }; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
173 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
174 // 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
|
175 res.extend(b"[^/]+$"); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
176 res |
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::Glob |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
179 | PatternSyntax::RelGlob |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
180 | PatternSyntax::RootGlob => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
181 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
|
182 if syntax == PatternSyntax::RelGlob { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
183 res.extend(b"(?:|.*/)"); |
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 res.extend(glob_to_re(pattern)); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
187 res.extend(globsuffix.iter()); |
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 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
190 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
191 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
192 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
193 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
|
194 [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
|
195 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
196 /// 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
|
197 /// 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
|
198 pub fn build_single_regex( |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
199 kind: &[u8], |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
200 pat: &[u8], |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
201 globsuffix: &[u8], |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
202 ) -> Result<Vec<u8>, PatternError> { |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
203 let enum_kind = parse_pattern_syntax(kind)?; |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
204 if enum_kind == PatternSyntax::RootGlob |
42438
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
205 && !pat.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
|
206 { |
42438
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
207 let mut escaped = escape_pattern(pat); |
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
208 escaped.extend(b"(?:/|$)"); |
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
209 Ok(escaped) |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
210 } else { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
211 Ok(_build_single_regex(enum_kind, pat, globsuffix)) |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
212 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
213 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
214 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
215 lazy_static! { |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
216 static ref SYNTAXES: HashMap<&'static [u8], &'static [u8]> = { |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
217 let mut m = HashMap::new(); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
218 |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
219 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
|
220 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
|
221 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
|
222 m.insert(b"rootglob".as_ref(), b"rootglob:".as_ref()); |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
223 m.insert(b"include".as_ref(), b"include".as_ref()); |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
224 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
|
225 m |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
226 }; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
227 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
228 |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
229 pub type PatternTuple = (Vec<u8>, LineNumber, Vec<u8>); |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
230 type WarningTuple = (String, String); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
231 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
232 pub fn parse_pattern_file_contents( |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
233 lines: &[u8], |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
234 file_path: &[u8], |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
235 warn: bool, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
236 ) -> (Vec<PatternTuple>, Vec<WarningTuple>) { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
237 let comment_regex = Regex::new(r"((?:^|[^\\])(?:\\\\)*)#.*").unwrap(); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
238 let mut inputs: Vec<PatternTuple> = vec![]; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
239 let mut warnings: Vec<WarningTuple> = vec![]; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
240 |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
241 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
|
242 |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
243 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
|
244 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
|
245 |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
246 if line.contains(&('#' as u8)) { |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
247 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
|
248 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
|
249 } |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
250 let mut line = line.to_owned(); |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
251 replace_slice(&mut line, br"\#", b"#"); |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
252 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
253 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
254 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
|
255 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
256 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
|
257 continue; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
258 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
259 |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
260 if line.starts_with(b"syntax:") { |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
261 let syntax = line[b"syntax:".len()..].trim(); |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
262 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
263 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
|
264 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
|
265 } else if warn { |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
266 warnings.push(( |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
267 String::from_utf8_lossy(file_path).to_string(), |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
268 String::from_utf8_lossy(syntax).to_string(), |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
269 )); |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
270 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
271 continue; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
272 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
273 |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
274 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
|
275 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
276 for (s, rels) in SYNTAXES.iter() { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
277 if line.starts_with(rels) { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
278 line_syntax = rels; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
279 line = &line[rels.len()..]; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
280 break; |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
281 } else if line.starts_with(&[s, b":".as_ref()].concat()) { |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
282 line_syntax = rels; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
283 line = &line[s.len() + 1..]; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
284 break; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
285 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
286 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
287 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
288 inputs.push(( |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
289 [line_syntax, line].concat(), |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
290 line_number, |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
291 line.to_owned(), |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
292 )); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
293 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
294 (inputs, warnings) |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
295 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
296 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
297 pub fn read_pattern_file( |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
298 file_path: &[u8], |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
299 warn: bool, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
300 ) -> Result<(Vec<PatternTuple>, Vec<WarningTuple>), PatternFileError> { |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
301 let mut f = File::open(get_path_from_bytes(file_path))?; |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
302 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
|
303 |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
304 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
|
305 |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
306 Ok(parse_pattern_file_contents(&contents, file_path, warn)) |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
307 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
308 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
309 #[cfg(test)] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
310 mod tests { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
311 use super::*; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
312 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
313 #[test] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
314 fn escape_pattern_test() { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
315 let untouched = br#"!"%',/0123456789:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ_`abcdefghijklmnopqrstuvwxyz"#; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
316 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
|
317 // All escape codes |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
318 assert_eq!( |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
319 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
|
320 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
|
321 .to_vec() |
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 } |
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 #[test] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
326 fn glob_test() { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
327 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
|
328 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
|
329 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
|
330 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
|
331 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
|
332 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
|
333 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
|
334 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
|
335 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
336 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
337 #[test] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
338 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
|
339 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
|
340 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
341 assert_eq!( |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
342 vec![(b"relglob:*.elc".to_vec(), 2, b"*.elc".to_vec())], |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
343 parse_pattern_file_contents(lines, b"file_path", false).0, |
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 |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
346 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
|
347 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
348 assert_eq!( |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
349 parse_pattern_file_contents(lines, b"file_path", false).0, |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
350 vec![] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
351 ); |
42437
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
352 let lines = b"glob:**.o"; |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
353 assert_eq!( |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
354 parse_pattern_file_contents(lines, b"file_path", false).0, |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
355 vec![(b"relglob:**.o".to_vec(), 1, b"**.o".to_vec())] |
9609430d3625
rust-filepatterns: use bytes instead of String
Raphaël Gomès <rgomes@octobus.net>
parents:
42327
diff
changeset
|
356 ); |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
357 } |
42438
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
358 |
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
359 #[test] |
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
360 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
|
361 assert_eq!( |
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
362 br"(?:/|$)".to_vec(), |
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
363 build_single_regex(b"rootglob", b"", b"").unwrap() |
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
364 ); |
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
365 assert_eq!( |
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
366 br"whatever(?:/|$)".to_vec(), |
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
367 build_single_regex(b"rootglob", b"whatever", b"").unwrap() |
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
368 ); |
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
369 assert_eq!( |
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
370 br"[^/]*\.o".to_vec(), |
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
371 build_single_regex(b"rootglob", b"*.o", b"").unwrap() |
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
372 ); |
48f1f864d928
rust-regex: fix shortcut for exact matches
Raphaël Gomès <rgomes@octobus.net>
parents:
42437
diff
changeset
|
373 } |
42327
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Raphaël Gomès <rgomes@octobus.net>
parents:
diff
changeset
|
374 } |