From fe63775ec5c61d323c8247cde60fb491ea79a537 Mon Sep 17 00:00:00 2001 From: Mahmoud Al-Qudsi Date: Tue, 16 Jul 2024 17:05:11 -0500 Subject: [PATCH] string: Also escape new lines with --style=regex This isn't *required* in the PCRE2 spec but it greatly increases the utility of escaped regex strings at the commandline. --- src/common.rs | 6 +++++- tests/checks/string.fish | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/common.rs b/src/common.rs index 31ca3cddc..ad10e3844 100644 --- a/src/common.rs +++ b/src/common.rs @@ -451,9 +451,13 @@ fn escape_string_var(input: &wstr) -> WString { /// \param in is the raw string to be searched for literally when substituted in a PCRE2 expression. fn escape_string_pcre2(input: &wstr) -> WString { let mut out = WString::new(); - out.reserve(input.len()); + out.reserve(input.len() + input.len() / 2); for c in input.chars() { + if c == '\n' { + out.push_str("\\n"); + continue; + } if [ '.', '^', '$', '*', '+', '(', ')', '?', '[', '{', '}', '\\', '|', // these two only *need* to be escaped within a character class, and technically it diff --git a/tests/checks/string.fish b/tests/checks/string.fish index 2519c68c3..c3494e582 100644 --- a/tests/checks/string.fish +++ b/tests/checks/string.fish @@ -309,9 +309,12 @@ string escape --style=var δΈ­ | string unescape --style=var string escape --style=regex ".ext" string escape --style=regex "bonjour, amigo" string escape --style=regex "^this is a literal string" +string escape --style=regex "hello +world" # CHECK: \.ext # CHECK: bonjour, amigo # CHECK: \^this is a literal string +# CHECK: hello\nworld ### Verify that we can correctly unescape the same strings # we tested escaping above.