use crate::prelude::*;
pub(crate) trait CowStrExt {
unsafe fn retain_range_unchecked<B: RangeBounds<usize>>(&mut self, range: B);
fn replace_range<B: RangeBounds<usize>>(&mut self, range: B, with: &str);
fn replace_substr(&mut self, substr: *const str, with: &str);
fn insert_with<const N: usize>(&mut self, idx: usize, strings: [&str; N]);
fn extend<const N: usize>(&mut self, strings: [&str; N]);
fn push_str(&mut self, value: &str);
fn with_insert_str(self, idx: usize, string: &str) -> Cow<'static, str>;
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)},
}
}
}