From ad5c86604b366c2dd5d9efbe57787a1dea8c2139 Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sat, 1 Apr 2023 13:00:58 +0200 Subject: [PATCH] Simplify string narrowing logic --- fish-rust/src/wcstringutil.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/fish-rust/src/wcstringutil.rs b/fish-rust/src/wcstringutil.rs index 384cc7d40..05b9b0ab1 100644 --- a/fish-rust/src/wcstringutil.rs +++ b/fish-rust/src/wcstringutil.rs @@ -14,7 +14,7 @@ pub fn wcs2string_callback(input: &wstr, mut func: impl FnMut(&[u8]) -> bool) -> let mut state = zero_mbstate(); let mut converted = [0_u8; AT_LEAST_MB_LEN_MAX]; - for mut c in input.chars() { + for c in input.chars() { // TODO: this doesn't seem sound. if c == INTERNAL_SEPARATOR { // do nothing @@ -26,11 +26,7 @@ pub fn wcs2string_callback(input: &wstr, mut func: impl FnMut(&[u8]) -> bool) -> } else if MB_CUR_MAX() == 1 { // single-byte locale (C/POSIX/ISO-8859) // If `c` contains a wide character we emit a question-mark. - if u32::from(c) & !0xFF != 0 { - c = '?'; - } - - converted[0] = c as u8; + converted[0] = u8::try_from(u32::from(c)).unwrap_or(b'?'); if !func(&converted[..1]) { return false; }