better-url 0.12.0

A better URL crate.
Documentation
//! [`CowStrExt`].

use crate::prelude::*;

/// Extension trait for `Cow<'_, str>`.
pub(crate) trait CowStrExt {
    /// Retrain the range without checking for validity.
    unsafe fn retain_range_unchecked<B: RangeBounds<usize>>(&mut self, range: B);

    /// Replace a range, trying to not allocate.
    fn replace_range<B: RangeBounds<usize>>(&mut self, range: B, with: &str);

    /// Replace a substring.
    fn replace_substr(&mut self, substr: *const str, with: &str);

    /// Insert multiple [`str`]s starting at `idx` without unnecessary allocations.
    fn insert_with<const N: usize>(&mut self, idx: usize, strings: [&str; N]);

    /// Extend with multiple [`str`]s without unnecessary allocations.
    fn extend<const N: usize>(&mut self, strings: [&str; N]);

    /// Push a [`str`].
    fn push_str(&mut self, value: &str);

    /// Insert a string at `idx` without unnecessary allocations.
    fn with_insert_str(self, idx: usize, string: &str) -> Cow<'static, str>;

    /// Truncate without any validity checks.
    unsafe fn truncate_unchecked(&mut self, len: usize);
}

impl CowStrExt for Cow<'_, str> {
    unsafe fn retain_range_unchecked<B: RangeBounds<usize>>(&mut self, range: B) {
        unsafe {
            match self {
                Cow::Owned(x) => {
                    let start = match range.start_bound() {
                        Bound::Unbounded    => 0,
                        Bound::Excluded(&y) => y + 1,
                        Bound::Included(&y) => y,
                    };

                    let after = match range.end_bound() {
                        Bound::Unbounded    => x.len(),
                        Bound::Excluded(&y) => y,
                        Bound::Included(&y) => y + 1,
                    };

                    let x = x.as_mut_vec();

                    std::ptr::copy(x.as_ptr().add(start), x.as_mut_ptr(), after - start);

                    x.set_len(x.len() - start - (x.len() - after));
                },
                Cow::Borrowed(x) => *x = x.get_unchecked((range.start_bound().cloned(), range.end_bound().cloned()))
            }
        }
    }

    fn replace_range<B: RangeBounds<usize>>(&mut self, range: B, with: &str) {
        match self {
            Cow::Owned(x) => x.replace_range(range, with),
            Cow::Borrowed(x) => {
                let start = match range.start_bound() {
                    Bound::Unbounded    => 0,
                    Bound::Included(&x) => x,
                    Bound::Excluded(&x) => x + 1,
                };

                let after = match range.end_bound() {
                    Bound::Unbounded    => x.len(),
                    Bound::Included(&x) => x + 1,
                    Bound::Excluded(&x) => x,
                };

                let mut ret = String::with_capacity(x.len() - (after - start) + with.len());

                ret.extend([&x[..start], with, &x[after..]]);

                *self = ret.into()
            }
        }
    }

    fn replace_substr(&mut self, substr: *const str, with: &str) {
        self.replace_range(self.my_substr_range(substr), with)
    }

    fn insert_with<const N: usize>(&mut self, idx: usize, strings: [&str; N]) {
        match self {
            Cow::Owned(x) => x.insert_with(idx, strings),
            Cow::Borrowed(x) => {
                let mut ret = String::with_capacity(x.len() + strings.into_iter().map(str::len).sum::<usize>());

                ret.push_str(&x[..idx]);
                ret.extend(strings);
                ret.push_str(&x[idx..]);

                *self = ret.into();
            }
        }
    }

    fn extend<const N: usize>(&mut self, strings: [&str; N]) {
        match self {
            Cow::Owned(x) => x.extend(strings),
            Cow::Borrowed(x) => {
                let mut ret = String::with_capacity(x.len() + strings.into_iter().map(str::len).sum::<usize>());

                ret.push_str(x);
                ret.extend(strings);

                *self = ret.into();
            }
        }
    }

    fn push_str(&mut self, value: &str) {
        match self {
            Cow::Owned(x) => x.push_str(value),
            Cow::Borrowed(x) => {
                let mut ret = String::with_capacity(x.len() + value.len());

                ret.push_str(x);
                ret.push_str(value);

                *self = ret.into();
            }
        }
    }

    fn with_insert_str(self, idx: usize, string: &str) -> Cow<'static, str> {
        let mut ret = String::with_capacity(self.len() + string.len());

        ret.push_str(&self[..idx]);
        ret.push_str(string);
        ret.push_str(&self[idx..]);

        ret.into()
    }

    unsafe fn truncate_unchecked(&mut self, len: usize) {
        match self {
            Cow::Owned   (x) =>      unsafe {x.as_mut_vec().set_len(len);},
            Cow::Borrowed(x) => *x = unsafe {x.get_unchecked(..len)},
        }
    }
}