spyparty/
utils.rs

1use std::io::{Read, Result};
2
3/// Read u8 values from a series of bytes.
4pub fn read_u8<R: Read>(reader: &mut R) -> Result<u8> {
5    let mut buf = [0; 1];
6    reader.read_exact(&mut buf)?;
7
8    Ok(buf[0])
9}
10
11/// Read u16 values from a series of bytes.
12///
13/// All the numeric values in the replays are little endian, so that is what we use here.
14pub fn read_u16<R: Read>(reader: &mut R) -> Result<u16> {
15    let mut buf = [0; 2];
16    reader.read_exact(&mut buf)?;
17
18    let val = u16::from_le_bytes(buf);
19
20    Ok(val)
21}
22
23/// Read u32 values from a series of bytes.
24///
25/// All the numeric values in the replays are little endian, so that is what we use here.
26pub fn read_u32<R: Read>(reader: &mut R) -> Result<u32> {
27    let mut buf = [0; 4];
28    reader.read_exact(&mut buf)?;
29
30    let val = u32::from_le_bytes(buf);
31
32    Ok(val)
33}
34
35/// Read u128 values from a series of bytes.
36///
37/// All the numeric values in the replays are little endian, so that is what we use here.
38pub fn read_u128<R: Read>(reader: &mut R) -> Result<u128> {
39    let mut buf = [0; 16];
40    reader.read_exact(&mut buf)?;
41
42    let val = u128::from_le_bytes(buf);
43
44    Ok(val)
45}
46
47/// Read f32 values from a series of bytes.
48///
49/// All the numeric values in the replays are little endian, so that is what we use here.
50pub fn read_f32<R: Read>(reader: &mut R) -> Result<f32> {
51    let mut buf = [0; 4];
52    reader.read_exact(&mut buf)?;
53
54    let val = f32::from_bits(u32::from_le_bytes(buf));
55
56    Ok(val)
57}
58
59/// Performs a case-insensitive string comparison.
60pub fn string_eq_ignore_case(a: &str, b: &str) -> bool {
61    a == b || a.to_lowercase() == b.to_lowercase()
62}