From 04fd13b801c8b44b105da412a0dfaebe908b2851 Mon Sep 17 00:00:00 2001 From: Ahurac Date: Sun, 14 Apr 2024 12:07:42 +0200 Subject: [PATCH] parser : gestion des whitespaces --- src/parser.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/parser.rs b/src/parser.rs index d258fbb..4e9a378 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,8 +1,31 @@ use crate::command::command_builder::CommandBuilder; use crate::command::command_sequence::CommandSequence; +fn build_argv(characters: &mut Vec, current_arg: &mut String) -> Vec { + if characters.is_empty() { + vec![] + } else { + let current_char = characters.pop().unwrap(); + + if current_char.is_whitespace() { + if !current_arg.is_empty() { + let mut argv: Vec = vec![current_arg.clone()]; + argv.append(&mut build_argv(characters, &mut String::from(""))); + + argv + } else { + build_argv(characters, current_arg) + } + } else { + current_arg.push(current_char); + build_argv(characters, current_arg) + } + } +} + pub fn parse_command_line(line: String) -> Option { - let argv: Vec = line.split_whitespace().map(|s| s.to_string()).collect(); + let mut characters: Vec = line.chars().rev().collect(); + let argv = build_argv(&mut characters, &mut String::from("")); if !argv.is_empty() { let command = CommandBuilder::new(argv).build();