Remove wcstringutil::split_string()

It is short and simple enough to write yourself if you need it and it encourages
bad behavior by a) always returning owned strings, b) always allocating them in
a vector. If/where possible, it is better to a) use &wstr, b) use an iterator.

In rust, it's an anti-pattern to unnecessarily abstract over allocating
operations. Some of the call sites even called split_string(..).into_iter().
This commit is contained in:
Mahmoud Al-Qudsi 2024-05-21 11:19:37 -05:00
parent 96b979077c
commit d14d8d5733
6 changed files with 20 additions and 22 deletions

View file

@ -4,7 +4,6 @@ use super::prelude::*;
use crate::env::{EnvMode, EnvStack}; use crate::env::{EnvMode, EnvStack};
use crate::exec::exec_subshell; use crate::exec::exec_subshell;
use crate::wcstringutil::split_string;
use crate::wutil::fish_iswalnum; use crate::wutil::fish_iswalnum;
const VAR_NAME_PREFIX: &wstr = L!("_flag_"); const VAR_NAME_PREFIX: &wstr = L!("_flag_");
@ -158,7 +157,7 @@ fn check_for_mutually_exclusive_flags(
// information to parse the values associated with any `--exclusive` flags. // information to parse the values associated with any `--exclusive` flags.
fn parse_exclusive_args(opts: &mut ArgParseCmdOpts, streams: &mut IoStreams) -> Option<c_int> { fn parse_exclusive_args(opts: &mut ArgParseCmdOpts, streams: &mut IoStreams) -> Option<c_int> {
for raw_xflags in &opts.raw_exclusive_flags { for raw_xflags in &opts.raw_exclusive_flags {
let xflags = split_string(raw_xflags, ','); let xflags: Vec<_> = raw_xflags.split(',').collect();
if xflags.len() < 2 { if xflags.len() < 2 {
streams.err.append(wgettext_fmt!( streams.err.append(wgettext_fmt!(
"%ls: exclusive flag string '%ls' is not valid\n", "%ls: exclusive flag string '%ls' is not valid\n",
@ -169,7 +168,7 @@ fn parse_exclusive_args(opts: &mut ArgParseCmdOpts, streams: &mut IoStreams) ->
} }
let exclusive_set: &mut Vec<char> = &mut vec![]; let exclusive_set: &mut Vec<char> = &mut vec![];
for flag in &xflags { for flag in xflags {
if flag.char_count() == 1 && opts.options.contains_key(&flag.char_at(0)) { if flag.char_count() == 1 && opts.options.contains_key(&flag.char_at(0)) {
let short = flag.char_at(0); let short = flag.char_at(0);
// It's a short flag. // It's a short flag.

View file

@ -1,7 +1,5 @@
use super::*; use super::*;
use crate::wcstringutil::split_string;
#[derive(Default)] #[derive(Default)]
pub struct Length { pub struct Length {
quiet: bool, quiet: bool,
@ -36,11 +34,17 @@ impl StringSubCommand<'_> for Length {
for (arg, _) in arguments(args, optind, streams) { for (arg, _) in arguments(args, optind, streams) {
if self.visible { if self.visible {
// Visible length only makes sense line-wise. // Visible length only makes sense line-wise.
for line in split_string(&arg, '\n') { for line in {
let val: &wstr = &arg;
val.split('\n')
} {
let mut max = 0; let mut max = 0;
// Carriage-return returns us to the beginning. The longest substring without // Carriage-return returns us to the beginning. The longest substring without
// carriage-return determines the overall width. // carriage-return determines the overall width.
for reset in split_string(&line, '\r') { for reset in {
let val = &line;
val.split('\r')
} {
let n = width_without_escapes(&reset, 0); let n = width_without_escapes(&reset, 0);
max = usize::max(max, n); max = usize::max(max, n);
} }

View file

@ -1,6 +1,5 @@
use super::*; use super::*;
use crate::common::get_ellipsis_str; use crate::common::get_ellipsis_str;
use crate::wcstringutil::split_string;
pub struct Shorten<'args> { pub struct Shorten<'args> {
ellipsis: &'args wstr, ellipsis: &'args wstr,
@ -92,13 +91,14 @@ impl<'args> StringSubCommand<'args> for Shorten<'args> {
// Visible width only makes sense line-wise. // Visible width only makes sense line-wise.
// So either we have no-newlines (which means we shorten on the first newline), // So either we have no-newlines (which means we shorten on the first newline),
// or we handle the lines separately. // or we handle the lines separately.
let mut splits = split_string(&arg, '\n').into_iter(); let mut splits = arg.split('\n').peekable();
if self.no_newline && splits.len() > 1 { if self.no_newline && splits.peek().is_some() {
let mut s = match self.shorten_from { let mut s = match self.shorten_from {
Direction::Right => splits.next(), Direction::Right => splits.next(),
Direction::Left => splits.last(), Direction::Left => splits.last(),
} }
.unwrap(); .unwrap()
.to_owned();
s.push_utfstr(&self.ellipsis); s.push_utfstr(&self.ellipsis);
let width = width_without_escapes(&s, 0); let width = width_without_escapes(&s, 0);
@ -112,7 +112,7 @@ impl<'args> StringSubCommand<'args> for Shorten<'args> {
if width > 0 && width < min_width { if width > 0 && width < min_width {
min_width = width; min_width = width;
} }
inputs.push(s); inputs.push(s.into());
} }
} }
} }

View file

@ -11,7 +11,7 @@ use crate::flog::{FLOG, FLOGF};
use crate::path::path_get_config; use crate::path::path_get_config;
use crate::path::{path_get_config_remoteness, DirRemoteness}; use crate::path::{path_get_config_remoteness, DirRemoteness};
use crate::wchar::{decode_byte_from_char, prelude::*}; use crate::wchar::{decode_byte_from_char, prelude::*};
use crate::wcstringutil::{join_strings, split_string, string_suffixes_string, LineIterator}; use crate::wcstringutil::{join_strings, string_suffixes_string, LineIterator};
use crate::wutil::{ use crate::wutil::{
file_id_for_fd, file_id_for_path, file_id_for_path_narrow, wdirname, wrealpath, wrename, wstat, file_id_for_fd, file_id_for_path, file_id_for_path_narrow, wdirname, wrealpath, wrename, wstat,
wunlink, FileId, INVALID_FILE_ID, wunlink, FileId, INVALID_FILE_ID,
@ -995,7 +995,7 @@ fn decode_serialized(val: &wstr) -> Vec<WString> {
if val == ENV_NULL { if val == ENV_NULL {
return vec![]; return vec![];
} }
split_string(val, UVAR_ARRAY_SEP) val.split(UVAR_ARRAY_SEP).map(|v| v.to_owned()).collect()
} }
/// Decode a a list into a serialized universal variable value. /// Decode a a list into a serialized universal variable value.

View file

@ -325,11 +325,6 @@ fn wcs2string_bad_char(c: char) {
); );
} }
/// Split a string by a separator character.
pub fn split_string(val: &wstr, sep: char) -> Vec<WString> {
val.split(sep).map(wstr::to_owned).collect()
}
/// Split a string by runs of any of the separator characters provided in `seps`. /// Split a string by runs of any of the separator characters provided in `seps`.
/// Note the delimiters are the characters in `seps`, not `seps` itself. /// Note the delimiters are the characters in `seps`, not `seps` itself.
/// `seps` may contain the NUL character. /// `seps` may contain the NUL character.

View file

@ -17,7 +17,7 @@ use crate::fds::AutoCloseFd;
use crate::flog::FLOGF; use crate::flog::FLOGF;
use crate::wchar::{wstr, WString, L}; use crate::wchar::{wstr, WString, L};
use crate::wchar_ext::WExt; use crate::wchar_ext::WExt;
use crate::wcstringutil::{join_strings, split_string, wcs2string_callback}; use crate::wcstringutil::{join_strings, wcs2string_callback};
use errno::errno; use errno::errno;
pub use gettext::{wgettext, wgettext_fmt, wgettext_maybe_fmt, wgettext_str}; pub use gettext::{wgettext, wgettext_fmt, wgettext_maybe_fmt, wgettext_str};
pub use printf::sprintf; pub use printf::sprintf;
@ -291,8 +291,8 @@ pub fn path_normalize_for_cd(wd: &wstr, path: &wstr) -> WString {
} }
// Split our strings by the sep. // Split our strings by the sep.
let mut wd_comps = split_string(wd, sep); let mut wd_comps = wd.split(sep).collect::<Vec<_>>();
let path_comps = split_string(path, sep); let path_comps = path.split(sep).collect::<Vec<_>>();
// Remove empty segments from wd_comps. // Remove empty segments from wd_comps.
// In particular this removes the leading and trailing empties. // In particular this removes the leading and trailing empties.