optimize the way to find tail node

This commit is contained in:
Alanscut 2020-09-02 20:23:52 +08:00
parent 0b13220419
commit c8ca78a3cc
2 changed files with 14 additions and 11 deletions

17
cJSON.c
View File

@ -1975,15 +1975,6 @@ static cJSON_bool add_item_to_array(cJSON *array, cJSON *item)
suffix_object(child->prev, item);
array->child->prev = item;
}
else
{
while (child->next)
{
child = child->next;
}
suffix_object(child, item);
array->child->prev = item;
}
}
return true;
@ -2571,6 +2562,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count)
}
p = n;
}
a->child->prev = n;
return a;
}
@ -2607,6 +2599,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count)
}
p = n;
}
a->child->prev = n;
return a;
}
@ -2643,6 +2636,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count)
}
p = n;
}
a->child->prev = n;
return a;
}
@ -2679,6 +2673,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int co
}
p = n;
}
a->child->prev = n;
return a;
}
@ -2751,6 +2746,10 @@ CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse)
}
child = child->next;
}
if (newitem && newitem->child)
{
newitem->child->prev = newchild;
}
return newitem;

View File

@ -403,7 +403,7 @@ static cJSON *detach_item_from_array(cJSON *array, size_t which)
/* item doesn't exist */
return NULL;
}
if (c->prev)
if (c != array->child)
{
/* not the first element */
c->prev->next = c->next;
@ -412,10 +412,14 @@ static cJSON *detach_item_from_array(cJSON *array, size_t which)
{
c->next->prev = c->prev;
}
if (c==array->child)
if (c == array->child)
{
array->child = c->next;
}
else if (c->next == NULL)
{
array->child->prev = c->prev;
}
/* make sure the detached item doesn't point anywhere anymore */
c->prev = c->next = NULL;