From c48c0bb226c23e16db62c7e8245d302d4bb14ecd Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Mon, 3 Jul 2023 16:06:21 -0700 Subject: [PATCH] Replace sprintf call with write! This reduces the time for the Rust tests from a few minutes to ~40 seconds. Also fix some bogus comments which were ported from C++. --- fish-rust/src/common.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/fish-rust/src/common.rs b/fish-rust/src/common.rs index 816b212c0..1136dc55e 100644 --- a/fish-rust/src/common.rs +++ b/fish-rust/src/common.rs @@ -25,6 +25,7 @@ use num_traits::ToPrimitive; use once_cell::sync::Lazy; use std::env; use std::ffi::{CStr, CString, OsString}; +use std::fmt::Write; use std::mem; use std::ops::{Deref, DerefMut}; use std::os::fd::{AsRawFd, RawFd}; @@ -343,14 +344,13 @@ fn escape_string_url(input: &wstr) -> WString { continue; } } - // All other chars need to have their UTF-8 representation encoded in hex. - sprintf!(=> &mut out, "%%%02X"L, byte); + // All other chars need to have their narrow representation encoded in hex. + write!(out, "%{:02X}", byte).expect("Writes to strings cannot fail"); } out } /// Escape a string in a fashion suitable for using as a fish var name. Store the result in out_str. -#[widestrs] fn escape_string_var(input: &wstr) -> WString { let mut prev_was_hex_encoded = false; let narrow = wcs2string(input); @@ -371,8 +371,8 @@ fn escape_string_var(input: &wstr) -> WString { out.push_str("__"); prev_was_hex_encoded = false; } else { - // All other chars need to have their UTF-8 representation encoded in hex. - sprintf!(=> &mut out, "_%02X"L, c); + // All other chars need to have their narrow representation encoded in hex. + write!(out, "_{:02X}", c).expect("Writes to strings cannot fail"); prev_was_hex_encoded = true; } }