use crate::{PhpKey, PhpValue};
pub fn is_empty(value: &PhpValue) -> bool {
match value {
PhpValue::Null => true,
PhpValue::Bool(b) => !b,
PhpValue::Int(i) => *i == 0,
PhpValue::Float(f) => *f == 0.0,
PhpValue::Str(s) => s.is_empty() || s.as_slice() == b"0",
PhpValue::Array(items) => items.is_empty(),
_ => false,
}
}
pub fn bytes_truthy(bytes: &[u8]) -> bool {
!matches!(bytes, b"" | b"0")
}
pub fn get<'a>(items: &'a [(PhpKey, PhpValue)], name: &str) -> Option<&'a PhpValue> {
items
.iter()
.find(|(k, _)| matches!(k, PhpKey::Str(s) if s.as_slice() == name.as_bytes()))
.map(|(_, v)| v)
}
pub fn isset<'a>(items: &'a [(PhpKey, PhpValue)], name: &str) -> Option<&'a PhpValue> {
get(items, name).filter(|v| !matches!(v, PhpValue::Null))
}
pub fn set(items: &mut Vec<(PhpKey, PhpValue)>, name: &str, value: PhpValue) {
let key = PhpKey::Str(name.as_bytes().to_vec());
match items.iter_mut().find(|(k, _)| *k == key) {
Some(slot) => slot.1 = value,
None => items.push((key, value)),
}
}
pub fn unset(items: &mut Vec<(PhpKey, PhpValue)>, name: &str) {
items.retain(|(k, _)| !matches!(k, PhpKey::Str(s) if s.as_slice() == name.as_bytes()));
}
pub fn array_merge(
base: Vec<(PhpKey, PhpValue)>,
over: Vec<(PhpKey, PhpValue)>,
) -> Vec<(PhpKey, PhpValue)> {
let mut out: Vec<(PhpKey, PhpValue)> = Vec::with_capacity(base.len() + over.len());
let mut next = 0;
for (key, value) in base.into_iter().chain(over) {
match key {
PhpKey::Int(_) => {
out.push((PhpKey::Int(next), value));
next += 1;
}
PhpKey::Str(_) => match out.iter_mut().find(|(k, _)| *k == key) {
Some(slot) => slot.1 = value,
None => out.push((key, value)),
},
}
}
out
}
pub fn is_integer_key(bytes: &[u8]) -> bool {
let digits = bytes.strip_prefix(b"-").unwrap_or(bytes);
let canonical = match digits {
[b'0'] => bytes.len() == 1,
[b'1'..=b'9', rest @ ..] => rest.iter().all(u8::is_ascii_digit),
_ => false,
};
canonical
&& std::str::from_utf8(bytes)
.ok()
.and_then(|s| s.parse::<i64>().ok())
.is_some()
}
pub fn to_array_key(value: &PhpValue) -> Option<PhpKey> {
match value {
PhpValue::Null => Some(PhpKey::Str(Vec::new())),
PhpValue::Bool(b) => Some(PhpKey::Int(i64::from(*b))),
PhpValue::Int(i) => Some(PhpKey::Int(*i)),
PhpValue::Float(f) if !f.is_finite() => Some(PhpKey::Int(0)),
PhpValue::Float(f) => {
let wrapped = f.trunc() % 18_446_744_073_709_551_616.0;
Some(PhpKey::Int(wrapped as i128 as i64))
}
PhpValue::Str(s) if is_integer_key(s) => std::str::from_utf8(s)
.ok()
.and_then(|s| s.parse().ok())
.map(PhpKey::Int),
PhpValue::Str(s) => Some(PhpKey::Str(s.clone())),
_ => None,
}
}
pub fn key_to_string(key: &PhpKey) -> String {
match key {
PhpKey::Int(i) => i.to_string(),
PhpKey::Str(s) => String::from_utf8_lossy(s).into_owned(),
}
}
pub fn to_array(value: PhpValue) -> Vec<(PhpKey, PhpValue)> {
match value {
PhpValue::Null => Vec::new(),
PhpValue::Array(items) => items,
PhpValue::Object { properties, .. } => properties
.into_iter()
.map(|(key, value)| match key {
PhpKey::Str(name) if is_integer_key(&name) => {
let int = std::str::from_utf8(&name)
.ok()
.and_then(|s| s.parse().ok())
.expect("an integer key parses");
(PhpKey::Int(int), value)
}
key => (key, value),
})
.collect(),
PhpValue::Enum { case, .. } => vec![(
PhpKey::Str(b"name".to_vec()),
PhpValue::Str(case.into_bytes()),
)],
other => vec![(PhpKey::Int(0), other)],
}
}
pub fn to_php_string(value: &PhpValue) -> Vec<u8> {
match value {
PhpValue::Null | PhpValue::Bool(false) => Vec::new(),
PhpValue::Bool(true) => b"1".to_vec(),
PhpValue::Int(i) => i.to_string().into_bytes(),
PhpValue::Float(f) => float_to_string(*f).into_bytes(),
PhpValue::Str(s) => s.clone(),
PhpValue::Array(_) => b"Array".to_vec(),
PhpValue::Object { class, .. }
| PhpValue::Custom { class, .. }
| PhpValue::Enum { class, .. } => class.clone().into_bytes(),
PhpValue::Ref(_) | PhpValue::ObjectRef(_) => Vec::new(),
}
}
pub fn to_php_int(value: &PhpValue) -> i64 {
match value {
PhpValue::Null | PhpValue::Bool(false) => 0,
PhpValue::Bool(true) => 1,
PhpValue::Int(i) => *i,
PhpValue::Float(f) => float_to_int(*f),
PhpValue::Str(s) => string_to_int(s),
PhpValue::Array(items) => i64::from(!items.is_empty()),
PhpValue::Object { .. }
| PhpValue::Custom { .. }
| PhpValue::Enum { .. }
| PhpValue::ObjectRef(_) => 1,
PhpValue::Ref(_) => 0,
}
}
fn float_to_int(f: f64) -> i64 {
const TWO_63: f64 = 9_223_372_036_854_775_808.0;
if !f.is_finite() {
return 0;
}
if (-TWO_63..TWO_63).contains(&f) {
return f as i64;
}
const TWO_64: f64 = 18_446_744_073_709_551_616.0;
let mut m = f.trunc() % TWO_64;
if m < 0.0 {
m += TWO_64;
}
(m as u64) as i64
}
fn string_to_int(bytes: &[u8]) -> i64 {
let ws = |b: &u8| matches!(b, b' ' | b'\t' | b'\n' | b'\r' | b'\x0b' | b'\x0c');
let start = bytes.iter().position(|b| !ws(b)).unwrap_or(bytes.len());
let s = &bytes[start..];
let mut i = 0;
if matches!(s.first(), Some(b'+' | b'-')) {
i += 1;
}
let int_digits = s[i..].iter().take_while(|b| b.is_ascii_digit()).count();
i += int_digits;
let mut is_float = false;
let mut frac_digits = 0;
if s.get(i) == Some(&b'.') {
frac_digits = s[i + 1..].iter().take_while(|b| b.is_ascii_digit()).count();
if int_digits + frac_digits > 0 {
is_float = true;
i += 1 + frac_digits;
}
}
if int_digits + frac_digits == 0 {
return 0;
}
if matches!(s.get(i), Some(b'e' | b'E')) {
let mut j = i + 1;
if matches!(s.get(j), Some(b'+' | b'-')) {
j += 1;
}
let exp_digits = s[j..].iter().take_while(|b| b.is_ascii_digit()).count();
if exp_digits > 0 {
is_float = true;
i = j + exp_digits;
}
}
let text = std::str::from_utf8(&s[..i]).expect("ASCII");
if !is_float {
return text.parse::<i64>().unwrap_or(if text.starts_with('-') {
i64::MIN
} else {
i64::MAX
});
}
let f: f64 = text.parse().expect("a float literal");
if !f.is_finite() {
0
} else {
f as i64
}
}
fn float_to_string(f: f64) -> String {
const PRECISION: i32 = 14;
if f.is_nan() {
return "NAN".into();
}
if f.is_infinite() {
return if f > 0.0 { "INF".into() } else { "-INF".into() };
}
if f == 0.0 {
return if f.is_sign_negative() {
"-0".into()
} else {
"0".into()
};
}
let sci = format!("{:.*e}", (PRECISION - 1) as usize, f.abs());
let (mantissa, exp) = sci.split_once('e').expect("{:e} has an exponent");
let exp: i32 = exp.parse().expect("an integer exponent");
let digits: String = mantissa.chars().filter(char::is_ascii_digit).collect();
let digits = digits.trim_end_matches('0');
let digits = if digits.is_empty() { "0" } else { digits };
let sign = if f < 0.0 { "-" } else { "" };
let n = digits.len() as i32;
if !(-4..PRECISION).contains(&exp) {
let frac = if n > 1 { &digits[1..] } else { "0" };
return format!(
"{sign}{}.{frac}E{}{}",
&digits[..1],
if exp < 0 { '-' } else { '+' },
exp.abs()
);
}
let body = if exp >= 0 {
if n > exp + 1 {
format!(
"{}.{}",
&digits[..(exp + 1) as usize],
&digits[(exp + 1) as usize..]
)
} else {
format!("{digits}{}", "0".repeat((exp + 1 - n) as usize))
}
} else {
format!("0.{}{digits}", "0".repeat((-exp - 1) as usize))
};
format!("{sign}{body}")
}
#[derive(Clone, Copy)]
enum Numeric {
Int(i64),
Float { value: f64, overflow: i8 },
}
impl Numeric {
fn as_f64(self) -> f64 {
match self {
Numeric::Int(i) => i as f64,
Numeric::Float { value, .. } => value,
}
}
}
fn numeric(bytes: &[u8]) -> Option<Numeric> {
let ws = |b: &u8| matches!(b, b' ' | b'\t' | b'\n' | b'\r' | b'\x0b' | b'\x0c');
let start = bytes.iter().position(|b| !ws(b))?;
let end = bytes.iter().rposition(|b| !ws(b))? + 1;
let s = &bytes[start..end];
let mut i = 0;
if matches!(s.first(), Some(b'+' | b'-')) {
i += 1;
}
let int_digits = s[i..].iter().take_while(|b| b.is_ascii_digit()).count();
i += int_digits;
let mut frac_digits = 0;
let mut is_float = false;
if s.get(i) == Some(&b'.') {
is_float = true;
i += 1;
frac_digits = s[i..].iter().take_while(|b| b.is_ascii_digit()).count();
i += frac_digits;
}
if int_digits + frac_digits == 0 {
return None;
}
if matches!(s.get(i), Some(b'e' | b'E')) {
let mut j = i + 1;
if matches!(s.get(j), Some(b'+' | b'-')) {
j += 1;
}
let exp_digits = s[j..].iter().take_while(|b| b.is_ascii_digit()).count();
if exp_digits > 0 {
is_float = true;
i = j + exp_digits;
}
}
if i != s.len() {
return None;
}
let text = std::str::from_utf8(s).ok()?;
if !is_float {
if let Ok(int) = text.parse::<i64>() {
return Some(Numeric::Int(int));
}
let value: f64 = text.parse().ok()?;
let overflow = if value < 0.0 { -1 } else { 1 };
return Some(Numeric::Float { value, overflow });
}
let value: f64 = text.parse().ok()?;
Some(Numeric::Float { value, overflow: 0 })
}
fn truthy(value: &PhpValue) -> bool {
!is_empty(value)
}
fn number(value: &PhpValue) -> Option<Numeric> {
match value {
PhpValue::Int(i) => Some(Numeric::Int(*i)),
PhpValue::Float(f) => Some(Numeric::Float {
value: *f,
overflow: 0,
}),
_ => None,
}
}
fn numbers_eq(a: Numeric, b: Numeric) -> bool {
match (a, b) {
(Numeric::Int(x), Numeric::Int(y)) => x == y,
_ => a.as_f64() == b.as_f64(),
}
}
fn strings_eq(a: &[u8], b: &[u8]) -> bool {
let (Some(x), Some(y)) = (numeric(a), numeric(b)) else {
return a == b;
};
match (x, y) {
(Numeric::Int(x), Numeric::Int(y)) => x == y,
(
Numeric::Float {
value: v1,
overflow: o1,
},
Numeric::Float {
value: v2,
overflow: o2,
},
) => {
if (o1 != 0 && o1 == o2 && v1 == v2) || (v1 == v2 && !v1.is_finite()) {
a == b
} else {
v1 == v2
}
}
(Numeric::Int(_), Numeric::Float { overflow, .. })
| (Numeric::Float { overflow, .. }, Numeric::Int(_))
if overflow != 0 =>
{
false
}
_ => x.as_f64() == y.as_f64(),
}
}
fn hashes_eq(a: &[(PhpKey, PhpValue)], b: &[(PhpKey, PhpValue)]) -> bool {
a.len() == b.len()
&& a.iter().all(|(key, value)| {
b.iter()
.find(|(k, _)| k == key)
.is_some_and(|(_, other)| loose_eq(value, other))
})
}
pub fn loose_eq(a: &PhpValue, b: &PhpValue) -> bool {
use PhpValue::{Array, Bool, Enum, Null, Object, Str};
match (a, b) {
(Null, Null) => true,
(Null, Str(s)) | (Str(s), Null) => s.is_empty(),
(Null | Bool(_), _) | (_, Null | Bool(_)) => truthy(a) == truthy(b),
(Str(x), Str(y)) => strings_eq(x, y),
(Str(s), n) | (n, Str(s)) if number(n).is_some() => {
let n = number(n).expect("checked");
if matches!(n, Numeric::Float { value, .. } if value.is_nan()) {
return false;
}
match numeric(s) {
Some(parsed) => numbers_eq(n, parsed),
None => {
to_php_string(&match n {
Numeric::Int(i) => PhpValue::Int(i),
Numeric::Float { value, .. } => PhpValue::Float(value),
}) == *s
}
}
}
(x, y) if number(x).is_some() && number(y).is_some() => {
numbers_eq(number(x).expect("checked"), number(y).expect("checked"))
}
(Array(x), Array(y)) => hashes_eq(x, y),
(
Object {
class: c1,
properties: p1,
},
Object {
class: c2,
properties: p2,
},
) => c1 == c2 && hashes_eq(p1, p2),
(Enum { .. }, Enum { .. }) => a == b,
_ => false,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bytes_truthy_is_false_only_for_empty_and_zero() {
assert!(!bytes_truthy(b""));
assert!(!bytes_truthy(b"0"));
assert!(bytes_truthy(b"00"));
assert!(bytes_truthy(b"0.0"));
assert!(bytes_truthy(b" "));
assert!(bytes_truthy(b"a"));
}
fn s(v: &str) -> PhpValue {
PhpValue::Str(v.as_bytes().to_vec())
}
fn k(v: &str) -> PhpKey {
PhpKey::Str(v.as_bytes().to_vec())
}
#[test]
fn empty_follows_php() {
for v in [
PhpValue::Null,
PhpValue::Bool(false),
PhpValue::Int(0),
PhpValue::Float(0.0),
s(""),
s("0"),
PhpValue::Array(vec![]),
] {
assert!(is_empty(&v), "{v:?}");
}
for v in [
PhpValue::Bool(true),
PhpValue::Int(-1),
s("00"),
s(" "),
s("a"),
PhpValue::Float(0.1),
] {
assert!(!is_empty(&v), "{v:?}");
}
}
#[test]
fn merge_replaces_in_place_and_renumbers_ints() {
let base = vec![(k("a"), s("1")), (k("b"), s("2"))];
let over = vec![(PhpKey::Int(7), s("x")), (k("b"), s("3")), (k("c"), s("4"))];
assert_eq!(
array_merge(base, over),
vec![
(k("a"), s("1")),
(k("b"), s("3")),
(PhpKey::Int(0), s("x")),
(k("c"), s("4"))
]
);
}
#[test]
fn array_keys_convert_as_php_does() {
use PhpValue::{Bool, Float, Int, Null};
let cases = [
(Null, Some(k(""))),
(Bool(false), Some(PhpKey::Int(0))),
(Bool(true), Some(PhpKey::Int(1))),
(Int(-7), Some(PhpKey::Int(-7))),
(Float(1.9), Some(PhpKey::Int(1))),
(Float(-2.5), Some(PhpKey::Int(-2))),
(Float(f64::NAN), Some(PhpKey::Int(0))),
(Float(f64::INFINITY), Some(PhpKey::Int(0))),
(Float(f64::NEG_INFINITY), Some(PhpKey::Int(0))),
(Float(1e20), Some(PhpKey::Int(7766279631452241920))),
(Float(-1e20), Some(PhpKey::Int(-7766279631452241920))),
(Float(9223372036854775808.0), Some(PhpKey::Int(i64::MIN))),
(Float(1e19), Some(PhpKey::Int(-8446744073709551616))),
(Float(18446744073709551616.0), Some(PhpKey::Int(0))),
(Float(1.5e300), Some(PhpKey::Int(0))),
(s("12"), Some(PhpKey::Int(12))),
(s("012"), Some(k("012"))),
(s("edit_book"), Some(k("edit_book"))),
(PhpValue::Array(vec![]), None),
];
for (value, want) in cases {
assert_eq!(to_array_key(&value), want, "{value:?}");
}
assert_eq!(key_to_string(&PhpKey::Int(-3)), "-3");
assert_eq!(key_to_string(&k("a")), "a");
}
#[test]
fn integer_keys_follow_php() {
for yes in ["0", "10", "-5", "9223372036854775807"] {
assert!(is_integer_key(yes.as_bytes()), "{yes}");
}
for no in [
"",
"-0",
"010",
"1e3",
" 1",
"1.5",
"9223372036854775808",
"color",
] {
assert!(!is_integer_key(no.as_bytes()), "{no}");
}
}
fn arr(items: Vec<(PhpKey, PhpValue)>) -> PhpValue {
PhpValue::Array(items)
}
#[test]
fn loose_eq_follows_php8() {
use PhpValue::{Bool, Float, Int, Null};
let t = Bool(true);
let f = Bool(false);
let cases: Vec<(PhpValue, PhpValue, bool)> = vec![
(t.clone(), s("yes"), true),
(Int(1), s("1"), true),
(Int(1), s("01"), true),
(s("1"), s("01"), true),
(s("1e1"), s("10"), true),
(s(" 1"), s("1"), true),
(s("1 "), s("1"), true),
(s("abc"), Int(0), false),
(Int(0), s(""), false),
(Null, f.clone(), true),
(Null, s(""), true),
(Null, s("0"), false),
(Null, Int(0), true),
(s("0"), f.clone(), true),
(arr(vec![]), f.clone(), true),
(
arr(vec![(PhpKey::Int(0), Int(1))]),
arr(vec![(PhpKey::Int(0), Int(1))]),
true,
),
(
arr(vec![(k("a"), Int(1)), (k("b"), Int(2))]),
arr(vec![(k("b"), Int(2)), (k("a"), s("1"))]),
true,
),
(arr(vec![(PhpKey::Int(0), Int(1))]), Int(1), false),
(Float(1.0), s("1"), true),
(s("9223372036854775807"), s("9223372036854775808"), false),
(s("1."), s("1"), true),
(s(".5"), s("0.5"), true),
(s("0x1"), s("1"), false),
(Float(1.5), s("1.5 "), true),
(Float(f64::INFINITY), s("INF"), true),
(s("abc"), s("ABC"), false),
(Int(1), Float(1.0), true),
(Null, arr(vec![]), true),
(t.clone(), arr(vec![]), false),
(Int(i64::MAX), s("9223372036854775808"), true),
(Int(i64::MIN), s("-9223372036854775809"), true),
(Float(f64::NAN), s("NAN"), false),
(Float(f64::NAN), s("abc"), false),
(Float(f64::NAN), s("1"), false),
(Float(f64::NAN), s(""), false),
];
for (a, b, want) in cases {
assert_eq!(loose_eq(&a, &b), want, "{a:?} == {b:?}");
assert_eq!(loose_eq(&b, &a), want, "{b:?} == {a:?}");
}
}
#[test]
fn to_php_string_follows_php() {
use PhpValue::{Bool, Float, Int, Null};
let cases: Vec<(PhpValue, &str)> = vec![
(Null, ""),
(Bool(false), ""),
(Bool(true), "1"),
(Int(-42), "-42"),
(s("x"), "x"),
(Float(1.0), "1"),
(Float(0.1), "0.1"),
(Float(0.1 + 0.2), "0.3"),
(Float(1.0 / 3.0), "0.33333333333333"),
(Float(1e14), "1.0E+14"),
(Float(1e13), "10000000000000"),
(Float(123456789012345.0), "1.2345678901234E+14"),
(Float(1.5e20), "1.5E+20"),
(Float(1e-5), "1.0E-5"),
(Float(0.0001), "0.0001"),
(Float(-0.0), "-0"),
(Float(-1.25), "-1.25"),
(Float(2.5e-7), "2.5E-7"),
(Float(f64::INFINITY), "INF"),
(Float(f64::NEG_INFINITY), "-INF"),
(Float(f64::NAN), "NAN"),
(arr(vec![]), "Array"),
];
for (v, want) in cases {
assert_eq!(to_php_string(&v), want.as_bytes(), "{v:?}");
}
}
#[test]
fn to_array_follows_the_cast() {
assert_eq!(to_array(PhpValue::Null), vec![]);
assert_eq!(to_array(s("x")), vec![(PhpKey::Int(0), s("x"))]);
let items = vec![(k("a"), PhpValue::Int(1))];
assert_eq!(to_array(arr(items.clone())), items);
let object = PhpValue::Object {
class: "stdClass".into(),
properties: items.clone(),
};
assert_eq!(to_array(object), items);
let object = PhpValue::Object {
class: "stdClass".into(),
properties: vec![
(k("0"), s("a")),
(k("10"), s("b")),
(k("010"), s("c")),
(k("x"), s("d")),
],
};
assert_eq!(
to_array(object),
vec![
(PhpKey::Int(0), s("a")),
(PhpKey::Int(10), s("b")),
(k("010"), s("c")),
(k("x"), s("d")),
]
);
}
#[test]
fn to_php_int_follows_php8() {
let cases = [
(PhpValue::Null, 0),
(PhpValue::Bool(false), 0),
(PhpValue::Bool(true), 1),
(PhpValue::Int(-7), -7),
(PhpValue::Float(1.9), 1),
(PhpValue::Float(-1.9), -1),
(PhpValue::Float(f64::NAN), 0),
(PhpValue::Float(f64::INFINITY), 0),
(PhpValue::Float(f64::NEG_INFINITY), 0),
(PhpValue::Float(1e30), 5_076_964_154_930_102_272),
(PhpValue::Float(-1e30), -5_076_964_154_930_102_272),
(PhpValue::Float(1e19), -8_446_744_073_709_551_616),
(PhpValue::Float(-9.3e18), 9_146_744_073_709_551_616),
(s("12abc"), 12),
(s("1e3"), 1000),
(s(" 7"), 7),
(s("abc"), 0),
(s(""), 0),
(s(" "), 0),
(s(" \n\t-4.7x"), -4),
(s("\x0b3"), 3),
(s("\x0c3"), 3),
(s("+5"), 5),
(s("-0"), 0),
(s(".5"), 0),
(s("5."), 5),
(s("1.e2"), 100),
(s("-.5e1"), -5),
(s("1.5e3"), 1500),
(s("1e"), 1),
(s("0x1A"), 0),
(s("9999999999999999999"), i64::MAX),
(s("-9999999999999999999"), i64::MIN),
(s("1e30"), i64::MAX),
(s("-1e30"), i64::MIN),
(s("1e999"), 0),
(s("-1e999"), 0),
(PhpValue::Array(vec![]), 0),
(PhpValue::Array(vec![(PhpKey::Int(0), PhpValue::Int(1))]), 1),
(
PhpValue::Object {
class: "stdClass".into(),
properties: vec![],
},
1,
),
];
for (value, int) in cases {
assert_eq!(to_php_int(&value), int, "(int) {value:?}");
}
}
}