From a14906f52fd2c6bd06384b1b22a30e02fabeaa00 Mon Sep 17 00:00:00 2001 From: Peter Ammon Date: Fri, 27 Dec 2024 15:00:41 -0800 Subject: [PATCH] Fix a todo! Stop cloning the delimiter unnecessarily in builtin read. --- src/builtins/read.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/builtins/read.rs b/src/builtins/read.rs index 7051897f3..d57e153b0 100644 --- a/src/builtins/read.rs +++ b/src/builtins/read.rs @@ -686,15 +686,15 @@ pub fn read(parser: &Parser, streams: &mut IoStreams, argv: &mut [&wstr]) -> Opt continue; } - // todo!("don't clone") - let delimiter = opts - .delimiter - .clone() - .or_else(|| { - let ifs = parser.vars().get_unless_empty(L!("IFS")); - ifs.map(|ifs| ifs.as_string()) - }) - .unwrap_or_default(); + let mut ifs_delimiter = WString::new(); + let delimiter: &wstr = opts.delimiter.as_deref().unwrap_or_else(|| { + ifs_delimiter = parser + .vars() + .get_unless_empty(L!("IFS")) + .map(|var| var.as_string()) + .unwrap_or_default(); + &ifs_delimiter + }); if delimiter.is_empty() { // Every character is a separate token with one wrinkle involving non-array mode where @@ -735,7 +735,7 @@ pub fn read(parser: &Parser, streams: &mut IoStreams, argv: &mut [&wstr]) -> Opt if opts.delimiter.is_none() { // We're using IFS, so tokenize the buffer using each IFS char. This is for backward // compatibility with old versions of fish. - let tokens = split_string_tok(&buff, &delimiter, None) + let tokens = split_string_tok(&buff, delimiter, None) .into_iter() .map(|s| s.to_owned()) .collect(); @@ -743,7 +743,7 @@ pub fn read(parser: &Parser, streams: &mut IoStreams, argv: &mut [&wstr]) -> Opt var_ptr += 1; } else { // We're using a delimiter provided by the user so use the `string split` behavior. - let splits = split_about(&buff, &delimiter, usize::MAX, false) + let splits = split_about(&buff, delimiter, usize::MAX, false) .into_iter() .map(|s| s.to_owned()) .collect(); @@ -757,7 +757,7 @@ pub fn read(parser: &Parser, streams: &mut IoStreams, argv: &mut [&wstr]) -> Opt // compatibility with old versions of fish. // Note the final variable gets any remaining text. let mut var_vals: Vec = - split_string_tok(&buff, &delimiter, Some(vars_left(var_ptr))) + split_string_tok(&buff, delimiter, Some(vars_left(var_ptr))) .into_iter() .map(|s| s.to_owned()) .collect(); @@ -775,7 +775,7 @@ pub fn read(parser: &Parser, streams: &mut IoStreams, argv: &mut [&wstr]) -> Opt // We're using a delimiter provided by the user so use the `string split` behavior. // We're making at most argc - 1 splits so the last variable // is set to the remaining string. - let splits = split_about(&buff, &delimiter, argc - 1, false); + let splits = split_about(&buff, delimiter, argc - 1, false); assert!(splits.len() <= vars_left(var_ptr)); for split in splits { parser.set_var_and_fire(argv[var_ptr], opts.place, vec![split.to_owned()]);