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

login
register
mail settings
Submitter Yuya Nishihara
Date July 21, 2019, 6:56 a.m.
Message ID <6b975c15fd687cfcb60c.1563692175@mimosa>
Download mbox | patch
Permalink /patch/40990/
State Accepted
Headers show

Comments

Yuya Nishihara - July 21, 2019, 6:56 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@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.
Augie Fackler - July 22, 2019, 6 p.m.
On Sun, Jul 21, 2019 at 03:56:15PM +0900, Yuya Nishihara wrote:
> # HG changeset patch
> # User Yuya Nishihara <yuya@tcha.org>
> # Date 1563687721 -32400
> #      Sun Jul 21 14:42:01 2019 +0900
> # Node ID 6b975c15fd687cfcb60c314ab7f3232d48313775
> # Parent  aa50f3ab3c8b106c16ac1d60772cd5a1eedb6d0a
> rust-filepatterns: unescape comment character property

queued, thanks

Patch

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();