Add simple tokenizer for parameter list
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by David Peter · parent 1de39a3 · Browse files · 4 files·+87 added−14 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+87 added−14 removed
- other:+87 added−14 removed
Provenance
- Commit
3e217f4- Tree
7c50760- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
1de39a3
4 files changed+87 added−14 removed
modified:src/hyperfine/benchmark.rs+7 added-3 removed
223 unmodified lines224 } else {225 &values[num]226 };227 match cmd.get_parameter() {228- Some((param, value)) => Command::new_parametrized(preparation_command, param, *value),228+ Some((param, value)) => {229+ Command::new_parametrized(preparation_command, param, value.clone())230+ }231 None => Command::new(preparation_command),232 }233 });234 184 unmodified lines419 options420 .cleanup_command421 .as_ref()422 .map(|cleanup_command| match cmd.get_parameter() {421- Some((param, value)) => Command::new_parametrized(cleanup_command, param, *value),423+ Some((param, value)) => {424+ Command::new_parametrized(cleanup_command, param, value.clone())425+ }426 None => Command::new(cleanup_command),427 });428 run_cleanup_command(&options.shell, &cleanup_cmd, options.show_output)?;429 6 unmodified lines436 system_mean,437 t_min,438 t_max,439 times_real,436- cmd.get_parameter().map(|p| p.1.to_string()),440+ cmd.get_parameter().as_ref().map(|p| p.1.to_string()),441 ))442 }modified:src/hyperfine/internal.rs+69 added
146 unmodified lines147 assert_relative_eq!(1.5, annotated_results[0].relative_speed);148 assert_relative_eq!(1.0, annotated_results[1].relative_speed);149 assert_relative_eq!(2.5, annotated_results[2].relative_speed);150 }151+152+pub fn tokenize<'a>(values: &'a str) -> Vec<String> {153+ if values == "" {154+ return vec![];155+ }156+157+ let mut tokens = vec![];158+ let mut buf = String::new();159+160+ let mut iter = values.chars();161+ while let Some(c) = iter.next() {162+ match c {163+ '\\' => match iter.next() {164+ Some(c2 @ ',') | Some(c2 @ '\\') => {165+ buf.push(c2);166+ }167+ Some(c2) => {168+ buf.push('\\');169+ buf.push(c2);170+ }171+ None => buf.push('\\'),172+ },173+ ',' => {174+ tokens.push(buf);175+ buf = String::new();176+ }177+ _ => {178+ buf.push(c);179+ }180+ };181+ }182+183+ tokens.push(buf);184+185+ tokens186+}187+188+#[test]189+fn test_tokenize_empty() {190+ assert!(tokenize("").is_empty());191+}192+193+#[test]194+fn test_tokenize_single_value() {195+ assert_eq!(tokenize(r"foo"), vec!["foo"]);196+ assert_eq!(tokenize(r" "), vec![" "]);197+ assert_eq!(tokenize(r"hello\, world!"), vec!["hello, world!"]);198+ assert_eq!(tokenize(r"\,"), vec![","]);199+ assert_eq!(tokenize(r"\,\,\,"), vec![",,,"]);200+ assert_eq!(tokenize(r"\n"), vec![r"\n"]);201+ assert_eq!(tokenize(r"\\"), vec![r"\"]);202+ assert_eq!(tokenize(r"\\\,"), vec![r"\,"]);203+}204+205+#[test]206+fn test_tokenize_multiple_values() {207+ assert_eq!(tokenize(r"foo,bar,baz"), vec!["foo", "bar", "baz"]);208+ assert_eq!(tokenize(r"hello world,foo"), vec!["hello world", "foo"]);209+210+ assert_eq!(tokenize(r"hello\,world!,baz"), vec!["hello,world!", "baz"]);211+}212+213+#[test]214+fn test_tokenize_empty_values() {215+ assert_eq!(tokenize(r"foo,,bar"), vec!["foo", "", "bar"]);216+ assert_eq!(tokenize(r",bar"), vec!["", "bar"]);217+ assert_eq!(tokenize(r"bar,"), vec!["bar", ""]);218+ assert_eq!(tokenize(r",,"), vec!["", "", ""]);219+}modified:src/hyperfine/types.rs+8 added-8 removed
37 unmodified lines38 NumericType::Decimal(self)39 }40 }41 42-#[derive(Debug, Clone, Copy)]43-pub enum ParameterValue<'a> {44- Text(&'a str),42+#[derive(Debug, Clone)]43+pub enum ParameterValue {44+ Text(String),45 Numeric(NumericType),46 }47 48-impl<'a> ToString for ParameterValue<'a> {48+impl<'a> ToString for ParameterValue {49 fn to_string(&self) -> String {50 match self {51- ParameterValue::Text(value) => value.to_string(),51+ ParameterValue::Text(ref value) => value.clone(),52 ParameterValue::Numeric(value) => value.to_string(),53 }54 }55 }4 unmodified lines60 /// The command that should be executed (without parameter substitution)61 expression: &'a str,62 63 /// A possible parameter value.64- parameter: Option<(&'a str, ParameterValue<'a>)>,64+ parameter: Option<(&'a str, ParameterValue)>,65 }66 67 impl<'a> Command<'a> {68 pub fn new(expression: &'a str) -> Command<'a> {5 unmodified lines74 75 pub fn new_parametrized(76 expression: &'a str,77 parameter: &'a str,78- value: ParameterValue<'a>,78+ value: ParameterValue,79 ) -> Command<'a> {80 Command {81 expression,82 parameter: Some((parameter, value)),9 unmodified lines92 None => self.expression.into(),93 }94 }95 96- pub fn get_parameter(&self) -> &Option<(&'a str, ParameterValue<'a>)> {96+ pub fn get_parameter(&self) -> &Option<(&'a str, ParameterValue)> {97 &self.parameter98 }99 }100 164 unmodified linesmodified:src/main.rs+3 added-3 removed
11 unmodified lines12 use crate::hyperfine::app::get_arg_matches;13 use crate::hyperfine::benchmark::{mean_shell_spawning_time, run_benchmark};14 use crate::hyperfine::error::OptionsError;15 use crate::hyperfine::export::{ExportManager, ExportType};16-use crate::hyperfine::internal::write_benchmark_comparison;16+use crate::hyperfine::internal::{tokenize, write_benchmark_comparison};17 use crate::hyperfine::parameter_range::get_parameterized_commands;18 use crate::hyperfine::types::{19 BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption, ParameterValue,20 };190 unmodified lines211 } else if let Some(mut args) = matches.values_of("parameter-list") {212 let param_name = args.next().unwrap();213 let param_list_str = args.next().unwrap();214 215- let param_list = param_list_str.split(',').collect::<Vec<&str>>();215+ let param_list = tokenize(param_list_str);216 let command_list = command_strings.collect::<Vec<&str>>();217 218 let mut commands = Vec::with_capacity(param_list.len() * command_list.len());219 1 unmodified line221 for cmd in &command_list {222 commands.push(Command::new_parametrized(223 cmd,224 param_name,225- ParameterValue::Text(value),225+ ParameterValue::Text(value.clone()),226 ));227 }228 }229 5 unmodified lines