use crate::simd::find_string_terminator;
pub(crate) enum ParsedStr<'a> {
    Borrowed(&'a [u8]),
    Owned(Vec<u8>),
}

impl<'a> ParsedStr<'a> {
    pub(crate) fn as_bytes(&self) -> &[u8] {
        match self {
            ParsedStr::Borrowed(s) => s,
            ParsedStr::Owned(v) => v,
        }
    }
}

#[inline]
fn decode_hex_u16(bytes: &[u8]) -> Option<u16> {
    if bytes.len() != 4 {
        return None;
    }
    let mut value = 0u16;
    for &byte in bytes {
        let digit = match byte {
            b'0'..=b'9' => byte - b'0',
            b'a'..=b'f' => byte - b'a' + 10,
            b'A'..=b'F' => byte - b'A' + 10,
            _ => return None,
        };
        value = (value << 4) | digit as u16;
    }
    Some(value)
}

#[inline]
fn push_code_unit_wtf8(output: &mut Vec<u8>, unit: u16) {
    if (0xD800..=0xDFFF).contains(&unit) {
        output.push(0xE0 | (unit >> 12) as u8);
        output.push(0x80 | ((unit >> 6) & 0x3F) as u8);
        output.push(0x80 | (unit & 0x3F) as u8);
    } else {
        let ch =
            char::from_u32(unit as u32).expect("non-surrogate UTF-16 unit is a Unicode scalar");
        let mut buf = [0u8; 4];
        output.extend_from_slice(ch.encode_utf8(&mut buf).as_bytes());
    }
}


pub struct DirectParser<'a> { pub input: &'a [u8], pub pos: usize, pub valid: bool }
impl<'a> DirectParser<'a> {
 pub fn new(input: &'a [u8]) -> Self {Self {input,pos:0,valid:true}}
 fn peek(&self) -> Option<u8> {self.input.get(self.pos).copied()}
 fn advance(&mut self) {self.pos+=1;}
