diff options
author | valmat <ufabiz@gmail.com> | 2014-06-11 18:34:13 +0600 |
---|---|---|
committer | valmat <ufabiz@gmail.com> | 2014-06-11 18:34:13 +0600 |
commit | e350c8f1b33d7c6c605f5450e8cf901cc3e5e1c4 (patch) | |
tree | cdb358e5c480fdab2ad00d6fe20d008255278c14 /zend | |
parent | a3aea78834102b7d0f206c73463de69050f15076 (diff) |
Value::operator==
Diffstat (limited to 'zend')
-rw-r--r-- | zend/value.cpp | 151 |
1 files changed, 108 insertions, 43 deletions
diff --git a/zend/value.cpp b/zend/value.cpp index 74c84e5..e1f6683 100644 --- a/zend/value.cpp +++ b/zend/value.cpp @@ -1311,36 +1311,127 @@ Value Value::exec(const char *name, int argc, struct _zval_struct ***params) return do_exec(&_val, method._val, argc, params); } +/** + * Helper method to prepare for comparison + * @param value + */ +bool Value::comparePrepare(const Value &value) const +{ + Type thisTp = type(), thatTp = value.type(); + if(thisTp == Type::Resource || thatTp == Type::Resource) + { + throw Php::Exception("Resource types is not comparable"); + return false; + } + if(thisTp == Type::Constant || thatTp == Type::Constant || thisTp == Type::ConstantArray || thatTp == Type::ConstantArray) + { + throw Php::Exception("Constant types can not be assigned to a PHP-CPP library variable"); + return false; + } + if(thisTp == Type::Callable || thatTp == Type::Callable) + { + throw Php::Exception("Callable types can not be assigned to a PHP-CPP library variable"); + return false; + } + return true; +} /** - * Comparison operators for hardcoded Value + * Comparison operators== for hardcoded Value * @param value */ bool Value::operator==(const Value &value) const { - Type tp = type(); - const _zval_struct *thatval = (tp == value.type()) ? value._val : value.clone(tp)._val; + if(!comparePrepare(value)) return false; - switch (tp) { - case Type::Null: return true; - case Type::Numeric: return (_val->value.lval == thatval->value.lval); - case Type::Float: return (_val->value.dval == thatval->value.dval); - case Type::Bool: return (_val->value.lval == thatval->value.lval); - case Type::String: return (_val->value.str.len && thatval->value.str.len) && (::strcmp(_val->value.str.val, thatval->value.str.val) == 0); - // @todo - case Type::Array: throw Php::Exception("TODO implement comparison arrays"); break; - // @todo - case Type::Object: throw Php::Exception("TODO implement comparison arrays"); break; - case Type::Resource: return (_val->value.lval == thatval->value.lval); - case Type::Constant: throw Php::Exception("Constant types can not be assigned to a PHP-CPP library variable"); break; - case Type::ConstantArray: throw Php::Exception("Constant types can not be assigned to a PHP-CPP library variable"); break; - case Type::Callable: throw Php::Exception("Callable types can not be assigned to a PHP-CPP library variable"); break; + Type thisTp = type(), thatTp = value.type(); + //Type tp = type(); + //const _zval_struct *thatval = (tp == value.type()) ? value._val : value.clone(tp)._val; + + if( (thatTp == Type::Array && thisTp != Type::Array) || (thisTp == Type::Array && thatTp != Type::Array) ) + { + return false; + } + if(thatTp == Type::Array && thisTp == Type::Array) + { + if(size() != value.size()) return false; + auto thisIt = begin(), thatIt = value.begin(), thisEnd = end();; + while(thisIt != thisEnd) + { + if(*thisIt != *thatIt) return false; + ++thisIt; + ++thatIt; + } + return true; } + + if(thatTp == Type::Null) + { + return (thisTp == Type::Null) || + ( + (thisTp == Type::Numeric || thisTp == Type::Bool) && + _val->value.lval == 0 + ) || + ( (thisTp == Type::String) && _val->value.str.len == 0); + } + if(thisTp == Type::Null) + { + return ( + (thatTp == Type::Numeric || thatTp == Type::Bool) && + value._val->value.lval == 0 + ) || + ( (thatTp == Type::String) && value._val->value.str.len == 0); + } + + if(thatTp == Type::Float) + { + return clone(Type::Float) == value._val->value.dval; + } + if(thisTp == Type::Float) + { + return value.clone(Type::Float) == _val->value.dval; + } + + if(thatTp == Type::String) + { + return clone(Type::String) == value.rawValue(); + } + if(thisTp == Type::String) + { + return value.clone(Type::String) == rawValue(); + } + + if(thatTp == Type::Numeric || thatTp == Type::Bool) + { + return clone(thatTp) == value._val->value.lval; + } + if(thisTp == Type::String) + { + return value.clone(thisTp) == _val->value.lval; + } + + + if( (thatTp == Type::Object && thisTp != Type::Object) || (thisTp == Type::Object && thatTp != Type::Object) ) + { + return false; + } + if(thatTp == Type::Object && thisTp == Type::Object) + { + return !ClassImpl::compare(_val, value._val TSRMLS_CC); + } + return false; } + +/** + * Comparison operators< for hardcoded Value + * @param value + */ bool Value::operator< (const Value &value) const { + if(!comparePrepare(value)) return false; + Type tp = type(); const _zval_struct *thatval = (tp == value.type()) ? value._val : value.clone(tp)._val; @@ -1354,32 +1445,6 @@ bool Value::operator< (const Value &value) const case Type::Array: throw Php::Exception("TODO implement comparison arrays"); break; // @todo case Type::Object: throw Php::Exception("TODO implement comparison arrays"); break; - case Type::Resource: throw Php::Exception("Resource types is not comparable"); break; - case Type::Constant: throw Php::Exception("Constant types can not be assigned to a PHP-CPP library variable"); break; - case Type::ConstantArray: throw Php::Exception("Constant types can not be assigned to a PHP-CPP library variable"); break; - case Type::Callable: throw Php::Exception("Callable types can not be assigned to a PHP-CPP library variable"); break; - } - return true; -} -bool Value::operator> (const Value &value) const -{ - Type tp = type(); - const _zval_struct *thatval = (tp == value.type()) ? value._val : value.clone(tp)._val; - - switch (tp) { - case Type::Null: return false; - case Type::Numeric: return (_val->value.lval > thatval->value.lval); - case Type::Float: return (_val->value.dval > thatval->value.dval); - case Type::Bool: return (_val->value.lval > thatval->value.lval); - case Type::String: return (::strcmp(_val->value.str.val, thatval->value.str.val) > 0); - // @todo - case Type::Array: throw Php::Exception("TODO implement comparison arrays"); break; - // @todo - case Type::Object: throw Php::Exception("TODO implement comparison arrays"); break; - case Type::Resource: throw Php::Exception("Resource types is not comparable"); break; - case Type::Constant: throw Php::Exception("Constant types can not be assigned to a PHP-CPP library variable"); break; - case Type::ConstantArray: throw Php::Exception("Constant types can not be assigned to a PHP-CPP library variable"); break; - case Type::Callable: throw Php::Exception("Callable types can not be assigned to a PHP-CPP library variable"); break; } return true; } |