use crate::prelude::*;
#[expect(clippy::type_complexity, reason = "It's fine.")]
#[inline]
pub fn try_percent_decode<'a, T: Into<Cow<'a, str>>>(value: T) -> Result<(bool, Cow<'a, str>), (std::str::Utf8Error, Cow<'a, [u8]>)> {
let value = value.into();
Ok(match value.memchr(b'%') {
Some(r) => (true , try_cow_bytes_to_str(_percent_decode(value, r))?),
None => (false, value),
})
}
#[expect(clippy::type_complexity, reason = "It's fine.")]
#[inline]
pub fn try_percent_decode_bytes<'a, T: Into<Cow<'a, [u8]>>>(value: T) -> Result<(bool, Cow<'a, str>), (std::str::Utf8Error, Cow<'a, [u8]>)> {
let value = value.into();
Ok(match value.memchr(b'%') {
Some(r) => (true , try_cow_bytes_to_str(_percent_decode_bytes(value, r))?),
None => (false, try_cow_bytes_to_str(value)?),
})
}
#[inline]
pub fn lossy_percent_decode<'a, T: Into<Cow<'a, str>>>(value: T) -> (bool, Cow<'a, str>) {
let value = value.into();
match value.memchr(b'%') {
Some(r) => (true , lossy_cow_bytes_to_str(_percent_decode(value, r))),
None => (false, value),
}
}
#[inline]
pub fn lossy_percent_decode_bytes<'a, T: Into<Cow<'a, [u8]>>>(value: T) -> (bool, Cow<'a, str>) {
let value = value.into();
match value.memchr(b'%') {
Some(r) => (true , lossy_cow_bytes_to_str(_percent_decode_bytes(value, r))),
None => (false, lossy_cow_bytes_to_str(value)),
}
}
#[inline]
pub fn percent_decode<'a, T: Into<Cow<'a, str>>>(value: T) -> (bool, Cow<'a, [u8]>) {
percent_decode_bytes(cow_str_to_bytes(value))
}
#[inline]
pub fn percent_decode_bytes<'a, T: Into<Cow<'a, [u8]>>>(value: T) -> (bool, Cow<'a, [u8]>) {
let value = value.into();
match value.memchr(b'%') {
Some(r) => (true , _percent_decode_bytes(value, r)),
None => (false, value)
}
}
fn _percent_decode<'a, T: Into<Cow<'a, str>>>(value: T, r: usize) -> Cow<'a, [u8]> {
_percent_decode_bytes(cow_str_to_bytes(value), r)
}
fn _percent_decode_bytes<'a, T: Into<Cow<'a, [u8]>>>(value: T, mut r: usize) -> Cow<'a, [u8]> {
let mut value = value.into();
let mut w = r;
let x = value.to_mut();
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;
},
[] => break,
}
}
x.set_len(w);
}
value
}
pub(crate) fn decode_hex_byte(h: u8, l: u8) -> Option<u8> {
Some(decode_hex_nibble(h)? * 16 + decode_hex_nibble(l)?)
}
pub(crate) fn decode_hex_nibble(x: u8) -> Option<u8> {
match x {
b'0'..=b'9' => Some(x - b'0'),
b'a'..=b'f' => Some(x - b'a' + 10),
b'A'..=b'F' => Some(x - b'A' + 10),
_ => None
}
}