Skip to content

Commit

Permalink
Merge pull request #503 from Alanscut/issue499
Browse files Browse the repository at this point in the history
optimize the way to find tail node
  • Loading branch information
Alanscut authored Sep 3, 2020
2 parents 0b13220 + c8ca78a commit 2e5171d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
17 changes: 8 additions & 9 deletions cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -2571,6 +2562,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count)
}
p = n;
}
a->child->prev = n;

return a;
}
Expand Down Expand Up @@ -2607,6 +2599,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count)
}
p = n;
}
a->child->prev = n;

return a;
}
Expand Down Expand Up @@ -2643,6 +2636,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count)
}
p = n;
}
a->child->prev = n;

return a;
}
Expand Down Expand Up @@ -2679,6 +2673,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int co
}
p = n;
}
a->child->prev = n;

return a;
}
Expand Down Expand Up @@ -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;

Expand Down
8 changes: 6 additions & 2 deletions cJSON_Utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down

0 comments on commit 2e5171d

Please sign in to comment.