use crate::prelude::*;
pub fn make_special_not_file_path<'a, T: Into<Cow<'a, str>>>(value: T) -> (bool, Cow<'a, str>) {
make_special_not_file_path_bytes(cow_str_to_bytes(value))
}
pub fn make_special_not_file_path_bytes<'a, T: Into<Cow<'a, [u8]>>>(value: T) -> (bool, Cow<'a, str>) {
let value = value.into();
if value.is_empty() {
return (true, "/".into());
}
let prepend_slash = !matches!(&*value, [b'/' | b'\\', ..]);
let mut to_encode = 0;
let mut class = 0;
for &b in &*value {
class |= SCHEME_PATH_BYTE_CLASS_DATA[b as usize];
}
if class & 1 == 1 {
for &b in &*value {
to_encode += PATH.contains(b) as usize;
}
}
#[cold]
fn outline<'a>(value: Cow<'a, [u8]>, to_encode: usize, prepend_slash: bool) -> Cow<'a, str> {
match to_encode {
0 => match prepend_slash {
true => unsafe {cow_bytes_to_str_unchecked(value)}.with_insert_str(0, "/"),
false => unsafe {cow_bytes_to_str_unchecked(value)},
},
_ => {
let len = value.len() + to_encode * 2 + prepend_slash as usize;
let mut ret = String::with_capacity(len);
if prepend_slash {
unsafe {
*ret.as_mut_ptr() = b'/';
}
}
let mut w = prepend_slash as usize;
unsafe {
for &b in &*value {
if PATH.contains(b) {
*ret.as_mut_ptr().add(w ) = b'%';
*ret.as_mut_ptr().add(w + 1) = NIBBLES[b as usize >> 4];
*ret.as_mut_ptr().add(w + 2) = NIBBLES[b as usize & 15];
w += 3;
} else {
*ret.as_mut_ptr().add(w) = b;
w += 1;
}
}
ret.as_mut_vec().set_len(len);
}
ret.into()
}
}
}
let value = match to_encode != 0 || prepend_slash {
false => unsafe {cow_bytes_to_str_unchecked(value)},
true => outline(value, to_encode, prepend_slash),
};
let (forwarded_slashes, value) = match class & 2 {
0 => (false, value),
_ => forward_slashes(value),
};
let (needed_resolve, value) = match class & 4 {
0 => (false, value),
_ => resolve_special_not_file_path(value),
};
(prepend_slash || to_encode != 0 || forwarded_slashes || needed_resolve, value)
}
pub fn non_special_path_to_special_not_file_path<'a, T: Into<Cow<'a, str>>>(value: T) -> (bool, Cow<'a, str>) {
match forward_slashes(value) {
(true , value) => (true , resolve_special_not_file_path(value).1),
(false, value) => (false, value),
}
}
pub fn resolve_special_not_file_path<'a, T: Into<Cow<'a, str>>>(value: T) -> (bool, Cow<'a, str>) {
resolve_special_not_file_path_range(value, ..)
}
pub fn resolve_special_not_file_path_range<'a, T: Into<Cow<'a, str>>, B: RangeBounds<usize>>(value: T, range: B) -> (bool, Cow<'a, str>) {
resolve_non_special_path_range(value, range)
}
pub fn opaque_path_to_special_not_file_path<'a, T: Into<Cow<'a, str>>>(value: T) -> (bool, Cow<'a, str>) {
let value = value.into().with_insert_str(0, "/");
let (_, value) = forward_slashes(value);
let (_, value) = resolve_special_not_file_path(value);
(true, value)
}