use alloc::{borrow::Cow, string::String};
#[inline]
pub fn delete_end_slash<S: ?Sized + AsRef<str>>(s: &S) -> &str {
let s = s.as_ref();
let length = s.len();
if length > 1 && s.ends_with('/') {
unsafe { s.get_unchecked(..length - 1) }
} else {
s
}
}
#[inline]
pub fn delete_end_slash_in_place(s: &mut String) {
let length = s.len();
if length > 1 && s.ends_with('/') {
unsafe {
s.as_mut_vec().set_len(length - 1);
}
}
}
#[inline]
pub fn delete_start_slash<S: ?Sized + AsRef<str>>(s: &S) -> &str {
let s = s.as_ref();
let length = s.len();
if length > 1 && s.starts_with('/') {
unsafe { s.get_unchecked(1..) }
} else {
s
}
}
#[inline]
pub fn delete_start_slash_in_place(s: &mut String) {
let length = s.len();
if length > 1 && s.starts_with('/') {
s.remove(0);
}
}
#[inline]
pub fn add_start_slash<S: ?Sized + AsRef<str>>(s: &S) -> Cow<str> {
let s = s.as_ref();
if s.starts_with('/') {
Cow::from(s)
} else {
Cow::from(format!("/{}", s))
}
}
#[inline]
pub fn add_start_slash_in_place(s: &mut String) {
if !s.starts_with('/') {
s.insert(0, '/');
}
}
#[inline]
pub fn add_end_slash<S: ?Sized + AsRef<str>>(s: &S) -> Cow<str> {
let s = s.as_ref();
if s.ends_with('/') {
Cow::from(s)
} else {
Cow::from(format!("{}/", s))
}
}
#[inline]
pub fn add_end_slash_in_place(s: &mut String) {
if !s.ends_with('/') {
s.push('/');
}
}
#[inline]
pub fn concat_with_slash<S1: Into<String>, S2: AsRef<str>>(s1: S1, s2: S2) -> String {
let mut s1 = s1.into();
concat_with_slash_in_place(&mut s1, s2);
s1
}
#[inline]
pub fn concat_with_slash_in_place<S2: AsRef<str>>(s1: &mut String, s2: S2) {
add_end_slash_in_place(s1);
s1.push_str(delete_start_slash(s2.as_ref()));
delete_end_slash_in_place(s1);
}
#[macro_export]
macro_rules! slash {
() => {
'/'
};
($s:expr $(, $sc:expr)* $(,)*) => {
{
let mut s = $s.to_owned();
$(
$crate::concat_with_slash_in_place(&mut s, $sc);
)*
s
}
};
}
#[macro_export]
macro_rules! slash_in_place {
() => {
'/'
};
($s:expr $(, $sc:expr)* $(,)*) => {
$(
$crate::concat_with_slash_in_place($s, $sc);
)*
};
}
concat_with::concat_impl! {
#[macro_export]
concat_with_slash => "/"
}