bojand / infer

Small crate to infer file and MIME type by checking the magic number signature
MIT License
299 stars 28 forks source link

use `slice::starts_with` #89

Closed marcospb19 closed 3 months ago

marcospb19 commented 1 year ago

I started doing this and gave up mid-way, there's simply too many of them.

Another suggestion, instead of

/// Returns whether a buffer is an RTF.
pub fn is_rtf(buf: &[u8]) -> bool {
    buf.starts_with(&[0x7B, 0x5C, 0x72, 0x74, 0x66])
}

/// Returns whether a buffer is a Nintendo NES ROM.
pub fn is_nes(buf: &[u8]) -> bool {
    buf.starts_with(&[0x4E, 0x45, 0x53, 0x1A])
}

/// Returns whether a buffer is Google Chrome Extension
pub fn is_crx(buf: &[u8]) -> bool {
    buf.starts_with(&[0x43, 0x72, 0x32, 0x34])
}

You can do this

/// Returns whether a buffer is an RTF.
pub fn is_rtf(buf: &[u8]) -> bool {
    buf.starts_with(b"\x7B\x5C\x72\x74\x66")
}

/// Returns whether a buffer is a Nintendo NES ROM.
pub fn is_nes(buf: &[u8]) -> bool {
    buf.starts_with(b"\x4E\x45\x53\x1A")
}

/// Returns whether a buffer is Google Chrome Extension
pub fn is_crx(buf: &[u8]) -> bool {
    buf.starts_with(b"\x43\x72\x32\x34")
}

It's the same thing.

Feel free to close the PR if you want, it's not like this really matters, you might prefer the buf[i] = x, buf[i + 1] = y, buf[i + 2] = z thing.

qarmin commented 1 year ago

What about performance?

I suspect that this change may change it, but not sure if this will be faster or slower than current version

marcospb19 commented 1 year ago

These should probably take, like, 200 nanoseconds, loading the file itself (either from disk or network) will definitely be the bottleneck (by a large magnitude).