filetypes/types/utils/
common.rs1use byteorder::{BigEndian, LittleEndian, ReadBytesExt};
2use std::{io::Cursor, str};
3pub fn compare_bytes(slice: &[u8], subs: &[u8], offset: usize) -> bool {
4 let s1 = subs.len();
5 s1 + offset <= slice.len() && *subs == slice[offset..s1 + offset]
6}
7
8pub fn bigendian_bytes(buf: &[u8]) -> u32 {
9 Cursor::new(buf).read_u32::<BigEndian>().unwrap()
10}
11
12pub fn littleendian_bytes(buf: &[u8]) -> u32 {
13 Cursor::new(buf).read_u32::<LittleEndian>().unwrap()
14}
15
16pub fn bytes_to_str(buf: &[u8]) -> &str {
17 match str::from_utf8(&buf) {
18 Ok(s) => s,
19 Err(_) => "",
20 }
21}
22
23pub fn bytes_index(buf: &[u8], subs: &[u8]) -> u32 {
25 let (len1, len2) = (buf.len(), subs.len());
26
27 if len2 == 0 {
28 return 0;
29 }
30 if len2 > len1 {
31 return u32::MAX;
32 }
33
34 for i in 0..=len1 - len2 {
35 if *subs == buf[i..i + len2] {
36 return i as u32;
37 }
38 }
39
40 u32::MAX
41}
42
43#[test]
44fn test_bytes_index() {
45 assert_eq!(0, bytes_index(b"abc", b""));
46 assert_eq!(0, bytes_index(b"abc", b"a"));
47 assert_eq!(u32::MAX, bytes_index(b"a", b"subs"));
48 assert_eq!(1, bytes_index(b"abc", b"b"));
49 assert_eq!(2, bytes_index(b"abcab", b"cab"));
50 assert_eq!(u32::MAX, bytes_index(b"abc", b"cd"));
51}