use core::borrow::Borrow;
#[cfg(feature = "serde")]
use alloc::boxed::Box;
#[repr(transparent)]
#[derive(PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct ByteStr([u8]);
impl ByteStr {
pub const fn from_byte_slice_with_value<'a, 'l>(
input: &'l [(&'a [u8], usize)],
) -> &'l [(&'a ByteStr, usize)] {
unsafe { core::mem::transmute(input) }
}
pub const fn from_str_slice_with_value<'a, 'l>(
input: &'l [(&'a str, usize)],
) -> &'l [(&'a ByteStr, usize)] {
unsafe { core::mem::transmute(input) }
}
pub fn from_bytes(input: &[u8]) -> &Self {
unsafe { core::mem::transmute(input) }
}
#[cfg(feature = "serde")]
pub fn from_boxed_bytes(input: Box<[u8]>) -> Box<Self> {
unsafe { core::mem::transmute(input) }
}
#[allow(dead_code)] pub fn from_str(input: &str) -> &Self {
Self::from_bytes(input.as_bytes())
}
#[allow(dead_code)] pub fn empty() -> &'static Self {
Self::from_bytes(&[])
}
#[allow(dead_code)] pub const fn as_bytes(&self) -> &[u8] {
&self.0
}
pub const fn len(&self) -> usize {
self.0.len()
}
#[allow(dead_code)] pub fn is_all_ascii(&self) -> bool {
for byte in self.0.iter() {
if !byte.is_ascii() {
return false;
}
}
true
}
#[allow(dead_code)] pub(crate) fn byte_at(&self, index: usize) -> Option<u8> {
self.0.get(index).copied()
}
pub(crate) const fn byte_at_or_panic(&self, index: usize) -> u8 {
self.0[index]
}
pub(crate) const fn is_less_then(&self, other: &Self) -> bool {
let mut i = 0;
while i < self.len() && i < other.len() {
if self.0[i] < other.0[i] {
return true;
}
if self.0[i] > other.0[i] {
return false;
}
i += 1;
}
self.len() < other.len()
}
pub(crate) const fn prefix_eq(&self, other: &ByteStr, prefix_len: usize) -> bool {
assert!(prefix_len <= self.len());
assert!(prefix_len <= other.len());
let mut i = 0;
while i < prefix_len {
if self.0[i] != other.0[i] {
return false;
}
i += 1;
}
true
}
}
impl Borrow<[u8]> for ByteStr {
fn borrow(&self) -> &[u8] {
self.as_bytes()
}
}
#[cfg(feature = "alloc")]
impl Borrow<[u8]> for alloc::boxed::Box<ByteStr> {
fn borrow(&self) -> &[u8] {
self.as_bytes()
}
}