1da0d961cSdjm /* 2d3425be1Sdjm * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com> 3da0d961cSdjm * 4da0d961cSdjm * libcbor is free software; you can redistribute it and/or modify 5da0d961cSdjm * it under the terms of the MIT license. See LICENSE for details. 6da0d961cSdjm */ 7da0d961cSdjm 8da0d961cSdjm #ifndef LIBCBOR_STRINGS_H 9da0d961cSdjm #define LIBCBOR_STRINGS_H 10da0d961cSdjm 11*4dcc46c4Sdjm #include "cbor/cbor_export.h" 12da0d961cSdjm #include "cbor/common.h" 13da0d961cSdjm 14da0d961cSdjm #ifdef __cplusplus 15da0d961cSdjm extern "C" { 16da0d961cSdjm #endif 17da0d961cSdjm 18da0d961cSdjm /* 19da0d961cSdjm * ============================================================================ 20da0d961cSdjm * String manipulation 21da0d961cSdjm * ============================================================================ 22da0d961cSdjm */ 23da0d961cSdjm 24*4dcc46c4Sdjm /** Returns the length of the underlying string in bytes 25da0d961cSdjm * 26*4dcc46c4Sdjm * There can be fewer unicode character than bytes (see 27*4dcc46c4Sdjm * `cbor_string_codepoint_count`). For definite strings only. 28da0d961cSdjm * 29da0d961cSdjm * @param item[borrow] a definite string 30da0d961cSdjm * @return length of the string. Zero if no chunk has been attached yet 31da0d961cSdjm */ 32*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT size_t cbor_string_length(const cbor_item_t *item); 33da0d961cSdjm 34da0d961cSdjm /** The number of codepoints in this string 35da0d961cSdjm * 36da0d961cSdjm * Might differ from length if there are multibyte ones 37da0d961cSdjm * 38da0d961cSdjm * @param item[borrow] A string 39da0d961cSdjm * @return The number of codepoints in this string 40da0d961cSdjm */ 41*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT size_t 42*4dcc46c4Sdjm cbor_string_codepoint_count(const cbor_item_t *item); 43da0d961cSdjm 44da0d961cSdjm /** Is the string definite? 45da0d961cSdjm * 46da0d961cSdjm * @param item[borrow] a string 47da0d961cSdjm * @return Is the string definite? 48da0d961cSdjm */ 49*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT bool cbor_string_is_definite( 50*4dcc46c4Sdjm const cbor_item_t *item); 51da0d961cSdjm 52da0d961cSdjm /** Is the string indefinite? 53da0d961cSdjm * 54da0d961cSdjm * @param item[borrow] a string 55da0d961cSdjm * @return Is the string indefinite? 56da0d961cSdjm */ 57*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT bool cbor_string_is_indefinite( 58*4dcc46c4Sdjm const cbor_item_t *item); 59da0d961cSdjm 60da0d961cSdjm /** Get the handle to the underlying string 61da0d961cSdjm * 629e5c2ddcSdjm * Definite items only. Modifying the data is allowed. In that case, the caller 639e5c2ddcSdjm * takes responsibility for the effect on items this item might be a part of 64da0d961cSdjm * 65da0d961cSdjm * @param item[borrow] A definite string 669e5c2ddcSdjm * @return The address of the underlying string. `NULL` if no data have been 679e5c2ddcSdjm * assigned yet. 68da0d961cSdjm */ 69*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT cbor_mutable_data 70*4dcc46c4Sdjm cbor_string_handle(const cbor_item_t *item); 71da0d961cSdjm 72da0d961cSdjm /** Set the handle to the underlying string 73da0d961cSdjm * 74da0d961cSdjm * 75da0d961cSdjm * \rst 769e5c2ddcSdjm * .. warning:: Using a pointer to a stack allocated constant is a common 77*4dcc46c4Sdjm * mistake. Lifetime of the string will expire when it goes out of scope and 78*4dcc46c4Sdjm * the CBOR item will be left inconsistent. 79*4dcc46c4Sdjm * \endrst 80da0d961cSdjm * 81da0d961cSdjm * @param item[borrow] A definite string 829e5c2ddcSdjm * @param data The memory block. The caller gives up the ownership of the block. 839e5c2ddcSdjm * libcbor will deallocate it when appropriate using its free function 84da0d961cSdjm * @param length Length of the data block 85da0d961cSdjm */ 86*4dcc46c4Sdjm CBOR_EXPORT void cbor_string_set_handle( 87*4dcc46c4Sdjm cbor_item_t *item, cbor_mutable_data CBOR_RESTRICT_POINTER data, 889e5c2ddcSdjm size_t length); 89da0d961cSdjm 90da0d961cSdjm /** Get the handle to the array of chunks 91da0d961cSdjm * 929e5c2ddcSdjm * Manipulations with the memory block (e.g. sorting it) are allowed, but the 939e5c2ddcSdjm * validity and the number of chunks must be retained. 94da0d961cSdjm * 95da0d961cSdjm * @param item[borrow] A indefinite string 96da0d961cSdjm * @return array of #cbor_string_chunk_count definite strings 97da0d961cSdjm */ 98*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT cbor_item_t **cbor_string_chunks_handle( 99*4dcc46c4Sdjm const cbor_item_t *item); 100da0d961cSdjm 101da0d961cSdjm /** Get the number of chunks this string consist of 102da0d961cSdjm * 103da0d961cSdjm * @param item[borrow] A indefinite string 104da0d961cSdjm * @return The chunk count. 0 for freshly created items. 105da0d961cSdjm */ 106*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT size_t 107*4dcc46c4Sdjm cbor_string_chunk_count(const cbor_item_t *item); 108da0d961cSdjm 109da0d961cSdjm /** Appends a chunk to the string 110da0d961cSdjm * 111da0d961cSdjm * Indefinite strings only. 112da0d961cSdjm * 113da0d961cSdjm * May realloc the chunk storage. 114da0d961cSdjm * 115da0d961cSdjm * @param item[borrow] An indefinite string 116da0d961cSdjm * @param item[incref] A definite string 1179e5c2ddcSdjm * @return true on success. false on realloc failure. In that case, the refcount 1189e5c2ddcSdjm * of `chunk` is not increased and the `item` is left intact. 119da0d961cSdjm */ 120*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT bool cbor_string_add_chunk(cbor_item_t *item, 121*4dcc46c4Sdjm cbor_item_t *chunk); 122da0d961cSdjm 123da0d961cSdjm /** Creates a new definite string 124da0d961cSdjm * 125da0d961cSdjm * The handle is initialized to `NULL` and length to 0 126da0d961cSdjm * 127da0d961cSdjm * @return **new** definite string. `NULL` on malloc failure. 128da0d961cSdjm */ 129*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_definite_string(void); 130da0d961cSdjm 131da0d961cSdjm /** Creates a new indefinite string 132da0d961cSdjm * 133da0d961cSdjm * The chunks array is initialized to `NULL` and chunkcount to 0 134da0d961cSdjm * 135da0d961cSdjm * @return **new** indefinite string. `NULL` on malloc failure. 136da0d961cSdjm */ 137*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_new_indefinite_string(void); 138da0d961cSdjm 139da0d961cSdjm /** Creates a new string and initializes it 140da0d961cSdjm * 141da0d961cSdjm * The `val` will be copied to a newly allocated block 142da0d961cSdjm * 143da0d961cSdjm * @param val A null-terminated UTF-8 string 144da0d961cSdjm * @return A **new** string with content `handle`. `NULL` on malloc failure. 145da0d961cSdjm */ 146*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_string(const char *val); 147da0d961cSdjm 148da0d961cSdjm /** Creates a new string and initializes it 149da0d961cSdjm * 150da0d961cSdjm * The `handle` will be copied to a newly allocated block 151da0d961cSdjm * 152da0d961cSdjm * @param val A UTF-8 string, at least \p length long (excluding the null byte) 153da0d961cSdjm * @return A **new** string with content `handle`. `NULL` on malloc failure. 154da0d961cSdjm */ 155*4dcc46c4Sdjm _CBOR_NODISCARD CBOR_EXPORT cbor_item_t *cbor_build_stringn(const char *val, 156*4dcc46c4Sdjm size_t length); 157da0d961cSdjm 158da0d961cSdjm #ifdef __cplusplus 159da0d961cSdjm } 160da0d961cSdjm #endif 161da0d961cSdjm 162da0d961cSdjm #endif // LIBCBOR_STRINGS_H 163