Mercurial > public > mercurial-scm > hg-stable
annotate rust/hg-core/src/filepatterns.rs @ 42869:62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Differential Revision: https://phab.mercurial-scm.org/D6766
author | Valentin Gatien-Baron <valentin.gatienbaron@gmail.com> |
---|---|
date | Mon, 26 Aug 2019 08:25:01 -0400 |
parents | ce6797ef6eab |
children | 72890d8f9860 |
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 |
42609
326fdce22fb2
rust: switch hg-core and hg-cpython to rust 2018 edition
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42498
diff
changeset
|
10 use crate::{ |
42636
12addcc7956c
rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents:
42635
diff
changeset
|
11 utils::{files::get_path_from_bytes, SliceExt}, |
42609
326fdce22fb2
rust: switch hg-core and hg-cpython to rust 2018 edition
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42498
diff
changeset
|
12 LineNumber, PatternError, PatternFileError, |
326fdce22fb2
rust: switch hg-core and hg-cpython to rust 2018 edition
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42498
diff
changeset
|
13 }; |
326fdce22fb2
rust: switch hg-core and hg-cpython to rust 2018 edition
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42498
diff
changeset
|
14 use lazy_static::lazy_static; |
42636
12addcc7956c
rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents:
42635
diff
changeset
|
15 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
|
16 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
|
17 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
|
18 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
|
19 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
|
20 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
21 lazy_static! { |
42498
a4a468b00d44
rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents:
42454
diff
changeset
|
22 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
|
23 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
|
24 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
|
25 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
|
26 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
|
27 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
28 v |
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 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
31 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
32 /// 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
|
33 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
|
34 &[(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
|
35 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
36 #[derive(Debug, Copy, Clone, PartialEq, Eq)] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
37 pub enum PatternSyntax { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
38 Regexp, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
39 /// 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
|
40 RootGlob, |
42851
ce6797ef6eab
rust: apply more formatting fixes
Yuya Nishihara <yuya@tcha.org>
parents:
42767
diff
changeset
|
41 /// 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
|
42 /// slashes) |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
43 Glob, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
44 Path, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
45 RelPath, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
46 RelGlob, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
47 RelRegexp, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
48 RootFiles, |
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 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
51 /// 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
|
52 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
|
53 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
|
54 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
|
55 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
|
56 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
57 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
|
58 input = rest; |
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 match c { |
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 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
|
63 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
|
64 input = rest; |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
65 res.extend(*repl); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
66 break; |
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 } |
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 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
|
71 b'[' => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
72 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
|
73 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
|
74 Some(end) => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
75 // 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
|
76 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
|
77 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
78 res.extend(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 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
|
81 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'^' && i == 0 { |
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 if *b == b'\\' { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
86 res.extend(b"\\\\") |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
87 } else { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
88 res.push(*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 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
91 res.extend(b"]"); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
92 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
|
93 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
94 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
95 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
96 b'{' => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
97 group_depth += 1; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
98 res.extend(b"(?:") |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
99 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
100 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
|
101 group_depth -= 1; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
102 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 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
104 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
|
105 b'\\' => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
106 let c = { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
107 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
|
108 input = rest; |
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 } else { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
111 c |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
112 } |
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 } |
42498
a4a468b00d44
rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents:
42454
diff
changeset
|
116 _ => 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
|
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 res |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
120 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
121 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
122 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
|
123 pattern |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
124 .iter() |
42498
a4a468b00d44
rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents:
42454
diff
changeset
|
125 .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
|
126 .collect() |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
127 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
128 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
129 fn 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
|
130 match kind { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
131 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
|
132 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
|
133 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
|
134 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
|
135 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
|
136 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
|
137 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
|
138 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
|
139 _ => Err(PatternError::UnsupportedSyntax( |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
140 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
|
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 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
144 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
145 /// 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
|
146 /// 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
|
147 /// 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
|
148 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
|
149 syntax: PatternSyntax, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
150 pattern: &[u8], |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
151 globsuffix: &[u8], |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
152 ) -> Vec<u8> { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
153 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
|
154 return vec![]; |
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 match syntax { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
157 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
|
158 PatternSyntax::RelRegexp => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
159 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
|
160 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
|
161 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
162 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
|
163 res.extend(pattern); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
164 res |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
165 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
166 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
|
167 if pattern == b"." { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
168 return vec![]; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
169 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
170 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
|
171 pattern.extend(b"(?:/|$)"); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
172 pattern |
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 PatternSyntax::RootFiles => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
175 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
|
176 vec![] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
177 } else { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
178 // 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
|
179 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
|
180 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
|
181 as_vec |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
182 }; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
183 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
184 // 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
|
185 res.extend(b"[^/]+$"); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
186 res |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
187 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
188 PatternSyntax::Glob |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
189 | PatternSyntax::RelGlob |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
190 | PatternSyntax::RootGlob => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
191 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
|
192 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
|
193 res.extend(b"(?:|.*/)"); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
194 } |
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 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
|
197 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
|
198 res |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
199 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
200 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
201 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
202 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
203 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
|
204 [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
|
205 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
206 /// 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
|
207 /// 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
|
208 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
|
209 kind: &[u8], |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
210 pat: &[u8], |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
211 globsuffix: &[u8], |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
212 ) -> Result<Vec<u8>, PatternError> { |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
213 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
|
214 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
|
215 && !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
|
216 { |
42454
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
217 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
|
218 escaped.extend(b"(?:/|$)"); |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
219 Ok(escaped) |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
220 } else { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
221 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
|
222 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
223 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
224 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
225 lazy_static! { |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
226 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
|
227 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
|
228 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
229 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
|
230 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
|
231 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
|
232 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
|
233 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
|
234 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
|
235 m |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
236 }; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
237 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
238 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
239 pub type PatternTuple = (Vec<u8>, LineNumber, Vec<u8>); |
42634
0247601869ba
rust-filepatterns: fix type of warnings tuple to (bytes, bytes)
Yuya Nishihara <yuya@tcha.org>
parents:
42609
diff
changeset
|
240 type WarningTuple = (Vec<u8>, Vec<u8>); |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
241 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
242 pub fn parse_pattern_file_contents( |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
243 lines: &[u8], |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
244 file_path: &[u8], |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
245 warn: bool, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
246 ) -> (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
|
247 let comment_regex = Regex::new(r"((?:^|[^\\])(?:\\\\)*)#.*").unwrap(); |
42636
12addcc7956c
rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents:
42635
diff
changeset
|
248 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
|
249 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
|
250 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
|
251 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
252 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
|
253 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
254 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
|
255 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
|
256 |
42636
12addcc7956c
rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents:
42635
diff
changeset
|
257 let line_buf; |
42635
30f8e786868c
rust-filepatterns: use literal b'#' instead of cast
Yuya Nishihara <yuya@tcha.org>
parents:
42634
diff
changeset
|
258 if line.contains(&b'#') { |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
259 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
|
260 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
|
261 } |
42636
12addcc7956c
rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents:
42635
diff
changeset
|
262 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
|
263 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
|
264 } |
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 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
|
267 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
268 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
|
269 continue; |
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 |
42869
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42851
diff
changeset
|
272 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
|
273 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
|
274 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
275 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
|
276 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
|
277 } else if warn { |
42634
0247601869ba
rust-filepatterns: fix type of warnings tuple to (bytes, bytes)
Yuya Nishihara <yuya@tcha.org>
parents:
42609
diff
changeset
|
278 warnings.push((file_path.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
|
279 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
280 continue; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
281 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
282 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
283 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
|
284 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
285 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
|
286 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
|
287 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
|
288 line = rest; |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
289 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
|
290 } |
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42851
diff
changeset
|
291 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
|
292 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
|
293 line = rest; |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
294 break; |
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 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
298 inputs.push(( |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
299 [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
|
300 line_number, |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
301 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
|
302 )); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
303 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
304 (inputs, warnings) |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
305 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
306 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
307 pub fn read_pattern_file( |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
308 file_path: &[u8], |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
309 warn: bool, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
310 ) -> Result<(Vec<PatternTuple>, Vec<WarningTuple>), PatternFileError> { |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
311 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:
42349
diff
changeset
|
312 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
|
313 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
314 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
|
315 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
316 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
|
317 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
318 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
319 #[cfg(test)] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
320 mod tests { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
321 use super::*; |
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 #[test] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
324 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
|
325 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
|
326 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
|
327 // All escape codes |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
328 assert_eq!( |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
329 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
|
330 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
|
331 .to_vec() |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
332 ); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
333 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
334 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
335 #[test] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
336 fn glob_test() { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
337 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
|
338 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
|
339 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
|
340 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
|
341 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
|
342 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
|
343 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
|
344 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
|
345 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
346 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
347 #[test] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
348 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
|
349 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
|
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!( |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
352 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:
42349
diff
changeset
|
353 parse_pattern_file_contents(lines, b"file_path", false).0, |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
354 ); |
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"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
|
357 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
358 assert_eq!( |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
359 parse_pattern_file_contents(lines, b"file_path", false).0, |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
360 vec![] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
361 ); |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
362 let lines = b"glob:**.o"; |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
363 assert_eq!( |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
364 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:
42349
diff
changeset
|
365 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
|
366 ); |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
367 } |
42454
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
368 |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
369 #[test] |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
370 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
|
371 assert_eq!( |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
372 br"(?:/|$)".to_vec(), |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
373 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
|
374 ); |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
375 assert_eq!( |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
376 br"whatever(?:/|$)".to_vec(), |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
377 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
|
378 ); |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
379 assert_eq!( |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
380 br"[^/]*\.o".to_vec(), |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
381 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
|
382 ); |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
383 } |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
384 } |