-
Notifications
You must be signed in to change notification settings - Fork 102
cbor_map_get and cbor_equal #96
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
base: master
Are you sure you want to change the base?
Changes from all commits
14ca082
bd8045d
c65fc96
f10ab2e
7045ecd
6cdf7c0
66d4e28
5d6eaf1
eea6939
f1f61d6
857d829
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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: | ||
| 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; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition is removed, done |
||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,8 @@ | |
| #include <stddef.h> | ||
| #include <stdint.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <stdio.h> // TODO REMOVE ME | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| #include "cbor/configuration.h" | ||
| #include "data.h" | ||
|
|
||
|
|
@@ -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 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This definitely requires more docs, please see the top level comment
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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)); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be the first thing in the function
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: inconsistent code style (please use the autoformatter)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please pick a better name for this (e.g.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.