From 825d4a9b1e9eb9fb9870200733fbafcf0f153592 Mon Sep 17 00:00:00 2001 From: Vinay Chandra Dommeti Date: Sun, 4 Jan 2026 20:14:45 -0800 Subject: [PATCH 1/2] Support Input types on ByteStr --- src/bstr.rs | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 11 ++++++- 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 src/bstr.rs diff --git a/src/bstr.rs b/src/bstr.rs new file mode 100644 index 00000000..8e6d67e0 --- /dev/null +++ b/src/bstr.rs @@ -0,0 +1,85 @@ +use super::*; +use alloc::bstr::ByteStr; + +impl<'src> Input<'src> for &'src ByteStr { + type Cursor = usize; + type Span = SimpleSpan; + + type Token = u8; + type MaybeToken = u8; + + type Cache = Self; + + #[inline] + fn begin(self) -> (Self::Cursor, Self::Cache) { + (0, self) + } + + #[inline] + fn cursor_location(cursor: &Self::Cursor) -> usize { + *cursor + } + + #[inline(always)] + unsafe fn next_maybe( + this: &mut Self::Cache, + cursor: &mut Self::Cursor, + ) -> Option { + if let Some(tok) = this.get(*cursor) { + *cursor += 1; + Some(*tok) + } else { + None + } + } + + #[inline(always)] + unsafe fn span(_this: &mut Self::Cache, range: Range<&Self::Cursor>) -> Self::Span { + (*range.start..*range.end).into() + } +} + +impl<'src> ExactSizeInput<'src> for &'src ByteStr { + #[inline(always)] + unsafe fn span_from(this: &mut Self::Cache, range: RangeFrom<&Self::Cursor>) -> Self::Span { + (*range.start..this.len()).into() + } +} + +impl Sealed for &ByteStr {} +impl<'src> StrInput<'src> for &'src ByteStr { + #[doc(hidden)] + fn stringify(slice: Self::Slice) -> String { + slice + .iter() + // .map(|e| core::ascii::Char::from_u8(e).unwrap_or(AsciiChar::Substitute).to_char()) + .map(|e| char::from(*e)) + .collect() + } +} + +impl<'src> SliceInput<'src> for &'src ByteStr { + type Slice = &'src ByteStr; + + #[inline(always)] + fn full_slice(this: &mut Self::Cache) -> Self::Slice { + *this + } + + #[inline(always)] + unsafe fn slice(this: &mut Self::Cache, range: Range<&Self::Cursor>) -> Self::Slice { + &this[*range.start..*range.end] + } + + #[inline(always)] + unsafe fn slice_from(this: &mut Self::Cache, from: RangeFrom<&Self::Cursor>) -> Self::Slice { + &this[*from.start..] + } +} + +impl<'src> ValueInput<'src> for &'src ByteStr { + #[inline(always)] + unsafe fn next(this: &mut Self::Cache, cursor: &mut Self::Cursor) -> Option { + Self::next_maybe(this, cursor) + } +} diff --git a/src/lib.rs b/src/lib.rs index 16baa6af..0366f49b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,14 @@ #![cfg_attr(docsrs, feature(doc_cfg), deny(rustdoc::all))] #![cfg_attr( feature = "nightly", - feature(never_type, fn_traits, tuple_trait, unboxed_closures, specialization) + feature( + never_type, + fn_traits, + tuple_trait, + unboxed_closures, + specialization, + bstr + ) )] #![cfg_attr(feature = "nightly", allow(incomplete_features))] #![doc = include_str!("../README.md")] @@ -33,6 +40,8 @@ macro_rules! go_extra { } mod blanket; +#[cfg(feature = "nightly")] +mod bstr; #[cfg(feature = "unstable")] pub mod cache; pub mod combinator; From 106fe95cf18415fe6822ab2b5751d599828a0c4a Mon Sep 17 00:00:00 2001 From: Vinay Chandra Dommeti Date: Sun, 4 Jan 2026 20:18:44 -0800 Subject: [PATCH 2/2] Remove unnecessary alloc import --- src/bstr.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bstr.rs b/src/bstr.rs index 8e6d67e0..b9910212 100644 --- a/src/bstr.rs +++ b/src/bstr.rs @@ -1,5 +1,5 @@ use super::*; -use alloc::bstr::ByteStr; +use core::bstr::ByteStr; impl<'src> Input<'src> for &'src ByteStr { type Cursor = usize;