From 47d94cf27b0a0597057c6948b22f489b99556dfb Mon Sep 17 00:00:00 2001 From: Lynne Date: Wed, 29 Sep 2021 23:29:21 +1000 Subject: [PATCH] simplify InferDb custom mimetypes using `buf.starts_with(b"\xAB\xCD")` instead of `buf[0] == 0xAB && buf[1] == 0xCD` is shorter, easier to read, removes the need for the `buf.len() > xyz` check, and actually compiles to (much) less assembly: https://godbolt.org/z/M7GePGn1T isn't that just lovely --- src/mime_db.rs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/mime_db.rs b/src/mime_db.rs index 420c4dd..fde0531 100644 --- a/src/mime_db.rs +++ b/src/mime_db.rs @@ -18,6 +18,8 @@ cfg_if! { db: infer::Infer, } + // TODO: msi + fn open_document_check(buf: &[u8], kind: &str) -> bool { let mime = format!("application/vnd.oasis.opendocument.{}", kind); let mime = mime.as_bytes(); @@ -48,15 +50,7 @@ cfg_if! { info.add("audio/x-aiff", "aiff", |buf| { // as added by https://github.com/bojand/infer/pull/48/files // this should be removed when (if) that PR is accepted - buf.len() > 11 - && buf[0] == 0x46 - && buf[1] == 0x4F - && buf[2] == 0x52 - && buf[3] == 0x4D - && buf[8] == 0x41 - && buf[9] == 0x49 - && buf[10] == 0x46 - && buf[11] == 0x46 + buf.starts_with(b"\x46\x4f\x52\x4d\x41\x49\x46\x46") }); info.add("image/svg+xml", "svg", |buf| {