use std::fmt::Write;
use std::net::Ipv6Addr;
use crate::prelude::*;
#[expect(clippy::missing_panics_doc, reason = "Shouldn't be possible.")]
pub fn make_ipv6_host<'a, T: Into<Cow<'a, str>>>(value: T) -> Result<(bool, Cow<'a, str>, Ipv6HostDetails), InvalidIpv6Host> {
let value = value.into();
let addr = match value.as_bytes() {
[b'[', addr @ .., b']'] => unsafe {str::from_utf8_unchecked(addr)}.parse::<Ipv6Addr>().map_err(|_| InvalidIpv6Host)?,
_ => Err(InvalidIpv6Host)?,
};
let mut normalizer = Normalizer::new(value);
write_ipv6_host(&addr, &mut normalizer).expect("???");
let (changed, host) = normalizer.done();
Ok((changed, host, addr.into()))
}
#[expect(clippy::missing_panics_doc, reason = "Shouldn't be possible.")]
pub fn make_ipv6_host_bytes<'a, T: Into<Cow<'a, [u8]>>>(value: T) -> Result<(bool, Cow<'a, str>, Ipv6HostDetails), InvalidIpv6Host> {
let value = value.into();
if !value.is_ascii() {
Err(InvalidIpv6Host)?;
}
let value = unsafe {cow_bytes_to_str_unchecked(value)};
let addr = match value.as_bytes() {
[b'[', addr @ .., b']'] => unsafe {str::from_utf8_unchecked(addr)}.parse::<Ipv6Addr>().map_err(|_| InvalidIpv6Host)?,
_ => Err(InvalidIpv6Host)?,
};
let mut normalizer = Normalizer::new(value);
write_ipv6_host(&addr, &mut normalizer).expect("???");
let (changed, host) = normalizer.done();
Ok((changed, host, addr.into()))
}
pub fn write_ipv6_host<F: Write>(addr: &Ipv6Addr, f: &mut F) -> std::fmt::Result {
struct Thing {
buf: [u8; 41],
len: usize,
}
impl Thing {
#[inline]
fn as_str(&self) -> &str {
unsafe {
str::from_utf8_unchecked(self.buf.get_unchecked(..self.len))
}
}
#[inline]
fn push(&mut self, b: u8) {
unsafe {
*self.buf.as_mut_ptr().add(self.len) = b;
}
self.len += 1;
}
#[inline]
fn push_str(&mut self, s: &str) {
unsafe {
std::ptr::copy_nonoverlapping(s.as_ptr(), self.buf.as_mut_ptr().add(self.len), s.len());
}
self.len += s.len();
}
}
let segments = addr.segments();
let (x, y) = find_ipv6_elision(&segments);
let mut temp = Thing {buf: [0; 41], len: 0};
temp.push(b'[');
for i in 0..x {
if i != 0 {
temp.push(b':');
}
temp.push_str(write_ipv6_num(segments[i as usize], &mut [0; 4]));
}
if x != y {
temp.push(b':');
temp.push(b':');
}
for i in y..8 {
temp.push_str(write_ipv6_num(segments[i as usize], &mut [0; 4]));
if i != 7 {
temp.push(b':');
}
}
temp.push(b']');
f.write_str(temp.as_str())
}
pub fn find_ipv6_elision(pieces: &[u16; 8]) -> (u8, u8) {
let mut x = 0u8;
for &piece in pieces {
x = (x << 1) | (piece == 0) as u8;
}
let mut i = 0;
while x & x << 1 != 0 {
x &= x << 1;
i += 1;
}
if i == 0 {
(0, 0)
} else {
let index = x.leading_zeros() as u8;
let length = i + 1;
(index, index + length)
}
}
pub fn write_ipv6_num(x: u16, buf: &mut [u8; 4]) -> &str {
const NIBBLES_LOWER: [u8; 16] = *b"0123456789abcdef";
buf[3] = NIBBLES_LOWER[(x as usize ) & 0xF];
buf[2] = NIBBLES_LOWER[(x as usize >> 4) & 0xF];
buf[1] = NIBBLES_LOWER[(x as usize >> 8) & 0xF];
buf[0] = NIBBLES_LOWER[(x as usize >> 12) & 0xF];
unsafe {
str::from_utf8_unchecked(buf.get_unchecked((x | 1).leading_zeros() as usize / 4 ..))
}
}