Mercurial > hg
changeset 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 | e386b5f4f836 ab1900323b1d |
files | rust/hg-core/src/filepatterns.rs |
diffstat | 1 files changed, 6 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/rust/hg-core/src/filepatterns.rs Sun Jul 21 13:00:54 2019 +0900 +++ b/rust/hg-core/src/filepatterns.rs Sun Jul 21 14:42:01 2019 +0900 @@ -1,9 +1,9 @@ use crate::{ - utils::{files::get_path_from_bytes, replace_slice, SliceExt}, + utils::{files::get_path_from_bytes, SliceExt}, LineNumber, PatternError, PatternFileError, }; use lazy_static::lazy_static; -use regex::bytes::Regex; +use regex::bytes::{NoExpand, Regex}; use std::collections::HashMap; use std::fs::File; use std::io::Read; @@ -235,6 +235,7 @@ warn: bool, ) -> (Vec<PatternTuple>, Vec<WarningTuple>) { let comment_regex = Regex::new(r"((?:^|[^\\])(?:\\\\)*)#.*").unwrap(); + let comment_escape_regex = Regex::new(r"\\#").unwrap(); let mut inputs: Vec<PatternTuple> = vec![]; let mut warnings: Vec<WarningTuple> = vec![]; @@ -243,12 +244,13 @@ for (line_number, mut line) in lines.split(|c| *c == b'\n').enumerate() { let line_number = line_number + 1; + let line_buf; if line.contains(&b'#') { if let Some(cap) = comment_regex.captures(line) { line = &line[..cap.get(1).unwrap().end()] } - let mut line = line.to_owned(); - replace_slice(&mut line, br"\#", b"#"); + line_buf = comment_escape_regex.replace_all(line, NoExpand(b"#")); + line = &line_buf; } let mut line = line.trim_end();