Skip to content

Commit c2933f5

Browse files
authored
Merge pull request #42 from jayvdb/use-native-from-bytes
Use u32 and u16 from_xx_bytes
2 parents 51383ac + 96f9f61 commit c2933f5

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

src/util.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,8 @@ pub fn read_u32<R: BufRead + Seek>(reader: &mut R, endianness: &Endian) -> Image
2121
reader.read_exact(&mut buf)?;
2222

2323
match endianness {
24-
Endian::Little => Ok(((buf[3] as u32) << 24)
25-
| ((buf[2] as u32) << 16)
26-
| ((buf[1] as u32) << 8)
27-
| (buf[0] as u32)),
28-
Endian::Big => Ok(((buf[0] as u32) << 24)
29-
| ((buf[1] as u32) << 16)
30-
| ((buf[2] as u32) << 8)
31-
| (buf[3] as u32)),
24+
Endian::Little => Ok(u32::from_le_bytes(buf)),
25+
Endian::Big => Ok(u32::from_be_bytes(buf)),
3226
}
3327
}
3428

@@ -47,8 +41,8 @@ pub fn read_u16<R: BufRead + Seek>(reader: &mut R, endianness: &Endian) -> Image
4741
reader.read_exact(&mut buf)?;
4842

4943
match endianness {
50-
Endian::Little => Ok(((buf[1] as u16) << 8) | (buf[0] as u16)),
51-
Endian::Big => Ok(((buf[0] as u16) << 8) | (buf[1] as u16)),
44+
Endian::Little => Ok(u16::from_le_bytes(buf)),
45+
Endian::Big => Ok(u16::from_be_bytes(buf)),
5246
}
5347
}
5448

0 commit comments

Comments
 (0)