1 #ifndef NU_STRINGS_H 2 #define NU_STRINGS_H 3 4 /** @defgroup strings String functions 5 * 6 * Note on "n" functions variant: "n" is in bytes in all functions, 7 * note though that those are not for memory overrun control. 8 * They are just for strings not having terminating 0 byte and those 9 * functions won't go further than m-th *codepoint* in string, but might go 10 * further than n-th byte in case of multibyte sequence. 11 * 12 * E.g.: ``nu_strnlen("абв", 3, nu_utf8_read);``. 13 * Since codepoints are 2-byte sequences, nu_strnlen() won't go further than 2nd 14 * codepoint, but will go further than 3rd byte while reading "б". 15 */ 16 17 #include <stdint.h> 18 #include <sys/types.h> 19 20 #include <libnu/config.h> 21 #include <libnu/defines.h> 22 23 #if defined (__cplusplus) || defined (c_plusplus) 24 extern "C" { 25 #endif 26 27 /** 28 * @defgroup iterators Iterators 29 * @defgroup transformations Codepoint transformations 30 * @defgroup transformations_internal Codepoint transformations (internal) 31 */ 32 33 /** Read (decode) iterator 34 * 35 * @ingroup iterators 36 * @see nu_utf8_read 37 */ 38 typedef const char* (*nu_read_iterator_t)(const char *encoded, uint32_t *unicode); 39 40 /** Read (decode) backwards iterator 41 * 42 * Arguments intentionally reversed to not mix this with nu_read_iterator_t. 43 * Reverse read is not compatible with any of string functions. 44 * 45 * @ingroup iterators 46 * @see nu_utf8_revread 47 */ 48 typedef const char* (*nu_revread_iterator_t)(uint32_t *unicode, const char *encoded); 49 50 /** Write (encode) iterator 51 * 52 * @ingroup iterators 53 * @see nu_utf8_write 54 */ 55 typedef char* (*nu_write_iterator_t)(uint32_t unicode, char *encoded); 56 57 /** Transform codepoint 58 * 59 * @ingroup transformations 60 * @see nu_toupper 61 * @see nu_tolower 62 */ 63 typedef const char* (*nu_transformation_t)(uint32_t codepoint); 64 65 /** Transform codepoint (used internally). This kind of transformation 66 * delegates iteration on string to transformation implementation. 67 * 68 * @ingroup transformations_internal 69 * @see _nu_toupper 70 * @see _nu_tolower 71 */ 72 typedef const char* (*nu_transform_read_t)( 73 const char *encoded, const char *limit, nu_read_iterator_t read, 74 uint32_t *u, const char **transformed, 75 void *context); 76 77 #if (defined NU_WITH_Z_STRINGS) || (defined NU_WITH_N_STRINGS) 78 79 #endif /* NU_WITH_Z_STRINGS NU_WITH_N_STRINGS */ 80 81 #ifdef NU_WITH_Z_STRINGS 82 83 /** Get decoded string codepoints length 84 * 85 * @ingroup strings 86 * @param encoded encoded string 87 * @param it decoding function 88 * @return string length or negative error 89 * 90 * @see nu_strnlen 91 */ 92 NU_EXPORT 93 ssize_t nu_strlen(const char *encoded, nu_read_iterator_t it); 94 95 /** Get encoded string bytes length (encoding variant) 96 * 97 * @ingroup strings 98 * @param unicode unicode codepoints 99 * @param it encoding function 100 * @return byte length or negative error 101 * 102 * @see nu_bytenlen 103 */ 104 NU_EXPORT 105 ssize_t nu_bytelen(const uint32_t *unicode, nu_write_iterator_t it); 106 107 /** Get encoded string bytes length 108 * 109 * @ingroup strings 110 * @param encoded encoded string 111 * @param it decoding function 112 * @return string length or negative error 113 */ 114 NU_EXPORT 115 ssize_t nu_strbytelen(const char *encoded, nu_read_iterator_t it); 116 117 #endif /* NU_WITH_Z_STRINGS */ 118 119 #ifdef NU_WITH_N_STRINGS 120 121 /** 122 * @ingroup strings 123 * @see nu_strlen 124 */ 125 NU_EXPORT 126 ssize_t nu_strnlen(const char *encoded, size_t max_len, nu_read_iterator_t it); 127 128 /** 129 * @ingroup strings 130 * @see nu_bytelen 131 */ 132 NU_EXPORT 133 ssize_t nu_bytenlen(const uint32_t *unicode, size_t max_len, 134 nu_write_iterator_t it); 135 136 #endif /* NU_WITH_N_STRINGS */ 137 138 #if defined (__cplusplus) || defined (c_plusplus) 139 } 140 #endif 141 142 #endif /* NU_STRINGS_H */ 143