#[inline(always)]
pub(crate) fn read_u16_le(data: &[u8], offset: usize) -> u16 {
data.get(offset..)
.and_then(|s| s.first_chunk::<2>())
.copied()
.map(u16::from_le_bytes)
.unwrap_or(0)
}
#[inline(always)]
pub(crate) fn read_u32_le(data: &[u8], offset: usize) -> u32 {
data.get(offset..)
.and_then(|s| s.first_chunk::<4>())
.copied()
.map(u32::from_le_bytes)
.unwrap_or(0)
}
#[inline(always)]
pub(crate) fn read_i32_le(data: &[u8], offset: usize) -> i32 {
data.get(offset..)
.and_then(|s| s.first_chunk::<4>())
.copied()
.map(i32::from_le_bytes)
.unwrap_or(0)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_read_u16_le() {
assert_eq!(read_u16_le(&[0x34, 0x12], 0), 0x1234);
}
#[test]
fn test_read_u16_le_offset() {
assert_eq!(read_u16_le(&[0x00, 0x34, 0x12], 1), 0x1234);
}
#[test]
fn test_read_u32_le() {
assert_eq!(read_u32_le(&[0x78, 0x56, 0x34, 0x12], 0), 0x12345678);
}
#[test]
fn test_read_u32_le_offset() {
assert_eq!(read_u32_le(&[0xFF, 0x78, 0x56, 0x34, 0x12], 1), 0x12345678);
}
#[test]
fn test_read_i32_le_positive() {
assert_eq!(read_i32_le(&[0x01, 0x00, 0x00, 0x00], 0), 1);
}
#[test]
fn test_read_i32_le_negative() {
assert_eq!(read_i32_le(&[0xFF, 0xFF, 0xFF, 0xFF], 0), -1);
}
#[test]
fn test_read_u16_le_out_of_bounds() {
assert_eq!(read_u16_le(&[0x00], 0), 0);
}
#[test]
fn test_read_u32_le_out_of_bounds() {
assert_eq!(read_u32_le(&[0x00, 0x01, 0x02], 0), 0);
}
#[test]
fn test_read_i32_le_out_of_bounds() {
assert_eq!(read_i32_le(&[], 0), 0);
}
}