#include "strings.h"
#include <string.h>
#include "internal/memory_utils.h"
#include "internal/unicode.h"
Go to the source code of this file.
◆ cbor_build_string()
Creates a new string and initializes it.
The data from val
will be copied to a newly allocated memory block.
Note that valid UTF-8 strings do not contain null bytes, so this routine is correct for all valid inputs. If the input is not guaranteed to be valid, use cbor_build_stringn
instead.
- Parameters
-
val | A null-terminated UTF-8 string |
- Returns
- Reference to the new string item. The item's reference count is initialized to one.
-
NULL
if memory allocation fails
Definition at line 44 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 data from handle
will be copied to a newly allocated memory block.
All `length`
bytes will be stored in the string, even if there are null bytes or invalid UTF-8 sequences.
- Parameters
-
val | A UTF-8 string, at least `length` bytes long |
length | Length (in bytes) of the string passed in `val` . |
- Returns
- Reference to the new string item. The item's reference count is initialized to one.
-
NULL
if memory allocation fails
Definition at line 55 of file strings.c.
◆ cbor_new_definite_string()
Creates a new definite string.
The handle is initialized to NULL
and length to 0
- Returns
- Reference to the new string item. The item's reference count is initialized to one.
-
NULL
if memory allocation fails
Definition at line 13 of file strings.c.
◆ cbor_new_indefinite_string()
Creates a new indefinite string.
The chunks array is initialized to NULL
and chunkcount to 0
- Returns
- Reference to the new string item. The item's reference count is initialized to one.
-
NULL
if memory allocation fails
Definition at line 25 of file strings.c.
◆ cbor_string_add_chunk()
Appends a chunk to the string.
Indefinite strings only.
May realloc the chunk storage.
- Parameters
-
item | An indefinite string |
chunk | A definite string item. Its reference count will be increased by one. |
- Returns
true
on success. false
on memory allocation failure. In that case, the refcount of `chunk`
is not increased and the `item`
is left intact.
Definition at line 95 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
-
- Returns
- The chunk count. 0 for freshly created items.
Definition at line 89 of file strings.c.
◆ cbor_string_chunks_handle()
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
-
- Returns
- array of cbor_string_chunk_count definite strings
Definition at line 83 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 cbor_string_length
if there are multibyte codepoints. If the string data is not valid UTF-8, returns 0.
- Parameters
-
- Returns
- The number of codepoints in this string
Definition at line 132 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
-
- Returns
- The address of the underlying string.
-
NULL
if no data have been assigned yet.
Definition at line 127 of file strings.c.
◆ cbor_string_is_definite()
bool cbor_string_is_definite |
( |
const cbor_item_t * | item | ) |
|
Is the string definite?
- Parameters
-
- Returns
- Is the string definite?
Definition at line 137 of file strings.c.
◆ cbor_string_is_indefinite()
bool cbor_string_is_indefinite |
( |
const cbor_item_t * | item | ) |
|
Is the string indefinite?
- Parameters
-
- Returns
- Is the string indefinite?
Definition at line 142 of file strings.c.
◆ cbor_string_length()
Returns the length of the underlying string in bytes.
There can be fewer unicode character than bytes (see cbor_string_codepoint_count
). For definite strings only.
- Parameters
-
- Returns
- length of the string. Zero if no chunk has been attached yet
Definition at line 122 of file strings.c.
◆ cbor_string_set_handle()
Set the handle to the underlying string.
The data is assumed to be a valid UTF-8 string. If the string is non-empty and invalid, cbor_string_codepoint_count
will return 0.
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 | A definite string |
data | The memory block. The caller gives up the ownership of the block. libcbor will deallocate it when appropriate using its free function |
length | Length of the data block |
Definition at line 65 of file strings.c.