From 4d6f25bd0f6eb56c8b0032034792672f58d644f6 Mon Sep 17 00:00:00 2001 From: Enzo Cioppettini Date: Sun, 5 Sep 2021 14:47:29 -0300 Subject: [PATCH] fix: unescaping with non-ascii chars str::find returns a byte index, but then this is used as a char index, which does not work if the string has multi-byte codepoints. --- src/lib/expansion/words/mod.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/lib/expansion/words/mod.rs b/src/lib/expansion/words/mod.rs index 77d2c143..2e5adfcf 100644 --- a/src/lib/expansion/words/mod.rs +++ b/src/lib/expansion/words/mod.rs @@ -30,8 +30,11 @@ pub fn unescape_characters<'a>(input: &'a str, characters: &[char]) -> Cow<'a, s let mut last_idx: usize = 0; let mut input: Cow<'_, str> = input.into(); - while let Some(idx) = input[last_idx..].find('\\') { - if let Some(next_character) = input.chars().nth(last_idx + idx + 1) { + let escape_char = '\\'; + while let Some(idx) = input[last_idx..].find(escape_char) { + if let Some(next_character) = + input.get(last_idx + idx + escape_char.len_utf8()..).and_then(|s| s.chars().nth(0)) + { if characters.contains(&next_character) { input.to_mut().remove(last_idx + idx); } -- GitLab