#![allow(
// We follow libstd's lead and prefer to define both.
clippy::partialeq_ne_impl,
// This is a really annoying clippy lint, since it's required for so many cases...
clippy::cast_ptr_alignment,
// For macros
clippy::redundant_slicing,
)]
use std::ops::{Range, RangeBounds};
use super::ArcStr;
type Idx = usize;
#[cfg(not(any(target_pointer_width = "64", target_pointer_width = "32")))]
compile_error!(
"Non-32/64-bit pointers not supported right now due to insufficient \
testing on a platform like that. Please file a issue with the \
`rama` project so we can talk about your use case if this is \
important to you."
);
#[derive(Clone)]
#[repr(C)] pub struct Substr(ArcStr, Idx, Idx);
#[inline]
#[cfg(target_pointer_width = "64")]
#[allow(clippy::let_unit_value)]
const fn to_idx_const(i: usize) -> Idx {
const DUMMY: [(); 1] = [()];
let _ = DUMMY[i >> 32];
i as Idx
}
#[inline]
#[cfg(not(target_pointer_width = "64"))]
const fn to_idx_const(i: usize) -> Idx {
i as Idx
}
#[inline]
#[cfg(target_pointer_width = "64")]
fn to_idx(i: usize) -> Idx {
if i > 0xffff_ffff {
index_overflow(i);
}
i as Idx
}
#[inline]
#[cfg(not(target_pointer_width = "64"))]
fn to_idx(i: usize) -> Idx {
i as Idx
}
#[cold]
#[inline(never)]
#[cfg(target_pointer_width = "64")]
fn index_overflow(i: usize) -> ! {
panic!(
"The index {i} is too large for arcstr::Substr (enable the `substr-usize-indices` feature in `arcstr` if you need this)"
);
}
#[cold]
#[inline(never)]
fn bad_substr_idx(s: &ArcStr, i: usize, e: usize) -> ! {
assert!(i <= e, "Bad substr range: start {i} must be <= end {e}");
let max = if cfg!(target_pointer_width = "64",) {
u32::MAX as usize
} else {
usize::MAX
};
let len = s.len().min(max);
assert!(
e <= len,
"Bad substr range: end {e} must be <= string length/index max size {len}"
);
assert!(
s.is_char_boundary(i) && s.is_char_boundary(e),
"Bad substr range: start and end must be on char boundaries"
);
unreachable!(
"[arcstr bug]: should have failed one of the above tests: \
please report me. debugging info: b={}, e={}, l={}, max={:#x}",
i,
e,
s.len(),
max
);
}
impl Substr {
#[inline]
#[must_use]
pub const fn new() -> Self {
Self(ArcStr::new(), 0, 0)
}
#[inline]
#[must_use]
pub fn full(a: ArcStr) -> Self {
let l = to_idx(a.len());
Self(a, 0, l)
}
#[inline]
pub(crate) fn from_parts(a: &ArcStr, range: impl RangeBounds<usize>) -> Self {
use core::ops::Bound;
let begin = match range.start_bound() {
Bound::Included(&n) => n,
Bound::Excluded(&n) => n + 1,
Bound::Unbounded => 0,
};
let end = match range.end_bound() {
Bound::Included(&n) => n + 1,
Bound::Excluded(&n) => n,
Bound::Unbounded => a.len(),
};
let _ = &a.as_str()[begin..end];
Self(ArcStr::clone(a), to_idx(begin), to_idx(end))
}
#[inline]
#[must_use]
pub fn substr(&self, range: impl RangeBounds<usize>) -> Self {
use core::ops::Bound;
let my_end = self.2;
let begin = match range.start_bound() {
Bound::Included(&n) => n,
Bound::Excluded(&n) => n + 1,
Bound::Unbounded => 0,
};
let end = match range.end_bound() {
Bound::Included(&n) => n + 1,
Bound::Excluded(&n) => n,
Bound::Unbounded => self.len(),
};
let new_begin = self.1 + begin;
let new_end = self.1 + end;
if begin > end
|| end > my_end
|| !self.0.is_char_boundary(new_begin)
|| !self.0.is_char_boundary(new_end)
{
bad_substr_idx(&self.0, new_begin, new_end);
}
debug_assert!(self.0.get(new_begin..new_end).is_some());
Self(ArcStr::clone(&self.0), new_begin as Idx, new_end as Idx)
}
#[inline]
#[must_use]
pub fn as_str(&self) -> &str {
self
}
#[inline]
#[must_use]
pub fn len(&self) -> usize {
debug_assert!(self.2 >= self.1);
self.2 - self.1
}
#[inline]
#[must_use]
pub fn is_empty(&self) -> bool {
self.2 == self.1
}
#[inline]
#[allow(clippy::inherent_to_string_shadow_display)]
#[must_use]
pub fn to_string(&self) -> std::string::String {
self.as_str().to_owned()
}
#[inline]
#[must_use]
pub const unsafe fn from_parts_unchecked(s: ArcStr, range: Range<usize>) -> Self {
Self(s, to_idx_const(range.start), to_idx_const(range.end))
}
#[inline]
#[must_use]
pub fn shallow_eq(this: &Self, o: &Self) -> bool {
ArcStr::ptr_eq(&this.0, &o.0) && (this.1 == o.1) && (this.2 == o.2)
}
#[inline]
#[must_use]
pub fn parent(&self) -> &ArcStr {
&self.0
}
#[inline]
#[must_use]
pub fn range(&self) -> Range<usize> {
self.1..self.2
}
#[must_use]
pub fn try_substr_from(&self, substr: &str) -> Option<Self> {
if substr.is_empty() {
return Some(Self::new());
}
let parent_ptr = self.0.as_ptr() as usize;
let self_start = parent_ptr + self.1;
let self_end = parent_ptr + self.2;
let substr_start = substr.as_ptr() as usize;
let substr_end = substr_start + substr.len();
if substr_start < self_start || substr_end > self_end {
return None;
}
let index = substr_start - self_start;
let end = index + substr.len();
Some(self.substr(index..end))
}
pub fn try_substr_using(&self, f: impl FnOnce(&str) -> &str) -> Option<Self> {
self.try_substr_from(f(self.as_str()))
}
}
impl From<ArcStr> for Substr {
#[inline]
fn from(a: ArcStr) -> Self {
Self::full(a)
}
}
impl From<&ArcStr> for Substr {
#[inline]
fn from(a: &ArcStr) -> Self {
Self::full(a.clone())
}
}
impl core::ops::Deref for Substr {
type Target = str;
#[inline]
fn deref(&self) -> &str {
debug_assert!(self.0.get(self.1..self.2).is_some());
unsafe { self.0.get_unchecked(self.1..self.2) }
}
}
impl PartialEq for Substr {
#[inline]
fn eq(&self, o: &Self) -> bool {
Self::shallow_eq(self, o) || PartialEq::eq(self.as_str(), o.as_str())
}
#[inline]
fn ne(&self, o: &Self) -> bool {
!Self::shallow_eq(self, o) && PartialEq::ne(self.as_str(), o.as_str())
}
}
impl PartialEq<ArcStr> for Substr {
#[inline]
fn eq(&self, o: &ArcStr) -> bool {
(ArcStr::ptr_eq(&self.0, o) && (self.1 == 0) && (self.2 == o.len()))
|| PartialEq::eq(self.as_str(), o.as_str())
}
#[inline]
fn ne(&self, o: &ArcStr) -> bool {
(!ArcStr::ptr_eq(&self.0, o) || (self.1 != 0) || (self.2 != o.len()))
&& PartialEq::ne(self.as_str(), o.as_str())
}
}
impl PartialEq<Substr> for ArcStr {
#[inline]
fn eq(&self, o: &Substr) -> bool {
PartialEq::eq(o, self)
}
#[inline]
fn ne(&self, o: &Substr) -> bool {
PartialEq::ne(o, self)
}
}
impl Eq for Substr {}
impl PartialOrd for Substr {
#[inline]
#[allow(clippy::non_canonical_partial_ord_impl)]
fn partial_cmp(&self, s: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(s))
}
}
impl Ord for Substr {
#[inline]
fn cmp(&self, s: &Self) -> core::cmp::Ordering {
self.as_str().cmp(s.as_str())
}
}
impl core::hash::Hash for Substr {
#[inline]
fn hash<H: core::hash::Hasher>(&self, h: &mut H) {
self.as_str().hash(h)
}
}
impl core::fmt::Debug for Substr {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Debug::fmt(self.as_str(), f)
}
}
impl core::fmt::Display for Substr {
#[inline]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Display::fmt(self.as_str(), f)
}
}
impl Default for Substr {
#[inline]
fn default() -> Self {
Self::new()
}
}
macro_rules! impl_from_via_arcstr {
($($SrcTy:ty),+) => {$(
impl From<$SrcTy> for Substr {
#[inline]
fn from(v: $SrcTy) -> Self {
Self::full(ArcStr::from(v))
}
}
)+};
}
impl_from_via_arcstr![
&str,
&mut str,
std::string::String,
&std::string::String,
std::boxed::Box<str>,
std::rc::Rc<str>,
std::sync::Arc<str>,
std::borrow::Cow<'_, str>
];
impl<'a> From<&'a Substr> for std::borrow::Cow<'a, str> {
#[inline]
fn from(s: &'a Substr) -> Self {
std::borrow::Cow::Borrowed(s)
}
}
impl<'a> From<Substr> for std::borrow::Cow<'a, str> {
#[inline]
fn from(s: Substr) -> Self {
if let Some(st) = ArcStr::as_static(&s.0) {
debug_assert!(st.get(s.range()).is_some());
std::borrow::Cow::Borrowed(unsafe { st.get_unchecked(s.range()) })
} else {
std::borrow::Cow::Owned(s.to_string())
}
}
}
macro_rules! impl_peq {
(@one $a:ty, $b:ty) => {
#[allow(clippy::extra_unused_lifetimes)]
impl<'a> PartialEq<$b> for $a {
#[inline]
fn eq(&self, s: &$b) -> bool {
PartialEq::eq(&self[..], &s[..])
}
#[inline]
fn ne(&self, s: &$b) -> bool {
PartialEq::ne(&self[..], &s[..])
}
}
};
($(($a:ty, $b:ty),)+) => {$(
impl_peq!(@one $a, $b);
impl_peq!(@one $b, $a);
)+};
}
impl_peq! {
(Substr, str),
(Substr, &'a str),
(Substr, std::string::String),
(Substr, std::borrow::Cow<'a, str>),
(Substr, std::boxed::Box<str>),
(Substr, std::sync::Arc<str>),
(Substr, std::rc::Rc<str>),
}
macro_rules! impl_index {
($($IdxT:ty,)*) => {$(
impl core::ops::Index<$IdxT> for Substr {
type Output = str;
#[inline]
fn index(&self, i: $IdxT) -> &Self::Output {
&self.as_str()[i]
}
}
)*};
}
impl_index! {
core::ops::RangeFull,
core::ops::Range<usize>,
core::ops::RangeFrom<usize>,
core::ops::RangeTo<usize>,
core::ops::RangeInclusive<usize>,
core::ops::RangeToInclusive<usize>,
}
impl AsRef<str> for Substr {
#[inline]
fn as_ref(&self) -> &str {
self
}
}
impl AsRef<[u8]> for Substr {
#[inline]
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}
impl core::borrow::Borrow<str> for Substr {
#[inline]
fn borrow(&self) -> &str {
self
}
}
impl core::str::FromStr for Substr {
type Err = core::convert::Infallible;
#[inline]
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::from(ArcStr::from(s)))
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
#[should_panic]
#[cfg(not(miri))] #[cfg(target_pointer_width = "64")]
fn test_from_parts_unchecked_err() {
let s = crate::str::arcstr::arcstr!("foo");
let _u = unsafe { Substr::from_parts_unchecked(s, 0x1_0000_0000usize..0x1_0000_0001) };
}
#[test]
fn test_from_parts_unchecked_valid() {
let s = crate::str::arcstr::arcstr!("foobar");
let u = unsafe { Substr::from_parts_unchecked(s, 2..5) };
assert_eq!(&*u, "oba");
}
}