##// END OF EJS Templates
rustfilepatterns: refactor the pattern of removing a prefix from a &[u8]...
Valentin Gatien-Baron -
r43129:62eabdf9 default
parent child Browse files
Show More
@@ -60,8 +60,8 b' fn glob_to_re(pat: &[u8]) -> Vec<u8> {'
60 match c {
60 match c {
61 b'*' => {
61 b'*' => {
62 for (source, repl) in GLOB_REPLACEMENTS {
62 for (source, repl) in GLOB_REPLACEMENTS {
63 if input.starts_with(source) {
63 if let Some(rest) = input.drop_prefix(source) {
64 input = &input[source.len()..];
64 input = rest;
65 res.extend(*repl);
65 res.extend(*repl);
66 break;
66 break;
67 }
67 }
@@ -269,8 +269,8 b' pub fn parse_pattern_file_contents('
269 continue;
269 continue;
270 }
270 }
271
271
272 if line.starts_with(b"syntax:") {
272 if let Some(syntax) = line.drop_prefix(b"syntax:") {
273 let syntax = line[b"syntax:".len()..].trim();
273 let syntax = syntax.trim();
274
274
275 if let Some(rel_syntax) = SYNTAXES.get(syntax) {
275 if let Some(rel_syntax) = SYNTAXES.get(syntax) {
276 current_syntax = rel_syntax;
276 current_syntax = rel_syntax;
@@ -283,13 +283,14 b' pub fn parse_pattern_file_contents('
283 let mut line_syntax: &[u8] = &current_syntax;
283 let mut line_syntax: &[u8] = &current_syntax;
284
284
285 for (s, rels) in SYNTAXES.iter() {
285 for (s, rels) in SYNTAXES.iter() {
286 if line.starts_with(rels) {
286 if let Some(rest) = line.drop_prefix(rels) {
287 line_syntax = rels;
287 line_syntax = rels;
288 line = &line[rels.len()..];
288 line = rest;
289 break;
289 break;
290 } else if line.starts_with(&[s, b":".as_ref()].concat()) {
290 }
291 if let Some(rest) = line.drop_prefix(&[s, &b":"[..]].concat()) {
291 line_syntax = rels;
292 line_syntax = rels;
292 line = &line[s.len() + 1..];
293 line = rest;
293 break;
294 break;
294 }
295 }
295 }
296 }
@@ -40,6 +40,7 b' pub trait SliceExt {'
40 fn trim_end(&self) -> &Self;
40 fn trim_end(&self) -> &Self;
41 fn trim_start(&self) -> &Self;
41 fn trim_start(&self) -> &Self;
42 fn trim(&self) -> &Self;
42 fn trim(&self) -> &Self;
43 fn drop_prefix(&self, needle: &Self) -> Option<&Self>;
43 }
44 }
44
45
45 fn is_not_whitespace(c: &u8) -> bool {
46 fn is_not_whitespace(c: &u8) -> bool {
@@ -80,4 +81,12 b' impl SliceExt for [u8] {'
80 fn trim(&self) -> &[u8] {
81 fn trim(&self) -> &[u8] {
81 self.trim_start().trim_end()
82 self.trim_start().trim_end()
82 }
83 }
84
85 fn drop_prefix(&self, needle: &Self) -> Option<&Self> {
86 if self.starts_with(needle) {
87 Some(&self[needle.len()..])
88 } else {
89 None
83 }
90 }
91 }
92 }
General Comments 0
You need to be logged in to leave comments. Login now