修复遍历时,删除第一个元素后不能继续遍历的BUG

This commit is contained in:
gb 2022-09-29 09:35:37 +08:00
parent 45c733cda7
commit 6e367f54da
2 changed files with 18 additions and 5 deletions

View File

@ -32,10 +32,12 @@ namespace gb
json::json(char* json_txt) : obj_(0), cur_child_(0), is_array_(false) json::json(char* json_txt) : obj_(0), cur_child_(0), is_array_(false)
{ {
memset(&walk_head_, 0, sizeof(walk_head_));
attach_text(json_txt); attach_text(json_txt);
} }
json::~json() json::~json()
{ {
memset(&walk_head_, 0, sizeof(walk_head_));
clear(); clear();
} }
@ -100,7 +102,7 @@ namespace gb
{ {
cJSON* obj = is_array ? cJSON_CreateArray() : cJSON_CreateObject(); cJSON* obj = is_array ? cJSON_CreateArray() : cJSON_CreateObject();
bzero(obj, sizeof(*obj)); // bzero(obj, sizeof(*obj)); // cleared in constructor already !
return obj; return obj;
} }
@ -571,10 +573,20 @@ namespace gb
if(ele) if(ele)
{ {
//if(addr) bool cur_child = cur_child_ == obj_->child;
// *addr = ele->next;
if(addr)
*addr = ele->next;
if (cur_child_ == ele) if (cur_child_ == ele)
{
if (cur_child)
{
walk_head_.next = obj_->child;
cur_child_ = &walk_head_;
}
else
cur_child_ = ele->prev; cur_child_ = ele->prev;
}
if (ele->prev) if (ele->prev)
ele->prev->next = ele->next; ele->prev->next = ele->next;
if (ele->next) if (ele->next)

View File

@ -15,7 +15,8 @@ namespace gb
class json class json
{ {
cJSON *obj_; cJSON *obj_;
cJSON* cur_child_; cJSON *cur_child_;
cJSON walk_head_;
bool is_array_; bool is_array_;
cJSON* find_sibling(cJSON* first, const char* name, cJSON*** addr); cJSON* find_sibling(cJSON* first, const char* name, cJSON*** addr);