Commit 4196b986 authored by Frederick Zhang's avatar Frederick Zhang

code cleaning

parent 0de7f40f
......@@ -168,14 +168,15 @@ zval * bdict::parse(const std::string &ben, size_t &pt) {
return bitem::throw_general_exception("Error parsing bdict");
zval _zv;
zval *zv = &_zv;
object_init_ex(zv, zend_container::bdict_ce);
zend_object *zo = zend_container::bdict_object_new(zend_container::bdict_ce);
ZVAL_OBJ(zv, zo);
bdict_object *intern = zend_container::bdict_fetch_object(Z_OBJ_P(zv));
intern->bdict_data = new bdict();
++pt;
while (ben[pt] != 'e') {
size_t start = pt;
while (ben[pt] >= '0' && ben[pt] <= '9') ++pt;
while (isdigit(ben[pt])) ++pt;
std::string key_len = ben.substr(start, pt - start);
++pt;
std::string key = ben.substr(pt, std::stoull(key_len));
......@@ -186,7 +187,7 @@ zval * bdict::parse(const std::string &ben, size_t &pt) {
} else if (ben[pt] == 'l') {
zval bnode = *blist::parse(ben, pt);
zend_hash_str_add(intern->bdict_data->_data, key.c_str(), key.length(), &bnode);
} else if (ben[pt] >= '0' && ben[pt] <= '9') {
} else if (isdigit(ben[pt])) {
zval bnode = *bstr::parse(ben, pt);
zend_hash_str_add(intern->bdict_data->_data, key.c_str(), key.length(), &bnode);
} else if (ben[pt] == 'i') {
......
......@@ -35,7 +35,8 @@ zval * bint::parse(const std::string &ben, size_t &pt) {
zval _zv;
zval *zv = &_zv;
object_init_ex(zv, zend_container::bint_ce);
zend_object *zo = zend_container::bint_object_new(zend_container::bint_ce);
ZVAL_OBJ(zv, zo);
bint_object *intern = zend_container::bint_fetch_object(Z_OBJ_P(zv));
intern->bint_data = new bint(result);
return zv;
......
......@@ -48,7 +48,7 @@ zval * bitem::parse(const std::string &ben) {
return bdict::parse(ben, pt);
} else if (ben[0] == 'l') {
return blist::parse(ben, pt);
} else if (ben[0] >= '0' && ben[0] <= '9') {
} else if (isdigit(ben[0])) {
return bstr::parse(ben, pt);
} else if (ben[0] == 'i') {
return bint::parse(ben, pt);
......
......@@ -105,15 +105,9 @@ bool blist::del(const size_t &key) {
zval * blist::get_path(const std::string &key, size_t &pt) const {
std::string current_key = bitem::get_current_key(key, pt);
size_t current_key_long;
try {
current_key_long = std::stoull(current_key);
} catch (int e) {
if (!bitem::is_ull(current_key))
return bitem::throw_general_exception("Invalid key for blist, only positive integer is allowed");
}
if (std::to_string(current_key_long) != current_key) {
return bitem::throw_general_exception("Invalid key for blist, only positive integer is allowed");
}
size_t current_key_long = std::stoull(current_key);
if (!zend_hash_index_exists(_data, current_key_long)) {
return bitem::get_zval_bool(false);
......@@ -207,7 +201,8 @@ zval * blist::parse(const std::string &ben, size_t &pt) {
++pt;
zval _zv;
zval *zv = &_zv;
object_init_ex(zv, zend_container::blist_ce);
zend_object *zo = zend_container::blist_object_new(zend_container::blist_ce);
ZVAL_OBJ(zv, zo);
blist_object *intern = zend_container::blist_fetch_object(Z_OBJ_P(zv));
intern->blist_data = new blist();
......@@ -218,7 +213,7 @@ zval * blist::parse(const std::string &ben, size_t &pt) {
} else if (ben[pt] == 'l') {
zval bnode = *blist::parse(ben, pt);
zend_hash_next_index_insert(intern->blist_data->_data, &bnode);
} else if (ben[pt] >= '0' && ben[pt] <= '9') {
} else if (isdigit(ben[pt])) {
zval bnode = *bstr::parse(ben, pt);
zend_hash_next_index_insert(intern->blist_data->_data, &bnode);
} else if (ben[pt] == 'i') {
......
......@@ -24,16 +24,17 @@ size_t bstr::length() const {
}
zval * bstr::parse(const std::string &ben, size_t &pt) {
if (ben[pt] < '0' || ben[pt] > '9')
if (!isdigit(ben[pt]))
return bitem::throw_general_exception("Error parsing bstr");
const size_t start = pt;
while (ben[pt] >= '0' && ben[pt] <= '9') ++pt;
while (isdigit(ben[pt])) ++pt;
std::string len = ben.substr(start, pt - start);
++pt;
zval _zv;
zval *zv = &_zv;
object_init_ex(zv, zend_container::bstr_ce);
zend_object *zo = zend_container::bstr_object_new(zend_container::bstr_ce);
ZVAL_OBJ(zv, zo);
bstr_object *intern = zend_container::bstr_fetch_object(Z_OBJ_P(zv));
intern->bstr_data = new bstr(ben.substr(pt, std::stoull(len)));
pt += std::stoull(len);
......
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