[PATCH 6 of 6] rust-filepatterns: unescape comment character property

Yuya Nishihara yuya at tcha.org
Sun Jul 21 02:56:15 EDT 2019


# HG changeset patch
# User Yuya Nishihara <yuya at tcha.org>
# Date 1563687721 -32400
#      Sun Jul 21 14:42:01 2019 +0900
# Node ID 6b975c15fd687cfcb60c314ab7f3232d48313775
# Parent  aa50f3ab3c8b106c16ac1d60772cd5a1eedb6d0a
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.

diff --git a/rust/hg-core/src/filepatterns.rs b/rust/hg-core/src/filepatterns.rs
--- a/rust/hg-core/src/filepatterns.rs
+++ b/rust/hg-core/src/filepatterns.rs
@@ -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 @@ pub fn parse_pattern_file_contents(
     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 @@ pub fn parse_pattern_file_contents(
     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();


More information about the Mercurial-devel mailing list