diff options
| author | Jack O'Connor <[email protected]> | 2025-02-28 17:32:37 -0800 |
|---|---|---|
| committer | Jack O'Connor <[email protected]> | 2025-02-28 17:34:39 -0800 |
| commit | 715f87450eeed237aeb42cedd325bd2f7d67d8fd (patch) | |
| tree | 8f60f5e4652e965f24488c0aea760dbc055eea4b | |
| parent | 7ff889e70f493da06630754c30eded23e61226fd (diff) | |
use rsplit_once for parsing tagged lines, add test cases
| -rw-r--r-- | b3sum/src/main.rs | 6 | ||||
| -rw-r--r-- | b3sum/src/unit_tests.rs | 30 |
2 files changed, 35 insertions, 1 deletions
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 "<hash> <file>". 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 (<file>) = <hash>". 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<ParsedCheckLine> { 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 ===== // ========================= |
