diff --git a/fish-rust/src/common.rs b/fish-rust/src/common.rs index 7afcd4c9a..6f6071e15 100644 --- a/fish-rust/src/common.rs +++ b/fish-rust/src/common.rs @@ -687,10 +687,10 @@ fn unescape_string_internal(input: &wstr, flags: UnescapeFlags) -> Option Option { - let mut result: Vec = vec![]; + let mut result: Vec = Vec::with_capacity(input.len()); let mut i = 0; while i < input.len() { let c = input.char_at(i); @@ -705,12 +705,9 @@ fn unescape_string_url(input: &wstr) -> Option { result.push(b'%'); i += 1; } else { - let c2 = input.char_at(i + 2); - if c2 == '\0' { - return None; // string ended prematurely - } let d1 = c1.to_digit(16)?; - let d2 = c2.to_digit(16)?; + let c2 = input.char_at(i + 2); + let d2 = c2.to_digit(16)?; // also fails if '\0' i.e. premature end result.push((16 * d1 + d2) as u8); i += 2; } @@ -723,10 +720,10 @@ fn unescape_string_url(input: &wstr) -> Option { Some(str2wcstring(&result)) } -/// Reverse the effects of `escape_string_var()`. By definition the string has consist of just ASCII -/// chars. +/// Reverse the effects of `escape_string_var()`. By definition the string should consist of just +/// ASCII chars. fn unescape_string_var(input: &wstr) -> Option { - let mut result: Vec = vec![]; + let mut result: Vec = Vec::with_capacity(input.len()); let mut prev_was_hex_encoded = false; let mut i = 0; while i < input.len() { @@ -741,17 +738,13 @@ fn unescape_string_var(input: &wstr) -> Option { break; } return None; // found unexpected escape char at end of string - } - if c1 == '_' { + } else if c1 == '_' { result.push(b'_'); i += 1; } else if ('0'..='9').contains(&c1) || ('A'..='F').contains(&c1) { + let d1 = c1.to_digit(16)?; let c2 = input.char_at(i + 2); - if c2 == '\0' { - return None; // string ended prematurely - } - let d1 = convert_hex_digit(c1)?; - let d2 = convert_hex_digit(c2)?; + let d2 = c2.to_digit(16)?; // also fails if '\0' i.e. premature end result.push((16 * d1 + d2) as u8); i += 2; prev_was_hex_encoded = true; @@ -946,18 +939,6 @@ pub fn read_unquoted_escape( Some(in_pos) } -/// This is a specialization of `char::to_digit()` that only handles base 16 and only uppercase. -fn convert_hex_digit(d: char) -> Option { - let val = if ('0'..='9').contains(&d) { - u32::from(d) - u32::from('0') - } else if ('A'..='Z').contains(&d) { - 10 + u32::from(d) - u32::from('A') - } else { - return None; - }; - Some(val) -} - pub const fn char_offset(base: char, offset: u32) -> char { match char::from_u32(base as u32 + offset) { Some(c) => c,