Skip to content

Commit fc9f90c

Browse files
committed
fix: handle ANSI escapes when counting exec. time
1 parent 01f0749 commit fc9f90c

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

src/lib.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,16 @@ pub fn parse_exec_time(output: &str) -> f64 {
5050
if !l.contains("elapsed:") {
5151
acc
5252
} else {
53-
let timing = l
54-
.split("(elapsed: ")
55-
.last()
56-
.unwrap();
57-
58-
// see [rust/library/core/src/time.rs](https://git.io/Jy1rI)
59-
if timing.ends_with("ns)") {
53+
let timing = l.split("(elapsed: ").last().unwrap();
54+
// use `contains` istd. of `ends_with`: string may contain ANSI escape sequences.
55+
// possible time formats: see [rust/library/core/src/time.rs](https://git.io/Jy1rI).
56+
if timing.contains("ns)") {
6057
acc // range below rounding precision.
61-
} else if timing.ends_with("µs)") {
58+
} else if timing.contains("µs)") {
6259
acc + parse_time(timing, "µs") / 1000_f64
63-
} else if timing.ends_with("ms)") {
60+
} else if timing.contains("ms)") {
6461
acc + parse_time(timing, "ms")
65-
} else if timing.ends_with("s)") {
62+
} else if timing.contains("s)") {
6663
acc + parse_time(timing, "s") * 1000_f64
6764
} else {
6865
acc
@@ -91,9 +88,10 @@ mod tests {
9188
#[test]
9289
fn test_parse_exec_time() {
9390
assert_approx_eq!(
94-
parse_exec_time(
95-
"🎄 Part 1 🎄\n0 (elapsed: 74.13ns)\n🎄 Part 2 🎄\n0 (elapsed: 50.00ns)"
96-
),
91+
parse_exec_time(&format!(
92+
"🎄 Part 1 🎄\n0 (elapsed: 74.13ns){}\n🎄 Part 2 🎄\n0 (elapsed: 50.00ns){}",
93+
ANSI_RESET, ANSI_RESET
94+
)),
9795
0_f64
9896
);
9997

0 commit comments

Comments
 (0)