1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
|
/*
* NASPRO - The NASPRO Architecture for Sound PROcessing
* Portable runtime library
*
* Copyright (C) 2007-2014 Stefano D'Angelo
*
* See the COPYING file for license conditions.
*/
/*
Title: String handling
Utility functions to operate on strings.
Some of them are portable versions and/or replacements for some useful GNU
and POSIX extesions.
*/
#ifndef _NASPRO_CORE_STRING_H
#define _NASPRO_CORE_STRING_H
#ifndef _NASPRO_CORE_LIB_H
# error Only <NASPRO/core/lib.h> can be included directly.
#endif
NACORE_BEGIN_C_DECLS
/*
Function: nacore_char_utf8_encode()
Encodes a Unicode code point into an UTF-8 character.
If utf8c is NULL, it does only calculate the length in bytes of the UTF-8
character corresponding to the given code point.
Parameters:
utf8c - Pointer to a large enough buffer to contain the resulting
UTF-8 character (worst case: 4 bytes wide) or NULL.
cp - Code point.
Returns:
Length in bytes of the resulting UTF-8 character or 0 if cp is outside of
the valid value range (0 to 0x10ffff, except 0xd800 to 0xdfff, 0xfeff and
0xfffe).
*/
_NACORE_DEF size_t
nacore_char_utf8_encode(char *utf8c, uint32_t cp);
/*
Function: nacore_char_utf8_decode()
Decodes the Unicode code point associated to an UTF-8 character.
If cp is not NULL and the encoding is valid, the code point is stored into
*cp.
Parameters:
utf8c - Pointer to the buffer containing the UTF-8 character to be
decoded.
cp - Pointer to the memory location where to put the code point
value or NULL.
Returns:
Length in bytes of the decoded character or 0 if the encoding is not valid,
in which case nothing is written in *cp.
*/
_NACORE_DEF size_t
nacore_char_utf8_decode(const char *utf8c, uint32_t *cp);
/*
Function: nacore_char_utf16_encode()
Encodes a Unicode code point into an UTF-16 character.
If utf16c is NULL, it does only calculate the length in bytes of the UTF-16
character corresponding to the given code point.
Parameters:
utf16c - Pointer to a large enough buffer to contain the resulting
UTF-16 character (worst case: 4 bytes wide) or NULL.
cp - Code point.
Returns:
Length in bytes of the resulting UTF-16 character or 0 if cp is outside of
the valid value range (0 to 0x10ffff, except 0xd800 to 0xdfff, 0xfeff and
0xfffe).
*/
_NACORE_DEF size_t
nacore_char_utf16_encode(uint16_t *utf16c, uint32_t cp);
/*
Function: nacore_char_utf16_decode()
Decodes the Unicode code point associated to an UTF-16 character.
If cp is not NULL and the encoding is valid, the code point is stored into
*cp.
Parameters:
utf16c - Pointer to the buffer containing the UTF-16 character to be
decoded.
cp - Pointer to the memory location where to put the code point
value or NULL.
Returns:
Length in bytes of the decoded character or 0 if the encoding is not valid,
in which case nothing is written in *cp.
*/
_NACORE_DEF size_t
nacore_char_utf16_decode(const uint16_t *utf16c, uint32_t *cp);
/*
Function: nacore_string_utf8_to_utf16_len()
Calculates the number of bytes needed to store the UTF-16 representation of a
UTF-8 encoded string, excluding the terminating null character.
It tries to skip badly encoded data, yet it's actually just guessing, hence
try to avoid relying on this behavior.
Parameters:
str_utf8 - UTF-8 encoded string.
Returns:
Number of bytes needed to store the UTF-16 representation of str_utf8,
excluding the terminating null character.
*/
_NACORE_DEF size_t
nacore_string_utf8_to_utf16_len(const char *str_utf8);
/*
Function: nacore_string_utf8_to_utf16()
Converts a UTF-8 encoded string to UTF-16 into a previously allocated buffer,
including the terminating null character.
It tries to skip badly encoded data, yet it's actually just guessing, hence
try to avoid relying on this behavior.
Parameters:
buf - Output buffer.
str_utf8 - UTF-8 encoded string to be converted.
*/
_NACORE_DEF void
nacore_string_utf8_to_utf16(uint16_t *buf, const char *str_utf8);
/*
Function: nacore_string_utf8_to_utf16_a()
Converts a UTF-8 encoded string to UTF-16, allocating the output string.
It tries to skip badly encoded data, yet it's actually just guessing, hence
try to avoid relying on this behavior.
Parameters:
str_utf8 - UTF-8 encoded string to be converted.
Returns:
A malloc()-allocated, UTF-16 encoded string or NULL if there was not enough
memory. The caller is in charge of free()ing such string.
*/
_NACORE_DEF uint16_t *
nacore_string_utf8_to_utf16_a(const char *str_utf8);
/*
Function: nacore_string_utf16_to_utf8_len()
Calculates the number of bytes needed to store the UTF-8 representation of a
UTF-16 encoded string, excluding the terminating null character.
It tries to skip badly encoded data, yet it's actually just guessing, hence
try to avoid relying on this behavior.
Parameters:
str_utf16 - UTF-16 encoded string.
Returns:
Number of bytes needed to store the UTF-8 representation of str_utf16,
excluding the terminating null character.
*/
_NACORE_DEF size_t
nacore_string_utf16_to_utf8_len(const uint16_t *str_utf16);
/*
Function: nacore_string_utf16_to_utf8()
Converts a UTF-16 encoded string to UTF-8 into a previously allocated buffer,
including the terminating null character.
It tries to skip badly encoded data, yet it's actually just guessing, hence
try to avoid relying on this behavior.
Parameters:
buf - Output buffer.
str_utf16 - UTF-16 encoded string to be converted.
*/
_NACORE_DEF void
nacore_string_utf16_to_utf8(char *buf, const uint16_t *str_utf16);
/*
Function: nacore_string_utf16_to_utf8_a()
Converts a UTF-16 encoded string to UTF-8, allocating the output string.
It tries to skip badly encoded data, yet it's actually just guessing, hence
try to avoid relying on this behavior.
Parameters:
str_utf16 - UTF-16 encoded string to be converted.
Returns:
A malloc()-allocated, UTF-8 encoded string or NULL if there was not enough
memory. The caller is in charge of free()ing such string.
*/
_NACORE_DEF char *
nacore_string_utf16_to_utf8_a(const uint16_t *str_utf16);
/*
Function: nacore_string_get_size()
Returns the number of bytes making up a string including the terminating null
character.
Can be safely casted to <nacore_get_size_cb> type.
Parameters:
s - The string.
unused - Unused, set to NULL.
Returns:
Number of bytes.
*/
_NACORE_DEF size_t
nacore_string_get_size(const char *s, void *unused);
/*
Function: nacore_strnlen()
Gets the number of bytes in a string, not including the terminating null
character, up to a certain length.
In doing this, the function looks only at the first maxlen bytes at s and
never beyond s + maxlen.
Parameters:
s - String to be examined.
maxlen - Maximum number of bytes.
Returns:
strlen(s) if that is less than maxlen, or maxlen if there is no null
character among the first maxlen characters pointed to by s.
*/
_NACORE_DEF size_t
nacore_strnlen(const char *s, size_t maxlen);
/*
Function: nacore_strdup()
Analog of strcpy() that allocates a string large enough to hold the output
including the terminating null character.
The allocated storage should be free()d when it is no longer needed.
Can be safely casted to <nacore_to_string_cb> type.
Parameters:
s - The string to copy.
unused - Unsed, set to NULL.
Returns:
A malloc()-allocated string copy or NULL if there was not enough memory.
*/
_NACORE_DEF char *
nacore_strdup(const char *s, void *unused);
/*
Function: nacore_asprintf()
Analog of sprintf() that allocates a string large enough to hold the output
including the terminating null character.
If strp is not NULL, it is set to a pointer to the malloc()-allocated string.
Such string should be free()d when it is no longer needed.
The function is affected by locale settings.
It supports all C99 conversion specifications, except %lc and %ls kinds.
Length and precision modifiers for %s kind of conversion specifications
indicate number of bytes.
This function can be considered thread-safe as long as no other thread
changes locale settings of the calling thread while it is running.
Parameters:
strp - Memory location where to put a pointer to the allocated
string or NULL.
fmt - printf()-like format string.
... - printf()-like extra arguments.
Returns:
Length of the output string in bytes excluding the terminating null
character. If there was not enough memory and strp is not NULL,
*strp is set to NULL.
*/
_NACORE_DEF NACORE_FORMAT_PRINTF(2, 3) int
nacore_asprintf(char **strp, const char *fmt, ...);
/*
Function: nacore_asprintf_nl()
Analog of sprintf() that allocates a string large enough to hold the output
including the terminating null character.
If strp is not NULL, it is set to a pointer to the malloc()-allocated string.
Such string should be free()d when it is no longer needed.
The function is not affected by locale settings (i.e., it acts as if the "C"
locale is used).
It supports all C99 conversion specifications, except %lc and %ls kinds.
Length and precision modifiers for %s kind of conversion specifications
indicate number of bytes.
Parameters:
strp - Memory location where to put a pointer to the allocated
string or NULL.
fmt - printf()-like format string.
... - printf()-like extra arguments.
Returns:
Length of the output string in bytes excluding the terminating null
character. If there was not enough memory and strp is not NULL,
*strp is set to NULL.
*/
_NACORE_DEF NACORE_FORMAT_PRINTF(2, 3) int
nacore_asprintf_nl(char **strp, const char *fmt, ...);
/*
Function: nacore_vasprintf()
Analog of vsprintf() that allocates a string large enough to hold the output
including the terminating null character.
If strp is not NULL, it is set to a pointer to the malloc()-allocated string.
Such string should be free()d when it is no longer needed.
The function is affected by locale settings.
It supports all C99 conversion specifications, except %lc and %ls kinds.
Length and precision modifiers for %s kind of conversion specifications
indicate number of bytes.
va_end() is not called on ap inside the function, hence its value is
undefined after the call.
This function can be considered thread-safe as long as no other thread
changes locale settings of the calling thread while it is running.
Parameters:
strp - Memory location where to put a pointer to the allocated
string or NULL.
fmt - vprintf()-like format string.
ap - vprintf()-like va_list.
Returns:
Length of the output string in bytes excluding the terminating null
character. If there was not enough memory and strp is not NULL,
*strp is set to NULL.
*/
_NACORE_DEF NACORE_FORMAT_VPRINTF(2) int
nacore_vasprintf(char **strp, const char *fmt, va_list ap);
/*
Function: nacore_vasprintf_nl()
Analog of vsprintf() that allocates a string large enough to hold the output
including the terminating null character.
If strp is not NULL, it is set to a pointer to the malloc()-allocated string.
Such string should be free()d when it is no longer needed.
The function is not affected by locale settings (i.e., it acts as if the "C"
locale is used).
It supports all C99 conversion specifications, except %lc and %ls kinds.
Length and precision modifiers for %s kind of conversion specifications
indicate number of bytes.
va_end() is not called on ap inside the function, hence its value is
undefined after the call.
Parameters:
strp - Memory location where to put a pointer to the allocated
string or NULL.
fmt - vprintf()-like format string.
ap - vprintf()-like va_list.
Returns:
Length of the output string in bytes excluding the terminating null
character. If there was not enough memory and strp is not NULL,
*strp is set to NULL.
*/
_NACORE_DEF NACORE_FORMAT_VPRINTF(2) int
nacore_vasprintf_nl(char **strp, const char *fmt, va_list ap);
/*
Function: nacore_string_split()
Creates an auto-allocating list of strings by splitting the given string on
boundaries formed by the given separator string.
The list will use <nacore_string_get_size()> as data size callback.
If filter_cb is not NULL, it will be called along with filter_opaque for each
substring; if it is to be filtered out (i.e., filter_cb returns 0) the
substring will not be included in the resulting list.
Parameters:
s - Input string.
sep - Separator string.
filter_cb - Value filtering callback.
filter_opaque - Extra opaque data to be passed to filter_cb or NULL.
Returns:
Auto-allocating list of strings or NULL if there was not enough memory.
*/
_NACORE_DEF nacore_list
nacore_string_split(const char *s, const char *sep, nacore_filter_cb filter_cb,
void *filter_opaque);
NACORE_END_C_DECLS
#endif
|