use crate::prelude::*;
#[expect(clippy::type_complexity, reason = "It's fine.")]
pub fn try_decode_query_part<'a, T: Into<Cow<'a, str>>>(value: T) -> Result<(bool, Cow<'a, str>), (std::str::Utf8Error, Cow<'a, [u8]>)> {
let value = value.into();
if memchr::memchr2(b'%', b'+', value.as_bytes()).is_none() {
return Ok((false, value));
}
Ok((true, try_cow_bytes_to_str(_decode_query_part(value))?))
}
pub fn lossy_decode_query_part<'a, T: Into<Cow<'a, str>>>(value: T) -> (bool, Cow<'a, str>) {
let value = value.into();
if memchr::memchr2(b'%', b'+', value.as_bytes()).is_none() {
return (false, value);
}
(true, lossy_cow_bytes_to_str(_decode_query_part(value)))
}
pub fn decode_query_part<'a, T: Into<Cow<'a, str>>>(value: T) -> (bool, Cow<'a, [u8]>) {
let value = value.into();
if memchr::memchr2(b'%', b'+', value.as_bytes()).is_none() {
return (false, cow_str_to_bytes(value));
}
(true, _decode_query_part(value))
}
pub fn decode_query_part_bytes<'a, T: Into<Cow<'a, [u8]>>>(value: T) -> (bool, Cow<'a, [u8]>) {
let value = value.into();
if memchr::memchr2(b'%', b'+', &value).is_none() {
return (false, value);
}
(true, _decode_query_part_bytes(value))
}
fn _decode_query_part<'a, T: Into<Cow<'a, str>>>(value: T) -> Cow<'a, [u8]> {
_decode_query_part_bytes(cow_str_to_bytes(value))
}
fn _decode_query_part_bytes<'a, T: Into<Cow<'a, [u8]>>>(value: T) -> Cow<'a, [u8]> {
let mut value = value.into();
let x = value.to_mut();
let mut r = 0;
let mut w = 0;
unsafe {
loop {
match *x.get_unchecked(r..) {
[b'%', h, l, ..] if let Some(b) = decode_hex_byte(h, l) => {
*x.get_unchecked_mut(w) = b;
r += 3;
w += 1;
},
[b'+', ..] => {*x.get_unchecked_mut(w) = b' '; r += 1; w += 1;},
[b , ..] => {*x.get_unchecked_mut(w) = b ; r += 1; w += 1;},
[] => break,
}
}
x.set_len(w);
}
value
}