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

decoder: Add dedicated functions for items remaining #83

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion include/nanocbor/nanocbor.h
Original file line number Diff line number Diff line change
Expand Up @@ -625,12 +625,14 @@ int nanocbor_get_subcbor(nanocbor_value_t *it, const uint8_t **start,
size_t *len);

/**
* @brief Retrieve the number of remaining values is a CBOR container
* @brief Retrieve the number of remaining values in a CBOR container: either array or map
*
* The returned value is undefined when not inside a container or when the
* container is of indefinite length. For a map, the number is the full number
* of CBOR items remaining (twice the number of key/value pairs).
*
* See also nanocbor_array_items_remaining and nanocbor_map_items_remaining
*
* @param[in] value value inside a CBOR container
*
* @return number of items remaining
Expand All @@ -641,6 +643,38 @@ nanocbor_container_remaining(const nanocbor_value_t *value)
return value->remaining;
}

/**
* @brief Retrieve the number of remaining items in a CBOR array
*
* The returned value is undefined when not inside an array or when the
* array is of indefinite length.
*
* @param[in] value nanocbor value inside a CBOR array
*
* @return number of array items remaining
*/
static inline uint32_t
nanocbor_array_items_remaining(const nanocbor_value_t *value)
{
return nanocbor_container_remaining(value);
}

/**
* @brief Retrieve the number of remaining values in a CBOR map
*
* The returned value is undefined when not inside a map or when the
* container is of indefinite length.
*
* @param[in] value nanocbor value inside a CBOR map
*
* @return number of key/value pairs remaining
*/
static inline uint32_t
nanocbor_map_items_remaining(const nanocbor_value_t *value)
{
return nanocbor_container_remaining(value)/2;
}

/**
* @brief Check whether a container is an indefinite-length container
*
Expand Down
Loading