diff options
| author | Jonas Maier <> | 2026-03-05 08:25:17 +0100 |
|---|---|---|
| committer | Jonas Maier <> | 2026-03-05 08:25:17 +0100 |
| commit | db1d0bb7c37e2c2943fe0fd8b6857110fdd00087 (patch) | |
| tree | ace74c464323ed5e32e96d5e13092cd280658062 | |
| parent | b2c463e8bbc8c4685d5979d2b8c5a0cb2706fba2 (diff) | |
| download | pish-db1d0bb7c37e2c2943fe0fd8b6857110fdd00087.tar.gz | |
switch back to byte buffers for strings
| -rw-r--r-- | src/parse.rs | 51 |
1 files 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<String>, + pub path: Vec<u8>, + pub args: Vec<Vec<u8>>, } enum ParseError { @@ -26,41 +26,40 @@ enum ParseError { UnexpectedPipe, - UnknownChar(char), + Unknownu8(u8), } type Result<T> = std::result::Result<T, ParseError>; -pub fn do_parse(x: &str) -> Result<Ast> { - let chars: Vec<char> = x.chars().collect(); - Ast::parse(&mut &chars[..]) +pub fn do_parse(mut x: &[u8]) -> Result<Ast> { + Ast::parse(&mut x) } trait Parse: Sized { - fn parse(b: &mut &[char]) -> Result<Self>; + fn parse(b: &mut &[u8]) -> Result<Self>; } #[inline(always)] -fn parse<T: Parse>(b: &mut &[char]) -> Result<T> { +fn parse<T: Parse>(b: &mut &[u8]) -> Result<T> { 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<String> { +fn parse_quoted_string(b: &mut &[u8], delim: u8) -> Result<Vec<u8>> { // 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<String> { Err(ParseError::Incomplete) } -impl Parse for String { - fn parse(b: &mut &[char]) -> Result<Self> { +impl Parse for Vec<u8> { + fn parse(b: &mut &[u8]) -> Result<Self> { 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<Self> { + fn parse(b: &mut &[u8]) -> Result<Self> { Ok(Self::Pipes(parse(b)?)) } } impl Parse for Command { - fn parse(b: &mut &[char]) -> Result<Self> { - let path: String = parse(b)?; + fn parse(b: &mut &[u8]) -> Result<Self> { + let path: Vec<u8> = parse(b)?; let mut args = Vec::new(); loop { - let arg: Result<String> = parse(b); + let arg: Result<Vec<u8>> = 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<Self> { + fn parse(b: &mut &[u8]) -> Result<Self> { let cmds: Vec<Command> = 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))?; } } } |
