Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cbor_map_get and cbor_equal #96

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
98 changes: 98 additions & 0 deletions src/cbor/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,101 @@ cbor_item_t *cbor_move(cbor_item_t *item) {
item->refcount--;
return item;
}

bool cbor_equal(cbor_item_t *item1, cbor_item_t *item2) {
size_t i;
cbor_item_t * cur_item1, * cur_item2;
bool ret;

if (item1 != NULL && item2 != NULL) {
if (item1->type != item2->type) {
return false;
} else {
switch (item1->type) {
case CBOR_TYPE_UINT:
case CBOR_TYPE_NEGINT:
PJK marked this conversation as resolved.
Show resolved Hide resolved
if (cbor_int_get_width(item1) != cbor_int_get_width(item2)) {
return false;
} else {
if (cbor_int_get_width(item1) == CBOR_INT_8)
return !memcmp(item1->data, item2->data, sizeof(uint8_t))?true:false;
PJK marked this conversation as resolved.
Show resolved Hide resolved
else if (cbor_int_get_width(item1) == CBOR_INT_16)
return !memcmp(item1->data, item2->data, sizeof(uint16_t))?true:false;
else if (cbor_int_get_width(item1) == CBOR_INT_32)
return !memcmp(item1->data, item2->data, sizeof(uint32_t))?true:false;
else if (cbor_int_get_width(item1) == CBOR_INT_64)
return !memcmp(item1->data, item2->data, sizeof(uint64_t))?true:false;
else
return false;
}
break;
case CBOR_TYPE_BYTESTRING:
if (cbor_bytestring_length(item1) != cbor_bytestring_length(item2))
return false;
else
return !memcmp(item1->data, item2->data, cbor_bytestring_length(item1))?true:false;
PJK marked this conversation as resolved.
Show resolved Hide resolved
break;
case CBOR_TYPE_STRING:
if (cbor_string_length(item1) != cbor_string_length(item2))
return false;
else
return !memcmp(item1->data, item2->data, cbor_string_length(item1))?true:false;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

break;
case CBOR_TYPE_ARRAY:
if (cbor_array_size(item1) != cbor_array_size(item2)) {
return false;
} else {
ret = true;
for (i=0; i<cbor_array_size(item1); i++) {
cur_item1 = cbor_array_get(item1, i);
cur_item2 = cbor_array_get(item2, i);
if (cbor_equal(cur_item1, cur_item2) == false) {
ret = false;
}
cbor_decref(&cur_item1);
cbor_decref(&cur_item2);
}
return ret;
}
break;
case CBOR_TYPE_MAP:
if (cbor_map_size(item1) != cbor_map_size(item2)) {
return false;
} else {
for (i=0; i<cbor_map_size(item1); i++) {
struct cbor_pair pair1 = cbor_map_handle(item1)[i];
struct cbor_pair pair2 = cbor_map_handle(item2)[i];
if (cbor_equal(pair1.key, pair2.key) == false || cbor_equal(pair1.value, pair2.value) == false)
PJK marked this conversation as resolved.
Show resolved Hide resolved
return false;
}
return true;
}
break;
case CBOR_TYPE_TAG:
if (cbor_tag_value(item1) != cbor_tag_value(item2)) {
return false;
} else {
return cbor_equal(cbor_tag_item(item1), cbor_tag_item(item2));
}
case CBOR_TYPE_FLOAT_CTRL:
if (cbor_is_float(item1) && cbor_is_float(item2)) {
if (cbor_float_get_width(item1) != cbor_float_get_width(item2))
return false;
else
return cbor_float_get_float(item1)==cbor_float_get_float(item2)?true:false;
} else if (cbor_is_bool(item1) && cbor_is_bool(item2)) {
return cbor_ctrl_value(item1)==cbor_ctrl_value(item2)?true:false;
PJK marked this conversation as resolved.
Show resolved Hide resolved
} else if ((cbor_is_null(item1) && cbor_is_null(item2)) || (cbor_is_undef(item1) && cbor_is_undef(item2))) {
PJK marked this conversation as resolved.
Show resolved Hide resolved
return true;
} else {
return false;
}
break;
}
}
} else if (item1 == NULL && item2 == NULL) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NULLs are not valid data items, could you please provide some rationale for accepting them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition is removed, done

return true;
} else {
return false;
}
}
12 changes: 12 additions & 0 deletions src/cbor/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h> // TODO REMOVE ME
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

#include "cbor/configuration.h"
#include "data.h"

Expand Down Expand Up @@ -285,6 +287,16 @@ size_t cbor_refcount(const cbor_item_t *item);
*/
cbor_item_t *cbor_move(cbor_item_t *item);

/** Compares two items
*
* Compares the type, metadata and value of item1 and item2
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely requires more docs, please see the top level comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doc improved, let me know if more information would be useful

*
* @param item1[borrow] the first item
* @param item2[borrow] the second item
* @return the true if item1 and item2 are equal, false otherwise
*/
bool cbor_equal(cbor_item_t *item1, cbor_item_t *item2);

#ifdef __cplusplus
}
#endif
Expand Down
17 changes: 17 additions & 0 deletions src/cbor/maps.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,20 @@ struct cbor_pair *cbor_map_handle(const cbor_item_t *item) {
assert(cbor_isa_map(item));
return (struct cbor_pair *)item->data;
}

struct cbor_item_t *cbor_map_get(const cbor_item_t *item, const cbor_item_t * key) {
size_t i;
struct cbor_pair pair;
cbor_item_t * value = NULL;
assert(cbor_isa_map(item));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be the first thing in the function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


for (i=0; i<cbor_map_size(item); i++) {
pair = cbor_map_handle(item)[i];
if (cbor_equal(pair.key, key)) {
value = cbor_incref(pair.value);
break;
}
}

return value;
}
9 changes: 9 additions & 0 deletions src/cbor/maps.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ bool cbor_map_is_indefinite(const cbor_item_t *item);
*/
struct cbor_pair *cbor_map_handle(const cbor_item_t *item);

/** Get the value storage from the given key
*
* @param item[borrow] A map
* @param key[incref] The key to look for
* @return cbor_item of the value corresponding to the given key, NULL if the
* key doesn't exist. Manipulation is possible as long as references remain valid.
*/
struct cbor_item_t *cbor_map_get(const cbor_item_t *item, const cbor_item_t * key);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: inconsistent code style (please use the autoformatter)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please pick a better name for this (e.g. map)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


#ifdef __cplusplus
}
#endif
Expand Down
Loading