refactor: use bytes instead of string

Some string handling functions deal with `Vec<u8>` or `&[u8]`, which
have been referred to as `string` or `str` in the function names. This
is confusing, since they don't deal with Rust's `String` type. Use
`bytes` in the function names instead to reduce confusion.

Closes #11969
This commit is contained in:
Daniel Rainer
2025-10-18 18:28:27 +02:00
committed by Johannes Altmanninger
parent 1fe6b28877
commit 61ee695e56
37 changed files with 191 additions and 186 deletions

View File

@@ -2,7 +2,7 @@
use crate::FLOGF;
#[cfg(feature = "embed-data")]
use crate::common::wcs2string;
use crate::common::wcs2bytes;
use crate::common::{ScopeGuard, escape};
use crate::env::Environment;
use crate::io::IoChain;
@@ -150,11 +150,11 @@ pub fn perform_autoload(path: &AutoloadPath, parser: &Parser) {
}
#[cfg(feature = "embed-data")]
AutoloadPath::Embedded(name) => {
use crate::common::str2wcstring;
use crate::common::bytes2wcstring;
use std::sync::Arc;
FLOGF!(autoload, "Loading embedded: %s", name);
let emfile = Asset::get(name).expect("Embedded file not found");
let src = str2wcstring(&emfile.data);
let src = bytes2wcstring(&emfile.data);
let mut widename = L!("embedded:").to_owned();
widename.push_str(name);
let ret = parser.eval_file_wstr(src, Arc::new(widename), &IoChain::new(), None);
@@ -474,7 +474,7 @@ fn locate_asset(&self, cmd: &wstr, asset_dir: AssetDir) -> Option<AutoloadableFi
if cfg!(test) {
return None;
}
let narrow = wcs2string(cmd);
let narrow = wcs2bytes(cmd);
let cmdstr = std::str::from_utf8(&narrow).ok()?;
let p = match asset_dir {
AssetDir::Functions => "functions/".to_owned() + cmdstr + ".fish",

View File

@@ -31,8 +31,8 @@
},
},
common::{
PACKAGE_NAME, PROFILING_ACTIVE, PROGRAM_NAME, escape, save_term_foreground_process_group,
str2wcstring, wcs2string,
PACKAGE_NAME, PROFILING_ACTIVE, PROGRAM_NAME, bytes2wcstring, escape,
save_term_foreground_process_group, wcs2bytes,
},
env::{
EnvMode, Statuses,
@@ -176,7 +176,7 @@ fn read_init(parser: &Parser, paths: &ConfigPaths) {
#[cfg(feature = "embed-data")]
{
let emfile = Asset::get("config.fish").expect("Embedded file not found");
let src = str2wcstring(&emfile.data);
let src = bytes2wcstring(&emfile.data);
parser.libdata_mut().within_fish_init = true;
let fname: Arc<WString> = Arc::new(L!("embedded:config.fish").into());
let ret = parser.eval_file_wstr(src, fname, &IoChain::new(), None);
@@ -187,7 +187,7 @@ fn read_init(parser: &Parser, paths: &ConfigPaths) {
}
#[cfg(not(feature = "embed-data"))]
{
let datapath = str2wcstring(paths.data.as_os_str().as_bytes());
let datapath = bytes2wcstring(paths.data.as_os_str().as_bytes());
if !source_config_in_directory(parser, &datapath) {
// If we cannot read share/config.fish, our internal configuration,
// something is wrong.
@@ -204,7 +204,10 @@ fn read_init(parser: &Parser, paths: &ConfigPaths) {
}
}
source_config_in_directory(parser, &str2wcstring(paths.sysconf.as_os_str().as_bytes()));
source_config_in_directory(
parser,
&bytes2wcstring(paths.sysconf.as_os_str().as_bytes()),
);
// We need to get the configuration directory before we can source the user configuration file.
// If path_get_config returns false then we have no configuration directory and no custom config
@@ -217,7 +220,7 @@ fn read_init(parser: &Parser, paths: &ConfigPaths) {
fn run_command_list(parser: &Parser, cmds: &[OsString]) -> Result<(), libc::c_int> {
let mut retval = Ok(());
for cmd in cmds {
let cmd_wcs = str2wcstring(cmd.as_bytes());
let cmd_wcs = bytes2wcstring(cmd.as_bytes());
let mut errors = ParseErrorList::new();
let ast = ast::parse(&cmd_wcs, ParseTreeFlags::empty(), Some(&mut errors));
@@ -279,10 +282,10 @@ fn fish_parse_opt(args: &mut [WString], opts: &mut FishCmdOpts) -> ControlFlow<i
match c {
'c' => opts
.batch_cmds
.push(OsString::from_vec(wcs2string(w.woptarg.unwrap()))),
.push(OsString::from_vec(wcs2bytes(w.woptarg.unwrap()))),
'C' => opts
.postconfig_cmds
.push(OsString::from_vec(wcs2string(w.woptarg.unwrap()))),
.push(OsString::from_vec(wcs2bytes(w.woptarg.unwrap()))),
'd' => {
activate_flog_categories_by_pattern(w.woptarg.unwrap());
for cat in flog::categories::all_categories() {
@@ -291,7 +294,7 @@ fn fish_parse_opt(args: &mut [WString], opts: &mut FishCmdOpts) -> ControlFlow<i
}
}
}
'o' => opts.debug_output = Some(OsString::from_vec(wcs2string(w.woptarg.unwrap()))),
'o' => opts.debug_output = Some(OsString::from_vec(wcs2bytes(w.woptarg.unwrap()))),
'f' => opts.features = w.woptarg.unwrap().to_owned(),
'h' => opts.batch_cmds.push("__fish_print_help fish".into()),
'i' => opts.is_interactive_session = true,
@@ -321,11 +324,11 @@ fn fish_parse_opt(args: &mut [WString], opts: &mut FishCmdOpts) -> ControlFlow<i
}
// "--profile" - this does not activate profiling right away,
// rather it's done after startup is finished.
'p' => opts.profile_output = Some(OsString::from_vec(wcs2string(w.woptarg.unwrap()))),
'p' => opts.profile_output = Some(OsString::from_vec(wcs2bytes(w.woptarg.unwrap()))),
PROFILE_STARTUP_ARG => {
// With "--profile-startup" we immediately turn profiling on.
opts.profile_startup_output =
Some(OsString::from_vec(wcs2string(w.woptarg.unwrap())));
Some(OsString::from_vec(wcs2bytes(w.woptarg.unwrap())));
PROFILING_ACTIVE.store(true);
}
'P' => opts.enable_private_mode = true,
@@ -419,12 +422,12 @@ fn throwing_main() -> i32 {
// Enable debug categories set in FISH_DEBUG.
// This is in *addition* to the ones given via --debug.
if let Some(debug_categories) = env::var_os("FISH_DEBUG") {
let s = str2wcstring(debug_categories.as_bytes());
let s = bytes2wcstring(debug_categories.as_bytes());
activate_flog_categories_by_pattern(&s);
}
let mut args: Vec<WString> = env::args_os()
.map(|osstr| str2wcstring(osstr.as_bytes()))
.map(|osstr| bytes2wcstring(osstr.as_bytes()))
.collect();
let mut opts = FishCmdOpts::default();
let mut my_optind = match fish_parse_opt(&mut args, &mut opts) {
@@ -592,7 +595,7 @@ fn throwing_main() -> i32 {
}
res = reader_read(parser, libc::STDIN_FILENO, &IoChain::new());
} else {
let n = wcs2string(&args[my_optind]);
let n = wcs2bytes(&args[my_optind]);
let path = OsStr::from_bytes(&n);
my_optind += 1;
// Rust sets cloexec by default, see above

View File

@@ -2,7 +2,7 @@
use super::prelude::*;
use crate::common::{
EscapeFlags, EscapeStringStyle, escape, escape_string, str2wcstring, valid_var_name,
EscapeFlags, EscapeStringStyle, bytes2wcstring, escape, escape_string, valid_var_name,
};
use crate::highlight::{colorize, highlight_shell};
use crate::input::{InputMappingSet, KeyNameStyle, input_function_get_names, input_mappings};
@@ -157,7 +157,7 @@ fn list_one(
let mut colors = Vec::new();
highlight_shell(&out, &mut colors, &parser.context(), false, None);
let colored = colorize(&out, &colors, parser.vars());
streams.out.append(str2wcstring(&colored));
streams.out.append(bytes2wcstring(&colored));
} else {
streams.out.append(out);
}

View File

@@ -12,7 +12,7 @@
use crate::reader::{commandline_get_state, completion_apply_to_command_line};
use crate::wcstringutil::string_suffixes_string;
use crate::{
common::str2wcstring,
common::bytes2wcstring,
complete::{
CompleteFlags, CompleteOptionType, CompletionMode, complete_add, complete_print,
complete_remove, complete_remove_all,
@@ -207,7 +207,7 @@ fn builtin_complete_print(cmd: &wstr, streams: &mut IoStreams, parser: &Parser)
highlight_shell(&repr, &mut colors, &parser.context(), false, None);
streams
.out
.append(str2wcstring(&colorize(&repr, &colors, parser.vars())));
.append(bytes2wcstring(&colorize(&repr, &colors, parser.vars())));
} else {
streams.out.append(repr);
}

View File

@@ -17,7 +17,7 @@
use super::prelude::*;
use crate::ast::{self, Ast, Kind, Leaf, Node, NodeVisitor, SourceRangeList, Traversal};
use crate::common::{
PROGRAM_NAME, UnescapeFlags, UnescapeStringStyle, str2wcstring, unescape_string, wcs2string,
PROGRAM_NAME, UnescapeFlags, UnescapeStringStyle, bytes2wcstring, unescape_string, wcs2bytes,
};
use crate::env::EnvStack;
use crate::env::env_init;
@@ -916,7 +916,7 @@ fn throwing_main() -> i32 {
}
let args: Vec<WString> = std::env::args_os()
.map(|osstr| str2wcstring(osstr.as_bytes()))
.map(|osstr| bytes2wcstring(osstr.as_bytes()))
.collect();
do_indent(&mut streams, args).builtin_status_code()
}
@@ -1016,10 +1016,10 @@ enum OutputType {
}
}
std::mem::forget(fd);
src = str2wcstring(&buf);
src = bytes2wcstring(&buf);
} else {
let arg = args[i];
match fs::File::open(OsStr::from_bytes(&wcs2string(arg))) {
match fs::File::open(OsStr::from_bytes(&wcs2bytes(arg))) {
Ok(file) => {
match read_file(file) {
Ok(s) => src = s,
@@ -1040,7 +1040,7 @@ enum OutputType {
if output_type == OutputType::PygmentsCsv {
let output = make_pygments_csv(&src);
streams.out.append(str2wcstring(&output));
streams.out.append(bytes2wcstring(&output));
i += 1;
continue;
}
@@ -1104,9 +1104,9 @@ enum OutputType {
}
OutputType::File => {
if output_wtext != src {
match fs::File::create(OsStr::from_bytes(&wcs2string(output_location))) {
match fs::File::create(OsStr::from_bytes(&wcs2bytes(output_location))) {
Ok(mut file) => {
let _ = file.write_all(&wcs2string(&output_wtext));
let _ = file.write_all(&wcs2bytes(&output_wtext));
}
Err(err) => {
streams.err.appendln(wgettext_fmt!(
@@ -1138,7 +1138,7 @@ enum OutputType {
}
}
streams.out.append(str2wcstring(&colored_output));
streams.out.append(bytes2wcstring(&colored_output));
i += 1;
}
if retval == 0 {
@@ -1154,7 +1154,7 @@ enum OutputType {
fn read_file(mut f: impl Read) -> Result<WString, ()> {
let mut buf = vec![];
f.read_to_end(&mut buf).map_err(|_| ())?;
Ok(str2wcstring(&buf))
Ok(bytes2wcstring(&buf))
}
fn highlight_role_to_string(role: HighlightRole) -> &'static wstr {
@@ -1316,9 +1316,9 @@ fn html_colorize(text: &wstr, colors: &[HighlightSpec]) -> Vec<u8> {
}
}
html.push_str("</span></code></pre>");
wcs2string(&html)
wcs2bytes(&html)
}
fn no_colorize(text: &wstr) -> Vec<u8> {
wcs2string(text)
wcs2bytes(text)
}

View File

@@ -15,7 +15,7 @@
use crate::future::IsSomeAnd;
use crate::{
builtins::shared::BUILTIN_ERR_UNKNOWN,
common::{PROGRAM_NAME, shell_modes, str2wcstring},
common::{PROGRAM_NAME, bytes2wcstring, shell_modes},
env::{EnvStack, Environment, env_init},
future_feature_flags,
input_common::{
@@ -299,7 +299,7 @@ fn throwing_main() -> i32 {
let mut verbose = false;
let args: Vec<WString> = std::env::args_os()
.map(|osstr| str2wcstring(osstr.as_bytes()))
.map(|osstr| bytes2wcstring(osstr.as_bytes()))
.collect();
if let ControlFlow::Break(s) =
parse_flags(&mut streams, args, &mut continuous_mode, &mut verbose)

View File

@@ -1,7 +1,7 @@
use super::prelude::*;
use crate::common::bytes2wcstring;
use crate::common::escape_string;
use crate::common::reformat_for_screen;
use crate::common::str2wcstring;
use crate::common::valid_func_name;
use crate::common::{EscapeFlags, EscapeStringStyle};
use crate::event::{self};
@@ -437,7 +437,7 @@ pub fn functions(parser: &Parser, streams: &mut IoStreams, args: &mut [&wstr]) -
highlight_shell(&def, &mut colors, &parser.context(), false, None);
streams
.out
.append(str2wcstring(&colorize(&def, &colors, parser.vars())));
.append(bytes2wcstring(&colorize(&def, &colors, parser.vars())));
} else {
streams.out.append(def);
}

View File

@@ -2,9 +2,9 @@
use super::prelude::*;
use crate::common::UnescapeStringStyle;
use crate::common::bytes2wcstring;
use crate::common::escape;
use crate::common::read_blocked;
use crate::common::str2wcstring;
use crate::common::unescape_string;
use crate::common::valid_var_name;
use crate::env::EnvMode;
@@ -366,7 +366,7 @@ fn read_in_chunks(fd: RawFd, buff: &mut WString, split_null: bool, do_seek: bool
}
}
*buff = str2wcstring(&narrow_buff);
*buff = bytes2wcstring(&narrow_buff);
if buff.is_empty() && eof {
exit_res = Err(STATUS_CMD_ERROR);
}

View File

@@ -2,7 +2,7 @@
use super::prelude::*;
use crate::color::Color;
use crate::common::str2wcstring;
use crate::common::bytes2wcstring;
use crate::screen::{is_dumb, only_grayscale};
use crate::terminal::{Outputter, use_terminfo};
use crate::text_face::{
@@ -47,7 +47,7 @@ fn print_colors(
} // conveniently, 'normal' is always the last color so we don't need to reset here
let contents = outp.contents();
streams.out.append(str2wcstring(contents));
streams.out.append(bytes2wcstring(contents));
}
/// set_color builtin.
@@ -147,7 +147,7 @@ pub fn set_color(parser: &Parser, streams: &mut IoStreams, argv: &mut [&wstr]) -
// Output the collected string.
let contents = outp.contents();
streams.out.append(str2wcstring(contents));
streams.out.append(bytes2wcstring(contents));
Ok(SUCCESS)
}

View File

@@ -1,5 +1,5 @@
use super::prelude::*;
use crate::common::{Named, escape, get_by_sorted_name, str2wcstring};
use crate::common::{Named, bytes2wcstring, escape, get_by_sorted_name};
use crate::io::OutputStream;
use crate::parse_constants::UNKNOWN_BUILTIN_ERR_MSG;
use crate::parse_util::parse_util_argument_is_help;
@@ -895,7 +895,7 @@ fn get_arg_stdin(&mut self) -> Option<(Cow<'args, wstr>, bool)> {
_ => unreachable!(),
};
let parsed = str2wcstring(&self.buffer[..end]);
let parsed = bytes2wcstring(&self.buffer[..end]);
let retval = Some((Cow::Owned(parsed), want_newline));
self.buffer.clear();

View File

@@ -1,7 +1,7 @@
use std::os::unix::prelude::*;
use super::prelude::*;
use crate::common::{PROGRAM_NAME, get_executable_path, str2wcstring};
use crate::common::{PROGRAM_NAME, bytes2wcstring, get_executable_path};
use crate::future_feature_flags::{self as features, feature_test};
use crate::proc::{
JobControl, get_job_control_mode, get_login, is_interactive_session, set_job_control_mode,
@@ -474,13 +474,13 @@ pub fn status(parser: &Parser, streams: &mut IoStreams, args: &mut [&wstr]) -> B
}
#[cfg(feature = "embed-data")]
{
let arg = crate::common::wcs2string(args[0]);
let arg = crate::common::wcs2bytes(args[0]);
let arg = std::str::from_utf8(&arg).unwrap();
let Some(emfile) = crate::autoload::Asset::get(arg).or_else(|| Docs::get(arg))
else {
return Err(STATUS_CMD_ERROR);
};
let src = str2wcstring(&emfile.data);
let src = bytes2wcstring(&emfile.data);
streams.out.append(src);
return Ok(SUCCESS);
}
@@ -508,11 +508,11 @@ pub fn status(parser: &Parser, streams: &mut IoStreams, args: &mut [&wstr]) -> B
use crate::util::wcsfilecmp_glob;
let mut paths = vec![];
let arg = crate::common::wcs2string(args.get(0).unwrap_or(&L!("")));
let arg = crate::common::wcs2bytes(args.get(0).unwrap_or(&L!("")));
let arg = std::str::from_utf8(&arg).unwrap();
for path in crate::autoload::Asset::iter().chain(Docs::iter()) {
if arg.is_empty() || path.starts_with(arg) {
paths.push(str2wcstring(path.as_bytes()));
paths.push(bytes2wcstring(path.as_bytes()));
}
}
@@ -575,16 +575,16 @@ pub fn status(parser: &Parser, streams: &mut IoStreams, args: &mut [&wstr]) -> B
}
match s {
STATUS_BUILD_INFO | STATUS_BUILDINFO => {
let version = str2wcstring(crate::BUILD_VERSION.as_bytes());
let target = str2wcstring(env!("BUILD_TARGET_TRIPLE").as_bytes());
let host = str2wcstring(env!("BUILD_HOST_TRIPLE").as_bytes());
let profile = str2wcstring(env!("BUILD_PROFILE").as_bytes());
let version = bytes2wcstring(crate::BUILD_VERSION.as_bytes());
let target = bytes2wcstring(env!("BUILD_TARGET_TRIPLE").as_bytes());
let host = bytes2wcstring(env!("BUILD_HOST_TRIPLE").as_bytes());
let profile = bytes2wcstring(env!("BUILD_PROFILE").as_bytes());
streams.out.append(L!("Build system: "));
let buildsystem = match option_env!("CMAKE") {
Some("1") => "CMake",
_ => "Cargo",
};
streams.out.appendln(str2wcstring(buildsystem.as_bytes()));
streams.out.appendln(bytes2wcstring(buildsystem.as_bytes()));
streams.out.append(L!("Version: "));
streams.out.appendln(version);
if target == host {
@@ -609,7 +609,7 @@ pub fn status(parser: &Parser, streams: &mut IoStreams, args: &mut [&wstr]) -> B
];
streams
.out
.appendln(str2wcstring(features.join(" ").as_bytes()));
.appendln(bytes2wcstring(features.join(" ").as_bytes()));
streams.out.appendln("");
return Ok(SUCCESS);
}
@@ -727,7 +727,7 @@ pub fn status(parser: &Parser, streams: &mut IoStreams, args: &mut [&wstr]) -> B
));
}
if path.is_absolute() {
let path = str2wcstring(path.as_os_str().as_bytes());
let path = bytes2wcstring(path.as_os_str().as_bytes());
// This is an absolute path, we can canonicalize it
let real = match wrealpath(&path) {
Some(p) if waccess(&p, F_OK) == 0 => p,
@@ -740,7 +740,7 @@ pub fn status(parser: &Parser, streams: &mut IoStreams, args: &mut [&wstr]) -> B
streams.out.append_char('\n');
} else {
// This is a relative path, we can't canonicalize it
let path = str2wcstring(path.as_os_str().as_bytes());
let path = bytes2wcstring(path.as_os_str().as_bytes());
streams.out.appendln(path);
}
}

View File

@@ -290,7 +290,7 @@ fn width_without_escapes(ins: &wstr, start_pos: usize) -> usize {
/// Empirically determined.
/// This is probably down to some pipe buffer or some such,
/// but too small means we need to call `read(2)` and str2wcstring a lot.
/// but too small means we need to call `read(2)` and bytes2wcstring a lot.
const STRING_CHUNK_SIZE: usize = 1024;
fn arguments<'iter, 'args>(
args: &'iter [&'args wstr],

View File

@@ -1,5 +1,5 @@
use super::prelude::*;
use crate::common::str2wcstring;
use crate::common::bytes2wcstring;
use crate::function;
use crate::highlight::{colorize, highlight_shell};
@@ -152,7 +152,7 @@ pub fn r#type(parser: &Parser, streams: &mut IoStreams, argv: &mut [&wstr]) -> B
/*io_ok=*/ false,
/*cursor=*/ None,
);
let col = str2wcstring(&colorize(&def, &color, parser.vars()));
let col = bytes2wcstring(&colorize(&def, &color, parser.vars()));
streams.out.append(col);
} else {
streams.out.append(def);

View File

@@ -14,7 +14,7 @@
use crate::terminal::Output;
use crate::termsize::Termsize;
use crate::wchar::{decode_byte_from_char, encode_byte_to_char, prelude::*};
use crate::wcstringutil::wcs2string_callback;
use crate::wcstringutil::wcs2bytes_callback;
use crate::wildcard::{ANY_CHAR, ANY_STRING, ANY_STRING_RECURSIVE};
use crate::wutil::encoding::{
AT_LEAST_MB_LEN_MAX, mbrtowc, probe_is_multibyte_locale, wcrtomb, zero_mbstate,
@@ -386,7 +386,7 @@ fn byte_to_hex(byte: u8) -> (char, char) {
/// Escape a string in a fashion suitable for using as a URL. Store the result in out_str.
fn escape_string_url(input: &wstr) -> WString {
let narrow = wcs2string(input);
let narrow = wcs2bytes(input);
let mut out = WString::new();
for byte in narrow.into_iter() {
if (byte & 0x80) == 0 {
@@ -409,7 +409,7 @@ fn escape_string_url(input: &wstr) -> WString {
/// Escape a string in a fashion suitable for using as a fish var name. Store the result in out_str.
fn escape_string_var(input: &wstr) -> WString {
let mut prev_was_hex_encoded = false;
let narrow = wcs2string(input);
let narrow = wcs2bytes(input);
let mut out = WString::new();
for c in narrow.into_iter() {
let ch: char = c.into();
@@ -790,7 +790,7 @@ fn unescape_string_url(input: &wstr) -> Option<WString> {
i += 1
}
Some(str2wcstring(&result))
Some(bytes2wcstring(&result))
}
/// Reverse the effects of `escape_string_var()`. By definition the string should consist of just
@@ -831,7 +831,7 @@ fn unescape_string_var(input: &wstr) -> Option<WString> {
i += 1;
}
Some(str2wcstring(&result))
Some(bytes2wcstring(&result))
}
/// Given a string starting with a backslash, read the escape as if it is unquoted, appending
@@ -1000,7 +1000,7 @@ pub fn read_unquoted_escape(
}
if !byte_buff.is_empty() {
result.push_utfstr(&str2wcstring(&byte_buff));
result.push_utfstr(&bytes2wcstring(&byte_buff));
}
break;
@@ -1108,7 +1108,7 @@ pub fn has_working_tty_timestamps() -> bool {
///
/// This function encodes illegal character sequences in a reversible way using the private use
/// area.
pub fn str2wcstring(inp: &[u8]) -> WString {
pub fn bytes2wcstring(inp: &[u8]) -> WString {
if inp.is_empty() {
return WString::new();
}
@@ -1196,12 +1196,12 @@ pub fn truncate_at_nul(input: &wstr) -> &wstr {
pub fn cstr2wcstring(input: &[u8]) -> WString {
let input = CStr::from_bytes_until_nul(input).unwrap().to_bytes();
str2wcstring(input)
bytes2wcstring(input)
}
pub(crate) fn charptr2wcstring(input: *const libc::c_char) -> WString {
let input: &[u8] = unsafe { CStr::from_ptr(input).to_bytes() };
str2wcstring(input)
bytes2wcstring(input)
}
/// Returns a newly allocated multibyte character string equivalent of the specified wide character
@@ -1209,13 +1209,13 @@ pub(crate) fn charptr2wcstring(input: *const libc::c_char) -> WString {
///
/// This function decodes illegal character sequences in a reversible way using the private use
/// area.
pub fn wcs2string(input: &wstr) -> Vec<u8> {
pub fn wcs2bytes(input: &wstr) -> Vec<u8> {
if input.is_empty() {
return vec![];
}
let mut result = vec![];
wcs2string_appending(&mut result, input);
wcs2bytes_appending(&mut result, input);
result
}
@@ -1225,11 +1225,11 @@ pub fn wcs2osstring(input: &wstr) -> OsString {
}
let mut result = vec![];
wcs2string_appending(&mut result, input);
wcs2bytes_appending(&mut result, input);
OsString::from_vec(result)
}
/// Same as [`wcs2string`]. Meant to be used when we need a zero-terminated string to feed legacy APIs.
/// Same as [`wcs2bytes`]. Meant to be used when we need a zero-terminated string to feed legacy APIs.
/// Note: if `input` contains any interior NUL bytes, the result will be truncated at the first!
pub fn wcs2zstring(input: &wstr) -> CString {
if input.is_empty() {
@@ -1237,7 +1237,7 @@ pub fn wcs2zstring(input: &wstr) -> CString {
}
let mut vec = Vec::with_capacity(input.len() + 1);
wcs2string_callback(input, |buff| {
wcs2bytes_callback(input, |buff| {
vec.extend_from_slice(buff);
true
});
@@ -1256,10 +1256,10 @@ pub fn wcs2zstring(input: &wstr) -> CString {
}
}
/// Like wcs2string, but appends to `receiver` instead of returning a new string.
pub fn wcs2string_appending(output: &mut Vec<u8>, input: &wstr) {
/// Like [`wcs2bytes`], but appends to `output` instead of returning a new string.
pub fn wcs2bytes_appending(output: &mut Vec<u8>, input: &wstr) {
output.reserve(input.len());
wcs2string_callback(input, |buff| {
wcs2bytes_callback(input, |buff| {
output.extend_from_slice(buff);
true
});
@@ -2106,7 +2106,7 @@ macro_rules! env_stack_set_from_env {
$vars.set_one(
L!($var_name),
$crate::env::EnvMode::GLOBAL,
$crate::common::str2wcstring(var.as_bytes()),
$crate::common::bytes2wcstring(var.as_bytes()),
);
}
}};

View File

@@ -1,5 +1,5 @@
use crate::common::BUILD_DIR;
use crate::common::wcs2string;
use crate::common::wcs2bytes;
use crate::wchar::prelude::*;
use crate::{FLOG, FLOGF, common::get_executable_path};
use fish_build_helper::workspace_root;
@@ -24,7 +24,7 @@ pub struct ConfigPaths {
impl ConfigPaths {
pub fn new(argv0: &wstr) -> Self {
let argv0 = PathBuf::from(OsString::from_vec(wcs2string(argv0)));
let argv0 = PathBuf::from(OsString::from_vec(wcs2bytes(argv0)));
let exec_path = get_executable_path(argv0);
FLOG!(config, format!("executable path: {}", exec_path.display()));
let paths = Self::from_exec_path(exec_path);

View File

@@ -5,7 +5,7 @@
};
use crate::abbrs::{Abbreviation, Position, abbrs_get_set};
use crate::builtins::shared::{BuiltinResult, SUCCESS};
use crate::common::{UnescapeStringStyle, str2wcstring, unescape_string, wcs2zstring};
use crate::common::{UnescapeStringStyle, bytes2wcstring, unescape_string, wcs2zstring};
use crate::env::config_paths::ConfigPaths;
use crate::env::{EnvMode, EnvVar, Statuses};
use crate::env_dispatch::{env_dispatch_init, env_dispatch_var_change};
@@ -439,7 +439,7 @@ fn get_hostname_identifier() -> Option<WString> {
let mut b = [0 as libc::c_char; HOSTNAME_LEN + 1];
if unsafe { libc::gethostname(b.as_mut_ptr(), b.len()) } == 0 {
let cstr = unsafe { CStr::from_ptr(b.as_ptr()) };
let res = str2wcstring(cstr.to_bytes());
let res = bytes2wcstring(cstr.to_bytes());
if res.is_empty() { None } else { Some(res) }
} else {
@@ -512,7 +512,7 @@ fn setup_user(vars: &EnvStack) {
vars.set_one(
L!("HOME"),
EnvMode::GLOBAL | EnvMode::EXPORT,
str2wcstring(s.to_bytes()),
bytes2wcstring(s.to_bytes()),
);
} else {
vars.set_empty(L!("HOME"), EnvMode::GLOBAL | EnvMode::EXPORT);
@@ -537,7 +537,7 @@ fn setup_user(vars: &EnvStack) {
if retval == 0 && !result.is_null() {
let userinfo = unsafe { userinfo.assume_init() };
let s = unsafe { CStr::from_ptr(userinfo.pw_name) };
let uname = str2wcstring(s.to_bytes());
let uname = bytes2wcstring(s.to_bytes());
vars.set_one(L!("USER"), EnvMode::GLOBAL | EnvMode::EXPORT, uname);
// Only change $HOME if it's empty, so we allow e.g. `HOME=(mktemp -d)`.
// This is okay with common `su` and `sudo` because they set $HOME.
@@ -547,7 +547,7 @@ fn setup_user(vars: &EnvStack) {
vars.set_one(
L!("HOME"),
EnvMode::GLOBAL | EnvMode::EXPORT,
str2wcstring(s.to_bytes()),
bytes2wcstring(s.to_bytes()),
);
} else {
// We cannot get $HOME. This triggers warnings for history and config.fish already,
@@ -572,7 +572,7 @@ fn setup_user(vars: &EnvStack) {
let buf = buf;
// safety: buf should contain a null-byte, and is not mutable unless we move ownership
let cstr = unsafe { CStr::from_ptr(buf.as_ptr()) };
colon_split(&[str2wcstring(cstr.to_bytes())])
colon_split(&[bytes2wcstring(cstr.to_bytes())])
} else {
vec![
WString::from_str(env!("PREFIX")) + L!("/bin"),
@@ -604,7 +604,7 @@ pub fn env_init(paths: Option<&ConfigPaths>, do_uvars: bool, default_paths: bool
let vars = EnvStack::globals();
let env_iter: Vec<_> = std::env::vars_os()
.map(|(k, v)| (str2wcstring(k.as_bytes()), str2wcstring(v.as_bytes())))
.map(|(k, v)| (bytes2wcstring(k.as_bytes()), bytes2wcstring(v.as_bytes())))
.collect();
let mut inherited_vars = HashMap::new();
@@ -642,7 +642,7 @@ pub fn env_init(paths: Option<&ConfigPaths>, do_uvars: bool, default_paths: bool
vars.set_empty(FISH_DATADIR_VAR, EnvMode::GLOBAL);
#[cfg(not(feature = "embed-data"))]
{
let datadir = str2wcstring(paths.data.as_os_str().as_bytes());
let datadir = bytes2wcstring(paths.data.as_os_str().as_bytes());
vars.set_one(FISH_DATADIR_VAR, EnvMode::GLOBAL, datadir.clone());
if default_paths {
@@ -657,7 +657,7 @@ pub fn env_init(paths: Option<&ConfigPaths>, do_uvars: bool, default_paths: bool
vars.set_one(
FISH_SYSCONFDIR_VAR,
EnvMode::GLOBAL,
str2wcstring(paths.sysconf.as_os_str().as_bytes()),
bytes2wcstring(paths.sysconf.as_os_str().as_bytes()),
);
#[cfg(feature = "embed-data")]
@@ -666,13 +666,13 @@ pub fn env_init(paths: Option<&ConfigPaths>, do_uvars: bool, default_paths: bool
vars.set_one(
FISH_HELPDIR_VAR,
EnvMode::GLOBAL,
str2wcstring(paths.doc.as_os_str().as_bytes()),
bytes2wcstring(paths.doc.as_os_str().as_bytes()),
);
if let Some(bp) = &paths.bin {
vars.set_one(
FISH_BIN_DIR,
EnvMode::GLOBAL,
str2wcstring(bp.as_os_str().as_bytes()),
bytes2wcstring(bp.as_os_str().as_bytes()),
);
} else {
vars.set_empty(FISH_BIN_DIR, EnvMode::GLOBAL);
@@ -711,7 +711,7 @@ pub fn env_init(paths: Option<&ConfigPaths>, do_uvars: bool, default_paths: bool
vars.set_one(L!("CMD_DURATION"), EnvMode::UNEXPORT, "0".into());
// Set up the version variable.
let version = str2wcstring(crate::BUILD_VERSION.as_bytes());
let version = bytes2wcstring(crate::BUILD_VERSION.as_bytes());
vars.set_one(L!("version"), EnvMode::GLOBAL, version.clone());
vars.set_one(L!("FISH_VERSION"), EnvMode::GLOBAL, version);
@@ -728,7 +728,7 @@ pub fn env_init(paths: Option<&ConfigPaths>, do_uvars: bool, default_paths: bool
let nshlvl_str = if let Some(shlvl_var) = std::env::var_os("SHLVL") {
// TODO: Figure out how to handle invalid numbers better. Shouldn't we issue a
// diagnostic?
match fish_wcstol(&str2wcstring(shlvl_var.as_os_str().as_bytes())) {
match fish_wcstol(&bytes2wcstring(shlvl_var.as_os_str().as_bytes())) {
Ok(shlvl_i) if shlvl_i >= 0 => (shlvl_i + 1).to_wstring(),
_ => L!("1").to_owned(),
}
@@ -742,7 +742,7 @@ pub fn env_init(paths: Option<&ConfigPaths>, do_uvars: bool, default_paths: bool
vars.set_one(
L!("SHLVL"),
EnvMode::GLOBAL | EnvMode::EXPORT,
str2wcstring(shlvl_var.as_os_str().as_bytes()),
bytes2wcstring(shlvl_var.as_os_str().as_bytes()),
);
}
}
@@ -756,7 +756,7 @@ pub fn env_init(paths: Option<&ConfigPaths>, do_uvars: bool, default_paths: bool
// (see #7636)
let incoming_pwd_cstr = std::env::var_os("PWD");
let incoming_pwd = incoming_pwd_cstr
.map(|s| str2wcstring(s.as_os_str().as_bytes()))
.map(|s| bytes2wcstring(s.as_os_str().as_bytes()))
.unwrap_or_default();
if !incoming_pwd.is_empty()
&& incoming_pwd.char_at(0) == '/'

View File

@@ -8,7 +8,7 @@
builtin_run,
};
use crate::common::{
ScopeGuard, exit_without_destructors, str2wcstring, truncate_at_nul, wcs2string, wcs2zstring,
ScopeGuard, bytes2wcstring, exit_without_destructors, truncate_at_nul, wcs2bytes, wcs2zstring,
write_loop,
};
use crate::env::{EnvMode, EnvStack, Environment, READ_BYTE_LIMIT, Statuses};
@@ -822,8 +822,8 @@ fn handle_builtin_output(
assert!(p.is_builtin(), "Process is not a builtin");
// Figure out any data remaining to write. We may have none, in which case we can short-circuit.
let outbuff = wcs2string(out.contents());
let errbuff = wcs2string(err.contents());
let outbuff = wcs2bytes(out.contents());
let errbuff = wcs2bytes(err.contents());
// Some historical behavior.
if !outbuff.is_empty() {
@@ -1413,7 +1413,7 @@ fn populate_subshell_output(lst: &mut Vec<WString>, buffer: &SeparatedBuffer, sp
let data = &elem.contents;
if elem.is_explicitly_separated() {
// Just append this one.
lst.push(str2wcstring(data));
lst.push(bytes2wcstring(data));
continue;
}
@@ -1431,7 +1431,7 @@ fn populate_subshell_output(lst: &mut Vec<WString>, buffer: &SeparatedBuffer, sp
// If it's not found, just use the end.
let stop = stop.map(|rel| cursor + rel).unwrap_or(data.len());
// Stop now points at the first character we do not want to copy.
lst.push(str2wcstring(&data[cursor..stop]));
lst.push(bytes2wcstring(&data[cursor..stop]));
// If we hit a separator, skip over it; otherwise we're at the end.
cursor = stop + if hit_separator { 1 } else { 0 };
@@ -1439,7 +1439,7 @@ fn populate_subshell_output(lst: &mut Vec<WString>, buffer: &SeparatedBuffer, sp
} else {
// We're not splitting output, but we still want to trim off a trailing newline.
let trailing_newline = if data.last() == Some(&b'\n') { 1 } else { 0 };
lst.push(str2wcstring(&data[..data.len() - trailing_newline]));
lst.push(bytes2wcstring(&data[..data.len() - trailing_newline]));
}
}
}

View File

@@ -1,4 +1,4 @@
use crate::common::wcs2string;
use crate::common::wcs2bytes;
use crate::wchar::prelude::*;
use crate::wildcard::wildcard_match;
use crate::wutil::write_to_fd;
@@ -161,13 +161,13 @@ fn to_flog_str(&self) -> Vec<u8> {
// Special handling for `WString` to decode PUA codepoints back into the original bytes.
impl FloggableDisplay for WString {
fn to_flog_str(&self) -> Vec<u8> {
wcs2string(self)
wcs2bytes(self)
}
}
impl FloggableDisplay for &wstr {
fn to_flog_str(&self) -> Vec<u8> {
wcs2string(self)
wcs2bytes(self)
}
}

View File

@@ -1,6 +1,6 @@
use crate::{
FLOG, FLOGF,
common::{str2wcstring, wcs2osstring, wcs2zstring},
common::{bytes2wcstring, wcs2osstring, wcs2zstring},
fds::wopen_cloexec,
path::{DirRemoteness, path_remoteness},
wchar::prelude::*,
@@ -64,7 +64,7 @@ pub fn create_temporary_file(name_template: &wstr) -> std::io::Result<(File, WSt
},
}
};
Ok((fd, str2wcstring(c_string_template.to_bytes())))
Ok((fd, bytes2wcstring(c_string_template.to_bytes())))
}
/// Use this struct for all accesses to file which need mutual exclusion.

View File

@@ -240,7 +240,7 @@ pub fn exists_no_autoload(cmd: &wstr) -> bool {
return true;
}
let narrow = crate::common::wcs2string(cmd);
let narrow = crate::common::wcs2bytes(cmd);
if let Ok(cmdstr) = std::str::from_utf8(&narrow) {
let cmd = "functions/".to_owned() + cmdstr + ".fish";
crate::autoload::has_asset(&cmd)

View File

@@ -43,7 +43,7 @@
use crate::{
ast::{self, Kind, Node},
common::{CancelChecker, UnescapeStringStyle, str2wcstring, unescape_string, valid_var_name},
common::{CancelChecker, UnescapeStringStyle, bytes2wcstring, unescape_string, valid_var_name},
env::{EnvMode, EnvStack, Environment},
expand::{ExpandFlags, expand_one},
fds::wopen_cloexec,
@@ -966,7 +966,7 @@ fn populate_from_bash<R: BufRead>(&mut self, contents: R) {
let Ok(line) = line else {
break;
};
let wide_line = trim(str2wcstring(&line), None);
let wide_line = trim(bytes2wcstring(&line), None);
// Add this line if it doesn't contain anything we know we can't handle.
if should_import_bash_history_line(&wide_line) {
self.add(

View File

@@ -13,7 +13,7 @@
use super::{HistoryItem, PersistenceMode};
use crate::{
common::{str2wcstring, subslice_position, wcs2string},
common::{bytes2wcstring, subslice_position, wcs2bytes},
flog::FLOG,
path::{DirRemoteness, path_get_data_remoteness},
};
@@ -217,7 +217,7 @@ fn try_from(region: MmapRegion) -> std::io::Result<Self> {
pub fn append_history_item_to_buffer(item: &HistoryItem, buffer: &mut Vec<u8>) {
assert!(item.should_write_to_disk(), "Item should not be persisted");
let mut cmd = wcs2string(item.str());
let mut cmd = wcs2bytes(item.str());
escape_yaml_fish_2_0(&mut cmd);
buffer.extend(b"- cmd: ");
buffer.extend(&cmd);
@@ -228,7 +228,7 @@ pub fn append_history_item_to_buffer(item: &HistoryItem, buffer: &mut Vec<u8>) {
if !paths.is_empty() {
writeln!(buffer, " paths:").unwrap();
for path in paths {
let mut path = wcs2string(path);
let mut path = wcs2bytes(path);
escape_yaml_fish_2_0(&mut path);
buffer.extend(b" - ");
buffer.extend(&path);
@@ -373,7 +373,7 @@ fn decode_item_fish_2_0(mut data: &[u8]) -> Option<HistoryItem> {
let (_key, value) = extract_prefix_and_unescape_yaml(line)?;
data = &data[advance..];
let cmd = str2wcstring(&value);
let cmd = bytes2wcstring(&value);
// Read the remaining lines.
let mut indent = None;
@@ -420,7 +420,7 @@ fn decode_item_fish_2_0(mut data: &[u8]) -> Option<HistoryItem> {
data = &data[advance..];
let line = maybe_unescape_yaml_fish_2_0(line);
paths.push(str2wcstring(&line));
paths.push(bytes2wcstring(&line));
}
}
}

View File

@@ -1,4 +1,4 @@
use crate::common::{Named, escape, get_by_sorted_name, str2wcstring};
use crate::common::{Named, bytes2wcstring, escape, get_by_sorted_name};
use crate::env::Environment;
use crate::event;
use crate::flog::FLOG;
@@ -437,7 +437,7 @@ fn paste_commit(&mut self) {
};
self.push_front(CharEvent::Command(sprintf!(
"__fish_paste %s",
escape(&str2wcstring(&buffer))
escape(&bytes2wcstring(&buffer))
)));
}
}

View File

@@ -1,6 +1,6 @@
use crate::common::{
WSL, fish_reserved_codepoint, get_is_multibyte_locale, is_windows_subsystem_for_linux,
read_blocked, shell_modes, str2wcstring,
WSL, bytes2wcstring, fish_reserved_codepoint, get_is_multibyte_locale,
is_windows_subsystem_for_linux, read_blocked, shell_modes,
};
use crate::env::{EnvStack, Environment};
use crate::fd_readable_set::{FdReadableSet, Timeout};
@@ -1421,7 +1421,7 @@ fn parse_xtversion(&mut self, buffer: &mut Vec<u8>) -> Option<()> {
return None;
}
XTVERSION.get_or_init(|| {
let xtversion = str2wcstring(&buffer[4..buffer.len()]);
let xtversion = bytes2wcstring(&buffer[4..buffer.len()]);
FLOG!(
reader,
format!("Received XTVERSION response: {}", xtversion)
@@ -1460,7 +1460,7 @@ fn parse_dcs(&mut self, buffer: &mut Vec<u8>) -> Option<KeyEvent> {
reader,
format!(
"Received XTGETTCAP failure response: {}",
str2wcstring(&parse_hex(buffer)?),
bytes2wcstring(&parse_hex(buffer)?),
)
);
return None;
@@ -1474,14 +1474,14 @@ fn parse_dcs(&mut self, buffer: &mut Vec<u8>) -> Option<KeyEvent> {
reader,
format!(
"Received XTGETTCAP response: {}={:?}",
str2wcstring(&key),
str2wcstring(&value)
bytes2wcstring(&key),
bytes2wcstring(&value)
)
);
} else {
FLOG!(
reader,
format!("Received XTGETTCAP response: {}", str2wcstring(&key))
format!("Received XTGETTCAP response: {}", bytes2wcstring(&key))
);
}
if key == SCROLL_CONTENT_UP_TERMINFO_CODE.as_bytes() {

View File

@@ -1,5 +1,5 @@
use crate::builtins::shared::{STATUS_CMD_ERROR, STATUS_CMD_OK, STATUS_READ_TOO_MUCH};
use crate::common::{EMPTY_STRING, str2wcstring, wcs2string};
use crate::common::{EMPTY_STRING, bytes2wcstring, wcs2bytes};
use crate::fd_monitor::{Callback, FdMonitor, FdMonitorItemId};
use crate::fds::{
AutoCloseFd, PIPE_ERROR, make_autoclose_pipes, make_fd_nonblocking, wopen_cloexec,
@@ -731,7 +731,7 @@ pub fn push(&mut self, c: char) -> bool {
pub fn append_narrow_buffer(&mut self, buffer: &SeparatedBuffer) -> bool {
for rhs_elem in buffer.elements() {
if !self.append_with_separation(
&str2wcstring(&rhs_elem.contents),
&bytes2wcstring(&rhs_elem.contents),
rhs_elem.separation,
false,
) {
@@ -745,7 +745,7 @@ pub fn append_narrow_buffer(&mut self, buffer: &SeparatedBuffer) -> bool {
impl Output for OutputStream {
fn write_bytes(&mut self, command_part: &[u8]) {
// TODO Retry on interrupt.
self.append(str2wcstring(command_part));
self.append(bytes2wcstring(command_part));
}
}
@@ -834,7 +834,7 @@ pub fn new(buffer: IoBuffer) -> Self {
Self { buffer }
}
fn append(&mut self, s: &wstr) -> bool {
self.buffer.append(&wcs2string(s), SeparationType::inferred)
self.buffer.append(&wcs2bytes(s), SeparationType::inferred)
}
fn append_with_separation(
&mut self,
@@ -842,7 +842,7 @@ fn append_with_separation(
typ: SeparationType,
_want_newline: bool,
) -> bool {
self.buffer.append(&wcs2string(s), typ)
self.buffer.append(&wcs2bytes(s), typ)
}
fn flush_and_check_error(&mut self) -> libc::c_int {
if self.buffer.discarded() {

View File

@@ -4,7 +4,7 @@
use crate::builtins::shared::STATUS_ILLEGAL_CMD;
use crate::common::{
CancelChecker, EscapeFlags, EscapeStringStyle, FilenameRef, PROFILING_ACTIVE, ScopeGuarding,
ScopedCell, ScopedRefCell, escape_string, wcs2string,
ScopedCell, ScopedRefCell, escape_string, wcs2bytes,
};
use crate::complete::CompletionList;
use crate::env::{EnvMode, EnvStack, EnvStackSetResult, Environment, Statuses};
@@ -1270,7 +1270,7 @@ fn print_profile(items: &[ProfileItem], out: &mut File) {
L!("\n"),
&(WString::from("\n") + &wstr::repeat(L!(" "), indentation_level)[..]),
);
let _ = out.write_all(&wcs2string(&indented_cmd));
let _ = out.write_all(&wcs2bytes(&indented_cmd));
let _ = out.write_all(b"\n");
}
}

View File

@@ -585,7 +585,7 @@ fn make_base_directory(xdg_var: &wstr, non_xdg_homepath: &wstr) -> BaseDirectory
// the actual $HOME or $XDG_XXX directories. This prevents the tests from failing and/or stops
// the tests polluting the user's actual $HOME if a sandbox environment has not been set up.
{
use crate::common::{BUILD_DIR, str2wcstring};
use crate::common::{BUILD_DIR, bytes2wcstring};
use std::path::PathBuf;
let mut build_dir = PathBuf::from(BUILD_DIR);
@@ -599,7 +599,7 @@ fn make_base_directory(xdg_var: &wstr, non_xdg_homepath: &wstr) -> BaseDirectory
};
return BaseDirectory {
path: str2wcstring(build_dir.as_os_str().as_bytes()),
path: bytes2wcstring(build_dir.as_os_str().as_bytes()),
remoteness: DirRemoteness::unknown,
used_xdg: false,
err,

View File

@@ -54,10 +54,10 @@
use crate::builtins::shared::STATUS_CMD_OK;
use crate::common::ScopeGuarding;
use crate::common::{
EscapeFlags, EscapeStringStyle, PROGRAM_NAME, ScopeGuard, UTF8_BOM_WCHAR, escape,
escape_string, exit_without_destructors, get_ellipsis_char, get_is_multibyte_locale,
EscapeFlags, EscapeStringStyle, PROGRAM_NAME, ScopeGuard, UTF8_BOM_WCHAR, bytes2wcstring,
escape, escape_string, exit_without_destructors, get_ellipsis_char, get_is_multibyte_locale,
get_obfuscation_read_char, restore_term_foreground_process_group_for_exit, shell_modes,
str2wcstring, write_loop,
write_loop,
};
use crate::complete::{
CompleteFlags, Completion, CompletionList, CompletionRequestOptions, complete, complete_load,
@@ -943,7 +943,7 @@ fn read_ni(parser: &Parser, fd: RawFd, io: &IoChain) -> Result<(), ErrorCode> {
}
}
let mut s = str2wcstring(&fd_contents);
let mut s = bytes2wcstring(&fd_contents);
// Eagerly deallocate to save memory.
drop(fd_contents);

View File

@@ -22,8 +22,8 @@
use libc::{ONLCR, STDERR_FILENO, STDOUT_FILENO};
use crate::common::{
get_ellipsis_char, get_omitted_newline_str, get_omitted_newline_width,
has_working_tty_timestamps, shell_modes, str2wcstring, wcs2string, write_loop,
bytes2wcstring, get_ellipsis_char, get_omitted_newline_str, get_omitted_newline_width,
has_working_tty_timestamps, shell_modes, wcs2bytes, write_loop,
};
use crate::env::Environment;
use crate::fallback::fish_wcwidth;
@@ -312,12 +312,12 @@ struct ScrolledCursor {
// If we are using a dumb terminal, don't try any fancy stuff, just print out the text.
// right_prompt not supported.
if is_dumb() {
let prompt_narrow = wcs2string(left_prompt);
let prompt_narrow = wcs2bytes(left_prompt);
let _ = write_loop(&STDOUT_FILENO, b"\r");
let _ = write_loop(&STDOUT_FILENO, &prompt_narrow);
let _ = write_loop(&STDOUT_FILENO, &wcs2string(explicit_before_suggestion));
let _ = write_loop(&STDOUT_FILENO, &wcs2string(explicit_after_suggestion));
let _ = write_loop(&STDOUT_FILENO, &wcs2bytes(explicit_before_suggestion));
let _ = write_loop(&STDOUT_FILENO, &wcs2bytes(explicit_after_suggestion));
return;
}
@@ -696,7 +696,7 @@ pub fn reset_abandoning_line(&mut self, screen_width: usize) {
let Some(s) = s else {
return false;
};
abandon_line_string.push_utfstr(&str2wcstring(s.as_bytes()));
abandon_line_string.push_utfstr(&bytes2wcstring(s.as_bytes()));
true
};
if let Some(enter_dim_mode) = term.enter_dim_mode.as_ref() {
@@ -725,7 +725,7 @@ pub fn reset_abandoning_line(&mut self, screen_width: usize) {
} else {
let mut tmp = Vec::<u8>::new();
tmp.write_command(EnterDimMode);
abandon_line_string.push_utfstr(&str2wcstring(&tmp));
abandon_line_string.push_utfstr(&bytes2wcstring(&tmp));
}
abandon_line_string.push_utfstr(&get_omitted_newline_str());
@@ -733,7 +733,7 @@ pub fn reset_abandoning_line(&mut self, screen_width: usize) {
// normal text ANSI escape sequence
let mut tmp = Vec::<u8>::new();
tmp.write_command(ExitAttributeMode);
abandon_line_string.push_utfstr(&str2wcstring(&tmp));
abandon_line_string.push_utfstr(&bytes2wcstring(&tmp));
for _ in 0..screen_width - non_space_width {
abandon_line_string.push(' ');
@@ -756,9 +756,9 @@ pub fn reset_abandoning_line(&mut self, screen_width: usize) {
// actual empty line.
let mut tmp = Vec::<u8>::new();
tmp.write_command(ClearToEndOfLine);
abandon_line_string.push_utfstr(&str2wcstring(&tmp));
abandon_line_string.push_utfstr(&bytes2wcstring(&tmp));
let narrow_abandon_line_string = wcs2string(&abandon_line_string);
let narrow_abandon_line_string = wcs2bytes(&abandon_line_string);
let _ = write_loop(&STDOUT_FILENO, &narrow_abandon_line_string);
self.actual.cursor.x = 0;

View File

@@ -2,7 +2,7 @@
use crate::FLOGF;
use crate::color::{Color, Color24};
use crate::common::ToCString;
use crate::common::{self, EscapeStringStyle, escape_string, wcs2string, wcs2string_appending};
use crate::common::{self, EscapeStringStyle, escape_string, wcs2bytes, wcs2bytes_appending};
use crate::future_feature_flags::{self, FeatureFlag};
use crate::screen::{is_dumb, only_grayscale};
use crate::text_face::{TextFace, TextStyling, UnderlineStyle};
@@ -374,7 +374,7 @@ fn query_kitty_progressive_enhancements(out: &mut impl Output) -> bool {
fn osc_0_window_title(out: &mut impl Output, title: &[WString]) -> bool {
out.write_bytes(b"\x1b]0;");
for title_line in title {
out.write_bytes(&wcs2string(title_line));
out.write_bytes(&wcs2bytes(title_line));
}
out.write_bytes(b"\x07"); // BEL
true
@@ -636,7 +636,7 @@ pub fn push(&mut self, ch: u8) {
/// Write a wide string.
pub fn write_wstr(&mut self, str: &wstr) {
wcs2string_appending(&mut self.contents, str);
wcs2bytes_appending(&mut self.contents, str);
self.maybe_flush();
}

View File

@@ -1,11 +1,11 @@
use crate::common::{str2wcstring, wcs2string};
use crate::common::{bytes2wcstring, wcs2bytes};
use crate::wchar::prelude::*;
/// Verify correct behavior with embedded nulls.
#[test]
fn test_convert_nulls() {
let input = L!("AAA\0BBB");
let out_str = wcs2string(input);
let out_str = wcs2bytes(input);
assert_eq!(
input.chars().collect::<Vec<_>>(),
std::str::from_utf8(&out_str)
@@ -14,20 +14,20 @@ fn test_convert_nulls() {
.collect::<Vec<_>>()
);
let out_wstr = str2wcstring(&out_str);
let out_wstr = bytes2wcstring(&out_str);
assert_eq!(input, &out_wstr);
}
#[cfg(feature = "benchmark")]
mod bench {
extern crate test;
use crate::tests::encoding::str2wcstring;
use crate::tests::encoding::bytes2wcstring;
use test::Bencher;
#[bench]
fn bench_convert_ascii(b: &mut Bencher) {
let s: [u8; 128 * 1024] = std::array::from_fn(|i| b'0' + u8::try_from(i % 10).unwrap());
b.bytes = u64::try_from(s.len()).unwrap();
b.iter(|| str2wcstring(&s));
b.iter(|| bytes2wcstring(&s));
}
}

View File

@@ -1,4 +1,4 @@
use crate::common::{ScopeGuard, str2wcstring, wcs2osstring, wcs2string};
use crate::common::{ScopeGuard, bytes2wcstring, wcs2bytes, wcs2osstring};
use crate::env::{EnvMode, EnvStack};
use crate::fs::{LockedFile, WriteMethod};
use crate::history::{
@@ -260,7 +260,7 @@ fn test_history_races() {
});
if LockedFile::new(
crate::fs::LockingMode::Exclusive(WriteMethod::RenameIntoPlace),
&str2wcstring(tmp_path.as_os_str().as_bytes()),
&bytes2wcstring(tmp_path.as_os_str().as_bytes()),
)
.is_err()
{
@@ -497,7 +497,7 @@ fn test_history_path_detection() {
let tmpdirbuff = CString::new("/tmp/fish_test_history.XXXXXX").unwrap();
let tmpdir = unsafe { libc::mkdtemp(tmpdirbuff.into_raw()) };
let tmpdir = unsafe { CString::from_raw(tmpdir) };
let mut tmpdir = str2wcstring(tmpdir.to_bytes());
let mut tmpdir = bytes2wcstring(tmpdir.to_bytes());
if !tmpdir.ends_with('/') {
tmpdir.push('/');
}
@@ -602,7 +602,7 @@ fn install_sample_history(name: &wstr) {
std::fs::copy(
workspace_root()
.join("tests")
.join(std::str::from_utf8(&wcs2string(name)).unwrap()),
.join(std::str::from_utf8(&wcs2bytes(name)).unwrap()),
wcs2osstring(&(path + L!("/") + name + L!("_history"))),
)
.unwrap();

View File

@@ -2,7 +2,7 @@
use crate::common::{
ENCODE_DIRECT_BASE, ENCODE_DIRECT_END, EscapeFlags, EscapeStringStyle, UnescapeStringStyle,
escape_string, fish_setlocale, str2wcstring, unescape_string, wcs2string,
bytes2wcstring, escape_string, fish_setlocale, unescape_string, wcs2bytes,
};
use crate::locale::LOCALE_LOCK;
use crate::util::{get_rng_seed, get_seeded_rng};
@@ -12,7 +12,7 @@
};
use rand::{Rng, RngCore};
/// wcs2string is locale-dependent, so ensure we have a multibyte locale
/// wcs2bytes is locale-dependent, so ensure we have a multibyte locale
/// before using it in a test.
fn setlocale() -> MutexGuard<'static, ()> {
let guard = LOCALE_LOCK.lock().unwrap();
@@ -173,7 +173,7 @@ fn test_escape_no_printables() {
pub const ESCAPE_TEST_CHAR: usize = 4000;
/// Helper to convert a narrow string to a sequence of hex digits.
fn str2hex(input: &[u8]) -> String {
fn bytes2hex(input: &[u8]) -> String {
let mut output = "".to_string();
for byte in input {
output += &format!("0x{:2X} ", *byte);
@@ -195,8 +195,8 @@ fn test_convert() {
origin.resize(length, 0);
rng.fill_bytes(&mut origin);
let w = str2wcstring(&origin[..]);
let n = wcs2string(&w);
let w = bytes2wcstring(&origin[..]);
let n = wcs2bytes(&w);
assert_eq!(
origin,
n,
@@ -205,9 +205,9 @@ fn test_convert() {
{:4} chars: {}\n
Use this seed to reproduce: {}",
origin.len(),
&str2hex(&origin),
&bytes2hex(&origin),
n.len(),
&str2hex(&n),
&bytes2hex(&n),
seed,
);
}
@@ -226,8 +226,8 @@ fn test_convert_ascii() {
for right in 0..16 {
let len = s.len() - left - right;
let input = &s[left..left + len];
let wide = str2wcstring(input);
let narrow = wcs2string(&wide);
let wide = bytes2wcstring(input);
let narrow = wcs2bytes(&wide);
assert_eq!(narrow, input);
}
}
@@ -236,7 +236,7 @@ fn test_convert_ascii() {
for i in 0..s.len() {
let saved = s[i];
s[i] = 0xF7;
assert_eq!(wcs2string(&str2wcstring(&s)), s);
assert_eq!(wcs2bytes(&bytes2wcstring(&s)), s);
s[i] = saved;
}
}
@@ -265,13 +265,13 @@ fn test_convert_private_use() {
}
let s = &converted[..len];
// Ask fish to decode this via str2wcstring.
// str2wcstring should notice that the decoded form collides with its private use
// Ask fish to decode this via bytes2wcstring.
// bytes2wcstring should notice that the decoded form collides with its private use
// and encode it directly.
let ws = str2wcstring(s);
let ws = bytes2wcstring(s);
// Each byte should be encoded directly, and round tripping should work.
assert_eq!(ws.len(), s.len());
assert_eq!(wcs2string(&ws), s);
assert_eq!(wcs2bytes(&ws), s);
}
}

View File

@@ -298,11 +298,11 @@ pub fn string_fuzzy_match_string(
StringFuzzyMatch::try_create(string, match_against, anchor_start)
}
/// Implementation of wcs2string that accepts a callback.
/// Implementation of wcs2bytes that accepts a callback.
/// This invokes `func` with (const char*, size_t) pairs.
/// If `func` returns false, it stops; otherwise it continues.
/// Return false if the callback returned false, otherwise true.
pub fn wcs2string_callback(input: &wstr, mut func: impl FnMut(&[u8]) -> bool) -> bool {
pub fn wcs2bytes_callback(input: &wstr, mut func: impl FnMut(&[u8]) -> bool) -> bool {
let mut state = zero_mbstate();
let mut converted = [0_u8; AT_LEAST_MB_LEN_MAX];
@@ -331,7 +331,7 @@ pub fn wcs2string_callback(input: &wstr, mut func: impl FnMut(&[u8]) -> bool) ->
)
};
if len == 0_usize.wrapping_sub(1) {
wcs2string_bad_char(c);
wcs2bytes_bad_char(c);
state = zero_mbstate();
} else if !func(&converted[..len]) {
return false;
@@ -341,7 +341,7 @@ pub fn wcs2string_callback(input: &wstr, mut func: impl FnMut(&[u8]) -> bool) ->
true
}
fn wcs2string_bad_char(c: char) {
fn wcs2bytes_bad_char(c: char) {
FLOGF!(
char_encoding,
L!("Wide character U+%4X has no narrow representation"),

View File

@@ -1,5 +1,5 @@
use super::wopendir;
use crate::common::{str2wcstring, wcs2zstring};
use crate::common::{bytes2wcstring, wcs2zstring};
use crate::wchar::{WString, wstr};
use crate::wutil::DevInode;
use cfg_if::cfg_if;
@@ -285,7 +285,7 @@ pub fn next(&mut self) -> Option<io::Result<&DirEntry>> {
}
self.entry.reset();
self.entry.name = str2wcstring(d_name);
self.entry.name = bytes2wcstring(d_name);
cfg_if!(
if #[cfg(bsd)] {
self.entry.inode = dent.d_fileno;

View File

@@ -11,10 +11,12 @@
pub mod wcstod;
pub mod wcstoi;
use crate::common::{fish_reserved_codepoint, str2wcstring, wcs2osstring, wcs2string, wcs2zstring};
use crate::common::{
bytes2wcstring, fish_reserved_codepoint, wcs2bytes, wcs2osstring, wcs2zstring,
};
use crate::wchar::{L, WString, wstr};
use crate::wchar_ext::WExt;
use crate::wcstringutil::{join_strings, wcs2string_callback};
use crate::wcstringutil::{join_strings, wcs2bytes_callback};
use crate::{FLOG, fallback};
use errno::errno;
pub use gettext::{
@@ -74,7 +76,7 @@ pub fn wunlink(file_name: &wstr) -> libc::c_int {
}
pub fn wperror(s: &wstr) {
let bytes = wcs2string(s);
let bytes = wcs2bytes(s);
// We can't guarantee the string is 100% Unicode (why?), so we don't use std::str::from_utf8()
let s = OsStr::from_bytes(&bytes).to_string_lossy();
perror(&s)
@@ -102,7 +104,7 @@ pub fn perror_io(s: &str, e: &io::Error) {
/// Wide character version of getcwd().
pub fn wgetcwd() -> WString {
match std::env::current_dir() {
Ok(cwd) => str2wcstring(cwd.into_os_string().as_bytes()),
Ok(cwd) => bytes2wcstring(cwd.into_os_string().as_bytes()),
Err(e) => {
FLOG!(error, "std::env::current_dir() failed with error:", e);
WString::new()
@@ -134,7 +136,7 @@ pub fn wreadlink(file_name: &wstr) -> Option<WString> {
if nbytes == bufsize {
return None;
}
Some(str2wcstring(&target_buf[0..nbytes]))
Some(bytes2wcstring(&target_buf[0..nbytes]))
}
/// Wide character realpath. The last path component does not need to be valid. If an error occurs,
@@ -192,7 +194,7 @@ pub fn wrealpath(pathname: &wstr) -> Option<WString> {
}
};
Some(str2wcstring(&real_path))
Some(bytes2wcstring(&real_path))
}
/// Given an input path, "normalize" it:
@@ -534,7 +536,7 @@ fn do_write(fd: RawFd, total_written: &mut usize, mut buf: &[u8]) -> bool {
true
};
let mut success = wcs2string_callback(input, |buff: &[u8]| {
let mut success = wcs2bytes_callback(input, |buff: &[u8]| {
if buff.len() + accumlen > maxaccum {
// We have to flush.
if !flush_accum(&mut total_written, &accum, &mut accumlen) {

View File

@@ -76,7 +76,7 @@ fn test_wwrite_to_fd() {
}
let amt = wwrite_to_fd(&input, fd.fd()).unwrap();
let narrow = wcs2string(&input);
let narrow = wcs2bytes(&input);
assert_eq!(amt, narrow.len());
assert!(unsafe { libc::lseek(fd.fd(), 0, SEEK_SET) } >= 0);