use alloc::borrow::ToOwned;
use alloc::vec;
use alloc::string::String;
use zerocopy::FromBytes;

pub(crate) fn parse_name(ucs2_name: &[u8]) -> Option<String> {
    if ucs2_name.len() == 1 {
        if ucs2_name[0] == 0 {
            return Some(String::from("."));
        } else if ucs2_name[0] == 1 {
            return Some(String::from(".."));
        }
    }

    debug_assert!(ucs2_name.len() % 2 == 0, "The length of UCS-2 name must be a multiple of two");

    let mut utf8_str_buf = vec![0u8; ucs2_name.len()];

    let utf16_buf: &[u16] = FromBytes::ref_from_bytes(ucs2_name).unwrap();

    ucs2::decode(utf16_buf, &mut utf8_str_buf).ok()?;

    str::from_utf8(&utf8_str_buf).map(|x| x.trim_end_matches('\0').to_owned()).ok()
}
