libcbor  0.5.0
libcbor is a C library for parsing and generating CBOR, the general-purpose schema-less binary data format.
Functions
strings.c File Reference
#include <string.h>
#include "strings.h"
#include "internal/memory_utils.h"

Go to the source code of this file.

Functions

cbor_item_tcbor_new_definite_string ()
 Creates a new definite string. More...
 
cbor_item_tcbor_new_indefinite_string ()
 Creates a new indefinite string. More...
 
cbor_item_tcbor_build_string (const char *val)
 Creates a new string and initializes it. More...
 
cbor_item_tcbor_build_stringn (const char *val, size_t length)
 Creates a new string and initializes it. More...
 
void cbor_string_set_handle (cbor_item_t *item, cbor_mutable_data CBOR_RESTRICT_POINTER data, size_t length)
 Set the handle to the underlying string. More...
 
cbor_item_t ** cbor_string_chunks_handle (const cbor_item_t *item)
 Get the handle to the array of chunks. More...
 
size_t cbor_string_chunk_count (const cbor_item_t *item)
 Get the number of chunks this string consist of. More...
 
bool cbor_string_add_chunk (cbor_item_t *item, cbor_item_t *chunk)
 Appends a chunk to the string. More...
 
size_t cbor_string_length (const cbor_item_t *item)
 Returns the length of the underlying string. More...
 
unsigned char * cbor_string_handle (const cbor_item_t *item)
 Get the handle to the underlying string. More...
 
size_t cbor_string_codepoint_count (const cbor_item_t *item)
 The number of codepoints in this string. More...
 
bool cbor_string_is_definite (const cbor_item_t *item)
 Is the string definite? More...
 
bool cbor_string_is_indefinite (const cbor_item_t *item)
 Is the string indefinite? More...
 

Function Documentation

§ cbor_build_string()

cbor_item_t* cbor_build_string ( const char *  val)

Creates a new string and initializes it.

The val will be copied to a newly allocated block

Parameters
valA null-terminated UTF-8 string
Returns
A new string with content handle. NULL on malloc failure.

Definition at line 40 of file strings.c.

§ cbor_build_stringn()

cbor_item_t* cbor_build_stringn ( const char *  val,
size_t  length 
)

Creates a new string and initializes it.

The handle will be copied to a newly allocated block

Parameters
valA UTF-8 string, at least length long (excluding the null byte)
Returns
A new string with content handle. NULL on malloc failure.

Definition at line 50 of file strings.c.

§ cbor_new_definite_string()

cbor_item_t* cbor_new_definite_string ( )

Creates a new definite string.

The handle is initialized to NULL and length to 0

Returns
new definite string. NULL on malloc failure.

Definition at line 12 of file strings.c.

§ cbor_new_indefinite_string()

cbor_item_t* cbor_new_indefinite_string ( )

Creates a new indefinite string.

The chunks array is initialized to NULL and chunkcount to 0

Returns
new indefinite string. NULL on malloc failure.

Definition at line 23 of file strings.c.

§ cbor_string_add_chunk()

bool cbor_string_add_chunk ( cbor_item_t item,
cbor_item_t chunk 
)

Appends a chunk to the string.

Indefinite strings only.

May realloc the chunk storage.

Parameters
item[borrow]An indefinite string
item[incref]A definite string
Returns
true on success. false on realloc failure. In that case, the refcount of chunk is not increased and the item is left intact.

Definition at line 82 of file strings.c.

§ cbor_string_chunk_count()

size_t cbor_string_chunk_count ( const cbor_item_t item)

Get the number of chunks this string consist of.

Parameters
item[borrow]A indefinite string
Returns
The chunk count. 0 for freshly created items.

Definition at line 74 of file strings.c.

§ cbor_string_chunks_handle()

cbor_item_t** cbor_string_chunks_handle ( const cbor_item_t item)

Get the handle to the array of chunks.

Manipulations with the memory block (e.g. sorting it) are allowed, but the validity and the number of chunks must be retained.

Parameters
item[borrow]A indefinite string
Returns
array of cbor_string_chunk_count definite strings

Definition at line 67 of file strings.c.

§ cbor_string_codepoint_count()

size_t cbor_string_codepoint_count ( const cbor_item_t item)

The number of codepoints in this string.

Might differ from length if there are multibyte ones

Parameters
item[borrow]A string
Returns
The number of codepoints in this string

Definition at line 118 of file strings.c.

§ cbor_string_handle()

unsigned char* cbor_string_handle ( const cbor_item_t item)

Get the handle to the underlying string.

Definite items only. Modifying the data is allowed. In that case, the caller takes responsibility for the effect on items this item might be a part of

Parameters
item[borrow]A definite string
Returns
The address of the underlying string. NULL if no data have been assigned yet.

Definition at line 112 of file strings.c.

§ cbor_string_is_definite()

bool cbor_string_is_definite ( const cbor_item_t item)

Is the string definite?

Parameters
item[borrow]a string
Returns
Is the string definite?

Definition at line 124 of file strings.c.

§ cbor_string_is_indefinite()

bool cbor_string_is_indefinite ( const cbor_item_t item)

Is the string indefinite?

Parameters
item[borrow]a string
Returns
Is the string indefinite?

Definition at line 130 of file strings.c.

§ cbor_string_length()

size_t cbor_string_length ( const cbor_item_t item)

Returns the length of the underlying string.

For definite strings only

Parameters
item[borrow]a definite string
Returns
length of the string. Zero if no chunk has been attached yet

Definition at line 106 of file strings.c.

§ cbor_string_set_handle()

void cbor_string_set_handle ( cbor_item_t item,
cbor_mutable_data CBOR_RESTRICT_POINTER  data,
size_t  length 
)

Set the handle to the underlying string.

embed:rst:leading-asterisk
* .. warning:: Using a pointer to a stack allocated constant is a common mistake. Lifetime of the string will expire when it goes out of scope and the CBOR item will be left inconsistent.
* 
Parameters
item[borrow]A definite string
dataThe memory block. The caller gives up the ownership of the block. libcbor will deallocate it when appropriate using its free function
lengthLength of the data block

Definition at line 59 of file strings.c.