From 715f87450eeed237aeb42cedd325bd2f7d67d8fd Mon Sep 17 00:00:00 2001 From: Jack O'Connor Date: Fri, 28 Feb 2025 17:32:37 -0800 Subject: use rsplit_once for parsing tagged lines, add test cases --- b3sum/src/main.rs | 6 +++++- b3sum/src/unit_tests.rs | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/b3sum/src/main.rs b/b3sum/src/main.rs index 2d0ba5d..4479d4d 100644 --- a/b3sum/src/main.rs +++ b/b3sum/src/main.rs @@ -335,15 +335,19 @@ struct ParsedCheckLine { } fn split_untagged_check_line(line_after_slash: &str) -> Option<(&str, &str)> { + // Of the form " ". The file might contain " ", so we need to split from the + // left. line_after_slash.split_once(" ") } fn split_tagged_check_line(line_after_slash: &str) -> Option<(&str, &str)> { + // Of the form "BLAKE3 () = ". The file might contain ") = ", so we need to split + // from the *right*. let prefix = "BLAKE3 ("; if !line_after_slash.starts_with(prefix) { return None; } - line_after_slash[prefix.len()..].split_once(") = ") + line_after_slash[prefix.len()..].rsplit_once(") = ") } fn parse_check_line(mut line: &str) -> anyhow::Result { diff --git a/b3sum/src/unit_tests.rs b/b3sum/src/unit_tests.rs index b95c4a2..75f672b 100644 --- a/b3sum/src/unit_tests.rs +++ b/b3sum/src/unit_tests.rs @@ -119,6 +119,36 @@ fn test_parse_check_line() { assert_eq!(file_string, "否认"); assert_eq!(file_path, Path::new("否认")); + // untagged separator " " in the file name + let crate::ParsedCheckLine { + file_string, + is_escaped, + file_path, + expected_hash, + } = crate::parse_check_line( + "4747474747474747474747474747474747474747474747474747474747474747 foo bar", + ) + .unwrap(); + assert_eq!(expected_hash, blake3::Hash::from([0x47; 32])); + assert!(!is_escaped); + assert_eq!(file_string, "foo bar"); + assert_eq!(file_path, Path::new("foo bar")); + + // tagged separator ") = " in the file name + let crate::ParsedCheckLine { + file_string, + is_escaped, + file_path, + expected_hash, + } = crate::parse_check_line( + "BLAKE3 (foo) = bar) = 4848484848484848484848484848484848484848484848484848484848484848", + ) + .unwrap(); + assert_eq!(expected_hash, blake3::Hash::from([0x48; 32])); + assert!(!is_escaped); + assert_eq!(file_string, "foo) = bar"); + assert_eq!(file_path, Path::new("foo) = bar")); + // ========================= // ===== Failure Cases ===== // ========================= -- cgit v1.2.3