Skip to content
Snippets Groups Projects
Commit 12addcc7956c authored by Yuya Nishihara's avatar Yuya Nishihara
Browse files

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.
parent 30f8e786868c
No related branches found
No related tags found
No related merge requests found
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;
......@@ -3,7 +3,7 @@
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,7 +244,8 @@
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()]
}
......@@ -246,9 +248,9 @@
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();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment