From db1d0bb7c37e2c2943fe0fd8b6857110fdd00087 Mon Sep 17 00:00:00 2001 From: Jonas Maier <> Date: Thu, 5 Mar 2026 08:25:17 +0100 Subject: switch back to byte buffers for strings --- src/parse.rs | 51 +++++++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/src/parse.rs b/src/parse.rs index fae00a3..ec4be30 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -13,8 +13,8 @@ pub struct Pipes { } pub struct Command { - pub path: String, - pub args: Vec, + pub path: Vec, + pub args: Vec>, } enum ParseError { @@ -26,41 +26,40 @@ enum ParseError { UnexpectedPipe, - UnknownChar(char), + Unknownu8(u8), } type Result = std::result::Result; -pub fn do_parse(x: &str) -> Result { - let chars: Vec = x.chars().collect(); - Ast::parse(&mut &chars[..]) +pub fn do_parse(mut x: &[u8]) -> Result { + Ast::parse(&mut x) } trait Parse: Sized { - fn parse(b: &mut &[char]) -> Result; + fn parse(b: &mut &[u8]) -> Result; } #[inline(always)] -fn parse(b: &mut &[char]) -> Result { +fn parse(b: &mut &[u8]) -> Result { T::parse(b) } -fn spaces(b: &mut &[char]) { - while let Some(' ' | '\t') = b.get(0) { +fn spaces(b: &mut &[u8]) { + while let Some(b' ' | b'\t') = b.get(0) { *b = &b[1..]; } } #[inline(always)] -fn adv(b: &mut &[char]) { +fn adv(b: &mut &[u8]) { *b = &b[1..] } -fn parse_quoted_string(b: &mut &[char], delim: char) -> Result { +fn parse_quoted_string(b: &mut &[u8], delim: u8) -> Result> { // TODO: escape sequence stuff *b = &b[1..]; - let mut s = String::new(); + let mut s = Vec::new(); while b.len() > 0 { if b[0] == delim { adv(b); @@ -73,38 +72,38 @@ fn parse_quoted_string(b: &mut &[char], delim: char) -> Result { Err(ParseError::Incomplete) } -impl Parse for String { - fn parse(b: &mut &[char]) -> Result { +impl Parse for Vec { + fn parse(b: &mut &[u8]) -> Result { spaces(b); if b.is_empty() { return Err(ParseError::Eof); } let c = b[0]; - if c == '|' { + if c == b'|' { Err(ParseError::UnexpectedPipe) - } else if c == '\'' || c == '"' { + } else if c == b'\'' || c == b'"' { adv(b); parse_quoted_string(b, c) } else if c.is_ascii_graphic() { - parse_quoted_string(b, ' ') + parse_quoted_string(b, b' ') } else { - Err(ParseError::UnknownChar(c)) + Err(ParseError::Unknownu8(c)) } } } impl Parse for Ast { - fn parse(b: &mut &[char]) -> Result { + fn parse(b: &mut &[u8]) -> Result { Ok(Self::Pipes(parse(b)?)) } } impl Parse for Command { - fn parse(b: &mut &[char]) -> Result { - let path: String = parse(b)?; + fn parse(b: &mut &[u8]) -> Result { + let path: Vec = parse(b)?; let mut args = Vec::new(); loop { - let arg: Result = parse(b); + let arg: Result> = parse(b); match arg { Ok(arg) => args.push(arg), Err(ParseError::Eof | ParseError::UnexpectedPipe) => break, @@ -116,7 +115,7 @@ impl Parse for Command { } impl Parse for Pipes { - fn parse(b: &mut &[char]) -> Result { + fn parse(b: &mut &[u8]) -> Result { let cmds: Vec = vec![parse(b)?]; loop { @@ -126,10 +125,10 @@ impl Parse for Pipes { } let c = b[0]; - if c == '|' { + if c == b'|' { adv(b); } else { - Err(ParseError::UnknownChar(c))?; + Err(ParseError::Unknownu8(c))?; } } } -- cgit v1.2.3