ptx: escape regular expression character class special chars

This commit is contained in:
Michael Kefeder 2022-04-30 10:01:11 +02:00
parent 4889128ede
commit 83a64f4afe
3 changed files with 9 additions and 2 deletions

View file

@ -31,6 +31,8 @@ const ABOUT: &str = "\
Mandatory arguments to long options are mandatory for short options too.\n\
With no FILE, or when FILE is -, read standard input. Default is '-F /'.";
const REGEX_CHARCLASS: &str = "^-]\\";
#[derive(Debug)]
enum OutFormat {
Dumb,
@ -166,7 +168,11 @@ impl WordFilter {
break_set
.unwrap()
.into_iter()
.map(|c| c.to_string())
.map(|c| if REGEX_CHARCLASS.contains(c) {
format!("\\{}", c)
} else {
c.to_string()
})
.collect::<Vec<String>>()
.join("")
)

View file

@ -1 +1 @@
abc_e
abc_e^-]\

View file

@ -29,6 +29,7 @@
.xx "" "let's check special characte" "rs:" ""
.xx "" """quote" "s"", for roff" ""
.xx "" "oh, and back\sla" "sh" ""
.xx "" "oh, and back\" "slash" ""
.xx "" "and" "some other like %a, b#, c$c" ""
.xx "" "let's check" "special characters:" ""
.xx "" "let's check special charac" "ters:" ""