From 2c411d57922b3be423e25f8242eaeb094d18ab11 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Thu, 9 Dec 2021 07:26:53 -0400 Subject: [PATCH] chore(cart): add test for all-whitespace title --- src/cartridge.rs | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/cartridge.rs b/src/cartridge.rs index e7ad07f..7c14909 100644 --- a/src/cartridge.rs +++ b/src/cartridge.rs @@ -70,21 +70,23 @@ impl Cartridge { } fn detect_title(title_mem: &[u8; ROM_TITLE_MAX_SIZE]) -> Option { + use std::str::from_utf8; + const ALT_TITLE_LEN: usize = ROM_MANUFACTURER_START - ROM_TITLE_START; - // byte slice we have here is purposely not null terminated + // ascii byte slie does not have a null-terminator let ascii = match title_mem.iter().position(|b| *b == 0x00) { - Some(end) => &title_mem[0..end], - None => &title_mem[0..ROM_TITLE_MAX_SIZE], + Some(end) => &title_mem[..end], + None => &title_mem[..ROM_TITLE_MAX_SIZE], }; - match std::str::from_utf8(ascii).ok() { - None => match std::str::from_utf8(&title_mem[0..ALT_TITLE_LEN]).ok() { + match from_utf8(ascii).map(str::trim).ok() { + None => match from_utf8(&title_mem[..ALT_TITLE_LEN]).map(str::trim).ok() { Some("") | None => None, - Some(title) => Some(String::from(title.trim())), + Some(title) => Some(String::from(title)), }, Some("") => None, - Some(title) => Some(String::from(title.trim())), + Some(title) => Some(String::from(title)), } } @@ -950,4 +952,14 @@ mod tests { Cartridge::detect_title(&title) ); } + + #[test] + fn all_whitespace_title() { + let title = [ + 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, + ]; + + assert_eq!(None, Cartridge::detect_title(&title)); + } }