Mercurial > public > mercurial-scm > hg-stable
annotate rust/hg-core/src/filepatterns.rs @ 42960:7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Differential Revision: https://phab.mercurial-scm.org/D6774
author | Rapha?l Gom?s <rgomes@octobus.net> |
---|---|
date | Sun, 01 Sep 2019 20:53:14 +0200 |
parents | 69195b6f8f97 |
children | 5ac243a92e37 |
rev | line source |
---|---|
42767
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 |
42960
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42871
diff
changeset
|
10 use crate::{utils::SliceExt, LineNumber, PatternError, PatternFileError}; |
42609
326fdce22fb2
rust: switch hg-core and hg-cpython to rust 2018 edition
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42498
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}; |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
13 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
|
14 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
|
15 use std::io::Read; |
42960
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42871
diff
changeset
|
16 use std::path::{Path, PathBuf}; |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
17 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
|
18 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
19 lazy_static! { |
42498
a4a468b00d44
rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents:
42454
diff
changeset
|
20 static ref RE_ESCAPE: Vec<Vec<u8>> = { |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
21 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
|
22 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
|
23 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
|
24 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
|
25 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
26 v |
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 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
30 /// 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
|
31 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
|
32 &[(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
|
33 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
34 #[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
|
35 pub enum PatternSyntax { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
36 Regexp, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
37 /// 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
|
38 RootGlob, |
42851
ce6797ef6eab
rust: apply more formatting fixes
Yuya Nishihara <yuya@tcha.org>
parents:
42767
diff
changeset
|
39 /// Glob that matches at any suffix of the path (still anchored at |
ce6797ef6eab
rust: apply more formatting fixes
Yuya Nishihara <yuya@tcha.org>
parents:
42767
diff
changeset
|
40 /// slashes) |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
41 Glob, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
42 Path, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
43 RelPath, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
44 RelGlob, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
45 RelRegexp, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
46 RootFiles, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
47 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
48 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
49 /// 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
|
50 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
|
51 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
|
52 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
|
53 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
|
54 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
55 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
|
56 input = rest; |
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 match c { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
59 b'*' => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
60 for (source, repl) in GLOB_REPLACEMENTS { |
42869
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42851
diff
changeset
|
61 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:
42851
diff
changeset
|
62 input = rest; |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
63 res.extend(*repl); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
64 break; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
65 } |
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 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
68 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
|
69 b'[' => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
70 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
|
71 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
|
72 Some(end) => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
73 // 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
|
74 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
|
75 |
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 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
78 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
|
79 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
|
80 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 } 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
|
82 res.extend(b"\\^") |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
83 } 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
|
84 res.extend(b"\\\\") |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
85 } else { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
86 res.push(*b) |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
87 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
88 } |
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 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
|
91 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
92 } |
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'{' => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
95 group_depth += 1; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
96 res.extend(b"(?:") |
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 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
|
99 group_depth -= 1; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
100 res.extend(b")"); |
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 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
|
103 b'\\' => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
104 let c = { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
105 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
|
106 input = rest; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
107 c |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
108 } else { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
109 c |
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 }; |
42498
a4a468b00d44
rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents:
42454
diff
changeset
|
112 res.extend(&RE_ESCAPE[*c as usize]) |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
113 } |
42498
a4a468b00d44
rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents:
42454
diff
changeset
|
114 _ => res.extend(&RE_ESCAPE[*c as usize]), |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
115 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
116 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
117 res |
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 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
120 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
|
121 pattern |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
122 .iter() |
42498
a4a468b00d44
rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents:
42454
diff
changeset
|
123 .flat_map(|c| RE_ESCAPE[*c as usize].clone()) |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
124 .collect() |
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 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
127 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
|
128 match kind { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
129 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
|
130 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
|
131 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
|
132 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
|
133 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
|
134 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
|
135 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
|
136 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
|
137 _ => Err(PatternError::UnsupportedSyntax( |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
138 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
|
139 )), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
140 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
141 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
142 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
143 /// 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
|
144 /// 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
|
145 /// 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
|
146 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
|
147 syntax: PatternSyntax, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
148 pattern: &[u8], |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
149 globsuffix: &[u8], |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
150 ) -> Vec<u8> { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
151 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
|
152 return vec![]; |
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 match syntax { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
155 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
|
156 PatternSyntax::RelRegexp => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
157 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
|
158 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
|
159 } |
42871
69195b6f8f97
rustfilepatterns: shorter code for concatenating slices
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42870
diff
changeset
|
160 [b".*", pattern].concat() |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
161 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
162 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
|
163 if pattern == b"." { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
164 return vec![]; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
165 } |
42871
69195b6f8f97
rustfilepatterns: shorter code for concatenating slices
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42870
diff
changeset
|
166 [escape_pattern(pattern).as_slice(), b"(?:/|$)"].concat() |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
167 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
168 PatternSyntax::RootFiles => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
169 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
|
170 vec![] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
171 } else { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
172 // Pattern is a directory name. |
42871
69195b6f8f97
rustfilepatterns: shorter code for concatenating slices
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42870
diff
changeset
|
173 [escape_pattern(pattern).as_slice(), b"/"].concat() |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
174 }; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
175 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
176 // 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
|
177 res.extend(b"[^/]+$"); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
178 res |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
179 } |
42870
72890d8f9860
match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42869
diff
changeset
|
180 PatternSyntax::RelGlob => { |
72890d8f9860
match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42869
diff
changeset
|
181 let glob_re = glob_to_re(pattern); |
72890d8f9860
match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42869
diff
changeset
|
182 if let Some(rest) = glob_re.drop_prefix(b"[^/]*") { |
42871
69195b6f8f97
rustfilepatterns: shorter code for concatenating slices
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42870
diff
changeset
|
183 [b".*", rest, globsuffix].concat() |
42870
72890d8f9860
match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42869
diff
changeset
|
184 } else { |
42871
69195b6f8f97
rustfilepatterns: shorter code for concatenating slices
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42870
diff
changeset
|
185 [b"(?:|.*/)", glob_re.as_slice(), globsuffix].concat() |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
186 } |
42870
72890d8f9860
match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42869
diff
changeset
|
187 } |
72890d8f9860
match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42869
diff
changeset
|
188 PatternSyntax::Glob | PatternSyntax::RootGlob => { |
42871
69195b6f8f97
rustfilepatterns: shorter code for concatenating slices
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42870
diff
changeset
|
189 [glob_to_re(pattern).as_slice(), globsuffix].concat() |
42349
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 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
194 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
|
195 [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
|
196 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
197 /// 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
|
198 /// 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
|
199 pub fn build_single_regex( |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
200 kind: &[u8], |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
201 pat: &[u8], |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
202 globsuffix: &[u8], |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
203 ) -> Result<Vec<u8>, PatternError> { |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
204 let enum_kind = parse_pattern_syntax(kind)?; |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
205 if enum_kind == PatternSyntax::RootGlob |
42454
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
206 && !pat.iter().any(|b| GLOB_SPECIAL_CHARACTERS.contains(b)) |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
207 { |
42454
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
208 let mut escaped = escape_pattern(pat); |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
209 escaped.extend(b"(?:/|$)"); |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
210 Ok(escaped) |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
211 } else { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
212 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
|
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 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
216 lazy_static! { |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
217 static ref SYNTAXES: HashMap<&'static [u8], &'static [u8]> = { |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
218 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
|
219 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
220 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:
42349
diff
changeset
|
221 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:
42349
diff
changeset
|
222 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:
42349
diff
changeset
|
223 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:
42349
diff
changeset
|
224 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:
42349
diff
changeset
|
225 m.insert(b"subinclude".as_ref(), b"subinclude".as_ref()); |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
226 m |
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 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
229 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
230 pub type PatternTuple = (Vec<u8>, LineNumber, Vec<u8>); |
42960
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42871
diff
changeset
|
231 type WarningTuple = (PathBuf, Vec<u8>); |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
232 |
42960
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42871
diff
changeset
|
233 pub fn parse_pattern_file_contents<P: AsRef<Path>>( |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
234 lines: &[u8], |
42960
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42871
diff
changeset
|
235 file_path: P, |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
236 warn: bool, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
237 ) -> (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
|
238 let comment_regex = Regex::new(r"((?:^|[^\\])(?:\\\\)*)#.*").unwrap(); |
42636
12addcc7956c
rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents:
42635
diff
changeset
|
239 let comment_escape_regex = Regex::new(r"\\#").unwrap(); |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
240 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
|
241 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
|
242 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
243 let mut current_syntax = b"relre:".as_ref(); |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
244 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
245 for (line_number, mut line) in lines.split(|c| *c == b'\n').enumerate() { |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
246 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
|
247 |
42636
12addcc7956c
rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents:
42635
diff
changeset
|
248 let line_buf; |
42635
30f8e786868c
rust-filepatterns: use literal b'#' instead of cast
Yuya Nishihara <yuya@tcha.org>
parents:
42634
diff
changeset
|
249 if line.contains(&b'#') { |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
250 if let Some(cap) = comment_regex.captures(line) { |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
251 line = &line[..cap.get(1).unwrap().end()] |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
252 } |
42636
12addcc7956c
rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents:
42635
diff
changeset
|
253 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
|
254 line = &line_buf; |
42349
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 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
257 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
|
258 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
259 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
|
260 continue; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
261 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
262 |
42869
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42851
diff
changeset
|
263 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:
42851
diff
changeset
|
264 let syntax = syntax.trim(); |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
265 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
266 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
|
267 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
|
268 } else if warn { |
42960
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42871
diff
changeset
|
269 warnings |
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42871
diff
changeset
|
270 .push((file_path.as_ref().to_owned(), syntax.to_owned())); |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
271 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
272 continue; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
273 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
274 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
275 let mut line_syntax: &[u8] = ¤t_syntax; |
42349
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 for (s, rels) in SYNTAXES.iter() { |
42869
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42851
diff
changeset
|
278 if let Some(rest) = line.drop_prefix(rels) { |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
279 line_syntax = rels; |
42869
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42851
diff
changeset
|
280 line = rest; |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
281 break; |
42869
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42851
diff
changeset
|
282 } |
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42851
diff
changeset
|
283 if let Some(rest) = line.drop_prefix(&[s, &b":"[..]].concat()) { |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
284 line_syntax = rels; |
42869
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42851
diff
changeset
|
285 line = rest; |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
286 break; |
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 } |
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 inputs.push(( |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
291 [line_syntax, line].concat(), |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
292 line_number, |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
293 line.to_owned(), |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
294 )); |
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 (inputs, warnings) |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
297 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
298 |
42960
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42871
diff
changeset
|
299 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:
42871
diff
changeset
|
300 file_path: P, |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
301 warn: bool, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
302 ) -> Result<(Vec<PatternTuple>, Vec<WarningTuple>), PatternFileError> { |
42960
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42871
diff
changeset
|
303 let mut f = File::open(file_path.as_ref())?; |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
304 let mut contents = Vec::new(); |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
305 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
306 f.read_to_end(&mut contents)?; |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
307 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
308 Ok(parse_pattern_file_contents(&contents, file_path, warn)) |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
309 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
310 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
311 #[cfg(test)] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
312 mod tests { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
313 use super::*; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
314 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
315 #[test] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
316 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
|
317 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
|
318 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
|
319 // All escape codes |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
320 assert_eq!( |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
321 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
|
322 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
|
323 .to_vec() |
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 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
326 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
327 #[test] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
328 fn glob_test() { |
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#"*"#), br#"[^/]*"#); |
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#"**"#), br#".*"#); |
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"#), br#"(?:.*/)?a"#); |
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#"[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
|
335 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
|
336 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
|
337 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
338 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
339 #[test] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
340 fn test_parse_pattern_file_contents() { |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
341 let lines = b"syntax: glob\n*.elc"; |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
342 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
343 assert_eq!( |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
344 vec![(b"relglob:*.elc".to_vec(), 2, b"*.elc".to_vec())], |
42960
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42871
diff
changeset
|
345 parse_pattern_file_contents(lines, Path::new("file_path"), false) |
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42871
diff
changeset
|
346 .0, |
42349
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 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
349 let lines = b"syntax: include\nsyntax: glob"; |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
350 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
351 assert_eq!( |
42960
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42871
diff
changeset
|
352 parse_pattern_file_contents(lines, Path::new("file_path"), false) |
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42871
diff
changeset
|
353 .0, |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
354 vec![] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
355 ); |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
356 let lines = b"glob:**.o"; |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
357 assert_eq!( |
42960
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42871
diff
changeset
|
358 parse_pattern_file_contents(lines, Path::new("file_path"), false) |
7a01778bc7b7
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42871
diff
changeset
|
359 .0, |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
360 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:
42349
diff
changeset
|
361 ); |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
362 } |
42454
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
363 |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
364 #[test] |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
365 fn test_build_single_regex_shortcut() { |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
366 assert_eq!( |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
367 br"(?:/|$)".to_vec(), |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
368 build_single_regex(b"rootglob", b"", b"").unwrap() |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
369 ); |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
370 assert_eq!( |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
371 br"whatever(?:/|$)".to_vec(), |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
372 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:
42453
diff
changeset
|
373 ); |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
374 assert_eq!( |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
375 br"[^/]*\.o".to_vec(), |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
376 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:
42453
diff
changeset
|
377 ); |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
378 } |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
379 } |