diff --git a/src/collapse/vsprof.rs b/src/collapse/vsprof.rs index 13b7b211..faab64aa 100644 --- a/src/collapse/vsprof.rs +++ b/src/collapse/vsprof.rs @@ -78,6 +78,10 @@ impl Folder { fn on_line(&mut self, line: &str, occurences: &mut Occurrences) -> io::Result<()> { let (depth, remainder) = get_next_number(line)?; + if remainder.is_empty() { + return invalid_data_error!("Missing function name in line:\n{}", line); + } + // Function names are always wrapped in quotes. By trimming the leading double quote, we // know that the next double quote is the double quote closing the function name. Splitting // on this double quote, we get the function name and the remainder of the line. @@ -188,12 +192,12 @@ fn get_next_number(line: &str) -> io::Result<(usize, &str)> { remove_leading_comma = true; line.split_once('"') } else { - line.split_once(',') + line.split_once(',').or(Some((line, ""))) }; - if let Some((num, remainder)) = field { + if let Some((num, mut remainder)) = field { // Parse the number - let thousands = num.split(|c: char| !c.is_ascii_digit()); + let thousands = num.split(|c: char| c == ',' || c == '.'); let mut n = 0; for thousand in thousands { if n == 0 { @@ -225,9 +229,7 @@ fn get_next_number(line: &str) -> io::Result<(usize, &str)> { // `remainder` still has a leading comma, because the number is >1000. We need to // remove it so we are consistent regardless of whether the number was wrapped in // double quotes or not. - if let Some(remainder) = remainder.strip_prefix(',') { - return Ok((n, remainder)); - } + remainder = remainder.strip_prefix(',').unwrap_or(remainder); } return Ok((n, remainder)); diff --git a/tests/collapse-vsprof.rs b/tests/collapse-vsprof.rs index 1f2fea85..8b7ebdb1 100644 --- a/tests/collapse-vsprof.rs +++ b/tests/collapse-vsprof.rs @@ -67,7 +67,9 @@ fn collapse_vsprof_should_return_error_for_missing_function_name() { let test_file = "./tests/data/collapse-vsprof/missing-function-name.csv"; let error = test_collapse_vsprof_error(test_file); assert_eq!(error.kind(), io::ErrorKind::InvalidData); - assert!(error.to_string().starts_with("Invalid number in line:")); + assert!(error + .to_string() + .starts_with("Missing function name in line:")); } #[test]