sort lists using mergesort */
| 490 | |
| 491 | /* sort lists using mergesort */ |
| 492 | static cJSON *sort_list(cJSON *list, const cJSON_bool case_sensitive) |
| 493 | { |
| 494 | cJSON *first = list; |
| 495 | cJSON *second = list; |
| 496 | cJSON *current_item = list; |
| 497 | cJSON *result = list; |
| 498 | cJSON *result_tail = NULL; |
| 499 | |
| 500 | if ((list == NULL) || (list->next == NULL)) |
| 501 | { |
| 502 | /* One entry is sorted already. */ |
| 503 | return result; |
| 504 | } |
| 505 | |
| 506 | while ((current_item != NULL) && (current_item->next != NULL) && |
| 507 | (compare_strings((unsigned char *)current_item->string, (unsigned char *)current_item->next->string, |
| 508 | case_sensitive) < 0)) |
| 509 | { |
| 510 | /* Test for list sorted. */ |
| 511 | current_item = current_item->next; |
| 512 | } |
| 513 | if ((current_item == NULL) || (current_item->next == NULL)) |
| 514 | { |
| 515 | /* Leave sorted lists unmodified. */ |
| 516 | return result; |
| 517 | } |
| 518 | |
| 519 | /* reset pointer to the beginning */ |
| 520 | current_item = list; |
| 521 | while (current_item != NULL) |
| 522 | { |
| 523 | /* Walk two pointers to find the middle. */ |
| 524 | second = second->next; |
| 525 | current_item = current_item->next; |
| 526 | /* advances current_item two steps at a time */ |
| 527 | if (current_item != NULL) |
| 528 | { |
| 529 | current_item = current_item->next; |
| 530 | } |
| 531 | } |
| 532 | if ((second != NULL) && (second->prev != NULL)) |
| 533 | { |
| 534 | /* Split the lists */ |
| 535 | second->prev->next = NULL; |
| 536 | second->prev = NULL; |
| 537 | } |
| 538 | |
| 539 | /* Recursively sort the sub-lists. */ |
| 540 | first = sort_list(first, case_sensitive); |
| 541 | second = sort_list(second, case_sensitive); |
| 542 | result = NULL; |
| 543 | |
| 544 | /* Merge the sub-lists */ |
| 545 | while ((first != NULL) && (second != NULL)) |
| 546 | { |
| 547 | cJSON *smaller = NULL; |
| 548 | if (compare_strings((unsigned char *)first->string, (unsigned char *)second->string, case_sensitive) < 0) |
| 549 | { |
no test coverage detected