From 0d6c6b2c8a1309b0df0c5f97c6a2d3eb204d0aa4 Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Sun, 7 Jan 2024 20:37:27 +0100 Subject: [PATCH] Check MB_CUR_MAX() outside of loop This is more correct - we don't want to change how we encode this string in the middle of encoding it, and also happens to be a bit faster in my benchmarks because this is actually a function call according to valgrind. --- fish-rust/src/wcstringutil.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/fish-rust/src/wcstringutil.rs b/fish-rust/src/wcstringutil.rs index 536f503f6..7af38b4b9 100644 --- a/fish-rust/src/wcstringutil.rs +++ b/fish-rust/src/wcstringutil.rs @@ -265,6 +265,8 @@ 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]; + let is_singlebyte_locale = MB_CUR_MAX() == 1; + for c in input.chars() { // TODO: this doesn't seem sound. if c == INTERNAL_SEPARATOR { @@ -274,7 +276,7 @@ pub fn wcs2string_callback(input: &wstr, mut func: impl FnMut(&[u8]) -> bool) -> if !func(&converted[..1]) { return false; } - } else if MB_CUR_MAX() == 1 { + } else if is_singlebyte_locale { // single-byte locale (C/POSIX/ISO-8859) // If `c` contains a wide character we emit a question-mark. converted[0] = u8::try_from(u32::from(c)).unwrap_or(b'?');