diff --git a/src/uu/env/src/split_iterator.rs b/src/uu/env/src/split_iterator.rs index 9b949363cb1..ab7c2c4e1a7 100644 --- a/src/uu/env/src/split_iterator.rs +++ b/src/uu/env/src/split_iterator.rs @@ -18,9 +18,11 @@ #![forbid(unsafe_code)] +use std::borrow::Cow; use std::ffi::OsStr; use std::ffi::OsString; +use crate::native_int_str::from_native_int_representation; use crate::native_int_str::from_native_int_representation_owned; use crate::native_int_str::to_native_int_representation; use crate::native_int_str::NativeCharInt; @@ -103,14 +105,15 @@ impl<'a> SplitIterator<'a> { let (name, default) = var_parse.parse_variable()?; - let value = std::env::var_os(name); + let varname_os_str_cow = from_native_int_representation(Cow::Borrowed(name)); + let value = std::env::var_os(varname_os_str_cow); match (&value, default) { (None, None) => {} // do nothing, just replace it with "" (Some(value), _) => { self.expander.put_string(value); } (None, Some(default)) => { - self.expander.put_string(default); + self.expander.put_native_string(default); } }; diff --git a/src/uu/env/src/string_expander.rs b/src/uu/env/src/string_expander.rs index ae56727c3ce..06e4699269f 100644 --- a/src/uu/env/src/string_expander.rs +++ b/src/uu/env/src/string_expander.rs @@ -82,6 +82,10 @@ impl<'a> StringExpander<'a> { self.output.extend(native.deref()); } + pub fn put_native_string(&mut self, n_str: &NativeIntStr) { + self.output.extend(n_str); + } + pub fn take_collected_output(&mut self) -> Vec { mem::take(&mut self.output) } diff --git a/src/uu/env/src/variable_parser.rs b/src/uu/env/src/variable_parser.rs index 5ed7a7039d5..e9abf6ef05d 100644 --- a/src/uu/env/src/variable_parser.rs +++ b/src/uu/env/src/variable_parser.rs @@ -3,10 +3,10 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -use std::{borrow::Cow, ffi::OsStr, ops::Range}; +use std::ops::Range; use crate::{ - native_int_str::from_native_int_representation, parse_error::ParseError, + native_int_str::NativeIntStr, parse_error::ParseError, string_parser::StringParser, }; @@ -37,7 +37,7 @@ impl<'a, 'b> VariableParser<'a, 'b> { fn parse_braced_variable_name( &mut self, - ) -> Result<(Cow<'a, OsStr>, Option>), ParseError> { + ) -> Result<(&'a NativeIntStr, Option<&'a NativeIntStr>), ParseError> { let pos_start = self.parser.get_peek_position(); self.check_variable_name_start()?; @@ -88,7 +88,7 @@ impl<'a, 'b> VariableParser<'a, 'b> { }; } - let default = if let Some(default_end) = default_end { + let default_opt = if let Some(default_end) = default_end { Some(self.parser.substring(&Range { start: varname_end + 1, end: default_end, @@ -102,13 +102,10 @@ impl<'a, 'b> VariableParser<'a, 'b> { end: varname_end, }); - let varname_cow = from_native_int_representation(Cow::Borrowed(varname)); - let default_opt_cow = default.map(|x| from_native_int_representation(Cow::Borrowed(x))); - - Ok((varname_cow, default_opt_cow)) + Ok((varname, default_opt)) } - fn parse_unbraced_variable_name(&mut self) -> Result, ParseError> { + fn parse_unbraced_variable_name(&mut self) -> Result<&'a NativeIntStr, ParseError> { let pos_start = self.parser.get_peek_position(); self.check_variable_name_start()?; @@ -136,14 +133,13 @@ impl<'a, 'b> VariableParser<'a, 'b> { start: pos_start, end: pos_end, }); - let varname_cow = Cow::Borrowed(varname); - Ok(from_native_int_representation(varname_cow)) + Ok(varname) } pub fn parse_variable( &mut self, - ) -> Result<(Cow<'a, OsStr>, Option>), ParseError> { + ) -> Result<(&'a NativeIntStr, Option<&'a NativeIntStr>), ParseError> { self.skip_one()?; let (name, default) = match self.get_current_char() {