blob: 72684f896aa434182f2c2f9e39917ae68c6814cb [file]
// Copyright 2026 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//! Non-panicking slice utilities and little-endian integer readers (`vx_core::slice`).
#![forbid(unsafe_code)]
pub use ::core::slice::{from_mut, from_raw_parts, from_raw_parts_mut, from_ref};
use crate::ascii::is_ascii_whitespace;
/// Read an unaligned little-endian `u16` at byte offset `off` from `bytes`.
///
/// Returns `None` if `off + 2` overflows or exceeds `bytes.len()`.
#[inline(always)]
pub fn read_u16_le(bytes: &[u8], off: usize) -> Option<u16> {
let chunk = bytes.get(off..)?.first_chunk::<2>()?;
Some(u16::from_le_bytes(*chunk))
}
/// Read an unaligned little-endian `u32` at byte offset `off` from `bytes`.
///
/// Returns `None` if `off + 4` overflows or exceeds `bytes.len()`.
#[inline(always)]
pub fn read_u32_le(bytes: &[u8], off: usize) -> Option<u32> {
let chunk = bytes.get(off..)?.first_chunk::<4>()?;
Some(u32::from_le_bytes(*chunk))
}
/// Read an unaligned little-endian `u64` at byte offset `off` from `bytes`.
///
/// Returns `None` if `off + 8` overflows or exceeds `bytes.len()`.
#[inline(always)]
pub fn read_u64_le(bytes: &[u8], off: usize) -> Option<u64> {
let chunk = bytes.get(off..)?.first_chunk::<8>()?;
Some(u64::from_le_bytes(*chunk))
}
/// Return the subslice `bytes[start..start + len]` without panicking, or
/// `None` if `start + len` overflows or exceeds `bytes.len()`.
#[curry::verified]
pub fn subslice(bytes: &[u8], start: usize, len: usize) -> Option<&[u8]> {
let end = start.checked_add(len)?;
bytes.get(start..end)
}
/// Split `bytes` at `mid` without panicking, returning `None` if `mid > bytes.len()`.
#[inline(always)]
pub fn split_at_checked(bytes: &[u8], mid: usize) -> Option<(&[u8], &[u8])> {
let head = bytes.get(..mid)?;
let tail = bytes.get(mid..)?;
Some((head, tail))
}
/// Return `true` iff `bytes` begins with `prefix` (zero panics, zero `bcmp`).
#[curry::verified]
pub fn starts_with(bytes: &[u8], prefix: &[u8]) -> bool {
if bytes.len() < prefix.len() {
return false;
}
let mut i: usize = 0;
while i < prefix.len() {
let (Some(&a), Some(&b)) = (bytes.get(i), prefix.get(i)) else {
return false;
};
if a != b {
return false;
}
i += 1;
}
true
}
/// Return `true` iff `bytes` ends with `suffix` (zero panics, zero `bcmp`).
#[curry::verified]
pub fn ends_with(bytes: &[u8], suffix: &[u8]) -> bool {
let Some(start) = bytes.len().checked_sub(suffix.len()) else {
return false;
};
let Some(tail) = bytes.get(start..) else {
return false;
};
starts_with(tail, suffix)
}
/// Return `true` iff every byte in `bytes` is `0x00`.
#[curry::verified]
pub fn all_zero(bytes: &[u8]) -> bool {
let mut i: usize = 0;
while i < bytes.len() {
let Some(&b) = bytes.get(i) else {
return false;
};
if b != 0 {
return false;
}
i += 1;
}
true
}
/// Strip leading ASCII whitespace bytes from `bytes`, returning the remaining suffix.
#[curry::verified]
pub fn trim_ascii_start(bytes: &[u8]) -> &[u8] {
let mut start: usize = 0;
while start < bytes.len() {
let Some(&b) = bytes.get(start) else {
break;
};
if !is_ascii_whitespace(b) {
break;
}
start += 1;
}
match bytes.get(start..) {
Some(rest) => rest,
None => &[],
}
}
/// Strip trailing ASCII whitespace bytes from `bytes`, returning the remaining prefix.
#[curry::verified]
pub fn trim_ascii_end(bytes: &[u8]) -> &[u8] {
let mut end: usize = bytes.len();
while end > 0 {
let Some(&b) = bytes.get(end - 1) else {
break;
};
if !is_ascii_whitespace(b) {
break;
}
end -= 1;
}
match bytes.get(..end) {
Some(prefix) => prefix,
None => &[],
}
}
/// Strip both leading and trailing ASCII whitespace bytes from `bytes`.
#[curry::verified]
pub fn trim_ascii(bytes: &[u8]) -> &[u8] {
let (start, end) = trim_ascii_bounds(bytes);
match bytes.get(start..end) {
Some(trimmed) => trimmed,
None => &[],
}
}
/// Return the `(start, end)` byte-offset bounds of `trim_ascii(bytes)` within
/// `bytes`, satisfying `0 <= start <= end <= bytes.len()` and
/// `&bytes[start..end] == trim_ascii(bytes)`.
#[curry::verified]
pub fn trim_ascii_bounds(bytes: &[u8]) -> (usize, usize) {
let trimmed_start = trim_ascii_start(bytes);
let start = bytes.len().saturating_sub(trimmed_start.len());
let trimmed = trim_ascii_end(trimmed_start);
(start, start + trimmed.len())
}