diff --git a/src/lib.rs b/src/lib.rs index fa11f47..bb069a9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,19 +50,16 @@ pub fn parse_exec_time(output: &str) -> f64 { if !l.contains("elapsed:") { acc } else { - let timing = l - .split("(elapsed: ") - .last() - .unwrap(); - - // see [rust/library/core/src/time.rs](https://git.io/Jy1rI) - if timing.ends_with("ns)") { + let timing = l.split("(elapsed: ").last().unwrap(); + // use `contains` istd. of `ends_with`: string may contain ANSI escape sequences. + // possible time formats: see [rust/library/core/src/time.rs](https://git.io/Jy1rI). + if timing.contains("ns)") { acc // range below rounding precision. - } else if timing.ends_with("ยตs)") { + } else if timing.contains("ยตs)") { acc + parse_time(timing, "ยตs") / 1000_f64 - } else if timing.ends_with("ms)") { + } else if timing.contains("ms)") { acc + parse_time(timing, "ms") - } else if timing.ends_with("s)") { + } else if timing.contains("s)") { acc + parse_time(timing, "s") * 1000_f64 } else { acc @@ -91,9 +88,10 @@ mod tests { #[test] fn test_parse_exec_time() { assert_approx_eq!( - parse_exec_time( - "๐ŸŽ„ Part 1 ๐ŸŽ„\n0 (elapsed: 74.13ns)\n๐ŸŽ„ Part 2 ๐ŸŽ„\n0 (elapsed: 50.00ns)" - ), + parse_exec_time(&format!( + "๐ŸŽ„ Part 1 ๐ŸŽ„\n0 (elapsed: 74.13ns){}\n๐ŸŽ„ Part 2 ๐ŸŽ„\n0 (elapsed: 50.00ns){}", + ANSI_RESET, ANSI_RESET + )), 0_f64 );