Path: blob/main/contrib/libcbor/src/cbor/strings.h
39507 views
/*1* Copyright (c) 2014-2020 Pavel Kalvoda <[email protected]>2*3* libcbor is free software; you can redistribute it and/or modify4* it under the terms of the MIT license. See LICENSE for details.5*/67#ifndef LIBCBOR_STRINGS_H8#define LIBCBOR_STRINGS_H910#include "cbor/cbor_export.h"11#include "cbor/common.h"1213#ifdef __cplusplus14extern "C" {15#endif1617/*18* ============================================================================19* String manipulation20* ============================================================================21*/2223/** Returns the length of the underlying string in bytes24*25* There can be fewer unicode character than bytes (see26* `cbor_string_codepoint_count`). For definite strings only.27*28* @param item a definite string29* @return length of the string. Zero if no chunk has been attached yet30*/31_CBOR_NODISCARD CBOR_EXPORT size_t cbor_string_length(const cbor_item_t *item);3233/** The number of codepoints in this string34*35* Might differ from `cbor_string_length` if there are multibyte codepoints.36* If the string data is not valid UTF-8, returns 0.37*38* @param item A string39* @return The number of codepoints in this string40*/41_CBOR_NODISCARD CBOR_EXPORT size_t42cbor_string_codepoint_count(const cbor_item_t *item);4344/** Is the string definite?45*46* @param item a string47* @return Is the string definite?48*/49_CBOR_NODISCARD CBOR_EXPORT bool cbor_string_is_definite(50const cbor_item_t *item);5152/** Is the string indefinite?53*54* @param item a string55* @return Is the string indefinite?56*/57_CBOR_NODISCARD CBOR_EXPORT bool cbor_string_is_indefinite(58const cbor_item_t *item);5960/** Get the handle to the underlying string61*62* Definite items only. Modifying the data is allowed. In that case, the caller63* takes responsibility for the effect on items this item might be a part of64*65* @param item A definite string66* @return The address of the underlying string.67* @return `NULL` if no data have been assigned yet.68*/69_CBOR_NODISCARD CBOR_EXPORT cbor_mutable_data70cbor_string_handle(const cbor_item_t *item);7172/** Set the handle to the underlying string73*74* The data is assumed to be a valid UTF-8 string. If the string is non-empty75* and invalid, `cbor_string_codepoint_count` will return 0.76*77* \rst78* .. warning:: Using a pointer to a stack allocated constant is a common79* mistake. Lifetime of the string will expire when it goes out of scope and80* the CBOR item will be left inconsistent.81* \endrst82*83* @param item A definite string84* @param data The memory block. The caller gives up the ownership of the block.85* libcbor will deallocate it when appropriate using its free function86* @param length Length of the data block87*/88CBOR_EXPORT void cbor_string_set_handle(89cbor_item_t *item, cbor_mutable_data CBOR_RESTRICT_POINTER data,90size_t length);9192/** Get the handle to the array of chunks93*94* Manipulations with the memory block (e.g. sorting it) are allowed, but the95* validity and the number of chunks must be retained.96*97* @param item A indefinite string98* @return array of #cbor_string_chunk_count definite strings99*/100_CBOR_NODISCARD CBOR_EXPORT cbor_item_t **cbor_string_chunks_handle(101const cbor_item_t *item);102103/** Get the number of chunks this string consist of104*105* @param item A indefinite string106* @return The chunk count. 0 for freshly created items.107*/108_CBOR_NODISCARD CBOR_EXPORT size_t109cbor_string_chunk_count(const cbor_item_t *item);110111/** Appends a chunk to the string112*113* Indefinite strings only.114*115* May realloc the chunk storage.116*117* @param item An indefinite string118* @param chunk A definite string item. Its reference count will be increased119* by one.120* @return `true` on success. `false` on memory allocation failure. In that121* case, the refcount of @p `chunk` is not increased and the @p `item` is left122* intact.123*/124_CBOR_NODISCARD CBOR_EXPORT bool cbor_string_add_chunk(cbor_item_t *item,125cbor_item_t *chunk);126127/** Creates a new definite string128*129* The handle is initialized to `NULL` and length to 0130*131* @return Reference to the new string item. The item's reference count is132* initialized to one.133* @return `NULL` if memory allocation fails134*/135_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_definite_string(void);136137/** Creates a new indefinite string138*139* The chunks array is initialized to `NULL` and chunkcount to 0140*141* @return Reference to the new string item. The item's reference count is142* initialized to one.143* @return `NULL` if memory allocation fails144*/145_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_indefinite_string(void);146147/** Creates a new string and initializes it148*149* The data from `val` will be copied to a newly allocated memory block.150*151* Note that valid UTF-8 strings do not contain null bytes, so this routine is152* correct for all valid inputs. If the input is not guaranteed to be valid,153* use `cbor_build_stringn` instead.154*155* @param val A null-terminated UTF-8 string156* @return Reference to the new string item. The item's reference count is157* initialized to one.158* @return `NULL` if memory allocation fails159*/160_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_string(const char *val);161162/** Creates a new string and initializes it163*164* The data from `handle` will be copied to a newly allocated memory block.165*166* All @p `length` bytes will be stored in the string, even if there are null167* bytes or invalid UTF-8 sequences.168*169* @param val A UTF-8 string, at least @p `length` bytes long170* @param length Length (in bytes) of the string passed in @p `val`.171* @return Reference to the new string item. The item's reference count is172* initialized to one.173* @return `NULL` if memory allocation fails174*/175_CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_stringn(const char *val,176size_t length);177178#ifdef __cplusplus179}180#endif181182#endif // LIBCBOR_STRINGS_H183184185