use super::DataValue;
#[inline(always)]
pub(crate) fn key_eq(a: &str, b: &str) -> bool {
let ab = a.as_bytes();
let bb = b.as_bytes();
if ab.len() != bb.len() {
return false;
}
let n = ab.len();
match n {
0 => true,
1 => ab[0] == bb[0],
2 => {
let x = u16::from_ne_bytes(ab[..2].try_into().unwrap());
let y = u16::from_ne_bytes(bb[..2].try_into().unwrap());
x == y
}
3 => {
let x = u16::from_ne_bytes(ab[..2].try_into().unwrap());
let y = u16::from_ne_bytes(bb[..2].try_into().unwrap());
x == y && ab[2] == bb[2]
}
4 => {
let x = u32::from_ne_bytes(ab[..4].try_into().unwrap());
let y = u32::from_ne_bytes(bb[..4].try_into().unwrap());
x == y
}
5..=7 => {
let x = u32::from_ne_bytes(ab[..4].try_into().unwrap());
let y = u32::from_ne_bytes(bb[..4].try_into().unwrap());
if x != y {
return false;
}
let xt = u32::from_ne_bytes(ab[n - 4..].try_into().unwrap());
let yt = u32::from_ne_bytes(bb[n - 4..].try_into().unwrap());
xt == yt
}
8 => {
let x = u64::from_ne_bytes(ab[..8].try_into().unwrap());
let y = u64::from_ne_bytes(bb[..8].try_into().unwrap());
x == y
}
9..=16 => {
let x = u64::from_ne_bytes(ab[..8].try_into().unwrap());
let y = u64::from_ne_bytes(bb[..8].try_into().unwrap());
if x != y {
return false;
}
let xt = u64::from_ne_bytes(ab[n - 8..].try_into().unwrap());
let yt = u64::from_ne_bytes(bb[n - 8..].try_into().unwrap());
xt == yt
}
_ => ab == bb,
}
}
#[inline(always)]
pub(crate) fn object_lookup_field<'a>(
pairs: &'a [(&'a str, DataValue<'a>)],
target: &str,
) -> Option<&'a DataValue<'a>> {
let tb = target.as_bytes();
let tlen = tb.len();
if tlen == 0 {
for (k, v) in pairs {
if k.is_empty() {
return Some(v);
}
}
return None;
}
let tfirst = tb[0];
for (k, v) in pairs {
let kb = k.as_bytes();
if kb.len() != tlen {
continue;
}
if kb[0] != tfirst {
continue;
}
if key_eq(k, target) {
return Some(v);
}
}
None
}