diff options
Diffstat (limited to 'mingling_core/src/program')
| -rw-r--r-- | mingling_core/src/program/repl_exec.rs | 6 | ||||
| -rw-r--r-- | mingling_core/src/program/repl_exec/splitter.rs | 134 |
2 files changed, 2 insertions, 138 deletions
diff --git a/mingling_core/src/program/repl_exec.rs b/mingling_core/src/program/repl_exec.rs index b91f7bc..2db5c5a 100644 --- a/mingling_core/src/program/repl_exec.rs +++ b/mingling_core/src/program/repl_exec.rs @@ -7,10 +7,8 @@ use std::io::Write; #[doc(hidden)] pub mod res; -mod splitter; - use crate::error::{ProgramInternalExecuteError, ProgramPanic}; -use crate::program::repl_exec::splitter::split_input_string; +use crate::utils::ArgumentSplitter; use crate::{Program, ProgramCollect, RenderResult}; use crate::{program::repl_exec::res::ResREPL, this}; @@ -58,7 +56,7 @@ where line: &mut readline, }); - let mut args = split_input_string(&readline); + let mut args = readline.split_args(); p.run_hook_repl_pre_exec(&crate::hook::HookREPLPreExecInfo { args: &args }); match might_be_async::invoke!(exec_once(p, &mut args)) { diff --git a/mingling_core/src/program/repl_exec/splitter.rs b/mingling_core/src/program/repl_exec/splitter.rs deleted file mode 100644 index 312ad73..0000000 --- a/mingling_core/src/program/repl_exec/splitter.rs +++ /dev/null @@ -1,134 +0,0 @@ -// Doc Not Optimize -/// Wraps `split_input` to work with owned `String` inputs. -pub fn split_input_string(input: &str) -> Vec<String> { - split_input(input) -} - -/// Splits a string input into arguments, respecting single quotes, double quotes, -/// and backslash escaping. -pub fn split_input(input: &str) -> Vec<String> { - let mut result: Vec<String> = Vec::new(); - let mut current = String::new(); - let mut chars = input.chars(); - - while let Some(ch) = chars.next() { - match ch { - '\\' => { - // Take the next character literally (if any) and add it to current. - if let Some(next) = chars.next() { - current.push(next); - } - // If there's no next character, the backslash is just ignored/lost. - } - '"' | '\'' => { - // Start of a quoted segment. - let quote_char = ch; - let mut escaped = false; - loop { - match chars.next() { - None => break, - Some(c) => { - if escaped { - current.push(c); - escaped = false; - } else if c == '\\' { - escaped = true; - } else if c == quote_char { - break; - } else { - current.push(c); - } - } - } - } - } - ' ' => { - if !current.is_empty() { - result.push(current.clone()); - current.clear(); - } - } - _ => { - current.push(ch); - } - } - } - - if !current.is_empty() { - result.push(current); - } - - result -} - -#[cfg(test)] -mod splitter_tests { - use crate::program::repl_exec::splitter::split_input; - - #[test] - fn test_split_with_double_quotes() { - let input = r#"a "b c" d"#; - let result = split_input(input); - assert_eq!(result, vec!["a", "b c", "d"]); - } - - #[test] - fn test_split_with_single_quotes() { - let input = "a 'b c' d"; - let result = split_input(input); - assert_eq!(result, vec!["a", "b c", "d"]); - } - - #[test] - fn test_empty_input() { - assert!(split_input("").is_empty()); - } - - #[test] - fn test_no_quotes() { - let result = split_input("hello world"); - assert_eq!(result, vec!["hello", "world"]); - } - - #[test] - fn test_double_quotes_at_edges() { - let result = split_input(r#""hello world" foo"#); - assert_eq!(result, vec!["hello world", "foo"]); - } - - #[test] - fn test_single_quotes_at_edges() { - let result = split_input("'hello world' foo"); - assert_eq!(result, vec!["hello world", "foo"]); - } - - #[test] - fn test_multiple_double_quoted_parts() { - let result = split_input(r#"a "b c" d "e f g""#); - assert_eq!(result, vec!["a", "b c", "d", "e f g"]); - } - - #[test] - fn test_multiple_single_quoted_parts() { - let result = split_input("a 'b c' d 'e f g'"); - assert_eq!(result, vec!["a", "b c", "d", "e f g"]); - } - - #[test] - fn test_backslash_escaped_space() { - let result = split_input("a b\\ c d"); - assert_eq!(result, vec!["a", "b c", "d"]); - } - - #[test] - fn test_backslash_escaped_double_quote() { - let result = split_input(r#"a b\"c d"#); - assert_eq!(result, vec!["a", r#"b"c"#, "d"]); - } - - #[test] - fn test_backslash_escaped_single_quote() { - let result = split_input("a b\\'c d"); - assert_eq!(result, vec!["a", "b'c", "d"]); - } -} |
