diff --git a/fish-rust/src/tokenizer.rs b/fish-rust/src/tokenizer.rs index 71dd98a75..5584f6274 100644 --- a/fish-rust/src/tokenizer.rs +++ b/fish-rust/src/tokenizer.rs @@ -769,16 +769,9 @@ pub fn quote_end(s: &wstr, mut pos: usize, quote: char) -> Option { loop { pos += 1; - if pos == s.len() { - return None; - } - - let c = s.char_at(pos); + let c = s.try_char_at(pos)?; if c == '\\' { pos += 1; - if pos == s.len() { - return None; - } } else if c == quote || // Command substitutions also end a double quoted string. This is how we // support command substitutions inside double quotes. diff --git a/fish-rust/src/wchar_ext.rs b/fish-rust/src/wchar_ext.rs index e4520b0bd..15b4456f3 100644 --- a/fish-rust/src/wchar_ext.rs +++ b/fish-rust/src/wchar_ext.rs @@ -222,6 +222,18 @@ fn char_at(&self, index: usize) -> char { } } + /// Return the char at an index. + /// If the index is equal to the length, return '\0'. + /// If the index exceeds the length, return None. + fn try_char_at(&self, index: usize) -> Option { + let chars = self.as_char_slice(); + match index { + _ if index == chars.len() => Some('\0'), + _ if index > chars.len() => None, + _ => Some(chars[index]), + } + } + /// \return an iterator over substrings, split by a given char. /// The split char is not included in the substrings. fn split(&self, c: char) -> WStrCharSplitIter { diff --git a/tests/checks/indent.fish b/tests/checks/indent.fish index 6c06c3e98..d11ae21d7 100644 --- a/tests/checks/indent.fish +++ b/tests/checks/indent.fish @@ -432,3 +432,6 @@ echo 'begin echo 'multiline-\\ -word' | $fish_indent --check echo $status #CHECK: 0 + +echo 'PATH={$PATH[echo " "' | $fish_indent --ansi +# CHECK: PATH={$PATH[echo " "