Skip to content
Myr

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

4 files+87 added−14 removed

modified:src/hyperfine/benchmark.rs+7 added-3 removed
223 unmodified lines
224 } 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 lines
419 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 lines
436 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 lines
147 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}151152pub fn tokenize<'a>(values: &'a str) -> Vec<String> {153 if values == "" {154 return vec![];155 }156157 let mut tokens = vec![];158 let mut buf = String::new();159160 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 }182183 tokens.push(buf);184185 tokens186}187188#[test]189fn test_tokenize_empty() {190 assert!(tokenize("").is_empty());191}192193#[test]194fn 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}204205#[test]206fn 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"]);209210 assert_eq!(tokenize(r"hello\,world!,baz"), vec!["hello,world!", "baz"]);211}212213#[test]214fn 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 lines
38 NumericType::Decimal(self)39 }40}4142#[derive(Debug, Clone, Copy)]43pub enum ParameterValue<'a> {44 Text(&'a str),42#[derive(Debug, Clone)]43pub enum ParameterValue {44 Text(String),45 Numeric(NumericType),46}4748impl<'a> ToString for ParameterValue<'a> {48impl<'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 lines
60 /// The command that should be executed (without parameter substitution)61 expression: &'a str,6263 /// A possible parameter value.64 parameter: Option<(&'a str, ParameterValue<'a>)>,64 parameter: Option<(&'a str, ParameterValue)>,65}6667impl<'a> Command<'a> {68 pub fn new(expression: &'a str) -> Command<'a> {
5 unmodified lines
7475 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 lines
92 None => self.expression.into(),93 }94 }9596 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 lines
modified:src/main.rs+3 added-3 removed
11 unmodified lines
12use crate::hyperfine::app::get_arg_matches;13use crate::hyperfine::benchmark::{mean_shell_spawning_time, run_benchmark};14use crate::hyperfine::error::OptionsError;15use crate::hyperfine::export::{ExportManager, ExportType};16use crate::hyperfine::internal::write_benchmark_comparison;16use crate::hyperfine::internal::{tokenize, write_benchmark_comparison};17use crate::hyperfine::parameter_range::get_parameterized_commands;18use crate::hyperfine::types::{19 BenchmarkResult, CmdFailureAction, Command, HyperfineOptions, OutputStyleOption, ParameterValue,20};
190 unmodified lines
211 } 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();214215 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>>();217218 let mut commands = Vec::with_capacity(param_list.len() * command_list.len());219
1 unmodified line
221 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