infer
infer copied to clipboard
use `slice::starts_with`
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.
What about performance?
I suspect that this change may change it, but not sure if this will be faster or slower than current version
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).