Skip to content

Commit

Permalink
cleanup, renames
Browse files Browse the repository at this point in the history
  • Loading branch information
cre4ture committed Feb 18, 2024
1 parent e348613 commit 4893245
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 84 deletions.
6 changes: 3 additions & 3 deletions src/uu/env/src/split_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::ffi::OsString;

use crate::parse_error::ParseError;
use crate::string_parser::RawStringExpander;
use crate::string_parser::RawStringParser;
use crate::string_parser::StringParser;
use crate::variable_parser::VariableParser;

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -101,11 +101,11 @@ impl<'a> SplitIterator<'a> {
self.words.push(word);
}

fn get_parser(&self) -> &RawStringParser<'a> {
fn get_parser(&self) -> &StringParser<'a> {
self.raw_parser.get_parser()
}

fn get_parser_mut(&mut self) -> &mut RawStringParser<'a> {
fn get_parser_mut(&mut self) -> &mut StringParser<'a> {
self.raw_parser.get_parser_mut()
}

Expand Down
16 changes: 8 additions & 8 deletions src/uu/env/src/string_expander.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

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

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



Expand All @@ -16,33 +16,33 @@ use crate::string_parser::{Chunk, Error, RawStringParser};
/// It provides "skip_one" and "take_one" that focus on
/// working with ASCII separators. Thus they will skip or take
/// all consecutive non-ascii char sequences at once.
pub struct RawStringExpander<'a> {
parser: RawStringParser<'a>,
pub struct StringExpander<'a> {
parser: StringParser<'a>,
output: OsString,
}



impl<'a> RawStringExpander<'a> {
impl<'a> StringExpander<'a> {
pub fn new<S: AsRef<OsStr> + ?Sized>(input: &'a S) -> Self {
Self {
parser: RawStringParser::new(input),
parser: StringParser::new(input),
output: OsString::default(),
}
}

pub fn new_at(input: &'a OsStr, pos: usize) -> Result<Self, Error> {
Ok(Self {
parser: RawStringParser::new_at(input, pos)?,
parser: StringParser::new_at(input, pos)?,
output: OsString::default(),
})
}

pub fn get_parser(&self) -> &RawStringParser<'a> {
pub fn get_parser(&self) -> &StringParser<'a> {
&self.parser
}

pub fn get_parser_mut(&mut self) -> &mut RawStringParser<'a> {
pub fn get_parser_mut(&mut self) -> &mut StringParser<'a> {
&mut self.parser
}

Expand Down
73 changes: 2 additions & 71 deletions src/uu/env/src/string_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,13 @@ pub enum Chunk<'a> {
/// This class makes parsing a OsString char by char more convenient.
///
/// It also allows to capturing of intermediate positions for later splitting.
pub struct RawStringParser<'a> {
pub struct StringParser<'a> {
pub input: &'a OsStr,
pointer: usize,
pointer_str: &'a OsStr,
}

/// This class makes parsing and word collection more convenient.
///
/// It manages an "output" buffer that is automatically filled.
/// It provides "skip_one" and "take_one" that focus on
/// working with ASCII separators. Thus they will skip or take
/// all consecutive non-ascii char sequences at once.
pub struct RawStringExpander<'a> {
parser: RawStringParser<'a>,
output: OsString,
}

impl<'a> RawStringParser<'a> {
impl<'a> StringParser<'a> {
pub fn new<S: AsRef<OsStr> + ?Sized>(input: &'a S) -> Self {
let input = input.as_ref();
Self {
Expand Down Expand Up @@ -187,61 +176,3 @@ impl<'a> RawStringParser<'a> {
self.pointer_str = self.look_at_remaining();
}
}


impl<'a> RawStringExpander<'a> {
pub fn new<S: AsRef<OsStr> + ?Sized>(input: &'a S) -> Self {
Self {
parser: RawStringParser::new(input),
output: OsString::default(),
}
}

pub fn new_at(input: &'a OsStr, pos: usize) -> Result<Self, Error> {
Ok(Self {
parser: RawStringParser::new_at(input, pos)?,
output: OsString::default(),
})
}

pub fn get_parser(&self) -> &RawStringParser<'a> {
&self.parser
}

pub fn get_parser_mut(&mut self) -> &mut RawStringParser<'a> {
&mut self.parser
}

pub fn skip_one(&mut self) -> Result<(), Error> {
self.get_parser_mut()
.consumer_one_ascii_or_all_non_ascii()?;
Ok(())
}

pub fn get_look_at_pos(&self) -> usize {
self.get_parser().get_look_at_pos()
}

pub fn take_one(&mut self) -> Result<(), Error> {
let chunks = self.parser.consumer_one_ascii_or_all_non_ascii()?;
for chunk in chunks {
match chunk {
Chunk::InvalidEncoding(invalid) => self.output.push(invalid),
Chunk::ValidChar(char) => self.output.push(char.to_string()),
}
}
Ok(())
}

pub fn put_one_char(&mut self, c: char) {
self.output.push(c.to_string());
}

pub fn put_string<S: AsRef<OsStr>>(&mut self, str: S) {
self.output.push(str);
}

pub fn take_collected_output(&mut self) -> OsString {
mem::take(&mut self.output)
}
}
4 changes: 2 additions & 2 deletions src/uu/env/src/variable_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

use std::{ffi::OsStr, ops::Range};

use crate::{parse_error::ParseError, string_parser::RawStringParser};
use crate::{parse_error::ParseError, string_parser::StringParser};

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

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

0 comments on commit 4893245

Please sign in to comment.