diff --git a/src/common.rs b/src/common.rs index d8637c826..2eabd722b 100644 --- a/src/common.rs +++ b/src/common.rs @@ -204,7 +204,7 @@ fn escape_string_script(input: &wstr, flags: EscapeFlags) -> WString { let mut out = WString::new(); - for c in input.chars() { + for (i, c) in input.chars().enumerate() { if let Some(val) = decode_byte_from_char(c) { out += "\\X"; @@ -308,7 +308,12 @@ fn escape_string_script(input: &wstr, flags: EscapeFlags) -> WString { if c == '$' { dollars += 1; } - let char_is_normal = (c == '~' && no_tilde) || (c == '?' && no_qmark); + let char_is_normal = (c == '~' && no_tilde) + || (c == '?' && no_qmark) + || (c == '#' + && i.checked_sub(1) + .map(|previ| input.char_at(previ).is_alphanumeric()) + .unwrap_or_default()); if !char_is_normal { need_escape = true; if escape_printables { diff --git a/tests/checks/string.fish b/tests/checks/string.fish index e7bc81ad7..9494264f1 100644 --- a/tests/checks/string.fish +++ b/tests/checks/string.fish @@ -305,7 +305,10 @@ string escape --style=script 'a b#c"\'d' # CHECK: 'a b#c"\'d' string escape --no-quoted --style=script 'a b#c"\'d' -# CHECK: a\ b\#c\"\'d +# CHECK: a\ b#c\"\'d + +string escape --no-quoted --style=script 'a #b' +# CHECK: a\ \#b string escape --style=url 'a b#c"\'d' # CHECK: a%20b%23c%22%27d