comparison 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
comparison
equal deleted inserted replaced
42635:30f8e786868c 42636:12addcc7956c
1 use crate::{ 1 use crate::{
2 utils::{files::get_path_from_bytes, replace_slice, SliceExt}, 2 utils::{files::get_path_from_bytes, SliceExt},
3 LineNumber, PatternError, PatternFileError, 3 LineNumber, PatternError, PatternFileError,
4 }; 4 };
5 use lazy_static::lazy_static; 5 use lazy_static::lazy_static;
6 use regex::bytes::Regex; 6 use regex::bytes::{NoExpand, Regex};
7 use std::collections::HashMap; 7 use std::collections::HashMap;
8 use std::fs::File; 8 use std::fs::File;
9 use std::io::Read; 9 use std::io::Read;
10 use std::vec::Vec; 10 use std::vec::Vec;
11 11
233 lines: &[u8], 233 lines: &[u8],
234 file_path: &[u8], 234 file_path: &[u8],
235 warn: bool, 235 warn: bool,
236 ) -> (Vec<PatternTuple>, Vec<WarningTuple>) { 236 ) -> (Vec<PatternTuple>, Vec<WarningTuple>) {
237 let comment_regex = Regex::new(r"((?:^|[^\\])(?:\\\\)*)#.*").unwrap(); 237 let comment_regex = Regex::new(r"((?:^|[^\\])(?:\\\\)*)#.*").unwrap();
238 let comment_escape_regex = Regex::new(r"\\#").unwrap();
238 let mut inputs: Vec<PatternTuple> = vec![]; 239 let mut inputs: Vec<PatternTuple> = vec![];
239 let mut warnings: Vec<WarningTuple> = vec![]; 240 let mut warnings: Vec<WarningTuple> = vec![];
240 241
241 let mut current_syntax = b"relre:".as_ref(); 242 let mut current_syntax = b"relre:".as_ref();
242 243
243 for (line_number, mut line) in lines.split(|c| *c == b'\n').enumerate() { 244 for (line_number, mut line) in lines.split(|c| *c == b'\n').enumerate() {
244 let line_number = line_number + 1; 245 let line_number = line_number + 1;
245 246
247 let line_buf;
246 if line.contains(&b'#') { 248 if line.contains(&b'#') {
247 if let Some(cap) = comment_regex.captures(line) { 249 if let Some(cap) = comment_regex.captures(line) {
248 line = &line[..cap.get(1).unwrap().end()] 250 line = &line[..cap.get(1).unwrap().end()]
249 } 251 }
250 let mut line = line.to_owned(); 252 line_buf = comment_escape_regex.replace_all(line, NoExpand(b"#"));
251 replace_slice(&mut line, br"\#", b"#"); 253 line = &line_buf;
252 } 254 }
253 255
254 let mut line = line.trim_end(); 256 let mut line = line.trim_end();
255 257
256 if line.is_empty() { 258 if line.is_empty() {