Allow comparing two lists

This feature was never implemented. I'm not sure whether we need a way to compare array and other types of variables (left as ToDo), since e.g. in PHP it's always false.

Bug: T179238
Change-Id: I5d2c33fd117e69cbc84c0b04b6cb82edbdcadf16
This commit is contained in:
Daimona Eaytoy 2018-03-25 12:03:26 +02:00
parent ec9732aac4
commit 284ab234fd
4 changed files with 37 additions and 8 deletions

View file

@ -188,13 +188,34 @@ class AFPData {
}
/**
* @ToDo Should we also build a proper system to compare arrays with different types?
* @param AFPData $d1
* @param AFPData $d2
* @param bool $strict whether to also check types
* @return bool
*/
public static function equals( $d1, $d2 ) {
return $d1->type != self::DLIST && $d2->type != self::DLIST &&
$d1->toString() === $d2->toString();
public static function equals( $d1, $d2, $strict = false ) {
if ( $d1->type != self::DLIST && $d2->type != self::DLIST ) {
$typecheck = $d1->type == $d2->type || !$strict;
return $typecheck && $d1->toString() === $d2->toString();
} elseif ( $d1->type == self::DLIST && $d2->type == self::DLIST ) {
$data1 = $d1->data;
$data2 = $d2->data;
if ( count( $data1 ) !== count( $data2 ) ) {
return false;
}
$length = count( $data1 );
for ( $i = 0; $i < $length; $i++ ) {
$result = self::equals( $data1[$i], $data2[$i], $strict );
if ( $result === false ) {
return false;
}
}
return true;
} else {
// Trying to compare an array to something else
return false;
}
}
/**
@ -304,10 +325,10 @@ class AFPData {
return new AFPData( self::DBOOL, !self::equals( $a, $b ) );
}
if ( $op == '===' ) {
return new AFPData( self::DBOOL, $a->type == $b->type && self::equals( $a, $b ) );
return new AFPData( self::DBOOL, self::equals( $a, $b, true ) );
}
if ( $op == '!==' ) {
return new AFPData( self::DBOOL, $a->type != $b->type || !self::equals( $a, $b ) );
return new AFPData( self::DBOOL, !self::equals( $a, $b, true ) );
}
$a = $a->toString();
$b = $b->toString();

View file

@ -0,0 +1,11 @@
a := [1, 2, 3];
b := [1, 2, 3];
c := [2, 3, 4];
d := [1, 2, 3, 4];
e := ['1', '2', '3'];
f := [[['1']]];
g := [[[1]]];
h := [[1, 2], 3];
i := [['1', 2], '3'];
a == b & a === b & a != c & b != d & a == e & a !== e & f == g & f !== g & h == i & h !== i & e != i

View file

@ -1,3 +0,0 @@
a := [1, 2, 3];
a != a