Mercurial > public > mercurial-scm > hg-stable
annotate rust/hg-core/src/filepatterns.rs @ 42871:69195b6f8f97
rustfilepatterns: shorter code for concatenating slices
Differential Revision: https://phab.mercurial-scm.org/D6765
author | Valentin Gatien-Baron <valentin.gatienbaron@gmail.com> |
---|---|
date | Sun, 25 Aug 2019 23:40:22 -0400 |
parents | 72890d8f9860 |
children | 7a01778bc7b7 |
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 } |
42871
69195b6f8f97
rustfilepatterns: shorter code for concatenating slices
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42870
diff
changeset
|
162 [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
|
163 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
164 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
|
165 if pattern == b"." { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
166 return vec![]; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
167 } |
42871
69195b6f8f97
rustfilepatterns: shorter code for concatenating slices
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42870
diff
changeset
|
168 [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
|
169 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
170 PatternSyntax::RootFiles => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
171 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
|
172 vec![] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
173 } else { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
174 // Pattern is a directory name. |
42871
69195b6f8f97
rustfilepatterns: shorter code for concatenating slices
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42870
diff
changeset
|
175 [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
|
176 }; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
177 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
178 // 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
|
179 res.extend(b"[^/]+$"); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
180 res |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
181 } |
42870
72890d8f9860
match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42869
diff
changeset
|
182 PatternSyntax::RelGlob => { |
72890d8f9860
match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42869
diff
changeset
|
183 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
|
184 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
|
185 [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
|
186 } else { |
42871
69195b6f8f97
rustfilepatterns: shorter code for concatenating slices
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42870
diff
changeset
|
187 [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
|
188 } |
42870
72890d8f9860
match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42869
diff
changeset
|
189 } |
72890d8f9860
match: simplify the regexps created for glob patterns
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42869
diff
changeset
|
190 PatternSyntax::Glob | PatternSyntax::RootGlob => { |
42871
69195b6f8f97
rustfilepatterns: shorter code for concatenating slices
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42870
diff
changeset
|
191 [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
|
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 } |
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 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
|
197 [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
|
198 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
199 /// 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
|
200 /// 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
|
201 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
|
202 kind: &[u8], |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
203 pat: &[u8], |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
204 globsuffix: &[u8], |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
205 ) -> Result<Vec<u8>, PatternError> { |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
206 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
|
207 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
|
208 && !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
|
209 { |
42454
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
210 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
|
211 escaped.extend(b"(?:/|$)"); |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
212 Ok(escaped) |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
213 } else { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
214 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
|
215 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
216 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
217 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
218 lazy_static! { |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
219 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
|
220 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
|
221 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
222 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
|
223 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
|
224 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
|
225 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
|
226 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
|
227 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
|
228 m |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
229 }; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
230 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
231 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
232 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
|
233 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
|
234 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
235 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
|
236 lines: &[u8], |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
237 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
|
238 warn: bool, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
239 ) -> (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
|
240 let comment_regex = Regex::new(r"((?:^|[^\\])(?:\\\\)*)#.*").unwrap(); |
42636
12addcc7956c
rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents:
42635
diff
changeset
|
241 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
|
242 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
|
243 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
|
244 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
245 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
|
246 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
247 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
|
248 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
|
249 |
42636
12addcc7956c
rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents:
42635
diff
changeset
|
250 let line_buf; |
42635
30f8e786868c
rust-filepatterns: use literal b'#' instead of cast
Yuya Nishihara <yuya@tcha.org>
parents:
42634
diff
changeset
|
251 if line.contains(&b'#') { |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
252 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
|
253 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
|
254 } |
42636
12addcc7956c
rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents:
42635
diff
changeset
|
255 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
|
256 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
|
257 } |
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 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
|
260 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
261 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
|
262 continue; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
263 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
264 |
42869
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42851
diff
changeset
|
265 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
|
266 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
|
267 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
268 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
|
269 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
|
270 } else if warn { |
42634
0247601869ba
rust-filepatterns: fix type of warnings tuple to (bytes, bytes)
Yuya Nishihara <yuya@tcha.org>
parents:
42609
diff
changeset
|
271 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
|
272 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
273 continue; |
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 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
276 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
|
277 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
278 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
|
279 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
|
280 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
|
281 line = rest; |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
282 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
|
283 } |
62eabdf91f85
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]
Valentin Gatien-Baron <valentin.gatienbaron@gmail.com>
parents:
42851
diff
changeset
|
284 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
|
285 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
|
286 line = rest; |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
287 break; |
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 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
291 inputs.push(( |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
292 [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
|
293 line_number, |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
294 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
|
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 (inputs, warnings) |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
298 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
299 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
300 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
|
301 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
|
302 warn: bool, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
303 ) -> 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
|
304 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
|
305 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
|
306 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
307 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
|
308 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
309 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
|
310 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
311 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
312 #[cfg(test)] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
313 mod tests { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
314 use super::*; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
315 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
316 #[test] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
317 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
|
318 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
|
319 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
|
320 // All escape codes |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
321 assert_eq!( |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
322 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
|
323 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
|
324 .to_vec() |
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 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
328 #[test] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
329 fn glob_test() { |
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#"**"#), br#".*"#); |
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"#), br#"(?:.*/)?a"#); |
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"#), br#"a/(?:.*/)?b"#); |
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][!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
|
336 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
|
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 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
339 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
340 #[test] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
341 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
|
342 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
|
343 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
344 assert_eq!( |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
345 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
|
346 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
|
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!( |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
352 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
|
353 vec![] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
354 ); |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
355 let lines = b"glob:**.o"; |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
356 assert_eq!( |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
357 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
|
358 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
|
359 ); |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
360 } |
42454
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
361 |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
362 #[test] |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
363 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
|
364 assert_eq!( |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
365 br"(?:/|$)".to_vec(), |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
366 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
|
367 ); |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
368 assert_eq!( |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
369 br"whatever(?:/|$)".to_vec(), |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
370 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
|
371 ); |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
372 assert_eq!( |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
373 br"[^/]*\.o".to_vec(), |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
374 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
|
375 ); |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
376 } |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
377 } |