Skip to content

Commit

Permalink
Merge pull request bergzand#83 from bergzand/pr/map_array_items_remai…
Browse files Browse the repository at this point in the history
…ning

decoder: Add dedicated functions for items remaining
  • Loading branch information
bergzand authored Oct 13, 2023
2 parents c3bb958 + 9c94760 commit ae01e39
Showing 1 changed file with 35 additions and 1 deletion.
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

0 comments on commit ae01e39

Please sign in to comment.