Skip to content

Commit

Permalink
fix(libccc/encode/kvt): fixes to KVT_Merge() function
Browse files Browse the repository at this point in the history
  • Loading branch information
LexouDuck committed Jul 9, 2021
1 parent 8c4ab86 commit ba40e3f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/encode/kvt/get.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,11 @@ s_kvt* KVT_GetObjectItem_(s_kvt const* object, t_char const* key, t_bool case_se
HANDLE_ERROR_SF(KEYNOTFOUND, (item == NULL),
return (NULL);,
": \"%s\"", key)
while (item && item->key)
while (item)
{
if (case_sensitive ?
if (item->key && (case_sensitive ?
String_Equals(key, item->key) :
String_Equals_IgnoreCase(key, item->key))
String_Equals_IgnoreCase(key, item->key)))
return (item);
item = item->next;
}
Expand Down
23 changes: 17 additions & 6 deletions src/encode/kvt/merge.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,25 @@ s_kvt* KVT_Merge(s_kvt const* kvt1, s_kvt const* kvt2, t_bool recurse)
s_kvt* item;
s_kvt* other;
s_kvt* append;
s_kvt* current;
s_kvt* new;

HANDLE_ERROR(WRONGTYPE, !KVT_IsObject(kvt1), return (NULL);)
HANDLE_ERROR(WRONGTYPE, !KVT_IsObject(kvt2), return (NULL);)
result = KVT_Duplicate(kvt1, recurse);
HANDLE_ERROR(ALLOCFAILURE, (result == NULL), return (NULL);)
item = result->value.child;
other = kvt2->value.child;
append = NULL;
item = result->value.child;
while (item)
{
s_kvt* new;

if (String_Equals(item->key, other->key))
other = kvt2->value.child;
while (other)
{
if (other->key && String_Equals(item->key, other->key))
break;
other = other->next;
}
if (other) // matching keys, merge values
{
if (recurse && KVT_IsObject(other))
{
Expand Down Expand Up @@ -54,13 +60,18 @@ s_kvt* KVT_Merge(s_kvt const* kvt1, s_kvt const* kvt2, t_bool recurse)
if (append == NULL)
{
append = new;
current = new;
}
else
{
KVT_AddToArray_Item(append, new);
current->next = new;
new->prev = current;
current = new;
}
other = other->next;
}
item = item->next;
}
KVT_AddToArray_Item(result, append);
return (result);
}

0 comments on commit ba40e3f

Please sign in to comment.