Compare commits
4 Commits
d1ce119192
...
ff79dae5cb
Author | SHA1 | Date |
---|---|---|
Lilly Rosaline | ff79dae5cb | 3 years ago |
Lilly Rosaline | f16073b955 | 3 years ago |
Lilly Rosaline | 315d97b358 | 3 years ago |
Lilly Rosaline | 952c01abfc | 3 years ago |
@ -1,140 +1,50 @@
|
||||
use std::str::Chars;
|
||||
use std::iter::FromIterator;
|
||||
use std::process::exit;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::*;
|
||||
|
||||
#[test]
|
||||
fn tests() {
|
||||
fn test_00() {
|
||||
dbg!(parse_args("Hello world", 0x00));
|
||||
dbg!(parse_args("\"Hello world\'\"", 0x07));
|
||||
}
|
||||
#[test]
|
||||
fn test_01() {
|
||||
dbg!(parse_args("Hello world", 0x01));
|
||||
}
|
||||
#[test]
|
||||
fn test_02() {
|
||||
dbg!(parse_args("Hello worLd", 0x02));
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses the command line arguments into a list of parameters.
|
||||
/// The following parsing modes can be utilized:
|
||||
/// 00: Returns exactly what was inputted.
|
||||
/// 01: Returns the contents, split by whitespace indiscriminately.
|
||||
/// 02: Returns the contents, split by whitespace indiscriminately and made lowercase.
|
||||
pub fn parse_args(input: &str, mode: u8) -> Vec<String> {
|
||||
dbg!(&input);
|
||||
|
||||
let args = &mut input.clone().chars().into_iter();
|
||||
let mut vargs: Vec<char> = vec![];
|
||||
for arg in args {
|
||||
vargs.push(arg);
|
||||
}
|
||||
|
||||
dbg!(&vargs);
|
||||
let args: &mut [char] = &mut vargs.as_mut_slice();
|
||||
let split_input: Vec<_> = input.chars().map(|arg| arg.clone()).collect(); // Converts from the input string to a vector of chars.
|
||||
dbg!(&split_input);
|
||||
match mode {
|
||||
/// Returns exact input string.
|
||||
0x00 => {vec![input.to_string()]},
|
||||
/// Seperates all strings
|
||||
0x07 => {
|
||||
let mut arguments: Vec<String> = vec![];
|
||||
// This is all the stuff that will be split, marked, preprocessed, or removed
|
||||
let mut singlequotes: Vec<usize> = vec![];
|
||||
let mut doublequotes: Vec<usize> = vec![];
|
||||
let mut variables: Vec<usize> = vec![];
|
||||
let mut escaped_spaces: Vec<usize> = vec![];
|
||||
let mut extraneous_backslashes: Vec<usize> = vec![];
|
||||
|
||||
let mut marked_for_removal: Vec<usize> = vec![];
|
||||
|
||||
|
||||
// Begin by marking characters that have special behavior.
|
||||
for (i, c) in args.iter().enumerate() {
|
||||
// This absolutely cursed line prevents array underflows, while making sure that the character is not escaped
|
||||
let escaped: bool = if i != 0 {args[i - 1] == '\\'} else {false} && if i > 1 {args[i - 2] == '\\' }else {false};
|
||||
dbg!(&escaped);
|
||||
dbg!(&i);
|
||||
dbg!(&c);
|
||||
|
||||
match c {
|
||||
'\'' => {
|
||||
if escaped {
|
||||
singlequotes.push(i);
|
||||
}
|
||||
},
|
||||
'\"' => {
|
||||
if escaped {
|
||||
doublequotes.push(i);
|
||||
}
|
||||
}
|
||||
'$' => {
|
||||
if escaped {
|
||||
variables.push(i);
|
||||
}
|
||||
}
|
||||
' ' => {
|
||||
if escaped {
|
||||
escaped_spaces.push(i);
|
||||
}
|
||||
}
|
||||
'\\' => {
|
||||
if args[if i != 0 {i - 1} else {0}] != '\\' && (args[if i != args.len() {i + 1} else {args.len()}] == ' ' || args[if i != args.len() {i + 1} else {args.len()}] == '\n') {
|
||||
extraneous_backslashes.push(i);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
};
|
||||
}
|
||||
dbg!(&singlequotes);
|
||||
dbg!(&doublequotes);
|
||||
dbg!(&variables);
|
||||
dbg!(&escaped_spaces);
|
||||
dbg!(&extraneous_backslashes);
|
||||
let mut quoted = false;
|
||||
let mut singlequoted = false;
|
||||
let mut doublequoted = false;
|
||||
|
||||
for i in singlequotes {
|
||||
if !singlequoted && !quoted {
|
||||
singlequoted = true;
|
||||
quoted = true;
|
||||
marked_for_removal.push(i);
|
||||
} else {
|
||||
singlequoted = false;
|
||||
if !doublequoted {
|
||||
quoted = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
for i in doublequotes {
|
||||
if !doublequoted && !quoted {
|
||||
doublequoted = true;
|
||||
quoted = true;
|
||||
marked_for_removal.push(i);
|
||||
} else {
|
||||
doublequoted = false;
|
||||
if !singlequoted {
|
||||
quoted = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
//TODO handle variables here
|
||||
for i in escaped_spaces {
|
||||
marked_for_removal.push(if i != 0 {i - 1} else {0});
|
||||
}
|
||||
for i in extraneous_backslashes {
|
||||
marked_for_removal.push(i);
|
||||
}
|
||||
for i in marked_for_removal {
|
||||
args[i] = char::from(0xFF);
|
||||
}
|
||||
dbg!(&args);
|
||||
let mut last: usize = 0;
|
||||
for (i, c) in args.iter().enumerate() {
|
||||
let mut output: String = "".to_string();
|
||||
for o in args.to_vec().get(last + 1..if i != 0 {i - 1} else {0}).unwrap().iter() {output.push(*o)};
|
||||
dbg!(&output);
|
||||
arguments.push(output);
|
||||
last = i;
|
||||
dbg!(&arguments);
|
||||
}
|
||||
return arguments;
|
||||
},
|
||||
///
|
||||
0x10 => {
|
||||
vec![]
|
||||
},
|
||||
0x00 => {vec![String::from_iter(split_input.iter())]}, // Simply converts the vector of chars into a string, and puts it as the first element.
|
||||
0x01 => {
|
||||
return input
|
||||
.to_string()
|
||||
.split_ascii_whitespace()
|
||||
.map(|x| x.to_string())
|
||||
.collect();
|
||||
}
|
||||
0x02 => {
|
||||
return input
|
||||
.to_string()
|
||||
.to_ascii_lowercase()
|
||||
.split_ascii_whitespace()
|
||||
.map(|x| x.to_string())
|
||||
.collect();
|
||||
}
|
||||
_ => panic!("Unexpected input")
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue