Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cre4ture committed Feb 18, 2024
1 parent 4893245 commit 18634c1
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/uu/env/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
// spell-checker:ignore (ToDO) chdir execvp progname subcommand subcommands unsets setenv putenv spawnp SIGSEGV SIGBUS sigaction

pub mod parse_error;
pub mod string_parser;
pub mod string_expander;
pub mod split_iterator;
pub mod string_expander;
pub mod string_parser;
pub mod variable_parser;

use clap::builder::ValueParser;
Expand Down
15 changes: 7 additions & 8 deletions src/uu/env/src/split_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::ffi::OsStr;
use std::ffi::OsString;

use crate::parse_error::ParseError;
use crate::string_parser::RawStringExpander;
use crate::string_expander::StringExpander;
use crate::string_parser::StringParser;
use crate::variable_parser::VariableParser;

Expand Down Expand Up @@ -67,14 +67,14 @@ const REPLACEMENTS: [(char, char); 9] = [
const ASCII_WHITESPACE_CHARS: [char; 6] = [' ', '\t', '\r', '\n', '\x0B', '\x0C'];

pub struct SplitIterator<'a> {
raw_parser: RawStringExpander<'a>,
raw_parser: StringExpander<'a>,
words: Vec<OsString>,
}

impl<'a> SplitIterator<'a> {
pub fn new<S: AsRef<OsStr> + ?Sized>(s: &'a S) -> Self {
Self {
raw_parser: RawStringExpander::new(s.as_ref()),
raw_parser: StringExpander::new(s.as_ref()),
words: Vec::<OsString>::new(),
}
}
Expand All @@ -93,7 +93,7 @@ impl<'a> SplitIterator<'a> {
}

fn push_char_to_word(&mut self, c: char) {
self.raw_parser.put_one_char(c)
self.raw_parser.put_one_char(c);
}

fn push_word_to_words(&mut self) {
Expand All @@ -109,10 +109,9 @@ impl<'a> SplitIterator<'a> {
self.raw_parser.get_parser_mut()
}

fn substitute_variable(&mut self) -> Result<(), ParseError>
{
let mut var_parse = VariableParser::<'a, '_>{
parser: self.get_parser_mut()
fn substitute_variable(&mut self) -> Result<(), ParseError> {
let mut var_parse = VariableParser::<'a, '_> {
parser: self.get_parser_mut(),
};

let (name, default) = var_parse.parse_variable()?;
Expand Down
10 changes: 4 additions & 6 deletions src/uu/env/src/string_expander.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

use std::{ffi::{OsStr, OsString}, mem};
use std::{
ffi::{OsStr, OsString},
mem,
};

use crate::string_parser::{Chunk, Error, StringParser};




/// This class makes parsing and word collection more convenient.
///
/// It manages an "output" buffer that is automatically filled.
Expand All @@ -21,8 +21,6 @@ pub struct StringExpander<'a> {
output: OsString,
}



impl<'a> StringExpander<'a> {
pub fn new<S: AsRef<OsStr> + ?Sized>(input: &'a S) -> Self {
Self {
Expand Down
10 changes: 5 additions & 5 deletions src/uu/env/src/string_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
// spell-checker:ignore (words) splitted
#![forbid(unsafe_code)]

use std::{
ffi::{OsStr, OsString},
mem,
};
use std::ffi::OsStr;

use os_str_bytes::OsStrBytesExt;

Expand Down Expand Up @@ -109,7 +106,10 @@ impl<'a> StringParser<'a> {
}

pub fn look_at_chunk(&self) -> Option<Chunk<'a>> {
return self.get_chunk_with_length_at(self.pointer).ok().map(|(chunk, _)| chunk);
return self
.get_chunk_with_length_at(self.pointer)
.ok()
.map(|(chunk, _)| chunk);
}

pub fn consume_one(&mut self) -> Result<Chunk<'a>, Error> {
Expand Down
6 changes: 3 additions & 3 deletions src/uu/env/src/variable_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ use std::{ffi::OsStr, ops::Range};
use crate::{parse_error::ParseError, string_parser::StringParser};

pub struct VariableParser<'a, 'b>
where 'a : 'b
where
'a: 'b,
{
pub parser: &'b mut StringParser<'a>
pub parser: &'b mut StringParser<'a>,
}

impl<'a, 'b> VariableParser<'a, 'b> {

fn get_current_char(&self) -> Option<char> {
self.parser.look_at().ok()
}
Expand Down
23 changes: 13 additions & 10 deletions tests/by-util/test_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,15 +910,18 @@ mod tests_split_iterator {
}

mod test_raw_string_parser {
use std::{ffi::{OsStr, OsString}, os::unix::ffi::OsStringExt};
use std::{
ffi::{OsStr, OsString},
os::unix::ffi::OsStringExt,
};

use env::string_parser;
use env::{string_expander::StringExpander, string_parser};
use os_str_bytes::OsStrBytesExt;

#[test]
fn test_ascii_only_take_one_look_at_correct_data_and_end_behavior() {
let input = "hello";
let mut uut = env::string_parser::RawStringExpander::new(&input);
let mut uut = StringExpander::new(&input);
for c in input.chars() {
assert_eq!(c, uut.get_parser().look_at().unwrap());
uut.take_one().unwrap();
Expand All @@ -941,7 +944,7 @@ mod test_raw_string_parser {
fn test_multi_byte_codes_take_one_look_at_correct_data_and_end_behavior() {
let input = OsString::from("πŸ¦‰πŸ¦‰πŸ¦‰xπŸ¦‰πŸ¦‰xπŸ¦‰xπŸ¦‰πŸ¦‰πŸ¦‰πŸ¦‰");
let owl: char = 'πŸ¦‰';
let mut uut = env::string_parser::RawStringExpander::new(&input);
let mut uut = StringExpander::new(&input);
for _i in 0..3 {
assert_eq!(uut.get_parser().look_at().unwrap(), owl);
uut.take_one().unwrap();
Expand All @@ -968,7 +971,7 @@ mod test_raw_string_parser {
fn test_multi_byte_codes_put_one_ascii_start_middle_end_try_invalid_ascii() {
let input = OsString::from("πŸ¦‰πŸ¦‰πŸ¦‰xπŸ¦‰πŸ¦‰xπŸ¦‰xπŸ¦‰πŸ¦‰πŸ¦‰πŸ¦‰");
let owl: char = 'πŸ¦‰';
let mut uut = env::string_parser::RawStringExpander::new(&input);
let mut uut = StringExpander::new(&input);
uut.put_one_char('a');
for _i in 0..3 {
assert_eq!(uut.get_parser().look_at().unwrap(), owl);
Expand Down Expand Up @@ -1003,7 +1006,7 @@ mod test_raw_string_parser {
#[test]
fn test_multi_byte_codes_skip_one_take_one_skip_until_ascii_char_or_end() {
let input = OsString::from("πŸ¦‰πŸ¦‰πŸ¦‰xπŸ¦‰πŸ¦‰xπŸ¦‰xπŸ¦‰πŸ¦‰πŸ¦‰πŸ¦‰");
let mut uut = env::string_parser::RawStringExpander::new(&input);
let mut uut = StringExpander::new(&input);

uut.skip_one().unwrap(); // skip πŸ¦‰πŸ¦‰πŸ¦‰
assert_eq!(uut.get_look_at_pos(), 12);
Expand All @@ -1028,7 +1031,7 @@ mod test_raw_string_parser {
#[test]
fn test_multi_byte_codes_skip_multiple_ascii_bounded_good_and_bad() {
let input = OsString::from("πŸ¦‰πŸ¦‰πŸ¦‰xπŸ¦‰πŸ¦‰xπŸ¦‰xπŸ¦‰πŸ¦‰πŸ¦‰πŸ¦‰");
let mut uut = env::string_parser::RawStringExpander::new(&input);
let mut uut = StringExpander::new(&input);

uut.get_parser_mut().skip_multiple_ascii_bounded(0);
assert_eq!(uut.get_look_at_pos(), 0);
Expand All @@ -1052,7 +1055,7 @@ mod test_raw_string_parser {
#[test]
fn test_multi_byte_codes_put_string_utf8_start_middle_end() {
let input = OsString::from("πŸ¦‰πŸ¦‰πŸ¦‰xπŸ¦‰πŸ¦‰xπŸ¦‰xπŸ¦‰πŸ¦‰πŸ¦‰πŸ¦‰");
let mut uut = env::string_parser::RawStringExpander::new(&input);
let mut uut = StringExpander::new(&input);

uut.put_string("πŸ¦”oo");
uut.take_one().unwrap(); // takes πŸ¦‰πŸ¦‰πŸ¦‰
Expand All @@ -1068,7 +1071,7 @@ mod test_raw_string_parser {
#[test]
fn test_multi_byte_codes_look_at_remaining_start_middle_end() {
let input = "πŸ¦‰πŸ¦‰πŸ¦‰xπŸ¦‰πŸ¦‰xπŸ¦‰xπŸ¦‰πŸ¦‰πŸ¦‰πŸ¦‰";
let mut uut = env::string_parser::RawStringExpander::new(&input);
let mut uut = StringExpander::new(&input);

assert_eq!(uut.get_parser().look_at_remaining(), input);
uut.take_one().unwrap(); // takes πŸ¦‰πŸ¦‰πŸ¦‰
Expand All @@ -1088,7 +1091,7 @@ mod test_raw_string_parser {
let owl_b = "πŸ¦‰".bytes().next().unwrap();
let input_u8 = [b'<', owl_b, b'>'];
let input_str = OsString::from_vec(input_u8.to_vec());
let mut uut = env::string_parser::RawStringExpander::new(&input_str);
let mut uut = StringExpander::new(&input_str);

assert_eq!(uut.get_parser().look_at_remaining(), input_str);
assert_eq!(uut.get_parser().look_at().unwrap(), '<');
Expand Down

0 comments on commit 18634c1

Please sign in to comment.