Mercurial > public > mercurial-scm > hg-stable
annotate rust/hg-core/src/filepatterns.rs @ 42636:12addcc7956c
rust-filepatterns: unescape comment character property
There were multiple issues in the original implementation:
a. the local variable "line" dropped soon after replace_slice() applied
b. replace_slice() was noop since br"\#".len() != b"#"
This patch uses bytes::Regex::replace_all() since it seems the simplest way
to replace bytes of arbitrary length, and I don't think we have to avoid
using Regexp here.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Sun, 21 Jul 2019 14:42:01 +0900 |
parents | 30f8e786868c |
children | 4b3b27d567d5 |
rev | line source |
---|---|
42609
326fdce22fb2
rust: switch hg-core and hg-cpython to rust 2018 edition
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42498
diff
changeset
|
1 use crate::{ |
42636
12addcc7956c
rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents:
42635
diff
changeset
|
2 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
|
3 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
|
4 }; |
326fdce22fb2
rust: switch hg-core and hg-cpython to rust 2018 edition
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42498
diff
changeset
|
5 use lazy_static::lazy_static; |
42636
12addcc7956c
rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents:
42635
diff
changeset
|
6 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
|
7 use std::collections::HashMap; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
8 use std::fs::File; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
9 use std::io::Read; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
10 use std::vec::Vec; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
11 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
12 lazy_static! { |
42498
a4a468b00d44
rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents:
42454
diff
changeset
|
13 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
|
14 let mut v: Vec<Vec<u8>> = (0..=255).map(|byte| vec![byte]).collect(); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
15 let to_escape = b"()[]{}?*+-|^$\\.&~# \t\n\r\x0b\x0c"; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
16 for byte in to_escape { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
17 v[*byte as usize].insert(0, b'\\'); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
18 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
19 v |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
20 }; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
21 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
22 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
23 /// These are matched in order |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
24 const GLOB_REPLACEMENTS: &[(&[u8], &[u8])] = |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
25 &[(b"*/", b"(?:.*/)?"), (b"*", b".*"), (b"", b"[^/]*")]; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
26 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
27 #[derive(Debug, Copy, Clone, PartialEq, Eq)] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
28 pub enum PatternSyntax { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
29 Regexp, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
30 /// Glob that matches at the front of the path |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
31 RootGlob, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
32 /// Glob that matches at any suffix of the path (still anchored at slashes) |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
33 Glob, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
34 Path, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
35 RelPath, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
36 RelGlob, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
37 RelRegexp, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
38 RootFiles, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
39 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
40 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
41 /// Transforms a glob pattern into a regex |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
42 fn glob_to_re(pat: &[u8]) -> Vec<u8> { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
43 let mut input = pat; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
44 let mut res: Vec<u8> = vec![]; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
45 let mut group_depth = 0; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
46 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
47 while let Some((c, rest)) = input.split_first() { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
48 input = rest; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
49 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
50 match c { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
51 b'*' => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
52 for (source, repl) in GLOB_REPLACEMENTS { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
53 if input.starts_with(source) { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
54 input = &input[source.len()..]; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
55 res.extend(*repl); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
56 break; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
57 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
58 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
59 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
60 b'?' => res.extend(b"."), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
61 b'[' => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
62 match input.iter().skip(1).position(|b| *b == b']') { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
63 None => res.extend(b"\\["), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
64 Some(end) => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
65 // Account for the one we skipped |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
66 let end = end + 1; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
67 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
68 res.extend(b"["); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
69 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
70 for (i, b) in input[..end].iter().enumerate() { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
71 if *b == b'!' && i == 0 { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
72 res.extend(b"^") |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
73 } else if *b == b'^' && i == 0 { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
74 res.extend(b"\\^") |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
75 } else if *b == b'\\' { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
76 res.extend(b"\\\\") |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
77 } else { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
78 res.push(*b) |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
79 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
80 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
81 res.extend(b"]"); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
82 input = &input[end + 1..]; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
83 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
84 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
85 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
86 b'{' => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
87 group_depth += 1; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
88 res.extend(b"(?:") |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
89 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
90 b'}' if group_depth > 0 => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
91 group_depth -= 1; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
92 res.extend(b")"); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
93 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
94 b',' if group_depth > 0 => res.extend(b"|"), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
95 b'\\' => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
96 let c = { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
97 if let Some((c, rest)) = input.split_first() { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
98 input = rest; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
99 c |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
100 } else { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
101 c |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
102 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
103 }; |
42498
a4a468b00d44
rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents:
42454
diff
changeset
|
104 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
|
105 } |
42498
a4a468b00d44
rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents:
42454
diff
changeset
|
106 _ => 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
|
107 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
108 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
109 res |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
110 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
111 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
112 fn escape_pattern(pattern: &[u8]) -> Vec<u8> { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
113 pattern |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
114 .iter() |
42498
a4a468b00d44
rust-filepatterns: silence warning of non_upper_case_globals
Yuya Nishihara <yuya@tcha.org>
parents:
42454
diff
changeset
|
115 .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
|
116 .collect() |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
117 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
118 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
119 fn parse_pattern_syntax(kind: &[u8]) -> Result<PatternSyntax, PatternError> { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
120 match kind { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
121 b"re" => Ok(PatternSyntax::Regexp), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
122 b"path" => Ok(PatternSyntax::Path), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
123 b"relpath" => Ok(PatternSyntax::RelPath), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
124 b"rootfilesin" => Ok(PatternSyntax::RootFiles), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
125 b"relglob" => Ok(PatternSyntax::RelGlob), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
126 b"relre" => Ok(PatternSyntax::RelRegexp), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
127 b"glob" => Ok(PatternSyntax::Glob), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
128 b"rootglob" => Ok(PatternSyntax::RootGlob), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
129 _ => Err(PatternError::UnsupportedSyntax( |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
130 String::from_utf8_lossy(kind).to_string(), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
131 )), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
132 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
133 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
134 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
135 /// Builds the regex that corresponds to the given pattern. |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
136 /// If within a `syntax: regexp` context, returns the pattern, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
137 /// otherwise, returns the corresponding regex. |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
138 fn _build_single_regex( |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
139 syntax: PatternSyntax, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
140 pattern: &[u8], |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
141 globsuffix: &[u8], |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
142 ) -> Vec<u8> { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
143 if pattern.is_empty() { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
144 return vec![]; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
145 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
146 match syntax { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
147 PatternSyntax::Regexp => pattern.to_owned(), |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
148 PatternSyntax::RelRegexp => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
149 if pattern[0] == b'^' { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
150 return pattern.to_owned(); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
151 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
152 let mut res = b".*".to_vec(); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
153 res.extend(pattern); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
154 res |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
155 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
156 PatternSyntax::Path | PatternSyntax::RelPath => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
157 if pattern == b"." { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
158 return vec![]; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
159 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
160 let mut pattern = escape_pattern(pattern); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
161 pattern.extend(b"(?:/|$)"); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
162 pattern |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
163 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
164 PatternSyntax::RootFiles => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
165 let mut res = if pattern == b"." { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
166 vec![] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
167 } else { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
168 // Pattern is a directory name. |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
169 let mut as_vec: Vec<u8> = escape_pattern(pattern); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
170 as_vec.push(b'/'); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
171 as_vec |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
172 }; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
173 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
174 // Anything after the pattern must be a non-directory. |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
175 res.extend(b"[^/]+$"); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
176 res |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
177 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
178 PatternSyntax::Glob |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
179 | PatternSyntax::RelGlob |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
180 | PatternSyntax::RootGlob => { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
181 let mut res: Vec<u8> = vec![]; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
182 if syntax == PatternSyntax::RelGlob { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
183 res.extend(b"(?:|.*/)"); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
184 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
185 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
186 res.extend(glob_to_re(pattern)); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
187 res.extend(globsuffix.iter()); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
188 res |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
189 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
190 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
191 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
192 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
193 const GLOB_SPECIAL_CHARACTERS: [u8; 7] = |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
194 [b'*', b'?', b'[', b']', b'{', b'}', b'\\']; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
195 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
196 /// Wrapper function to `_build_single_regex` that short-circuits 'exact' globs |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
197 /// that don't need to be transformed into a regex. |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
198 pub fn build_single_regex( |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
199 kind: &[u8], |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
200 pat: &[u8], |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
201 globsuffix: &[u8], |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
202 ) -> Result<Vec<u8>, PatternError> { |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
203 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
|
204 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
|
205 && !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
|
206 { |
42454
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
207 let mut escaped = escape_pattern(pat); |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
208 escaped.extend(b"(?:/|$)"); |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
209 Ok(escaped) |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
210 } else { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
211 Ok(_build_single_regex(enum_kind, pat, globsuffix)) |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
212 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
213 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
214 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
215 lazy_static! { |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
216 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
|
217 let mut m = HashMap::new(); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
218 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
219 m.insert(b"re".as_ref(), b"relre:".as_ref()); |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
220 m.insert(b"regexp".as_ref(), b"relre:".as_ref()); |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
221 m.insert(b"glob".as_ref(), b"relglob:".as_ref()); |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
222 m.insert(b"rootglob".as_ref(), b"rootglob:".as_ref()); |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
223 m.insert(b"include".as_ref(), b"include".as_ref()); |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
224 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
|
225 m |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
226 }; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
227 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
228 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
229 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
|
230 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
|
231 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
232 pub fn parse_pattern_file_contents( |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
233 lines: &[u8], |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
234 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
|
235 warn: bool, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
236 ) -> (Vec<PatternTuple>, Vec<WarningTuple>) { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
237 let comment_regex = Regex::new(r"((?:^|[^\\])(?:\\\\)*)#.*").unwrap(); |
42636
12addcc7956c
rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents:
42635
diff
changeset
|
238 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
|
239 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
|
240 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
|
241 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
242 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
|
243 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
244 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
|
245 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
|
246 |
42636
12addcc7956c
rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents:
42635
diff
changeset
|
247 let line_buf; |
42635
30f8e786868c
rust-filepatterns: use literal b'#' instead of cast
Yuya Nishihara <yuya@tcha.org>
parents:
42634
diff
changeset
|
248 if line.contains(&b'#') { |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
249 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
|
250 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
|
251 } |
42636
12addcc7956c
rust-filepatterns: unescape comment character property
Yuya Nishihara <yuya@tcha.org>
parents:
42635
diff
changeset
|
252 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
|
253 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
|
254 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
255 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
256 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
|
257 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
258 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
|
259 continue; |
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 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
262 if line.starts_with(b"syntax:") { |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
263 let syntax = line[b"syntax:".len()..].trim(); |
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 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
|
266 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
|
267 } else if warn { |
42634
0247601869ba
rust-filepatterns: fix type of warnings tuple to (bytes, bytes)
Yuya Nishihara <yuya@tcha.org>
parents:
42609
diff
changeset
|
268 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
|
269 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
270 continue; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
271 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
272 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
273 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
|
274 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
275 for (s, rels) in SYNTAXES.iter() { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
276 if line.starts_with(rels) { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
277 line_syntax = rels; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
278 line = &line[rels.len()..]; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
279 break; |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
280 } else if line.starts_with(&[s, b":".as_ref()].concat()) { |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
281 line_syntax = rels; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
282 line = &line[s.len() + 1..]; |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
283 break; |
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 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
286 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
287 inputs.push(( |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
288 [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
|
289 line_number, |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
290 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
|
291 )); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
292 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
293 (inputs, warnings) |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
294 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
295 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
296 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
|
297 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
|
298 warn: bool, |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
299 ) -> 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
|
300 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
|
301 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
|
302 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
303 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
|
304 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
305 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
|
306 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
307 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
308 #[cfg(test)] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
309 mod tests { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
310 use super::*; |
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 #[test] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
313 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
|
314 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
|
315 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
|
316 // All escape codes |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
317 assert_eq!( |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
318 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
|
319 br#"\(\)\[\]\{\}\?\*\+\-\|\^\$\\\\\.\&\~\#\ \\t\\n\\r\\v\\f"# |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
320 .to_vec() |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
321 ); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
322 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
323 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
324 #[test] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
325 fn glob_test() { |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
326 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
|
327 assert_eq!(glob_to_re(br#"*"#), br#"[^/]*"#); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
328 assert_eq!(glob_to_re(br#"**"#), br#".*"#); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
329 assert_eq!(glob_to_re(br#"**/a"#), br#"(?:.*/)?a"#); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
330 assert_eq!(glob_to_re(br#"a/**/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
|
331 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
|
332 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
|
333 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
|
334 } |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
335 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
336 #[test] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
337 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
|
338 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
|
339 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
340 assert_eq!( |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
341 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
|
342 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
|
343 ); |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
344 |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
345 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
|
346 |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
347 assert_eq!( |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
348 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
|
349 vec![] |
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
350 ); |
42453
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
351 let lines = b"glob:**.o"; |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
352 assert_eq!( |
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, |
9609430d3625
rust-filepatterns: use bytes instead of String
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42349
diff
changeset
|
354 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
|
355 ); |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
356 } |
42454
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
357 |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
358 #[test] |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
359 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
|
360 assert_eq!( |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
361 br"(?:/|$)".to_vec(), |
48f1f864d928
rust-regex: fix shortcut for exact matches
Rapha?l Gom?s <rgomes@octobus.net>
parents:
42453
diff
changeset
|
362 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
|
363 ); |
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"whatever(?:/|$)".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"whatever", 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"[^/]*\.o".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"*.o", 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 } |
42349
e8f3740cc067
rust-filepatterns: add a Rust implementation of pattern-related utils
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
373 } |