Commit 8c9a5fe4 authored by Frederick Zhang's avatar Frederick Zhang

simplify to_array

parent c15937f7
Pipeline #340 passed with stage
in 47 seconds
......@@ -257,40 +257,32 @@ zval * bdict::to_array(const bool include_meta) const {
std::string class_name = zend_container::bnode_object_get_class_name(value);
char *str_index = (char *)emalloc(ZSTR_LEN(_str_index) + 1);
memcpy(str_index, ZSTR_VAL(_str_index), ZSTR_LEN(_str_index));
zval *subarray = nullptr;
if (class_name == "bdict") {
bdict_object *bnode = zend_container::bdict_fetch_object(Z_OBJ_P(value));
zval *subarray = bnode->bdict_data->to_array(include_meta);
add_assoc_zval_ex(zv, str_index, ZSTR_LEN(_str_index), subarray);
subarray = bnode->bdict_data->to_array(include_meta);
} else if (class_name == "blist") {
blist_object *bnode = zend_container::blist_fetch_object(Z_OBJ_P(value));
zval *subarray = bnode->blist_data->to_array(include_meta);
add_assoc_zval_ex(zv, str_index, ZSTR_LEN(_str_index), subarray);
subarray = bnode->blist_data->to_array(include_meta);
} else if (class_name == "bstr") {
bstr_object *bnode = zend_container::bstr_fetch_object(Z_OBJ_P(value));
zval *subarray = bnode->bstr_data->to_array(include_meta);
add_assoc_zval_ex(zv, str_index, ZSTR_LEN(_str_index), subarray);
subarray = bnode->bstr_data->to_array(include_meta);
} else if (class_name == "bint") {
bint_object *bnode = zend_container::bint_fetch_object(Z_OBJ_P(value));
zval *subarray = bnode->bint_data->to_array(include_meta);
subarray = bnode->bint_data->to_array(include_meta);
}
if (subarray != nullptr) {
add_assoc_zval_ex(zv, str_index, ZSTR_LEN(_str_index), subarray);
}
efree(str_index);
}
if (include_meta) {
char *_type = estrdup("_type");
char *_type_data = estrdup("bdict");
char *_length = estrdup("_length");
char *_data = estrdup("_data");
zval *zv_outer = new zval();
array_init(zv_outer);
add_assoc_string(zv_outer, _type, _type_data);
add_assoc_long(zv_outer, _length, length());
add_assoc_zval(zv_outer, _data, zv);
efree(_type);
efree(_type_data);
efree(_length);
efree(_data);
add_assoc_string(zv_outer, (char *) "_type", (char *) "bdict");
add_assoc_long(zv_outer, (char *) "_length", length());
add_assoc_zval(zv_outer, (char *) "_data", zv);
return zv_outer;
}
return zv;
......
......@@ -49,17 +49,9 @@ zval * bint::to_array(const bool include_meta) const {
zval *zv = new zval();
if (include_meta) {
array_init(zv);
char *_type = estrdup("_type");
char *_type_data = estrdup("bint");
char *_length = estrdup("_length");
char *_data = estrdup("_data");
add_assoc_string(zv, _type, _type_data);
add_assoc_long(zv, _length, length());
add_assoc_long(zv, _data, _value);
efree(_type);
efree(_type_data);
efree(_length);
efree(_data);
add_assoc_string(zv, (char *) "_type", (char *) "bint");
add_assoc_long(zv, (char *) "_length", length());
add_assoc_long(zv, (char *) "_data", _value);
} else {
ZVAL_LONG(zv, _value);
}
......
......@@ -282,39 +282,31 @@ zval * blist::to_array(const bool include_meta) const {
zend_hash_move_forward(_data)) {
zval *value = zend_hash_get_current_data(_data);
std::string class_name = zend_container::bnode_object_get_class_name(value);
zval *subarray = nullptr;
if (class_name == "bdict") {
bdict_object *bnode = zend_container::bdict_fetch_object(Z_OBJ_P(value));
zval *subarray = bnode->bdict_data->to_array(include_meta);
add_next_index_zval(zv, subarray);
subarray = bnode->bdict_data->to_array(include_meta);
} else if (class_name == "blist") {
blist_object *bnode = zend_container::blist_fetch_object(Z_OBJ_P(value));
zval *subarray = bnode->blist_data->to_array(include_meta);
add_next_index_zval(zv, subarray);
subarray = bnode->blist_data->to_array(include_meta);
} else if (class_name == "bstr") {
bstr_object *bnode = zend_container::bstr_fetch_object(Z_OBJ_P(value));
zval *subarray = bnode->bstr_data->to_array(include_meta);
add_next_index_zval(zv, subarray);
subarray = bnode->bstr_data->to_array(include_meta);
} else if (class_name == "bint") {
bint_object *bnode = zend_container::bint_fetch_object(Z_OBJ_P(value));
zval *subarray = bnode->bint_data->to_array(include_meta);
subarray = bnode->bint_data->to_array(include_meta);
}
if (subarray != nullptr) {
add_next_index_zval(zv, subarray);
}
}
if (include_meta) {
char *_type = estrdup("_type");
char *_type_data = estrdup("blist");
char *_length = estrdup("_length");
char *_data = estrdup("_data");
zval *zv_outer = new zval();
array_init(zv_outer);
add_assoc_string(zv_outer, _type, _type_data);
add_assoc_long(zv_outer, _length, length());
add_assoc_zval(zv_outer, _data, zv);
efree(_type);
efree(_type_data);
efree(_length);
efree(_data);
add_assoc_string(zv_outer, (char *) "_type", (char *) "blist");
add_assoc_long(zv_outer, (char *) "_length", length());
add_assoc_zval(zv_outer, (char *) "_data", zv);
return zv_outer;
}
return zv;
......
......@@ -46,28 +46,17 @@ std::string bstr::encode() const {
zval * bstr::to_array(const bool include_meta) const {
zval *zv = new zval();
char *_data = (char *)emalloc(_value.length() + 1);
memcpy(_data, _value.c_str(), _value.length());
if (include_meta) {
array_init(zv);
if (_value.length() == 0) return zv;
char *_type = estrdup("_type");
char *_type_data = estrdup("bstr");
char *_length = estrdup("_length");
char *_data = estrdup("_data");
char *_data_data = (char*)emalloc(_value.length() + 1);
memcpy(_data_data, _value.c_str(), _value.length());
add_assoc_string(zv, _type, _type_data);
add_assoc_long(zv, _length, length());
add_assoc_stringl(zv, _data, _data_data, _value.length());
efree(_type);
efree(_type_data);
efree(_length);
efree(_data);
efree(_data_data);
add_assoc_string(zv, (char *) "_type", (char *) "bstr");
add_assoc_long(zv, (char *) "_length", length());
add_assoc_stringl(zv, (char *) "_data", _data, _value.length());
} else {
char *_data_data = (char *)emalloc(_value.length() + 1);
memcpy(_data_data, _value.c_str(), _value.length());
ZVAL_STRINGL(zv, _data_data, _value.length());
efree(_data_data);
ZVAL_STRINGL(zv, _data, _value.length());
}
efree(_data);
return zv;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment