mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-26 12:53:13 +00:00
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.
This commit is contained in:
parent
6020fc497a
commit
fe63775ec5
2 changed files with 8 additions and 1 deletions
|
@ -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.
|
/// \param in is the raw string to be searched for literally when substituted in a PCRE2 expression.
|
||||||
fn escape_string_pcre2(input: &wstr) -> WString {
|
fn escape_string_pcre2(input: &wstr) -> WString {
|
||||||
let mut out = WString::new();
|
let mut out = WString::new();
|
||||||
out.reserve(input.len());
|
out.reserve(input.len() + input.len() / 2);
|
||||||
|
|
||||||
for c in input.chars() {
|
for c in input.chars() {
|
||||||
|
if c == '\n' {
|
||||||
|
out.push_str("\\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if [
|
if [
|
||||||
'.', '^', '$', '*', '+', '(', ')', '?', '[', '{', '}', '\\', '|',
|
'.', '^', '$', '*', '+', '(', ')', '?', '[', '{', '}', '\\', '|',
|
||||||
// these two only *need* to be escaped within a character class, and technically it
|
// these two only *need* to be escaped within a character class, and technically it
|
||||||
|
|
|
@ -309,9 +309,12 @@ string escape --style=var 中 | string unescape --style=var
|
||||||
string escape --style=regex ".ext"
|
string escape --style=regex ".ext"
|
||||||
string escape --style=regex "bonjour, amigo"
|
string escape --style=regex "bonjour, amigo"
|
||||||
string escape --style=regex "^this is a literal string"
|
string escape --style=regex "^this is a literal string"
|
||||||
|
string escape --style=regex "hello
|
||||||
|
world"
|
||||||
# CHECK: \.ext
|
# CHECK: \.ext
|
||||||
# CHECK: bonjour, amigo
|
# CHECK: bonjour, amigo
|
||||||
# CHECK: \^this is a literal string
|
# CHECK: \^this is a literal string
|
||||||
|
# CHECK: hello\nworld
|
||||||
|
|
||||||
### Verify that we can correctly unescape the same strings
|
### Verify that we can correctly unescape the same strings
|
||||||
# we tested escaping above.
|
# we tested escaping above.
|
||||||
|
|
Loading…
Reference in a new issue