Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use slice::starts_with #89

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

marcospb19
Copy link
Contributor

No description provided.

@marcospb19
Copy link
Contributor Author

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
Copy link
Contributor

qarmin commented Oct 2, 2023

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
Copy link
Contributor Author

marcospb19 commented Oct 2, 2023

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants