VirtualBox

source: vbox/trunk/include/iprt/string.h@ 93962

Last change on this file since 93962 was 93962, checked in by vboxsync, 3 years ago

IPRT/string.h: Tweak for for darwin.x86 GA build.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 144.4 KB
Line 
1/** @file
2 * IPRT - String Manipulation.
3 */
4
5/*
6 * Copyright (C) 2006-2022 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef IPRT_INCLUDED_string_h
27#define IPRT_INCLUDED_string_h
28#ifndef RT_WITHOUT_PRAGMA_ONCE
29# pragma once
30#endif
31
32#include <iprt/cdefs.h>
33#include <iprt/types.h>
34#include <iprt/assert.h>
35#include <iprt/stdarg.h>
36#include <iprt/errcore.h> /* for VINF_SUCCESS */
37#if defined(RT_OS_LINUX) && defined(__KERNEL__)
38 /* no C++ hacks ('new' etc) here anymore! */
39# include <linux/string.h>
40
41#elif defined(IN_XF86_MODULE) && !defined(NO_ANSIC)
42 RT_C_DECLS_BEGIN
43# include "xf86_ansic.h"
44 RT_C_DECLS_END
45
46#elif defined(RT_OS_FREEBSD) && defined(_KERNEL)
47 RT_C_DECLS_BEGIN
48# include <sys/libkern.h>
49 RT_C_DECLS_END
50
51#elif defined(RT_OS_NETBSD) && defined(_KERNEL)
52 RT_C_DECLS_BEGIN
53# include <lib/libkern/libkern.h>
54 RT_C_DECLS_END
55
56#elif defined(RT_OS_SOLARIS) && defined(_KERNEL)
57 /*
58 * Same case as with FreeBSD kernel:
59 * The string.h stuff clashes with sys/system.h
60 * ffs = find first set bit.
61 */
62# define ffs ffs_string_h
63# define fls fls_string_h
64# include <string.h>
65# undef fls
66# undef ffs
67# undef strpbrk
68
69#else
70# include <string.h>
71#endif
72
73/*
74 * Supply prototypes for standard string functions provided by
75 * IPRT instead of the operating environment.
76 */
77#if defined(RT_OS_DARWIN) && defined(KERNEL)
78RT_C_DECLS_BEGIN
79void *memchr(const void *pv, int ch, size_t cb);
80char *strpbrk(const char *pszStr, const char *pszChars);
81RT_C_DECLS_END
82#endif
83
84#if defined(RT_OS_FREEBSD) && defined(_KERNEL)
85RT_C_DECLS_BEGIN
86char *strpbrk(const char *pszStr, const char *pszChars);
87RT_C_DECLS_END
88#endif
89
90#if defined(RT_OS_NETBSD) && defined(_KERNEL)
91RT_C_DECLS_BEGIN
92char *strpbrk(const char *pszStr, const char *pszChars);
93RT_C_DECLS_END
94#endif
95
96#if (defined(RT_OS_DARWIN) || defined(RT_OS_SOLARIS) || defined(RT_OS_WINDOWS)) && !defined(IPRT_NO_CRT)
97RT_C_DECLS_BEGIN
98# if !defined(RT_OS_DARWIN) || RT_CLANG_PREREQ(7 /* whatever post gcc-4.2 */, 0)
99RTDECL(void *) mempcpy(void *pvDst, const void *pvSrc, size_t cb);
100# else
101void *mempcpy(void *pvDst, const void *pvSrc, size_t cb);
102# endif
103RT_C_DECLS_END
104#endif
105
106#if (!defined(RT_OS_LINUX) || !defined(_GNU_SOURCE)) \
107 && (!defined(RT_OS_OS2) || !defined(_GNU_SOURCE)) \
108 && !defined(RT_OS_FREEBSD) \
109 && !defined(RT_OS_NETBSD)
110RT_C_DECLS_BEGIN
111void *memrchr(const void *pv, int ch, size_t cb);
112RT_C_DECLS_END
113#endif
114
115
116/** @def RT_USE_RTC_3629
117 * When defined the UTF-8 range will stop at 0x10ffff. If not defined, the
118 * range stops at 0x7fffffff.
119 * @remarks Must be defined both when building and using the IPRT. */
120#ifdef DOXYGEN_RUNNING
121# define RT_USE_RTC_3629
122#endif
123
124
125/**
126 * Byte zero the specified object.
127 *
128 * This will use sizeof(Obj) to figure the size and will call memset, bzero
129 * or some compiler intrinsic to perform the actual zeroing.
130 *
131 * @param Obj The object to zero. Make sure to dereference pointers.
132 *
133 * @remarks Because the macro may use memset it has been placed in string.h
134 * instead of cdefs.h to avoid build issues because someone forgot
135 * to include this header.
136 *
137 * @ingroup grp_rt_cdefs
138 */
139#define RT_ZERO(Obj) RT_BZERO(&(Obj), sizeof(Obj))
140
141/**
142 * Byte zero the specified memory area.
143 *
144 * This will call memset, bzero or some compiler intrinsic to clear the
145 * specified bytes of memory.
146 *
147 * @param pv Pointer to the memory.
148 * @param cb The number of bytes to clear. Please, don't pass 0.
149 *
150 * @remarks Because the macro may use memset it has been placed in string.h
151 * instead of cdefs.h to avoid build issues because someone forgot
152 * to include this header.
153 *
154 * @ingroup grp_rt_cdefs
155 */
156#define RT_BZERO(pv, cb) do { memset((pv), 0, cb); } while (0)
157
158
159/**
160 * For copying a volatile variable to a non-volatile one.
161 * @param a_Dst The non-volatile destination variable.
162 * @param a_VolatileSrc The volatile source variable / dereferenced pointer.
163 */
164#define RT_COPY_VOLATILE(a_Dst, a_VolatileSrc) \
165 do { \
166 void const volatile *a_pvVolatileSrc_BCopy_Volatile = &(a_VolatileSrc); \
167 AssertCompile(sizeof(a_Dst) == sizeof(a_VolatileSrc)); \
168 memcpy(&(a_Dst), (void const *)a_pvVolatileSrc_BCopy_Volatile, sizeof(a_Dst)); \
169 } while (0)
170
171/**
172 * For copy a number of bytes from a volatile buffer to a non-volatile one.
173 *
174 * @param a_pDst Pointer to the destination buffer.
175 * @param a_pVolatileSrc Pointer to the volatile source buffer.
176 * @param a_cbToCopy Number of bytes to copy.
177 */
178#define RT_BCOPY_VOLATILE(a_pDst, a_pVolatileSrc, a_cbToCopy) \
179 do { \
180 void const volatile *a_pvVolatileSrc_BCopy_Volatile = (a_pVolatileSrc); \
181 memcpy((a_pDst), (void const *)a_pvVolatileSrc_BCopy_Volatile, (a_cbToCopy)); \
182 } while (0)
183
184
185/** @defgroup grp_rt_str RTStr - String Manipulation
186 * Mostly UTF-8 related helpers where the standard string functions won't do.
187 * @ingroup grp_rt
188 * @{
189 */
190
191RT_C_DECLS_BEGIN
192
193
194/**
195 * The maximum string length.
196 */
197#define RTSTR_MAX (~(size_t)0)
198
199
200/** @def RTSTR_TAG
201 * The default allocation tag used by the RTStr allocation APIs.
202 *
203 * When not defined before the inclusion of iprt/string.h, this will default to
204 * the pointer to the current file name. The string API will make of use of
205 * this as pointer to a volatile but read-only string.
206 */
207#if !defined(RTSTR_TAG) || defined(DOXYGEN_RUNNING)
208# define RTSTR_TAG (__FILE__)
209#endif
210
211
212#ifdef IN_RING3
213
214/**
215 * Allocates tmp buffer with default tag, translates pszString from UTF8 to
216 * current codepage.
217 *
218 * @returns iprt status code.
219 * @param ppszString Receives pointer of allocated native CP string.
220 * The returned pointer must be freed using RTStrFree().
221 * @param pszString UTF-8 string to convert.
222 */
223#define RTStrUtf8ToCurrentCP(ppszString, pszString) RTStrUtf8ToCurrentCPTag((ppszString), (pszString), RTSTR_TAG)
224
225/**
226 * Allocates tmp buffer with custom tag, translates pszString from UTF-8 to
227 * current codepage.
228 *
229 * @returns iprt status code.
230 * @param ppszString Receives pointer of allocated native CP string.
231 * The returned pointer must be freed using
232 * RTStrFree()., const char *pszTag
233 * @param pszString UTF-8 string to convert.
234 * @param pszTag Allocation tag used for statistics and such.
235 */
236RTR3DECL(int) RTStrUtf8ToCurrentCPTag(char **ppszString, const char *pszString, const char *pszTag);
237
238/**
239 * Allocates tmp buffer with default tag, translates pszString from UTF-8 to
240 * current codepage, extended version.
241 *
242 * @returns iprt status code.
243 * @param ppszString Receives pointer of allocated native CP string.
244 * The returned pointer must be freed using RTStrFree().
245 * @param pszString UTF-8 string to convert.
246 * @param cchString The maximum size in chars (the type) to convert. The conversion stop
247 * when it reaches cchString or the string terminator ('\\0').
248 * Use RTSTR_MAX to translate the entire string.
249 */
250#define RTStrUtf8ToCurrentCPEx(ppszString, pszString, cchString) \
251 RTStrUtf8ToCurrentCPExTag((ppszString), (pszString), (cchString), RTSTR_TAG)
252
253/**
254 * Allocates tmp buffer with custom tag, translates pszString from UTF8 to
255 * current codepage.
256 *
257 * @returns iprt status code.
258 * @param ppszString Receives pointer of allocated native CP string.
259 * The returned pointer must be freed using
260 * RTStrFree()., const char *pszTag
261 * @param pszString UTF-8 string to convert.
262 * @param cchString The maximum size in chars (the type) to convert. The conversion stop
263 * when it reaches cchString or the string terminator ('\\0').
264 * Use RTSTR_MAX to translate the entire string.
265 * @param pszTag Allocation tag used for statistics and such.
266 */
267RTR3DECL(int) RTStrUtf8ToCurrentCPExTag(char **ppszString, const char *pszString, size_t cchString, const char *pszTag);
268
269/**
270 * Allocates tmp buffer, translates pszString from current codepage to UTF-8.
271 *
272 * @returns iprt status code.
273 * @param ppszString Receives pointer of allocated UTF-8 string.
274 * The returned pointer must be freed using RTStrFree().
275 * @param pszString Native string to convert.
276 */
277#define RTStrCurrentCPToUtf8(ppszString, pszString) RTStrCurrentCPToUtf8Tag((ppszString), (pszString), RTSTR_TAG)
278
279/**
280 * Allocates tmp buffer, translates pszString from current codepage to UTF-8.
281 *
282 * @returns iprt status code.
283 * @param ppszString Receives pointer of allocated UTF-8 string.
284 * The returned pointer must be freed using RTStrFree().
285 * @param pszString Native string to convert.
286 * @param pszTag Allocation tag used for statistics and such.
287 */
288RTR3DECL(int) RTStrCurrentCPToUtf8Tag(char **ppszString, const char *pszString, const char *pszTag);
289
290/**
291 * Allocates tmp buffer, translates pszString from console codepage to UTF-8.
292 *
293 * @returns iprt status code.
294 * @param ppszString Receives pointer of allocated UTF-8 string.
295 * The returned pointer must be freed using RTStrFree().
296 * @param pszString Native string to convert.
297 */
298#define RTStrConsoleCPToUtf8(ppszString, pszString) RTStrConsoleCPToUtf8Tag((ppszString), (pszString), RTSTR_TAG)
299
300/**
301 * Allocates tmp buffer, translates pszString from console codepage to UTF-8.
302 *
303 * @returns iprt status code.
304 * @param ppszString Receives pointer of allocated UTF-8 string.
305 * The returned pointer must be freed using RTStrFree().
306 * @param pszString Native string to convert.
307 * @param pszTag Allocation tag used for statistics and such.
308 */
309RTR3DECL(int) RTStrConsoleCPToUtf8Tag(char **ppszString, const char *pszString, const char *pszTag);
310
311#endif /* IN_RING3 */
312
313/**
314 * Free string allocated by any of the non-UCS-2 string functions.
315 *
316 * @returns iprt status code.
317 * @param pszString Pointer to buffer with string to free.
318 * NULL is accepted.
319 */
320RTDECL(void) RTStrFree(char *pszString);
321
322/**
323 * Allocates a new copy of the given UTF-8 string (default tag).
324 *
325 * @returns Pointer to the allocated UTF-8 string.
326 * @param pszString UTF-8 string to duplicate.
327 */
328#define RTStrDup(pszString) RTStrDupTag((pszString), RTSTR_TAG)
329
330/**
331 * Allocates a new copy of the given UTF-8 string (custom tag).
332 *
333 * @returns Pointer to the allocated UTF-8 string.
334 * @param pszString UTF-8 string to duplicate.
335 * @param pszTag Allocation tag used for statistics and such.
336 */
337RTDECL(char *) RTStrDupTag(const char *pszString, const char *pszTag);
338
339/**
340 * Allocates a new copy of the given UTF-8 string (default tag).
341 *
342 * @returns iprt status code.
343 * @param ppszCopy Receives pointer of the allocated UTF-8 string.
344 * The returned pointer must be freed using RTStrFree().
345 * @param pszString UTF-8 string to duplicate.
346 */
347#define RTStrDupEx(ppszCopy, pszString) RTStrDupExTag((ppszCopy), (pszString), RTSTR_TAG)
348
349/**
350 * Allocates a new copy of the given UTF-8 string (custom tag).
351 *
352 * @returns iprt status code.
353 * @param ppszCopy Receives pointer of the allocated UTF-8 string.
354 * The returned pointer must be freed using RTStrFree().
355 * @param pszString UTF-8 string to duplicate.
356 * @param pszTag Allocation tag used for statistics and such.
357 */
358RTDECL(int) RTStrDupExTag(char **ppszCopy, const char *pszString, const char *pszTag);
359
360/**
361 * Allocates a new copy of the given UTF-8 substring (default tag).
362 *
363 * @returns Pointer to the allocated UTF-8 substring.
364 * @param pszString UTF-8 string to duplicate.
365 * @param cchMax The max number of chars to duplicate, not counting
366 * the terminator.
367 */
368#define RTStrDupN(pszString, cchMax) RTStrDupNTag((pszString), (cchMax), RTSTR_TAG)
369
370/**
371 * Allocates a new copy of the given UTF-8 substring (custom tag).
372 *
373 * @returns Pointer to the allocated UTF-8 substring.
374 * @param pszString UTF-8 string to duplicate.
375 * @param cchMax The max number of chars to duplicate, not counting
376 * the terminator.
377 * @param pszTag Allocation tag used for statistics and such.
378 */
379RTDECL(char *) RTStrDupNTag(const char *pszString, size_t cchMax, const char *pszTag);
380
381/**
382 * Allocates a new copy of the given UTF-8 substring (default tag).
383 *
384 * @returns iprt status code (VINF_SUCCESS or VERR_NO_STR_MEMORY).
385 * @param ppszCopy Receives pointer of the allocated UTF-8 substring.
386 * The returned pointer must be freed using RTStrFree().
387 * @param pszString UTF-8 string to duplicate.
388 * @param cchMax The max number of chars to duplicate, not counting
389 * the terminator.
390 */
391#define RTStrDupNEx(ppszCopy, pszString, cchMax) RTStrDupNExTag((ppszCopy), (pszString), (cchMax), RTSTR_TAG)
392
393/**
394 * Allocates a new copy of the given UTF-8 substring (custom tag).
395 *
396 * @returns iprt status code (VINF_SUCCESS or VERR_NO_STR_MEMORY).
397 * @param ppszCopy Receives pointer of the allocated UTF-8 substring.
398 * The returned pointer must be freed using RTStrFree().
399 * @param pszString UTF-8 string to duplicate.
400 * @param cchMax The max number of chars to duplicate, not counting
401 * the terminator.
402 * @param pszTag Allocation tag used for statistics and such.
403 */
404RTDECL(int) RTStrDupNExTag(char **ppszCopy, const char *pszString, size_t cchMax, const char *pszTag);
405
406/**
407 * Appends a string onto an existing IPRT allocated string (default tag).
408 *
409 * @retval VINF_SUCCESS
410 * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
411 * remains unchanged.
412 *
413 * @param ppsz Pointer to the string pointer. The string
414 * pointer must either be NULL or point to a string
415 * returned by an IPRT string API. (In/Out)
416 * @param pszAppend The string to append. NULL and empty strings
417 * are quietly ignored.
418 */
419#define RTStrAAppend(ppsz, pszAppend) RTStrAAppendTag((ppsz), (pszAppend), RTSTR_TAG)
420
421/**
422 * Appends a string onto an existing IPRT allocated string (custom tag).
423 *
424 * @retval VINF_SUCCESS
425 * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
426 * remains unchanged.
427 *
428 * @param ppsz Pointer to the string pointer. The string
429 * pointer must either be NULL or point to a string
430 * returned by an IPRT string API. (In/Out)
431 * @param pszAppend The string to append. NULL and empty strings
432 * are quietly ignored.
433 * @param pszTag Allocation tag used for statistics and such.
434 */
435RTDECL(int) RTStrAAppendTag(char **ppsz, const char *pszAppend, const char *pszTag);
436
437/**
438 * Appends N bytes from a strings onto an existing IPRT allocated string
439 * (default tag).
440 *
441 * @retval VINF_SUCCESS
442 * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
443 * remains unchanged.
444 *
445 * @param ppsz Pointer to the string pointer. The string
446 * pointer must either be NULL or point to a string
447 * returned by an IPRT string API. (In/Out)
448 * @param pszAppend The string to append. Can be NULL if cchAppend
449 * is NULL.
450 * @param cchAppend The number of chars (not code points) to append
451 * from pszAppend. Must not be more than
452 * @a pszAppend contains, except for the special
453 * value RTSTR_MAX that can be used to indicate all
454 * of @a pszAppend without having to strlen it.
455 */
456#define RTStrAAppendN(ppsz, pszAppend, cchAppend) RTStrAAppendNTag((ppsz), (pszAppend), (cchAppend), RTSTR_TAG)
457
458/**
459 * Appends N bytes from a strings onto an existing IPRT allocated string (custom
460 * tag).
461 *
462 * @retval VINF_SUCCESS
463 * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
464 * remains unchanged.
465 *
466 * @param ppsz Pointer to the string pointer. The string
467 * pointer must either be NULL or point to a string
468 * returned by an IPRT string API. (In/Out)
469 * @param pszAppend The string to append. Can be NULL if cchAppend
470 * is NULL.
471 * @param cchAppend The number of chars (not code points) to append
472 * from pszAppend. Must not be more than
473 * @a pszAppend contains, except for the special
474 * value RTSTR_MAX that can be used to indicate all
475 * of @a pszAppend without having to strlen it.
476 * @param pszTag Allocation tag used for statistics and such.
477 */
478RTDECL(int) RTStrAAppendNTag(char **ppsz, const char *pszAppend, size_t cchAppend, const char *pszTag);
479
480/**
481 * Appends one or more strings onto an existing IPRT allocated string.
482 *
483 * This is a very flexible and efficient alternative to using RTStrAPrintf to
484 * combine several strings together.
485 *
486 * @retval VINF_SUCCESS
487 * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
488 * remains unchanged.
489 *
490 * @param ppsz Pointer to the string pointer. The string
491 * pointer must either be NULL or point to a string
492 * returned by an IPRT string API. (In/Out)
493 * @param cPairs The number of string / length pairs in the
494 * @a va.
495 * @param va List of string (const char *) and length
496 * (size_t) pairs. The strings will be appended to
497 * the string in the first argument.
498 */
499#define RTStrAAppendExNV(ppsz, cPairs, va) RTStrAAppendExNVTag((ppsz), (cPairs), (va), RTSTR_TAG)
500
501/**
502 * Appends one or more strings onto an existing IPRT allocated string.
503 *
504 * This is a very flexible and efficient alternative to using RTStrAPrintf to
505 * combine several strings together.
506 *
507 * @retval VINF_SUCCESS
508 * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
509 * remains unchanged.
510 *
511 * @param ppsz Pointer to the string pointer. The string
512 * pointer must either be NULL or point to a string
513 * returned by an IPRT string API. (In/Out)
514 * @param cPairs The number of string / length pairs in the
515 * @a va.
516 * @param va List of string (const char *) and length
517 * (size_t) pairs. The strings will be appended to
518 * the string in the first argument.
519 * @param pszTag Allocation tag used for statistics and such.
520 */
521RTDECL(int) RTStrAAppendExNVTag(char **ppsz, size_t cPairs, va_list va, const char *pszTag);
522
523/**
524 * Appends one or more strings onto an existing IPRT allocated string
525 * (untagged).
526 *
527 * This is a very flexible and efficient alternative to using RTStrAPrintf to
528 * combine several strings together.
529 *
530 * @retval VINF_SUCCESS
531 * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
532 * remains unchanged.
533 *
534 * @param ppsz Pointer to the string pointer. The string
535 * pointer must either be NULL or point to a string
536 * returned by an IPRT string API. (In/Out)
537 * @param cPairs The number of string / length pairs in the
538 * ellipsis.
539 * @param ... List of string (const char *) and length
540 * (size_t) pairs. The strings will be appended to
541 * the string in the first argument.
542 */
543DECLINLINE(int) RTStrAAppendExN(char **ppsz, size_t cPairs, ...)
544{
545 int rc;
546 va_list va;
547 va_start(va, cPairs);
548 rc = RTStrAAppendExNVTag(ppsz, cPairs, va, RTSTR_TAG);
549 va_end(va);
550 return rc;
551}
552
553/**
554 * Appends one or more strings onto an existing IPRT allocated string (custom
555 * tag).
556 *
557 * This is a very flexible and efficient alternative to using RTStrAPrintf to
558 * combine several strings together.
559 *
560 * @retval VINF_SUCCESS
561 * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
562 * remains unchanged.
563 *
564 * @param ppsz Pointer to the string pointer. The string
565 * pointer must either be NULL or point to a string
566 * returned by an IPRT string API. (In/Out)
567 * @param pszTag Allocation tag used for statistics and such.
568 * @param cPairs The number of string / length pairs in the
569 * ellipsis.
570 * @param ... List of string (const char *) and length
571 * (size_t) pairs. The strings will be appended to
572 * the string in the first argument.
573 */
574DECLINLINE(int) RTStrAAppendExNTag(char **ppsz, const char *pszTag, size_t cPairs, ...)
575{
576 int rc;
577 va_list va;
578 va_start(va, cPairs);
579 rc = RTStrAAppendExNVTag(ppsz, cPairs, va, pszTag);
580 va_end(va);
581 return rc;
582}
583
584/**
585 * Truncates an IPRT allocated string (default tag).
586 *
587 * @retval VINF_SUCCESS.
588 * @retval VERR_OUT_OF_RANGE if cchNew is too long. Nothing is done.
589 *
590 * @param ppsz Pointer to the string pointer. The string
591 * pointer can be NULL if @a cchNew is 0, no change
592 * is made then. If we actually reallocate the
593 * string, the string pointer might be changed by
594 * this call. (In/Out)
595 * @param cchNew The new string length (excluding the
596 * terminator). The string must be at least this
597 * long or we'll return VERR_OUT_OF_RANGE and
598 * assert on you.
599 */
600#define RTStrATruncate(ppsz, cchNew) RTStrATruncateTag((ppsz), (cchNew), RTSTR_TAG)
601
602/**
603 * Truncates an IPRT allocated string.
604 *
605 * @retval VINF_SUCCESS.
606 * @retval VERR_OUT_OF_RANGE if cchNew is too long. Nothing is done.
607 *
608 * @param ppsz Pointer to the string pointer. The string
609 * pointer can be NULL if @a cchNew is 0, no change
610 * is made then. If we actually reallocate the
611 * string, the string pointer might be changed by
612 * this call. (In/Out)
613 * @param cchNew The new string length (excluding the
614 * terminator). The string must be at least this
615 * long or we'll return VERR_OUT_OF_RANGE and
616 * assert on you.
617 * @param pszTag Allocation tag used for statistics and such.
618 */
619RTDECL(int) RTStrATruncateTag(char **ppsz, size_t cchNew, const char *pszTag);
620
621/**
622 * Allocates memory for string storage (default tag).
623 *
624 * You should normally not use this function, except if there is some very
625 * custom string handling you need doing that isn't covered by any of the other
626 * APIs.
627 *
628 * @returns Pointer to the allocated string. The first byte is always set
629 * to the string terminator char, the contents of the remainder of the
630 * memory is undefined. The string must be freed by calling RTStrFree.
631 *
632 * NULL is returned if the allocation failed. Please translate this to
633 * VERR_NO_STR_MEMORY and not VERR_NO_MEMORY. Also consider
634 * RTStrAllocEx if an IPRT status code is required.
635 *
636 * @param cb How many bytes to allocate. If this is zero, we
637 * will allocate a terminator byte anyway.
638 */
639#define RTStrAlloc(cb) RTStrAllocTag((cb), RTSTR_TAG)
640
641/**
642 * Allocates memory for string storage (custom tag).
643 *
644 * You should normally not use this function, except if there is some very
645 * custom string handling you need doing that isn't covered by any of the other
646 * APIs.
647 *
648 * @returns Pointer to the allocated string. The first byte is always set
649 * to the string terminator char, the contents of the remainder of the
650 * memory is undefined. The string must be freed by calling RTStrFree.
651 *
652 * NULL is returned if the allocation failed. Please translate this to
653 * VERR_NO_STR_MEMORY and not VERR_NO_MEMORY. Also consider
654 * RTStrAllocEx if an IPRT status code is required.
655 *
656 * @param cb How many bytes to allocate. If this is zero, we
657 * will allocate a terminator byte anyway.
658 * @param pszTag Allocation tag used for statistics and such.
659 */
660RTDECL(char *) RTStrAllocTag(size_t cb, const char *pszTag);
661
662/**
663 * Allocates memory for string storage, with status code (default tag).
664 *
665 * You should normally not use this function, except if there is some very
666 * custom string handling you need doing that isn't covered by any of the other
667 * APIs.
668 *
669 * @retval VINF_SUCCESS
670 * @retval VERR_NO_STR_MEMORY
671 *
672 * @param ppsz Where to return the allocated string. This will
673 * be set to NULL on failure. On success, the
674 * returned memory will always start with a
675 * terminator char so that it is considered a valid
676 * C string, the contents of rest of the memory is
677 * undefined.
678 * @param cb How many bytes to allocate. If this is zero, we
679 * will allocate a terminator byte anyway.
680 */
681#define RTStrAllocEx(ppsz, cb) RTStrAllocExTag((ppsz), (cb), RTSTR_TAG)
682
683/**
684 * Allocates memory for string storage, with status code (custom tag).
685 *
686 * You should normally not use this function, except if there is some very
687 * custom string handling you need doing that isn't covered by any of the other
688 * APIs.
689 *
690 * @retval VINF_SUCCESS
691 * @retval VERR_NO_STR_MEMORY
692 *
693 * @param ppsz Where to return the allocated string. This will
694 * be set to NULL on failure. On success, the
695 * returned memory will always start with a
696 * terminator char so that it is considered a valid
697 * C string, the contents of rest of the memory is
698 * undefined.
699 * @param cb How many bytes to allocate. If this is zero, we
700 * will allocate a terminator byte anyway.
701 * @param pszTag Allocation tag used for statistics and such.
702 */
703RTDECL(int) RTStrAllocExTag(char **ppsz, size_t cb, const char *pszTag);
704
705/**
706 * Reallocates the specified string (default tag).
707 *
708 * You should normally not have use this function, except perhaps to truncate a
709 * really long string you've got from some IPRT string API, but then you should
710 * use RTStrATruncate.
711 *
712 * @returns VINF_SUCCESS.
713 * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
714 * remains unchanged.
715 *
716 * @param ppsz Pointer to the string variable containing the
717 * input and output string.
718 *
719 * When not freeing the string, the result will
720 * always have the last byte set to the terminator
721 * character so that when used for string
722 * truncation the result will be a valid C string
723 * (your job to keep it a valid UTF-8 string).
724 *
725 * When the input string is NULL and we're supposed
726 * to reallocate, the returned string will also
727 * have the first byte set to the terminator char
728 * so it will be a valid C string.
729 *
730 * @param cbNew When @a cbNew is zero, we'll behave like
731 * RTStrFree and @a *ppsz will be set to NULL.
732 *
733 * When not zero, this will be the new size of the
734 * memory backing the string, i.e. it includes the
735 * terminator char.
736 */
737#define RTStrRealloc(ppsz, cbNew) RTStrReallocTag((ppsz), (cbNew), RTSTR_TAG)
738
739/**
740 * Reallocates the specified string (custom tag).
741 *
742 * You should normally not have use this function, except perhaps to truncate a
743 * really long string you've got from some IPRT string API, but then you should
744 * use RTStrATruncate.
745 *
746 * @returns VINF_SUCCESS.
747 * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
748 * remains unchanged.
749 *
750 * @param ppsz Pointer to the string variable containing the
751 * input and output string.
752 *
753 * When not freeing the string, the result will
754 * always have the last byte set to the terminator
755 * character so that when used for string
756 * truncation the result will be a valid C string
757 * (your job to keep it a valid UTF-8 string).
758 *
759 * When the input string is NULL and we're supposed
760 * to reallocate, the returned string will also
761 * have the first byte set to the terminator char
762 * so it will be a valid C string.
763 *
764 * @param cbNew When @a cbNew is zero, we'll behave like
765 * RTStrFree and @a *ppsz will be set to NULL.
766 *
767 * When not zero, this will be the new size of the
768 * memory backing the string, i.e. it includes the
769 * terminator char.
770 * @param pszTag Allocation tag used for statistics and such.
771 */
772RTDECL(int) RTStrReallocTag(char **ppsz, size_t cbNew, const char *pszTag);
773
774/**
775 * Validates the UTF-8 encoding of the string.
776 *
777 * @returns iprt status code.
778 * @param psz The string.
779 */
780RTDECL(int) RTStrValidateEncoding(const char *psz);
781
782/** @name Flags for RTStrValidateEncodingEx and RTUtf16ValidateEncodingEx
783 * @{
784 */
785/** Check that the string is zero terminated within the given size.
786 * VERR_BUFFER_OVERFLOW will be returned if the check fails. */
787#define RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED RT_BIT_32(0)
788/** Check that the string is exactly the given length.
789 * If it terminates early, VERR_BUFFER_UNDERFLOW will be returned. When used
790 * together with RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED, the given length must
791 * include the terminator or VERR_BUFFER_OVERFLOW will be returned. */
792#define RTSTR_VALIDATE_ENCODING_EXACT_LENGTH RT_BIT_32(1)
793/** @} */
794
795/**
796 * Validates the UTF-8 encoding of the string.
797 *
798 * @returns iprt status code.
799 * @param psz The string.
800 * @param cch The max string length (/ size). Use RTSTR_MAX to
801 * process the entire string.
802 * @param fFlags Combination of RTSTR_VALIDATE_ENCODING_XXX flags.
803 */
804RTDECL(int) RTStrValidateEncodingEx(const char *psz, size_t cch, uint32_t fFlags);
805
806/**
807 * Checks if the UTF-8 encoding is valid.
808 *
809 * @returns true / false.
810 * @param psz The string.
811 */
812RTDECL(bool) RTStrIsValidEncoding(const char *psz);
813
814/**
815 * Purge all bad UTF-8 encoding in the string, replacing it with '?'.
816 *
817 * @returns The number of bad characters (0 if nothing was done).
818 * @param psz The string to purge.
819 */
820RTDECL(size_t) RTStrPurgeEncoding(char *psz);
821
822/**
823 * Sanitizes a (valid) UTF-8 string by replacing all characters outside a white
824 * list in-place by an ASCII replacedment character.
825 *
826 * Multi-byte characters will be replaced byte by byte.
827 *
828 * @returns The number of code points replaced. In the case of an incorrectly
829 * encoded string -1 will be returned, and the string is not completely
830 * processed. In the case of puszValidPairs having an odd number of
831 * code points, -1 will be also return but without any modification to
832 * the string.
833 * @param psz The string to sanitise.
834 * @param puszValidPairs A zero-terminated array of pairs of Unicode points.
835 * Each pair is the start and end point of a range,
836 * and the union of these ranges forms the white list.
837 * @param chReplacement The ASCII replacement character.
838 */
839RTDECL(ssize_t) RTStrPurgeComplementSet(char *psz, PCRTUNICP puszValidPairs, char chReplacement);
840
841/**
842 * Gets the number of code points the string is made up of, excluding
843 * the terminator.
844 *
845 *
846 * @returns Number of code points (RTUNICP).
847 * @returns 0 if the string was incorrectly encoded.
848 * @param psz The string.
849 */
850RTDECL(size_t) RTStrUniLen(const char *psz);
851
852/**
853 * Gets the number of code points the string is made up of, excluding
854 * the terminator.
855 *
856 * This function will validate the string, and incorrectly encoded UTF-8
857 * strings will be rejected.
858 *
859 * @returns iprt status code.
860 * @param psz The string.
861 * @param cch The max string length. Use RTSTR_MAX to process the entire string.
862 * @param pcuc Where to store the code point count.
863 * This is undefined on failure.
864 */
865RTDECL(int) RTStrUniLenEx(const char *psz, size_t cch, size_t *pcuc);
866
867/**
868 * Translate a UTF-8 string into an unicode string (i.e. RTUNICPs), allocating the string buffer.
869 *
870 * @returns iprt status code.
871 * @param pszString UTF-8 string to convert.
872 * @param ppUniString Receives pointer to the allocated unicode string.
873 * The returned string must be freed using RTUniFree().
874 */
875RTDECL(int) RTStrToUni(const char *pszString, PRTUNICP *ppUniString);
876
877/**
878 * Translates pszString from UTF-8 to an array of code points, allocating the result
879 * array if requested.
880 *
881 * @returns iprt status code.
882 * @param pszString UTF-8 string to convert.
883 * @param cchString The maximum size in chars (the type) to convert. The conversion stop
884 * when it reaches cchString or the string terminator ('\\0').
885 * Use RTSTR_MAX to translate the entire string.
886 * @param ppaCps If cCps is non-zero, this must either be pointing to pointer to
887 * a buffer of the specified size, or pointer to a NULL pointer.
888 * If *ppusz is NULL or cCps is zero a buffer of at least cCps items
889 * will be allocated to hold the translated string.
890 * If a buffer was requested it must be freed using RTUtf16Free().
891 * @param cCps The number of code points in the unicode string. This includes the terminator.
892 * @param pcCps Where to store the length of the translated string,
893 * excluding the terminator. (Optional)
894 *
895 * This may be set under some error conditions,
896 * however, only for VERR_BUFFER_OVERFLOW and
897 * VERR_NO_STR_MEMORY will it contain a valid string
898 * length that can be used to resize the buffer.
899 */
900RTDECL(int) RTStrToUniEx(const char *pszString, size_t cchString, PRTUNICP *ppaCps, size_t cCps, size_t *pcCps);
901
902/**
903 * Calculates the length of the string in RTUTF16 items.
904 *
905 * This function will validate the string, and incorrectly encoded UTF-8
906 * strings will be rejected. The primary purpose of this function is to
907 * help allocate buffers for RTStrToUtf16Ex of the correct size. For most
908 * other purposes RTStrCalcUtf16LenEx() should be used.
909 *
910 * @returns Number of RTUTF16 items.
911 * @returns 0 if the string was incorrectly encoded.
912 * @param psz The string.
913 */
914RTDECL(size_t) RTStrCalcUtf16Len(const char *psz);
915
916/**
917 * Calculates the length of the string in RTUTF16 items.
918 *
919 * This function will validate the string, and incorrectly encoded UTF-8
920 * strings will be rejected.
921 *
922 * @returns iprt status code.
923 * @param psz The string.
924 * @param cch The max string length. Use RTSTR_MAX to process the entire string.
925 * @param pcwc Where to store the string length. Optional.
926 * This is undefined on failure.
927 */
928RTDECL(int) RTStrCalcUtf16LenEx(const char *psz, size_t cch, size_t *pcwc);
929
930/**
931 * Translate a UTF-8 string into a UTF-16 allocating the result buffer (default
932 * tag).
933 *
934 * @returns iprt status code.
935 * @param pszString UTF-8 string to convert.
936 * @param ppwszString Receives pointer to the allocated UTF-16 string.
937 * The returned string must be freed using RTUtf16Free().
938 */
939#define RTStrToUtf16(pszString, ppwszString) RTStrToUtf16Tag((pszString), (ppwszString), RTSTR_TAG)
940
941/**
942 * Translate a UTF-8 string into a UTF-16 allocating the result buffer (custom
943 * tag).
944 *
945 * This differs from RTStrToUtf16 in that it always produces a
946 * big-endian string.
947 *
948 * @returns iprt status code.
949 * @param pszString UTF-8 string to convert.
950 * @param ppwszString Receives pointer to the allocated UTF-16 string.
951 * The returned string must be freed using RTUtf16Free().
952 * @param pszTag Allocation tag used for statistics and such.
953 */
954RTDECL(int) RTStrToUtf16Tag(const char *pszString, PRTUTF16 *ppwszString, const char *pszTag);
955
956/**
957 * Translate a UTF-8 string into a UTF-16BE allocating the result buffer
958 * (default tag).
959 *
960 * This differs from RTStrToUtf16Tag in that it always produces a
961 * big-endian string.
962 *
963 * @returns iprt status code.
964 * @param pszString UTF-8 string to convert.
965 * @param ppwszString Receives pointer to the allocated UTF-16BE string.
966 * The returned string must be freed using RTUtf16Free().
967 */
968#define RTStrToUtf16Big(pszString, ppwszString) RTStrToUtf16BigTag((pszString), (ppwszString), RTSTR_TAG)
969
970/**
971 * Translate a UTF-8 string into a UTF-16BE allocating the result buffer (custom
972 * tag).
973 *
974 * @returns iprt status code.
975 * @param pszString UTF-8 string to convert.
976 * @param ppwszString Receives pointer to the allocated UTF-16BE string.
977 * The returned string must be freed using RTUtf16Free().
978 * @param pszTag Allocation tag used for statistics and such.
979 */
980RTDECL(int) RTStrToUtf16BigTag(const char *pszString, PRTUTF16 *ppwszString, const char *pszTag);
981
982/**
983 * Translates pszString from UTF-8 to UTF-16, allocating the result buffer if requested.
984 *
985 * @returns iprt status code.
986 * @param pszString UTF-8 string to convert.
987 * @param cchString The maximum size in chars (the type) to convert. The conversion stop
988 * when it reaches cchString or the string terminator ('\\0').
989 * Use RTSTR_MAX to translate the entire string.
990 * @param ppwsz If cwc is non-zero, this must either be pointing to pointer to
991 * a buffer of the specified size, or pointer to a NULL pointer.
992 * If *ppwsz is NULL or cwc is zero a buffer of at least cwc items
993 * will be allocated to hold the translated string.
994 * If a buffer was requested it must be freed using RTUtf16Free().
995 * @param cwc The buffer size in RTUTF16s. This includes the terminator.
996 * @param pcwc Where to store the length of the translated string,
997 * excluding the terminator. (Optional)
998 *
999 * This may be set under some error conditions,
1000 * however, only for VERR_BUFFER_OVERFLOW and
1001 * VERR_NO_STR_MEMORY will it contain a valid string
1002 * length that can be used to resize the buffer.
1003 */
1004#define RTStrToUtf16Ex(pszString, cchString, ppwsz, cwc, pcwc) \
1005 RTStrToUtf16ExTag((pszString), (cchString), (ppwsz), (cwc), (pcwc), RTSTR_TAG)
1006
1007/**
1008 * Translates pszString from UTF-8 to UTF-16, allocating the result buffer if
1009 * requested (custom tag).
1010 *
1011 * @returns iprt status code.
1012 * @param pszString UTF-8 string to convert.
1013 * @param cchString The maximum size in chars (the type) to convert. The conversion stop
1014 * when it reaches cchString or the string terminator ('\\0').
1015 * Use RTSTR_MAX to translate the entire string.
1016 * @param ppwsz If cwc is non-zero, this must either be pointing to pointer to
1017 * a buffer of the specified size, or pointer to a NULL pointer.
1018 * If *ppwsz is NULL or cwc is zero a buffer of at least cwc items
1019 * will be allocated to hold the translated string.
1020 * If a buffer was requested it must be freed using RTUtf16Free().
1021 * @param cwc The buffer size in RTUTF16s. This includes the terminator.
1022 * @param pcwc Where to store the length of the translated string,
1023 * excluding the terminator. (Optional)
1024 *
1025 * This may be set under some error conditions,
1026 * however, only for VERR_BUFFER_OVERFLOW and
1027 * VERR_NO_STR_MEMORY will it contain a valid string
1028 * length that can be used to resize the buffer.
1029 * @param pszTag Allocation tag used for statistics and such.
1030 */
1031RTDECL(int) RTStrToUtf16ExTag(const char *pszString, size_t cchString,
1032 PRTUTF16 *ppwsz, size_t cwc, size_t *pcwc, const char *pszTag);
1033
1034
1035/**
1036 * Translates pszString from UTF-8 to UTF-16BE, allocating the result buffer if requested.
1037 *
1038 * This differs from RTStrToUtf16Ex in that it always produces a
1039 * big-endian string.
1040 *
1041 * @returns iprt status code.
1042 * @param pszString UTF-8 string to convert.
1043 * @param cchString The maximum size in chars (the type) to convert. The conversion stop
1044 * when it reaches cchString or the string terminator ('\\0').
1045 * Use RTSTR_MAX to translate the entire string.
1046 * @param ppwsz If cwc is non-zero, this must either be pointing to pointer to
1047 * a buffer of the specified size, or pointer to a NULL pointer.
1048 * If *ppwsz is NULL or cwc is zero a buffer of at least cwc items
1049 * will be allocated to hold the translated string.
1050 * If a buffer was requested it must be freed using RTUtf16Free().
1051 * @param cwc The buffer size in RTUTF16s. This includes the terminator.
1052 * @param pcwc Where to store the length of the translated string,
1053 * excluding the terminator. (Optional)
1054 *
1055 * This may be set under some error conditions,
1056 * however, only for VERR_BUFFER_OVERFLOW and
1057 * VERR_NO_STR_MEMORY will it contain a valid string
1058 * length that can be used to resize the buffer.
1059 */
1060#define RTStrToUtf16BigEx(pszString, cchString, ppwsz, cwc, pcwc) \
1061 RTStrToUtf16BigExTag((pszString), (cchString), (ppwsz), (cwc), (pcwc), RTSTR_TAG)
1062
1063/**
1064 * Translates pszString from UTF-8 to UTF-16BE, allocating the result buffer if
1065 * requested (custom tag).
1066 *
1067 * This differs from RTStrToUtf16ExTag in that it always produces a
1068 * big-endian string.
1069 *
1070 * @returns iprt status code.
1071 * @param pszString UTF-8 string to convert.
1072 * @param cchString The maximum size in chars (the type) to convert. The conversion stop
1073 * when it reaches cchString or the string terminator ('\\0').
1074 * Use RTSTR_MAX to translate the entire string.
1075 * @param ppwsz If cwc is non-zero, this must either be pointing to pointer to
1076 * a buffer of the specified size, or pointer to a NULL pointer.
1077 * If *ppwsz is NULL or cwc is zero a buffer of at least cwc items
1078 * will be allocated to hold the translated string.
1079 * If a buffer was requested it must be freed using RTUtf16Free().
1080 * @param cwc The buffer size in RTUTF16s. This includes the terminator.
1081 * @param pcwc Where to store the length of the translated string,
1082 * excluding the terminator. (Optional)
1083 *
1084 * This may be set under some error conditions,
1085 * however, only for VERR_BUFFER_OVERFLOW and
1086 * VERR_NO_STR_MEMORY will it contain a valid string
1087 * length that can be used to resize the buffer.
1088 * @param pszTag Allocation tag used for statistics and such.
1089 */
1090RTDECL(int) RTStrToUtf16BigExTag(const char *pszString, size_t cchString,
1091 PRTUTF16 *ppwsz, size_t cwc, size_t *pcwc, const char *pszTag);
1092
1093
1094/**
1095 * Calculates the length of the string in Latin-1 characters.
1096 *
1097 * This function will validate the string, and incorrectly encoded UTF-8
1098 * strings as well as string with codepoints outside the latin-1 range will be
1099 * rejected. The primary purpose of this function is to help allocate buffers
1100 * for RTStrToLatin1Ex of the correct size. For most other purposes
1101 * RTStrCalcLatin1LenEx() should be used.
1102 *
1103 * @returns Number of Latin-1 characters.
1104 * @returns 0 if the string was incorrectly encoded.
1105 * @param psz The string.
1106 */
1107RTDECL(size_t) RTStrCalcLatin1Len(const char *psz);
1108
1109/**
1110 * Calculates the length of the string in Latin-1 characters.
1111 *
1112 * This function will validate the string, and incorrectly encoded UTF-8
1113 * strings as well as string with codepoints outside the latin-1 range will be
1114 * rejected.
1115 *
1116 * @returns iprt status code.
1117 * @param psz The string.
1118 * @param cch The max string length. Use RTSTR_MAX to process the
1119 * entire string.
1120 * @param pcch Where to store the string length. Optional.
1121 * This is undefined on failure.
1122 */
1123RTDECL(int) RTStrCalcLatin1LenEx(const char *psz, size_t cch, size_t *pcch);
1124
1125/**
1126 * Translate a UTF-8 string into a Latin-1 allocating the result buffer (default
1127 * tag).
1128 *
1129 * @returns iprt status code.
1130 * @param pszString UTF-8 string to convert.
1131 * @param ppszString Receives pointer to the allocated Latin-1 string.
1132 * The returned string must be freed using RTStrFree().
1133 */
1134#define RTStrToLatin1(pszString, ppszString) RTStrToLatin1Tag((pszString), (ppszString), RTSTR_TAG)
1135
1136/**
1137 * Translate a UTF-8 string into a Latin-1 allocating the result buffer (custom
1138 * tag).
1139 *
1140 * @returns iprt status code.
1141 * @param pszString UTF-8 string to convert.
1142 * @param ppszString Receives pointer to the allocated Latin-1 string.
1143 * The returned string must be freed using RTStrFree().
1144 * @param pszTag Allocation tag used for statistics and such.
1145 */
1146RTDECL(int) RTStrToLatin1Tag(const char *pszString, char **ppszString, const char *pszTag);
1147
1148/**
1149 * Translates pszString from UTF-8 to Latin-1, allocating the result buffer if requested.
1150 *
1151 * @returns iprt status code.
1152 * @param pszString UTF-8 string to convert.
1153 * @param cchString The maximum size in chars (the type) to convert.
1154 * The conversion stop when it reaches cchString or
1155 * the string terminator ('\\0'). Use RTSTR_MAX to
1156 * translate the entire string.
1157 * @param ppsz If cch is non-zero, this must either be pointing to
1158 * pointer to a buffer of the specified size, or
1159 * pointer to a NULL pointer. If *ppsz is NULL or cch
1160 * is zero a buffer of at least cch items will be
1161 * allocated to hold the translated string. If a
1162 * buffer was requested it must be freed using
1163 * RTStrFree().
1164 * @param cch The buffer size in bytes. This includes the
1165 * terminator.
1166 * @param pcch Where to store the length of the translated string,
1167 * excluding the terminator. (Optional)
1168 *
1169 * This may be set under some error conditions,
1170 * however, only for VERR_BUFFER_OVERFLOW and
1171 * VERR_NO_STR_MEMORY will it contain a valid string
1172 * length that can be used to resize the buffer.
1173 */
1174#define RTStrToLatin1Ex(pszString, cchString, ppsz, cch, pcch) \
1175 RTStrToLatin1ExTag((pszString), (cchString), (ppsz), (cch), (pcch), RTSTR_TAG)
1176
1177/**
1178 * Translates pszString from UTF-8 to Latin1, allocating the result buffer if
1179 * requested (custom tag).
1180 *
1181 * @returns iprt status code.
1182 * @param pszString UTF-8 string to convert.
1183 * @param cchString The maximum size in chars (the type) to convert.
1184 * The conversion stop when it reaches cchString or
1185 * the string terminator ('\\0'). Use RTSTR_MAX to
1186 * translate the entire string.
1187 * @param ppsz If cch is non-zero, this must either be pointing to
1188 * pointer to a buffer of the specified size, or
1189 * pointer to a NULL pointer. If *ppsz is NULL or cch
1190 * is zero a buffer of at least cch items will be
1191 * allocated to hold the translated string. If a
1192 * buffer was requested it must be freed using
1193 * RTStrFree().
1194 * @param cch The buffer size in bytes. This includes the
1195 * terminator.
1196 * @param pcch Where to store the length of the translated string,
1197 * excluding the terminator. (Optional)
1198 *
1199 * This may be set under some error conditions,
1200 * however, only for VERR_BUFFER_OVERFLOW and
1201 * VERR_NO_STR_MEMORY will it contain a valid string
1202 * length that can be used to resize the buffer.
1203 * @param pszTag Allocation tag used for statistics and such.
1204 */
1205RTDECL(int) RTStrToLatin1ExTag(const char *pszString, size_t cchString, char **ppsz, size_t cch, size_t *pcch, const char *pszTag);
1206
1207/**
1208 * Get the unicode code point at the given string position.
1209 *
1210 * @returns unicode code point.
1211 * @returns RTUNICP_INVALID if the encoding is invalid.
1212 * @param psz The string.
1213 */
1214RTDECL(RTUNICP) RTStrGetCpInternal(const char *psz);
1215
1216/**
1217 * Get the unicode code point at the given string position.
1218 *
1219 * @returns iprt status code
1220 * @returns VERR_INVALID_UTF8_ENCODING if the encoding is invalid.
1221 * @param ppsz The string cursor.
1222 * This is advanced one character forward on failure.
1223 * @param pCp Where to store the unicode code point.
1224 * Stores RTUNICP_INVALID if the encoding is invalid.
1225 */
1226RTDECL(int) RTStrGetCpExInternal(const char **ppsz, PRTUNICP pCp);
1227
1228/**
1229 * Get the unicode code point at the given string position for a string of a
1230 * given length.
1231 *
1232 * @returns iprt status code
1233 * @retval VERR_INVALID_UTF8_ENCODING if the encoding is invalid.
1234 * @retval VERR_END_OF_STRING if *pcch is 0. *pCp is set to RTUNICP_INVALID.
1235 *
1236 * @param ppsz The string.
1237 * @param pcch Pointer to the length of the string. This will be
1238 * decremented by the size of the code point.
1239 * @param pCp Where to store the unicode code point.
1240 * Stores RTUNICP_INVALID if the encoding is invalid.
1241 */
1242RTDECL(int) RTStrGetCpNExInternal(const char **ppsz, size_t *pcch, PRTUNICP pCp);
1243
1244/**
1245 * Put the unicode code point at the given string position
1246 * and return the pointer to the char following it.
1247 *
1248 * This function will not consider anything at or following the
1249 * buffer area pointed to by psz. It is therefore not suitable for
1250 * inserting code points into a string, only appending/overwriting.
1251 *
1252 * @returns pointer to the char following the written code point.
1253 * @param psz The string.
1254 * @param CodePoint The code point to write.
1255 * This should not be RTUNICP_INVALID or any other
1256 * character out of the UTF-8 range.
1257 *
1258 * @remark This is a worker function for RTStrPutCp().
1259 *
1260 */
1261RTDECL(char *) RTStrPutCpInternal(char *psz, RTUNICP CodePoint);
1262
1263/**
1264 * Get the unicode code point at the given string position.
1265 *
1266 * @returns unicode code point.
1267 * @returns RTUNICP_INVALID if the encoding is invalid.
1268 * @param psz The string.
1269 *
1270 * @remark We optimize this operation by using an inline function for
1271 * the most frequent and simplest sequence, the rest is
1272 * handled by RTStrGetCpInternal().
1273 */
1274DECLINLINE(RTUNICP) RTStrGetCp(const char *psz)
1275{
1276 const unsigned char uch = *(const unsigned char *)psz;
1277 if (!(uch & RT_BIT(7)))
1278 return uch;
1279 return RTStrGetCpInternal(psz);
1280}
1281
1282/**
1283 * Get the unicode code point at the given string position.
1284 *
1285 * @returns iprt status code.
1286 * @param ppsz Pointer to the string pointer. This will be updated to
1287 * point to the char following the current code point.
1288 * This is advanced one character forward on failure.
1289 * @param pCp Where to store the code point.
1290 * RTUNICP_INVALID is stored here on failure.
1291 *
1292 * @remark We optimize this operation by using an inline function for
1293 * the most frequent and simplest sequence, the rest is
1294 * handled by RTStrGetCpExInternal().
1295 */
1296DECLINLINE(int) RTStrGetCpEx(const char **ppsz, PRTUNICP pCp)
1297{
1298 const unsigned char uch = **(const unsigned char **)ppsz;
1299 if (!(uch & RT_BIT(7)))
1300 {
1301 (*ppsz)++;
1302 *pCp = uch;
1303 return VINF_SUCCESS;
1304 }
1305 return RTStrGetCpExInternal(ppsz, pCp);
1306}
1307
1308/**
1309 * Get the unicode code point at the given string position for a string of a
1310 * given maximum length.
1311 *
1312 * @returns iprt status code.
1313 * @retval VERR_INVALID_UTF8_ENCODING if the encoding is invalid.
1314 * @retval VERR_END_OF_STRING if *pcch is 0. *pCp is set to RTUNICP_INVALID.
1315 *
1316 * @param ppsz Pointer to the string pointer. This will be updated to
1317 * point to the char following the current code point.
1318 * @param pcch Pointer to the maximum string length. This will be
1319 * decremented by the size of the code point found.
1320 * @param pCp Where to store the code point.
1321 * RTUNICP_INVALID is stored here on failure.
1322 *
1323 * @remark We optimize this operation by using an inline function for
1324 * the most frequent and simplest sequence, the rest is
1325 * handled by RTStrGetCpNExInternal().
1326 */
1327DECLINLINE(int) RTStrGetCpNEx(const char **ppsz, size_t *pcch, PRTUNICP pCp)
1328{
1329 if (RT_LIKELY(*pcch != 0))
1330 {
1331 const unsigned char uch = **(const unsigned char **)ppsz;
1332 if (!(uch & RT_BIT(7)))
1333 {
1334 (*ppsz)++;
1335 (*pcch)--;
1336 *pCp = uch;
1337 return VINF_SUCCESS;
1338 }
1339 }
1340 return RTStrGetCpNExInternal(ppsz, pcch, pCp);
1341}
1342
1343/**
1344 * Get the UTF-8 size in characters of a given Unicode code point.
1345 *
1346 * The code point is expected to be a valid Unicode one, but not necessarily in
1347 * the range supported by UTF-8.
1348 *
1349 * @returns The number of chars (bytes) required to encode the code point, or
1350 * zero if there is no UTF-8 encoding.
1351 * @param CodePoint The unicode code point.
1352 */
1353DECLINLINE(size_t) RTStrCpSize(RTUNICP CodePoint)
1354{
1355 if (CodePoint < 0x00000080)
1356 return 1;
1357 if (CodePoint < 0x00000800)
1358 return 2;
1359 if (CodePoint < 0x00010000)
1360 return 3;
1361#ifdef RT_USE_RTC_3629
1362 if (CodePoint < 0x00011000)
1363 return 4;
1364#else
1365 if (CodePoint < 0x00200000)
1366 return 4;
1367 if (CodePoint < 0x04000000)
1368 return 5;
1369 if (CodePoint < 0x7fffffff)
1370 return 6;
1371#endif
1372 return 0;
1373}
1374
1375/**
1376 * Put the unicode code point at the given string position
1377 * and return the pointer to the char following it.
1378 *
1379 * This function will not consider anything at or following the
1380 * buffer area pointed to by psz. It is therefore not suitable for
1381 * inserting code points into a string, only appending/overwriting.
1382 *
1383 * @returns pointer to the char following the written code point.
1384 * @param psz The string.
1385 * @param CodePoint The code point to write.
1386 * This should not be RTUNICP_INVALID or any other
1387 * character out of the UTF-8 range.
1388 *
1389 * @remark We optimize this operation by using an inline function for
1390 * the most frequent and simplest sequence, the rest is
1391 * handled by RTStrPutCpInternal().
1392 */
1393DECLINLINE(char *) RTStrPutCp(char *psz, RTUNICP CodePoint)
1394{
1395 if (CodePoint < 0x80)
1396 {
1397 *psz++ = (char)CodePoint;
1398 return psz;
1399 }
1400 return RTStrPutCpInternal(psz, CodePoint);
1401}
1402
1403/**
1404 * Skips ahead, past the current code point.
1405 *
1406 * @returns Pointer to the char after the current code point.
1407 * @param psz Pointer to the current code point.
1408 * @remark This will not move the next valid code point, only past the current one.
1409 */
1410DECLINLINE(char *) RTStrNextCp(const char *psz)
1411{
1412 RTUNICP Cp;
1413 RTStrGetCpEx(&psz, &Cp);
1414 return (char *)psz;
1415}
1416
1417/**
1418 * Skips back to the previous code point.
1419 *
1420 * @returns Pointer to the char before the current code point.
1421 * @returns pszStart on failure.
1422 * @param pszStart Pointer to the start of the string.
1423 * @param psz Pointer to the current code point.
1424 */
1425RTDECL(char *) RTStrPrevCp(const char *pszStart, const char *psz);
1426
1427
1428/** @page pg_rt_str_format The IPRT Format Strings
1429 *
1430 * IPRT implements most of the commonly used format types and flags with the
1431 * exception of floating point which is completely missing. In addition IPRT
1432 * provides a number of IPRT specific format types for the IPRT typedefs and
1433 * other useful things. Note that several of these extensions are similar to
1434 * \%p and doesn't care much if you try add formating flags/width/precision.
1435 *
1436 *
1437 * Group 0a, The commonly used format types:
1438 * - \%s - Takes a pointer to a zero terminated string (UTF-8) and
1439 * prints it with the optionally adjustment (width, -) and
1440 * length restriction (precision).
1441 * - \%ls - Same as \%s except that the input is UTF-16 (output UTF-8).
1442 * - \%Ls - Same as \%s except that the input is UCS-32 (output UTF-8).
1443 * - \%S - Same as \%s, used to convert to current codeset but this is
1444 * now done by the streams code. Deprecated, use \%s.
1445 * - \%lS - Ditto. Deprecated, use \%ls.
1446 * - \%LS - Ditto. Deprecated, use \%Ls.
1447 * - \%c - Takes a char and prints it.
1448 * - \%d - Takes a signed integer and prints it as decimal. Thousand
1449 * separator (\'), zero padding (0), adjustment (-+), width,
1450 * precision
1451 * - \%i - Same as \%d.
1452 * - \%u - Takes an unsigned integer and prints it as decimal. Thousand
1453 * separator (\'), zero padding (0), adjustment (-+), width,
1454 * precision
1455 * - \%x - Takes an unsigned integer and prints it as lowercased
1456 * hexadecimal. The special hash (\#) flag causes a '0x'
1457 * prefixed to be printed. Zero padding (0), adjustment (-+),
1458 * width, precision.
1459 * - \%X - Same as \%x except that it is uppercased.
1460 * - \%o - Takes an unsigned (?) integer and prints it as octal. Zero
1461 * padding (0), adjustment (-+), width, precision.
1462 * - \%p - Takes a pointer (void technically) and prints it. Zero
1463 * padding (0), adjustment (-+), width, precision.
1464 *
1465 * The \%d, \%i, \%u, \%x, \%X and \%o format types support the following
1466 * argument type specifiers:
1467 * - \%ll - long long (uint64_t).
1468 * - \%L - long long (uint64_t).
1469 * - \%l - long (uint32_t, uint64_t)
1470 * - \%h - short (int16_t).
1471 * - \%hh - char (int8_t).
1472 * - \%H - char (int8_t).
1473 * - \%z - size_t.
1474 * - \%j - intmax_t (int64_t).
1475 * - \%t - ptrdiff_t.
1476 * The type in parentheses is typical sizes, however when printing those types
1477 * you are better off using the special group 2 format types below (\%RX32 and
1478 * such).
1479 *
1480 *
1481 * Group 0b, IPRT format tricks:
1482 * - %M - Replaces the format string, takes a string pointer.
1483 * - %N - Nested formatting, takes a pointer to a format string
1484 * followed by the pointer to a va_list variable. The va_list
1485 * variable will not be modified and the caller must do va_end()
1486 * on it. Make sure the va_list variable is NOT in a parameter
1487 * list or some gcc versions/targets may get it all wrong.
1488 *
1489 *
1490 * Group 1, the basic runtime typedefs (excluding those which obviously are
1491 * pointer):
1492 * - \%RTbool - Takes a bool value and prints 'true', 'false', or '!%d!'.
1493 * - \%RTeic - Takes a #PCRTERRINFO value outputting 'rc: msg',
1494 * or 'rc - msg' with the \# flag.
1495 * - \%RTeim - Takes a #PCRTERRINFO value outputting ': msg', or
1496 * ' - msg' with the \# flag.
1497 * - \%RTfile - Takes a #RTFILE value.
1498 * - \%RTfmode - Takes a #RTFMODE value.
1499 * - \%RTfoff - Takes a #RTFOFF value.
1500 * - \%RTfp16 - Takes a #RTFAR16 value.
1501 * - \%RTfp32 - Takes a #RTFAR32 value.
1502 * - \%RTfp64 - Takes a #RTFAR64 value.
1503 * - \%RTgid - Takes a #RTGID value.
1504 * - \%RTino - Takes a #RTINODE value.
1505 * - \%RTint - Takes a #RTINT value.
1506 * - \%RTiop - Takes a #RTIOPORT value.
1507 * - \%RTldrm - Takes a #RTLDRMOD value.
1508 * - \%RTmac - Takes a #PCRTMAC pointer.
1509 * - \%RTnaddr - Takes a #PCRTNETADDR value.
1510 * - \%RTnaipv4 - Takes a #RTNETADDRIPV4 value.
1511 * - \%RTnaipv6 - Takes a #PCRTNETADDRIPV6 value.
1512 * - \%RTnthrd - Takes a #RTNATIVETHREAD value.
1513 * - \%RTnthrd - Takes a #RTNATIVETHREAD value.
1514 * - \%RTproc - Takes a #RTPROCESS value.
1515 * - \%RTptr - Takes a #RTINTPTR or #RTUINTPTR value (but not void *).
1516 * - \%RTreg - Takes a #RTCCUINTREG value.
1517 * - \%RTsel - Takes a #RTSEL value.
1518 * - \%RTsem - Takes a #RTSEMEVENT, #RTSEMEVENTMULTI, #RTSEMMUTEX, #RTSEMFASTMUTEX, or #RTSEMRW value.
1519 * - \%RTsock - Takes a #RTSOCKET value.
1520 * - \%RTthrd - Takes a #RTTHREAD value.
1521 * - \%RTuid - Takes a #RTUID value.
1522 * - \%RTuint - Takes a #RTUINT value.
1523 * - \%RTunicp - Takes a #RTUNICP value.
1524 * - \%RTutf16 - Takes a #RTUTF16 value.
1525 * - \%RTuuid - Takes a #PCRTUUID and will print the UUID as a string.
1526 * - \%RTxuint - Takes a #RTUINT or #RTINT value, formatting it as hex.
1527 * - \%RGi - Takes a #RTGCINT value.
1528 * - \%RGp - Takes a #RTGCPHYS value.
1529 * - \%RGr - Takes a #RTGCUINTREG value.
1530 * - \%RGu - Takes a #RTGCUINT value.
1531 * - \%RGv - Takes a #RTGCPTR, #RTGCINTPTR or #RTGCUINTPTR value.
1532 * - \%RGx - Takes a #RTGCUINT or #RTGCINT value, formatting it as hex.
1533 * - \%RHi - Takes a #RTHCINT value.
1534 * - \%RHp - Takes a #RTHCPHYS value.
1535 * - \%RHr - Takes a #RTHCUINTREG value.
1536 * - \%RHu - Takes a #RTHCUINT value.
1537 * - \%RHv - Takes a #RTHCPTR, #RTHCINTPTR or #RTHCUINTPTR value.
1538 * - \%RHx - Takes a #RTHCUINT or #RTHCINT value, formatting it as hex.
1539 * - \%RRv - Takes a #RTRCPTR, #RTRCINTPTR or #RTRCUINTPTR value.
1540 * - \%RCi - Takes a #RTINT value.
1541 * - \%RCp - Takes a #RTCCPHYS value.
1542 * - \%RCr - Takes a #RTCCUINTREG value.
1543 * - \%RCu - Takes a #RTUINT value.
1544 * - \%RCv - Takes a #uintptr_t, #intptr_t, void * value.
1545 * - \%RCx - Takes a #RTUINT or #RTINT value, formatting it as hex.
1546 *
1547 *
1548 * Group 2, the generic integer types which are prefered over relying on what
1549 * bit-count a 'long', 'short', or 'long long' has on a platform. This are
1550 * highly prefered for the [u]intXX_t kind of types:
1551 * - \%RI[8|16|32|64] - Signed integer value of the specifed bit count.
1552 * - \%RU[8|16|32|64] - Unsigned integer value of the specifed bit count.
1553 * - \%RX[8|16|32|64] - Hexadecimal integer value of the specifed bit count.
1554 *
1555 *
1556 * Group 3, hex dumpers and other complex stuff which requires more than simple
1557 * formatting:
1558 * - \%Rhxd - Takes a pointer to the memory which is to be dumped in typical
1559 * hex format. Use the precision to specify the length, and the width to
1560 * set the number of bytes per line. Default width and precision is 16.
1561 * - \%RhxD - Same as \%Rhxd, except that it skips duplicate lines.
1562 * - \%Rhxs - Takes a pointer to the memory to be displayed as a hex string,
1563 * i.e. a series of space separated bytes formatted as two digit hex value.
1564 * Use the precision to specify the length. Default length is 16 bytes.
1565 * The width, if specified, is ignored.
1566 * The space separtor can get change to a colon by
1567 * using the ' flag, and removed entirely using \#.
1568 * - \%RhXd - Same as \%Rhxd, but takes an additional uint64_t
1569 * value with the memory start address/offset after
1570 * the memory pointer.
1571 * - \%RhXD - Same as \%RhxD, but takes an additional uint64_t
1572 * value with the memory start address/offset after
1573 * the memory pointer.
1574 * - \%RhXs - Same as \%Rhxs, but takes an additional uint64_t
1575 * value with the memory start address/offset after
1576 * the memory pointer.
1577 *
1578 * - \%Rhcb - Human readable byte size formatting, using
1579 * binary unit prefixes (GiB, MiB and such). Takes a
1580 * 64-bit unsigned integer as input. Does one
1581 * decimal point by default, can do 0-3 via precision
1582 * field. No rounding when calculating fraction.
1583 * The space flag add a space between the value and
1584 * unit.
1585 * - \%RhcB - Same a \%Rhcb only the 'i' is skipped in the unit.
1586 * - \%Rhci - SI variant of \%Rhcb, fraction is rounded.
1587 * - \%Rhub - Human readable number formatting, using
1588 * binary unit prefixes. Takes a 64-bit unsigned
1589 * integer as input. Does one decimal point by
1590 * default, can do 0-3 via precision field. No
1591 * rounding when calculating fraction. The space
1592 * flag add a space between the value and unit.
1593 * - \%RhuB - Same a \%Rhub only the 'i' is skipped in the unit.
1594 * - \%Rhui - SI variant of \%Rhub, fraction is rounded.
1595 *
1596 * - \%Rrc - Takes an integer iprt status code as argument. Will insert the
1597 * status code define corresponding to the iprt status code.
1598 * - \%Rrs - Takes an integer iprt status code as argument. Will insert the
1599 * short description of the specified status code.
1600 * - \%Rrf - Takes an integer iprt status code as argument. Will insert the
1601 * full description of the specified status code.
1602 * Note! Works like \%Rrs when IN_RT_STATIC is defined (so please avoid).
1603 * - \%Rra - Takes an integer iprt status code as argument. Will insert the
1604 * status code define + full description.
1605 * Note! Reduced output when IN_RT_STATIC is defined (so please avoid).
1606 * - \%Rwc - Takes a long Windows error code as argument. Will insert the status
1607 * code define corresponding to the Windows error code.
1608 * - \%Rwf - Takes a long Windows error code as argument. Will insert the
1609 * full description of the specified status code.
1610 * Note! Works like \%Rwc when IN_RT_STATIC is defined.
1611 * - \%Rwa - Takes a long Windows error code as argument. Will insert the
1612 * error code define + full description.
1613 * Note! Reduced output when IN_RT_STATIC is defined (so please avoid).
1614 *
1615 * - \%Rhrc - Takes a COM/XPCOM status code as argument. Will insert the status
1616 * code define corresponding to the Windows error code.
1617 * - \%Rhrf - Takes a COM/XPCOM status code as argument. Will insert the
1618 * full description of the specified status code.
1619 * Note! Works like \%Rhrc when IN_RT_STATIC is
1620 * defined on Windows (so please avoid).
1621 * - \%Rhra - Takes a COM/XPCOM error code as argument. Will insert the
1622 * error code define + full description.
1623 * Note! Reduced output when IN_RT_STATIC is defined on Windows (so please avoid).
1624 *
1625 * - \%Rfn - Pretty printing of a function or method. It drops the
1626 * return code and parameter list.
1627 * - \%Rbn - Prints the base name. For dropping the path in
1628 * order to save space when printing a path name.
1629 *
1630 * - \%lRbs - Same as \%ls except inlut is big endian UTF-16.
1631 *
1632 * On other platforms, \%Rw? simply prints the argument in a form of 0xXXXXXXXX.
1633 *
1634 *
1635 * Group 4, structure dumpers:
1636 * - \%RDtimespec - Takes a PCRTTIMESPEC.
1637 *
1638 *
1639 * Group 5, XML / HTML, JSON and URI escapers:
1640 * - \%RMas - Takes a string pointer (const char *) and outputs
1641 * it as an attribute value with the proper escaping.
1642 * This typically ends up in double quotes.
1643 *
1644 * - \%RMes - Takes a string pointer (const char *) and outputs
1645 * it as an element with the necessary escaping.
1646 *
1647 * - \%RMjs - Takes a string pointer (const char *) and outputs
1648 * it in quotes with proper JSON escaping.
1649 *
1650 * - \%RMpa - Takes a string pointer (const char *) and outputs
1651 * it percent-encoded (RFC-3986). All reserved characters
1652 * are encoded.
1653 *
1654 * - \%RMpf - Takes a string pointer (const char *) and outputs
1655 * it percent-encoded (RFC-3986), form style. This
1656 * means '+' is used to escape space (' ') and '%2B'
1657 * is used to escape '+'.
1658 *
1659 * - \%RMpp - Takes a string pointer (const char *) and outputs
1660 * it percent-encoded (RFC-3986), path style. This
1661 * means '/' will not be escaped.
1662 *
1663 * - \%RMpq - Takes a string pointer (const char *) and outputs
1664 * it percent-encoded (RFC-3986), query style. This
1665 * means '+' will not be escaped.
1666 *
1667 *
1668 * Group 6, CPU Architecture Register dumpers:
1669 * - \%RAx86[reg] - Takes a 64-bit register value if the register is
1670 * 64-bit or smaller. Check the code wrt which
1671 * registers are implemented.
1672 *
1673 */
1674
1675#ifndef DECLARED_FNRTSTROUTPUT /* duplicated in iprt/log.h & errcore.h */
1676# define DECLARED_FNRTSTROUTPUT
1677/**
1678 * Output callback.
1679 *
1680 * @returns number of bytes written.
1681 * @param pvArg User argument.
1682 * @param pachChars Pointer to an array of utf-8 characters.
1683 * @param cbChars Number of bytes in the character array pointed to by pachChars.
1684 */
1685typedef DECLCALLBACKTYPE(size_t, FNRTSTROUTPUT,(void *pvArg, const char *pachChars, size_t cbChars));
1686/** Pointer to callback function. */
1687typedef FNRTSTROUTPUT *PFNRTSTROUTPUT;
1688#endif
1689
1690/** @name Format flag.
1691 * These are used by RTStrFormat extensions and RTStrFormatNumber, mind
1692 * that not all flags makes sense to both of the functions.
1693 * @{ */
1694#define RTSTR_F_CAPITAL 0x0001
1695#define RTSTR_F_LEFT 0x0002
1696#define RTSTR_F_ZEROPAD 0x0004
1697#define RTSTR_F_SPECIAL 0x0008
1698#define RTSTR_F_VALSIGNED 0x0010
1699#define RTSTR_F_PLUS 0x0020
1700#define RTSTR_F_BLANK 0x0040
1701#define RTSTR_F_WIDTH 0x0080
1702#define RTSTR_F_PRECISION 0x0100
1703#define RTSTR_F_THOUSAND_SEP 0x0200
1704#define RTSTR_F_OBFUSCATE_PTR 0x0400
1705
1706#define RTSTR_F_BIT_MASK 0xf800
1707#define RTSTR_F_8BIT 0x0800
1708#define RTSTR_F_16BIT 0x1000
1709#define RTSTR_F_32BIT 0x2000
1710#define RTSTR_F_64BIT 0x4000
1711#define RTSTR_F_128BIT 0x8000
1712/** @} */
1713
1714/** @def RTSTR_GET_BIT_FLAG
1715 * Gets the bit flag for the specified type.
1716 */
1717#define RTSTR_GET_BIT_FLAG(type) \
1718 ( sizeof(type) * 8 == 32 ? RTSTR_F_32BIT \
1719 : sizeof(type) * 8 == 64 ? RTSTR_F_64BIT \
1720 : sizeof(type) * 8 == 16 ? RTSTR_F_16BIT \
1721 : sizeof(type) * 8 == 8 ? RTSTR_F_8BIT \
1722 : sizeof(type) * 8 == 128 ? RTSTR_F_128BIT \
1723 : 0)
1724
1725
1726/**
1727 * Callback to format non-standard format specifiers.
1728 *
1729 * @returns The number of bytes formatted.
1730 * @param pvArg Formatter argument.
1731 * @param pfnOutput Pointer to output function.
1732 * @param pvArgOutput Argument for the output function.
1733 * @param ppszFormat Pointer to the format string pointer. Advance this till the char
1734 * after the format specifier.
1735 * @param pArgs Pointer to the argument list. Use this to fetch the arguments.
1736 * @param cchWidth Format Width. -1 if not specified.
1737 * @param cchPrecision Format Precision. -1 if not specified.
1738 * @param fFlags Flags (RTSTR_NTFS_*).
1739 * @param chArgSize The argument size specifier, 'l' or 'L'.
1740 */
1741typedef DECLCALLBACKTYPE(size_t, FNSTRFORMAT,(void *pvArg, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
1742 const char **ppszFormat, va_list *pArgs, int cchWidth,
1743 int cchPrecision, unsigned fFlags, char chArgSize));
1744/** Pointer to a FNSTRFORMAT() function. */
1745typedef FNSTRFORMAT *PFNSTRFORMAT;
1746
1747
1748/**
1749 * Partial implementation of a printf like formatter.
1750 * It doesn't do everything correct, and there is no floating point support.
1751 * However, it supports custom formats by the means of a format callback.
1752 *
1753 * @returns number of bytes formatted.
1754 * @param pfnOutput Output worker.
1755 * Called in two ways. Normally with a string and its length.
1756 * For termination, it's called with NULL for string, 0 for length.
1757 * @param pvArgOutput Argument to the output worker.
1758 * @param pfnFormat Custom format worker.
1759 * @param pvArgFormat Argument to the format worker.
1760 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1761 * @param InArgs Argument list.
1762 */
1763RTDECL(size_t) RTStrFormatV(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat,
1764 const char *pszFormat, va_list InArgs) RT_IPRT_FORMAT_ATTR(5, 0);
1765
1766/**
1767 * Partial implementation of a printf like formatter.
1768 *
1769 * It doesn't do everything correct, and there is no floating point support.
1770 * However, it supports custom formats by the means of a format callback.
1771 *
1772 * @returns number of bytes formatted.
1773 * @param pfnOutput Output worker.
1774 * Called in two ways. Normally with a string and its length.
1775 * For termination, it's called with NULL for string, 0 for length.
1776 * @param pvArgOutput Argument to the output worker.
1777 * @param pfnFormat Custom format worker.
1778 * @param pvArgFormat Argument to the format worker.
1779 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1780 * @param ... Argument list.
1781 */
1782RTDECL(size_t) RTStrFormat(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat,
1783 const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(5, 6);
1784
1785/**
1786 * Formats an integer number according to the parameters.
1787 *
1788 * @returns Length of the formatted number.
1789 * @param psz Pointer to output string buffer of sufficient size.
1790 * @param u64Value Value to format.
1791 * @param uiBase Number representation base.
1792 * @param cchWidth Width.
1793 * @param cchPrecision Precision.
1794 * @param fFlags Flags, RTSTR_F_XXX.
1795 */
1796RTDECL(int) RTStrFormatNumber(char *psz, uint64_t u64Value, unsigned int uiBase, signed int cchWidth, signed int cchPrecision,
1797 unsigned int fFlags);
1798
1799/**
1800 * Formats an unsigned 8-bit number.
1801 *
1802 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1803 * @param pszBuf The output buffer.
1804 * @param cbBuf The size of the output buffer.
1805 * @param u8Value The value to format.
1806 * @param uiBase Number representation base.
1807 * @param cchWidth Width.
1808 * @param cchPrecision Precision.
1809 * @param fFlags Flags, RTSTR_F_XXX.
1810 */
1811RTDECL(ssize_t) RTStrFormatU8(char *pszBuf, size_t cbBuf, uint8_t u8Value, unsigned int uiBase,
1812 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1813
1814/**
1815 * Formats an unsigned 16-bit number.
1816 *
1817 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1818 * @param pszBuf The output buffer.
1819 * @param cbBuf The size of the output buffer.
1820 * @param u16Value The value to format.
1821 * @param uiBase Number representation base.
1822 * @param cchWidth Width.
1823 * @param cchPrecision Precision.
1824 * @param fFlags Flags, RTSTR_F_XXX.
1825 */
1826RTDECL(ssize_t) RTStrFormatU16(char *pszBuf, size_t cbBuf, uint16_t u16Value, unsigned int uiBase,
1827 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1828
1829/**
1830 * Formats an unsigned 32-bit number.
1831 *
1832 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1833 * @param pszBuf The output buffer.
1834 * @param cbBuf The size of the output buffer.
1835 * @param u32Value The value to format.
1836 * @param uiBase Number representation base.
1837 * @param cchWidth Width.
1838 * @param cchPrecision Precision.
1839 * @param fFlags Flags, RTSTR_F_XXX.
1840 */
1841RTDECL(ssize_t) RTStrFormatU32(char *pszBuf, size_t cbBuf, uint32_t u32Value, unsigned int uiBase,
1842 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1843
1844/**
1845 * Formats an unsigned 64-bit number.
1846 *
1847 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1848 * @param pszBuf The output buffer.
1849 * @param cbBuf The size of the output buffer.
1850 * @param u64Value The value to format.
1851 * @param uiBase Number representation base.
1852 * @param cchWidth Width.
1853 * @param cchPrecision Precision.
1854 * @param fFlags Flags, RTSTR_F_XXX.
1855 */
1856RTDECL(ssize_t) RTStrFormatU64(char *pszBuf, size_t cbBuf, uint64_t u64Value, unsigned int uiBase,
1857 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1858
1859/**
1860 * Formats an unsigned 128-bit number.
1861 *
1862 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1863 * @param pszBuf The output buffer.
1864 * @param cbBuf The size of the output buffer.
1865 * @param pu128Value The value to format.
1866 * @param uiBase Number representation base.
1867 * @param cchWidth Width.
1868 * @param cchPrecision Precision.
1869 * @param fFlags Flags, RTSTR_F_XXX.
1870 * @remarks The current implementation is limited to base 16 and doesn't do
1871 * width or precision and probably ignores few flags too.
1872 */
1873RTDECL(ssize_t) RTStrFormatU128(char *pszBuf, size_t cbBuf, PCRTUINT128U pu128Value, unsigned int uiBase,
1874 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1875
1876/**
1877 * Formats an unsigned 256-bit number.
1878 *
1879 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1880 * @param pszBuf The output buffer.
1881 * @param cbBuf The size of the output buffer.
1882 * @param pu256Value The value to format.
1883 * @param uiBase Number representation base.
1884 * @param cchWidth Width.
1885 * @param cchPrecision Precision.
1886 * @param fFlags Flags, RTSTR_F_XXX.
1887 * @remarks The current implementation is limited to base 16 and doesn't do
1888 * width or precision and probably ignores few flags too.
1889 */
1890RTDECL(ssize_t) RTStrFormatU256(char *pszBuf, size_t cbBuf, PCRTUINT256U pu256Value, unsigned int uiBase,
1891 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1892
1893/**
1894 * Formats an unsigned 512-bit number.
1895 *
1896 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1897 * @param pszBuf The output buffer.
1898 * @param cbBuf The size of the output buffer.
1899 * @param pu512Value The value to format.
1900 * @param uiBase Number representation base.
1901 * @param cchWidth Width.
1902 * @param cchPrecision Precision.
1903 * @param fFlags Flags, RTSTR_F_XXX.
1904 * @remarks The current implementation is limited to base 16 and doesn't do
1905 * width or precision and probably ignores few flags too.
1906 */
1907RTDECL(ssize_t) RTStrFormatU512(char *pszBuf, size_t cbBuf, PCRTUINT512U pu512Value, unsigned int uiBase,
1908 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1909
1910
1911/**
1912 * Formats an 80-bit extended floating point number.
1913 *
1914 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1915 * @param pszBuf The output buffer.
1916 * @param cbBuf The size of the output buffer.
1917 * @param pr80Value The value to format.
1918 * @param cchWidth Width.
1919 * @param cchPrecision Precision.
1920 * @param fFlags Flags, RTSTR_F_XXX.
1921 */
1922RTDECL(ssize_t) RTStrFormatR80(char *pszBuf, size_t cbBuf, PCRTFLOAT80U pr80Value, signed int cchWidth,
1923 signed int cchPrecision, uint32_t fFlags);
1924
1925/**
1926 * Formats an 80-bit extended floating point number, version 2.
1927 *
1928 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1929 * @param pszBuf The output buffer.
1930 * @param cbBuf The size of the output buffer.
1931 * @param pr80Value The value to format.
1932 * @param cchWidth Width.
1933 * @param cchPrecision Precision.
1934 * @param fFlags Flags, RTSTR_F_XXX.
1935 */
1936RTDECL(ssize_t) RTStrFormatR80u2(char *pszBuf, size_t cbBuf, PCRTFLOAT80U2 pr80Value, signed int cchWidth,
1937 signed int cchPrecision, uint32_t fFlags);
1938
1939
1940
1941/**
1942 * Callback for formatting a type.
1943 *
1944 * This is registered using the RTStrFormatTypeRegister function and will
1945 * be called during string formatting to handle the specified %R[type].
1946 * The argument for this format type is assumed to be a pointer and it's
1947 * passed in the @a pvValue argument.
1948 *
1949 * @returns Length of the formatted output.
1950 * @param pfnOutput Output worker.
1951 * @param pvArgOutput Argument to the output worker.
1952 * @param pszType The type name.
1953 * @param pvValue The argument value.
1954 * @param cchWidth Width.
1955 * @param cchPrecision Precision.
1956 * @param fFlags Flags (NTFS_*).
1957 * @param pvUser The user argument.
1958 */
1959typedef DECLCALLBACKTYPE(size_t, FNRTSTRFORMATTYPE,(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
1960 const char *pszType, void const *pvValue,
1961 int cchWidth, int cchPrecision, unsigned fFlags,
1962 void *pvUser));
1963/** Pointer to a FNRTSTRFORMATTYPE. */
1964typedef FNRTSTRFORMATTYPE *PFNRTSTRFORMATTYPE;
1965
1966
1967/**
1968 * Register a format handler for a type.
1969 *
1970 * The format handler is used to handle '%R[type]' format types, where the argument
1971 * in the vector is a pointer value (a bit restrictive, but keeps it simple).
1972 *
1973 * The caller must ensure that no other thread will be making use of any of
1974 * the dynamic formatting type facilities simultaneously with this call.
1975 *
1976 * @returns IPRT status code.
1977 * @retval VINF_SUCCESS on success.
1978 * @retval VERR_ALREADY_EXISTS if the type has already been registered.
1979 * @retval VERR_TOO_MANY_OPEN_FILES if all the type slots has been allocated already.
1980 *
1981 * @param pszType The type name.
1982 * @param pfnHandler The handler address. See FNRTSTRFORMATTYPE for details.
1983 * @param pvUser The user argument to pass to the handler. See RTStrFormatTypeSetUser
1984 * for how to update this later.
1985 */
1986RTDECL(int) RTStrFormatTypeRegister(const char *pszType, PFNRTSTRFORMATTYPE pfnHandler, void *pvUser);
1987
1988/**
1989 * Deregisters a format type.
1990 *
1991 * The caller must ensure that no other thread will be making use of any of
1992 * the dynamic formatting type facilities simultaneously with this call.
1993 *
1994 * @returns IPRT status code.
1995 * @retval VINF_SUCCESS on success.
1996 * @retval VERR_FILE_NOT_FOUND if not found.
1997 *
1998 * @param pszType The type to deregister.
1999 */
2000RTDECL(int) RTStrFormatTypeDeregister(const char *pszType);
2001
2002/**
2003 * Sets the user argument for a type.
2004 *
2005 * This can be used if a user argument needs relocating in GC.
2006 *
2007 * @returns IPRT status code.
2008 * @retval VINF_SUCCESS on success.
2009 * @retval VERR_FILE_NOT_FOUND if not found.
2010 *
2011 * @param pszType The type to update.
2012 * @param pvUser The new user argument value.
2013 */
2014RTDECL(int) RTStrFormatTypeSetUser(const char *pszType, void *pvUser);
2015
2016
2017/**
2018 * String printf.
2019 *
2020 * @returns The length of the returned string (in pszBuffer) excluding the
2021 * terminator.
2022 * @param pszBuffer Output buffer.
2023 * @param cchBuffer Size of the output buffer.
2024 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2025 * @param args The format argument.
2026 *
2027 * @deprecated Use RTStrPrintf2V! Problematic return value on overflow.
2028 */
2029RTDECL(size_t) RTStrPrintfV(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(3, 0);
2030
2031/**
2032 * String printf.
2033 *
2034 * @returns The length of the returned string (in pszBuffer) excluding the
2035 * terminator.
2036 * @param pszBuffer Output buffer.
2037 * @param cchBuffer Size of the output buffer.
2038 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2039 * @param ... The format argument.
2040 *
2041 * @deprecated Use RTStrPrintf2! Problematic return value on overflow.
2042 */
2043RTDECL(size_t) RTStrPrintf(char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
2044
2045/**
2046 * String printf with custom formatting.
2047 *
2048 * @returns The length of the returned string (in pszBuffer) excluding the
2049 * terminator.
2050 * @param pfnFormat Pointer to handler function for the custom formats.
2051 * @param pvArg Argument to the pfnFormat function.
2052 * @param pszBuffer Output buffer.
2053 * @param cchBuffer Size of the output buffer.
2054 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2055 * @param args The format argument.
2056 *
2057 * @deprecated Use RTStrPrintf2ExV! Problematic return value on overflow.
2058 */
2059RTDECL(size_t) RTStrPrintfExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer,
2060 const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(5, 0);
2061
2062/**
2063 * String printf with custom formatting.
2064 *
2065 * @returns The length of the returned string (in pszBuffer) excluding the
2066 * terminator.
2067 * @param pfnFormat Pointer to handler function for the custom formats.
2068 * @param pvArg Argument to the pfnFormat function.
2069 * @param pszBuffer Output buffer.
2070 * @param cchBuffer Size of the output buffer.
2071 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2072 * @param ... The format argument.
2073 *
2074 * @deprecated Use RTStrPrintf2Ex! Problematic return value on overflow.
2075 */
2076RTDECL(size_t) RTStrPrintfEx(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer,
2077 const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(5, 6);
2078
2079/**
2080 * String printf, version 2.
2081 *
2082 * @returns On success, positive count of formatted character excluding the
2083 * terminator. On buffer overflow, negative number giving the required
2084 * buffer size (including terminator char).
2085 *
2086 * @param pszBuffer Output buffer.
2087 * @param cbBuffer Size of the output buffer.
2088 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2089 * @param args The format argument.
2090 */
2091RTDECL(ssize_t) RTStrPrintf2V(char *pszBuffer, size_t cbBuffer, const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(3, 0);
2092
2093/**
2094 * String printf, version 2.
2095 *
2096 * @returns On success, positive count of formatted character excluding the
2097 * terminator. On buffer overflow, negative number giving the required
2098 * buffer size (including terminator char).
2099 *
2100 * @param pszBuffer Output buffer.
2101 * @param cbBuffer Size of the output buffer.
2102 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2103 * @param ... The format argument.
2104 */
2105RTDECL(ssize_t) RTStrPrintf2(char *pszBuffer, size_t cbBuffer, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
2106
2107/**
2108 * String printf with custom formatting, version 2.
2109 *
2110 * @returns On success, positive count of formatted character excluding the
2111 * terminator. On buffer overflow, negative number giving the required
2112 * buffer size (including terminator char).
2113 *
2114 * @param pfnFormat Pointer to handler function for the custom formats.
2115 * @param pvArg Argument to the pfnFormat function.
2116 * @param pszBuffer Output buffer.
2117 * @param cbBuffer Size of the output buffer.
2118 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2119 * @param args The format argument.
2120 */
2121RTDECL(ssize_t) RTStrPrintf2ExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cbBuffer,
2122 const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(5, 0);
2123
2124/**
2125 * String printf with custom formatting, version 2.
2126 *
2127 * @returns On success, positive count of formatted character excluding the
2128 * terminator. On buffer overflow, negative number giving the required
2129 * buffer size (including terminator char).
2130 *
2131 * @param pfnFormat Pointer to handler function for the custom formats.
2132 * @param pvArg Argument to the pfnFormat function.
2133 * @param pszBuffer Output buffer.
2134 * @param cbBuffer Size of the output buffer.
2135 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2136 * @param ... The format argument.
2137 */
2138RTDECL(ssize_t) RTStrPrintf2Ex(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cbBuffer,
2139 const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(5, 6);
2140
2141/**
2142 * Allocating string printf (default tag).
2143 *
2144 * @returns The length of the string in the returned *ppszBuffer excluding the
2145 * terminator.
2146 * @returns -1 on failure.
2147 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
2148 * The buffer should be freed using RTStrFree().
2149 * On failure *ppszBuffer will be set to NULL.
2150 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2151 * @param args The format argument.
2152 */
2153#define RTStrAPrintfV(ppszBuffer, pszFormat, args) RTStrAPrintfVTag((ppszBuffer), (pszFormat), (args), RTSTR_TAG)
2154
2155/**
2156 * Allocating string printf (custom tag).
2157 *
2158 * @returns The length of the string in the returned *ppszBuffer excluding the
2159 * terminator.
2160 * @returns -1 on failure.
2161 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
2162 * The buffer should be freed using RTStrFree().
2163 * On failure *ppszBuffer will be set to NULL.
2164 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2165 * @param args The format argument.
2166 * @param pszTag Allocation tag used for statistics and such.
2167 */
2168RTDECL(int) RTStrAPrintfVTag(char **ppszBuffer, const char *pszFormat, va_list args, const char *pszTag) RT_IPRT_FORMAT_ATTR(2, 0);
2169
2170/**
2171 * Allocating string printf.
2172 *
2173 * @returns The length of the string in the returned *ppszBuffer excluding the
2174 * terminator.
2175 * @returns -1 on failure.
2176 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
2177 * The buffer should be freed using RTStrFree().
2178 * On failure *ppszBuffer will be set to NULL.
2179 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2180 * @param ... The format argument.
2181 */
2182DECLINLINE(int) RT_IPRT_FORMAT_ATTR(2, 3) RTStrAPrintf(char **ppszBuffer, const char *pszFormat, ...)
2183{
2184 int cbRet;
2185 va_list va;
2186 va_start(va, pszFormat);
2187 cbRet = RTStrAPrintfVTag(ppszBuffer, pszFormat, va, RTSTR_TAG);
2188 va_end(va);
2189 return cbRet;
2190}
2191
2192/**
2193 * Allocating string printf (custom tag).
2194 *
2195 * @returns The length of the string in the returned *ppszBuffer excluding the
2196 * terminator.
2197 * @returns -1 on failure.
2198 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
2199 * The buffer should be freed using RTStrFree().
2200 * On failure *ppszBuffer will be set to NULL.
2201 * @param pszTag Allocation tag used for statistics and such.
2202 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2203 * @param ... The format argument.
2204 */
2205DECLINLINE(int) RT_IPRT_FORMAT_ATTR(3, 4) RTStrAPrintfTag(char **ppszBuffer, const char *pszTag, const char *pszFormat, ...)
2206{
2207 int cbRet;
2208 va_list va;
2209 va_start(va, pszFormat);
2210 cbRet = RTStrAPrintfVTag(ppszBuffer, pszFormat, va, pszTag);
2211 va_end(va);
2212 return cbRet;
2213}
2214
2215/**
2216 * Allocating string printf, version 2.
2217 *
2218 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
2219 * memory.
2220 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2221 * @param args The format argument.
2222 */
2223#define RTStrAPrintf2V(pszFormat, args) RTStrAPrintf2VTag((pszFormat), (args), RTSTR_TAG)
2224
2225/**
2226 * Allocating string printf, version 2.
2227 *
2228 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
2229 * memory.
2230 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2231 * @param args The format argument.
2232 * @param pszTag Allocation tag used for statistics and such.
2233 */
2234RTDECL(char *) RTStrAPrintf2VTag(const char *pszFormat, va_list args, const char *pszTag) RT_IPRT_FORMAT_ATTR(1, 0);
2235
2236/**
2237 * Allocating string printf, version 2 (default tag).
2238 *
2239 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
2240 * memory.
2241 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2242 * @param ... The format argument.
2243 */
2244DECLINLINE(char *) RT_IPRT_FORMAT_ATTR(1, 2) RTStrAPrintf2(const char *pszFormat, ...)
2245{
2246 char *pszRet;
2247 va_list va;
2248 va_start(va, pszFormat);
2249 pszRet = RTStrAPrintf2VTag(pszFormat, va, RTSTR_TAG);
2250 va_end(va);
2251 return pszRet;
2252}
2253
2254/**
2255 * Allocating string printf, version 2 (custom tag).
2256 *
2257 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
2258 * memory.
2259 * @param pszTag Allocation tag used for statistics and such.
2260 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2261 * @param ... The format argument.
2262 */
2263DECLINLINE(char *) RT_IPRT_FORMAT_ATTR(2, 3) RTStrAPrintf2Tag(const char *pszTag, const char *pszFormat, ...)
2264{
2265 char *pszRet;
2266 va_list va;
2267 va_start(va, pszFormat);
2268 pszRet = RTStrAPrintf2VTag(pszFormat, va, pszTag);
2269 va_end(va);
2270 return pszRet;
2271}
2272
2273/**
2274 * Strips blankspaces from both ends of the string.
2275 *
2276 * @returns Pointer to first non-blank char in the string.
2277 * @param psz The string to strip.
2278 */
2279RTDECL(char *) RTStrStrip(char *psz);
2280
2281/**
2282 * Strips blankspaces from the start of the string.
2283 *
2284 * @returns Pointer to first non-blank char in the string.
2285 * @param psz The string to strip.
2286 */
2287RTDECL(char *) RTStrStripL(const char *psz);
2288
2289/**
2290 * Strips blankspaces from the end of the string.
2291 *
2292 * @returns psz.
2293 * @param psz The string to strip.
2294 */
2295RTDECL(char *) RTStrStripR(char *psz);
2296
2297/**
2298 * String copy with overflow handling.
2299 *
2300 * @retval VINF_SUCCESS on success.
2301 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2302 * buffer will contain as much of the string as it can hold, fully
2303 * terminated.
2304 *
2305 * @param pszDst The destination buffer.
2306 * @param cbDst The size of the destination buffer (in bytes).
2307 * @param pszSrc The source string. NULL is not OK.
2308 */
2309RTDECL(int) RTStrCopy(char *pszDst, size_t cbDst, const char *pszSrc);
2310
2311/**
2312 * String copy with overflow handling.
2313 *
2314 * @retval VINF_SUCCESS on success.
2315 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2316 * buffer will contain as much of the string as it can hold, fully
2317 * terminated.
2318 *
2319 * @param pszDst The destination buffer.
2320 * @param cbDst The size of the destination buffer (in bytes).
2321 * @param pszSrc The source string. NULL is not OK.
2322 * @param cchSrcMax The maximum number of chars (not code points) to
2323 * copy from the source string, not counting the
2324 * terminator as usual.
2325 */
2326RTDECL(int) RTStrCopyEx(char *pszDst, size_t cbDst, const char *pszSrc, size_t cchSrcMax);
2327
2328/**
2329 * String copy with overflow handling and buffer advancing.
2330 *
2331 * @retval VINF_SUCCESS on success.
2332 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2333 * buffer will contain as much of the string as it can hold, fully
2334 * terminated.
2335 *
2336 * @param ppszDst Pointer to the destination buffer pointer.
2337 * This will be advanced to the end of the copied
2338 * bytes (points at the terminator). This is also
2339 * updated on overflow.
2340 * @param pcbDst Pointer to the destination buffer size
2341 * variable. This will be updated in accord with
2342 * the buffer pointer.
2343 * @param pszSrc The source string. NULL is not OK.
2344 */
2345RTDECL(int) RTStrCopyP(char **ppszDst, size_t *pcbDst, const char *pszSrc);
2346
2347/**
2348 * String copy with overflow handling.
2349 *
2350 * @retval VINF_SUCCESS on success.
2351 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2352 * buffer will contain as much of the string as it can hold, fully
2353 * terminated.
2354 *
2355 * @param ppszDst Pointer to the destination buffer pointer.
2356 * This will be advanced to the end of the copied
2357 * bytes (points at the terminator). This is also
2358 * updated on overflow.
2359 * @param pcbDst Pointer to the destination buffer size
2360 * variable. This will be updated in accord with
2361 * the buffer pointer.
2362 * @param pszSrc The source string. NULL is not OK.
2363 * @param cchSrcMax The maximum number of chars (not code points) to
2364 * copy from the source string, not counting the
2365 * terminator as usual.
2366 */
2367RTDECL(int) RTStrCopyPEx(char **ppszDst, size_t *pcbDst, const char *pszSrc, size_t cchSrcMax);
2368
2369/**
2370 * String concatenation with overflow handling.
2371 *
2372 * @retval VINF_SUCCESS on success.
2373 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2374 * buffer will contain as much of the string as it can hold, fully
2375 * terminated.
2376 *
2377 * @param pszDst The destination buffer.
2378 * @param cbDst The size of the destination buffer (in bytes).
2379 * @param pszSrc The source string. NULL is not OK.
2380 */
2381RTDECL(int) RTStrCat(char *pszDst, size_t cbDst, const char *pszSrc);
2382
2383/**
2384 * String concatenation with overflow handling.
2385 *
2386 * @retval VINF_SUCCESS on success.
2387 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2388 * buffer will contain as much of the string as it can hold, fully
2389 * terminated.
2390 *
2391 * @param pszDst The destination buffer.
2392 * @param cbDst The size of the destination buffer (in bytes).
2393 * @param pszSrc The source string. NULL is not OK.
2394 * @param cchSrcMax The maximum number of chars (not code points) to
2395 * copy from the source string, not counting the
2396 * terminator as usual.
2397 */
2398RTDECL(int) RTStrCatEx(char *pszDst, size_t cbDst, const char *pszSrc, size_t cchSrcMax);
2399
2400/**
2401 * String concatenation with overflow handling.
2402 *
2403 * @retval VINF_SUCCESS on success.
2404 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2405 * buffer will contain as much of the string as it can hold, fully
2406 * terminated.
2407 *
2408 * @param ppszDst Pointer to the destination buffer pointer.
2409 * This will be advanced to the end of the copied
2410 * bytes (points at the terminator). This is also
2411 * updated on overflow.
2412 * @param pcbDst Pointer to the destination buffer size
2413 * variable. This will be updated in accord with
2414 * the buffer pointer.
2415 * @param pszSrc The source string. NULL is not OK.
2416 */
2417RTDECL(int) RTStrCatP(char **ppszDst, size_t *pcbDst, const char *pszSrc);
2418
2419/**
2420 * String concatenation with overflow handling and buffer advancing.
2421 *
2422 * @retval VINF_SUCCESS on success.
2423 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2424 * buffer will contain as much of the string as it can hold, fully
2425 * terminated.
2426 *
2427 * @param ppszDst Pointer to the destination buffer pointer.
2428 * This will be advanced to the end of the copied
2429 * bytes (points at the terminator). This is also
2430 * updated on overflow.
2431 * @param pcbDst Pointer to the destination buffer size
2432 * variable. This will be updated in accord with
2433 * the buffer pointer.
2434 * @param pszSrc The source string. NULL is not OK.
2435 * @param cchSrcMax The maximum number of chars (not code points) to
2436 * copy from the source string, not counting the
2437 * terminator as usual.
2438 */
2439RTDECL(int) RTStrCatPEx(char **ppszDst, size_t *pcbDst, const char *pszSrc, size_t cchSrcMax);
2440
2441/**
2442 * Performs a case sensitive string compare between two UTF-8 strings.
2443 *
2444 * Encoding errors are ignored by the current implementation. So, the only
2445 * difference between this and the CRT strcmp function is the handling of
2446 * NULL arguments.
2447 *
2448 * @returns < 0 if the first string less than the second string.
2449 * @returns 0 if the first string identical to the second string.
2450 * @returns > 0 if the first string greater than the second string.
2451 * @param psz1 First UTF-8 string. Null is allowed.
2452 * @param psz2 Second UTF-8 string. Null is allowed.
2453 */
2454RTDECL(int) RTStrCmp(const char *psz1, const char *psz2);
2455
2456/**
2457 * Performs a case sensitive string compare between two UTF-8 strings, given
2458 * a maximum string length.
2459 *
2460 * Encoding errors are ignored by the current implementation. So, the only
2461 * difference between this and the CRT strncmp function is the handling of
2462 * NULL arguments.
2463 *
2464 * @returns < 0 if the first string less than the second string.
2465 * @returns 0 if the first string identical to the second string.
2466 * @returns > 0 if the first string greater than the second string.
2467 * @param psz1 First UTF-8 string. Null is allowed.
2468 * @param psz2 Second UTF-8 string. Null is allowed.
2469 * @param cchMax The maximum string length
2470 */
2471RTDECL(int) RTStrNCmp(const char *psz1, const char *psz2, size_t cchMax);
2472
2473/**
2474 * Performs a case insensitive string compare between two UTF-8 strings.
2475 *
2476 * This is a simplified compare, as only the simplified lower/upper case folding
2477 * specified by the unicode specs are used. It does not consider character pairs
2478 * as they are used in some languages, just simple upper & lower case compares.
2479 *
2480 * The result is the difference between the mismatching codepoints after they
2481 * both have been lower cased.
2482 *
2483 * If the string encoding is invalid the function will assert (strict builds)
2484 * and use RTStrCmp for the remainder of the string.
2485 *
2486 * @returns < 0 if the first string less than the second string.
2487 * @returns 0 if the first string identical to the second string.
2488 * @returns > 0 if the first string greater than the second string.
2489 * @param psz1 First UTF-8 string. Null is allowed.
2490 * @param psz2 Second UTF-8 string. Null is allowed.
2491 */
2492RTDECL(int) RTStrICmp(const char *psz1, const char *psz2);
2493
2494/**
2495 * Performs a case insensitive string compare between two UTF-8 strings, given a
2496 * maximum string length.
2497 *
2498 * This is a simplified compare, as only the simplified lower/upper case folding
2499 * specified by the unicode specs are used. It does not consider character pairs
2500 * as they are used in some languages, just simple upper & lower case compares.
2501 *
2502 * The result is the difference between the mismatching codepoints after they
2503 * both have been lower cased.
2504 *
2505 * If the string encoding is invalid the function will assert (strict builds)
2506 * and use RTStrNCmp for the remainder of the string.
2507 *
2508 * @returns < 0 if the first string less than the second string.
2509 * @returns 0 if the first string identical to the second string.
2510 * @returns > 0 if the first string greater than the second string.
2511 * @param psz1 First UTF-8 string. Null is allowed.
2512 * @param psz2 Second UTF-8 string. Null is allowed.
2513 * @param cchMax Maximum string length
2514 */
2515RTDECL(int) RTStrNICmp(const char *psz1, const char *psz2, size_t cchMax);
2516
2517/**
2518 * Performs a case insensitive string compare between a UTF-8 string and a 7-bit
2519 * ASCII string.
2520 *
2521 * This is potentially faster than RTStrICmp and drags in less dependencies. It
2522 * is really handy for hardcoded inputs.
2523 *
2524 * If the string encoding is invalid the function will assert (strict builds)
2525 * and use RTStrCmp for the remainder of the string.
2526 *
2527 * @returns < 0 if the first string less than the second string.
2528 * @returns 0 if the first string identical to the second string.
2529 * @returns > 0 if the first string greater than the second string.
2530 * @param psz1 First UTF-8 string. Null is allowed.
2531 * @param psz2 Second string, 7-bit ASCII. Null is allowed.
2532 * @sa RTStrICmp, RTUtf16ICmpAscii
2533 */
2534RTDECL(int) RTStrICmpAscii(const char *psz1, const char *psz2);
2535
2536/**
2537 * Performs a case insensitive string compare between a UTF-8 string and a 7-bit
2538 * ASCII string, given a maximum string length.
2539 *
2540 * This is potentially faster than RTStrNICmp and drags in less dependencies.
2541 * It is really handy for hardcoded inputs.
2542 *
2543 * If the string encoding is invalid the function will assert (strict builds)
2544 * and use RTStrNCmp for the remainder of the string.
2545 *
2546 * @returns < 0 if the first string less than the second string.
2547 * @returns 0 if the first string identical to the second string.
2548 * @returns > 0 if the first string greater than the second string.
2549 * @param psz1 First UTF-8 string. Null is allowed.
2550 * @param psz2 Second string, 7-bit ASCII. Null is allowed.
2551 * @param cchMax Maximum string length
2552 * @sa RTStrNICmp, RTUtf16NICmpAscii
2553 */
2554RTDECL(int) RTStrNICmpAscii(const char *psz1, const char *psz2, size_t cchMax);
2555
2556/**
2557 * Checks whether @a pszString starts with @a pszStart.
2558 *
2559 * @returns true / false.
2560 * @param pszString The string to check.
2561 * @param pszStart The start string to check for.
2562 */
2563RTDECL(bool) RTStrStartsWith(const char *pszString, const char *pszStart);
2564
2565/**
2566 * Checks whether @a pszString starts with @a pszStart, case insensitive.
2567 *
2568 * @returns true / false.
2569 * @param pszString The string to check.
2570 * @param pszStart The start string to check for.
2571 */
2572RTDECL(bool) RTStrIStartsWith(const char *pszString, const char *pszStart);
2573
2574/**
2575 * Splits a string buffer with a given separator into separate strings.
2576 * If no separators are found, no strings are returned. Consequtive separators will be skipped.
2577 *
2578 * @returns iprt status code.
2579 * @param pcszStrings String buffer to split.
2580 * @param cbStrings Size (in bytes) of string buffer to split, including terminator.
2581 * @param pcszSeparator Separator to use / find for splitting strings.
2582 * @param ppapszStrings Where to return the allocated string array on success. Needs to be free'd by the caller.
2583 * @param pcStrings Where to return the number of split strings in \a ppapszStrings.
2584 */
2585RTDECL(int) RTStrSplit(const char *pcszStrings, size_t cbStrings,
2586 const char *pcszSeparator, char ***ppapszStrings, size_t *pcStrings);
2587
2588/**
2589 * Locates a case sensitive substring.
2590 *
2591 * If any of the two strings are NULL, then NULL is returned. If the needle is
2592 * an empty string, then the haystack is returned (i.e. matches anything).
2593 *
2594 * @returns Pointer to the first occurrence of the substring if found, NULL if
2595 * not.
2596 *
2597 * @param pszHaystack The string to search.
2598 * @param pszNeedle The substring to search for.
2599 *
2600 * @remarks The difference between this and strstr is the handling of NULL
2601 * pointers.
2602 */
2603RTDECL(char *) RTStrStr(const char *pszHaystack, const char *pszNeedle);
2604
2605/**
2606 * Locates a case insensitive substring.
2607 *
2608 * If any of the two strings are NULL, then NULL is returned. If the needle is
2609 * an empty string, then the haystack is returned (i.e. matches anything).
2610 *
2611 * @returns Pointer to the first occurrence of the substring if found, NULL if
2612 * not.
2613 *
2614 * @param pszHaystack The string to search.
2615 * @param pszNeedle The substring to search for.
2616 *
2617 */
2618RTDECL(char *) RTStrIStr(const char *pszHaystack, const char *pszNeedle);
2619
2620/**
2621 * Converts the string to lower case.
2622 *
2623 * @returns Pointer to the converted string.
2624 * @param psz The string to convert.
2625 */
2626RTDECL(char *) RTStrToLower(char *psz);
2627
2628/**
2629 * Converts the string to upper case.
2630 *
2631 * @returns Pointer to the converted string.
2632 * @param psz The string to convert.
2633 */
2634RTDECL(char *) RTStrToUpper(char *psz);
2635
2636/**
2637 * Checks if the string is case foldable, i.e. whether it would change if
2638 * subject to RTStrToLower or RTStrToUpper.
2639 *
2640 * @returns true / false
2641 * @param psz The string in question.
2642 */
2643RTDECL(bool) RTStrIsCaseFoldable(const char *psz);
2644
2645/**
2646 * Checks if the string is upper cased (no lower case chars in it).
2647 *
2648 * @returns true / false
2649 * @param psz The string in question.
2650 */
2651RTDECL(bool) RTStrIsUpperCased(const char *psz);
2652
2653/**
2654 * Checks if the string is lower cased (no upper case chars in it).
2655 *
2656 * @returns true / false
2657 * @param psz The string in question.
2658 */
2659RTDECL(bool) RTStrIsLowerCased(const char *psz);
2660
2661/**
2662 * Find the length of a zero-terminated byte string, given
2663 * a max string length.
2664 *
2665 * See also RTStrNLenEx.
2666 *
2667 * @returns The string length or cbMax. The returned length does not include
2668 * the zero terminator if it was found.
2669 *
2670 * @param pszString The string.
2671 * @param cchMax The max string length.
2672 */
2673RTDECL(size_t) RTStrNLen(const char *pszString, size_t cchMax);
2674
2675/**
2676 * Find the length of a zero-terminated byte string, given
2677 * a max string length.
2678 *
2679 * See also RTStrNLen.
2680 *
2681 * @returns IPRT status code.
2682 * @retval VINF_SUCCESS if the string has a length less than cchMax.
2683 * @retval VERR_BUFFER_OVERFLOW if the end of the string wasn't found
2684 * before cchMax was reached.
2685 *
2686 * @param pszString The string.
2687 * @param cchMax The max string length.
2688 * @param pcch Where to store the string length excluding the
2689 * terminator. This is set to cchMax if the terminator
2690 * isn't found.
2691 */
2692RTDECL(int) RTStrNLenEx(const char *pszString, size_t cchMax, size_t *pcch);
2693
2694/** The maximum size argument of a memchr call. */
2695#define RTSTR_MEMCHR_MAX ((~(size_t)0 >> 1) - 15)
2696
2697/**
2698 * Find the zero terminator in a string with a limited length.
2699 *
2700 * @returns Pointer to the zero terminator.
2701 * @returns NULL if the zero terminator was not found.
2702 *
2703 * @param pszString The string.
2704 * @param cchMax The max string length. RTSTR_MAX is fine.
2705 */
2706RTDECL(char *) RTStrEnd(char const *pszString, size_t cchMax);
2707
2708/**
2709 * Finds the offset at which a simple character first occurs in a string.
2710 *
2711 * @returns The offset of the first occurence or the terminator offset.
2712 * @param pszHaystack The string to search.
2713 * @param chNeedle The character to search for.
2714 */
2715DECLINLINE(size_t) RTStrOffCharOrTerm(const char *pszHaystack, char chNeedle)
2716{
2717 const char *psz = pszHaystack;
2718 char ch;
2719 while ( (ch = *psz) != chNeedle
2720 && ch != '\0')
2721 psz++;
2722 return (size_t)(psz - pszHaystack);
2723}
2724
2725/**
2726 * Matches a simple string pattern.
2727 *
2728 * @returns true if the string matches the pattern, otherwise false.
2729 *
2730 * @param pszPattern The pattern. Special chars are '*' and '?', where the
2731 * asterisk matches zero or more characters and question
2732 * mark matches exactly one character.
2733 * @param pszString The string to match against the pattern.
2734 */
2735RTDECL(bool) RTStrSimplePatternMatch(const char *pszPattern, const char *pszString);
2736
2737/**
2738 * Matches a simple string pattern, neither which needs to be zero terminated.
2739 *
2740 * This is identical to RTStrSimplePatternMatch except that you can optionally
2741 * specify the length of both the pattern and the string. The function will
2742 * stop when it hits a string terminator or either of the lengths.
2743 *
2744 * @returns true if the string matches the pattern, otherwise false.
2745 *
2746 * @param pszPattern The pattern. Special chars are '*' and '?', where the
2747 * asterisk matches zero or more characters and question
2748 * mark matches exactly one character.
2749 * @param cchPattern The pattern length. Pass RTSTR_MAX if you don't know the
2750 * length and wish to stop at the string terminator.
2751 * @param pszString The string to match against the pattern.
2752 * @param cchString The string length. Pass RTSTR_MAX if you don't know the
2753 * length and wish to match up to the string terminator.
2754 */
2755RTDECL(bool) RTStrSimplePatternNMatch(const char *pszPattern, size_t cchPattern,
2756 const char *pszString, size_t cchString);
2757
2758/**
2759 * Matches multiple patterns against a string.
2760 *
2761 * The patterns are separated by the pipe character (|).
2762 *
2763 * @returns true if the string matches the pattern, otherwise false.
2764 *
2765 * @param pszPatterns The patterns.
2766 * @param cchPatterns The lengths of the patterns to use. Pass RTSTR_MAX to
2767 * stop at the terminator.
2768 * @param pszString The string to match against the pattern.
2769 * @param cchString The string length. Pass RTSTR_MAX stop stop at the
2770 * terminator.
2771 * @param poffPattern Offset into the patterns string of the patttern that
2772 * matched. If no match, this will be set to RTSTR_MAX.
2773 * This is optional, NULL is fine.
2774 */
2775RTDECL(bool) RTStrSimplePatternMultiMatch(const char *pszPatterns, size_t cchPatterns,
2776 const char *pszString, size_t cchString,
2777 size_t *poffPattern);
2778
2779/**
2780 * Compares two version strings RTStrICmp fashion.
2781 *
2782 * The version string is split up into sections at punctuation, spaces,
2783 * underscores, dashes and plus signs. The sections are then split up into
2784 * numeric and string sub-sections. Finally, the sub-sections are compared
2785 * in a numeric or case insesntivie fashion depending on what they are.
2786 *
2787 * The following strings are considered to be equal: "1.0.0", "1.00.0", "1.0",
2788 * "1". These aren't: "1.0.0r993", "1.0", "1.0r993", "1.0_Beta3", "1.1"
2789 *
2790 * @returns < 0 if the first string less than the second string.
2791 * @returns 0 if the first string identical to the second string.
2792 * @returns > 0 if the first string greater than the second string.
2793 *
2794 * @param pszVer1 First version string to compare.
2795 * @param pszVer2 Second version string to compare first version with.
2796 */
2797RTDECL(int) RTStrVersionCompare(const char *pszVer1, const char *pszVer2);
2798
2799
2800/** @defgroup rt_str_conv String To/From Number Conversions
2801 * @{ */
2802
2803/**
2804 * Converts a string representation of a number to a 64-bit unsigned number.
2805 *
2806 * @returns iprt status code.
2807 * Warnings are used to indicate conversion problems.
2808 * @retval VWRN_NUMBER_TOO_BIG
2809 * @retval VWRN_NEGATIVE_UNSIGNED
2810 * @retval VWRN_TRAILING_CHARS
2811 * @retval VWRN_TRAILING_SPACES
2812 * @retval VINF_SUCCESS
2813 * @retval VERR_NO_DIGITS
2814 *
2815 * @param pszValue Pointer to the string value.
2816 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2817 * @param uBase The base of the representation used.
2818 * If 0 the function will look for known prefixes before defaulting to 10.
2819 * @param pu64 Where to store the converted number. (optional)
2820 */
2821RTDECL(int) RTStrToUInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint64_t *pu64);
2822
2823/**
2824 * Converts a string representation of a number to a 64-bit unsigned number,
2825 * making sure the full string is converted.
2826 *
2827 * @returns iprt status code.
2828 * Warnings are used to indicate conversion problems.
2829 * @retval VWRN_NUMBER_TOO_BIG
2830 * @retval VWRN_NEGATIVE_UNSIGNED
2831 * @retval VINF_SUCCESS
2832 * @retval VERR_NO_DIGITS
2833 * @retval VERR_TRAILING_SPACES
2834 * @retval VERR_TRAILING_CHARS
2835 *
2836 * @param pszValue Pointer to the string value.
2837 * @param uBase The base of the representation used.
2838 * If 0 the function will look for known prefixes before defaulting to 10.
2839 * @param pu64 Where to store the converted number. (optional)
2840 */
2841RTDECL(int) RTStrToUInt64Full(const char *pszValue, unsigned uBase, uint64_t *pu64);
2842
2843/**
2844 * Converts a string representation of a number to a 64-bit unsigned number.
2845 * The base is guessed.
2846 *
2847 * @returns 64-bit unsigned number on success.
2848 * @returns 0 on failure.
2849 * @param pszValue Pointer to the string value.
2850 */
2851RTDECL(uint64_t) RTStrToUInt64(const char *pszValue);
2852
2853/**
2854 * Converts a string representation of a number to a 32-bit unsigned number.
2855 *
2856 * @returns iprt status code.
2857 * Warnings are used to indicate conversion problems.
2858 * @retval VWRN_NUMBER_TOO_BIG
2859 * @retval VWRN_NEGATIVE_UNSIGNED
2860 * @retval VWRN_TRAILING_CHARS
2861 * @retval VWRN_TRAILING_SPACES
2862 * @retval VINF_SUCCESS
2863 * @retval VERR_NO_DIGITS
2864 *
2865 * @param pszValue Pointer to the string value.
2866 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2867 * @param uBase The base of the representation used.
2868 * If 0 the function will look for known prefixes before defaulting to 10.
2869 * @param pu32 Where to store the converted number. (optional)
2870 */
2871RTDECL(int) RTStrToUInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint32_t *pu32);
2872
2873/**
2874 * Converts a string representation of a number to a 32-bit unsigned number,
2875 * making sure the full string is converted.
2876 *
2877 * @returns iprt status code.
2878 * Warnings are used to indicate conversion problems.
2879 * @retval VWRN_NUMBER_TOO_BIG
2880 * @retval VWRN_NEGATIVE_UNSIGNED
2881 * @retval VINF_SUCCESS
2882 * @retval VERR_NO_DIGITS
2883 * @retval VERR_TRAILING_SPACES
2884 * @retval VERR_TRAILING_CHARS
2885 *
2886 * @param pszValue Pointer to the string value.
2887 * @param uBase The base of the representation used.
2888 * If 0 the function will look for known prefixes before defaulting to 10.
2889 * @param pu32 Where to store the converted number. (optional)
2890 */
2891RTDECL(int) RTStrToUInt32Full(const char *pszValue, unsigned uBase, uint32_t *pu32);
2892
2893/**
2894 * Converts a string representation of a number to a 32-bit unsigned number.
2895 * The base is guessed.
2896 *
2897 * @returns 32-bit unsigned number on success.
2898 * @returns 0 on failure.
2899 * @param pszValue Pointer to the string value.
2900 */
2901RTDECL(uint32_t) RTStrToUInt32(const char *pszValue);
2902
2903/**
2904 * Converts a string representation of a number to a 16-bit unsigned number.
2905 *
2906 * @returns iprt status code.
2907 * Warnings are used to indicate conversion problems.
2908 * @retval VWRN_NUMBER_TOO_BIG
2909 * @retval VWRN_NEGATIVE_UNSIGNED
2910 * @retval VWRN_TRAILING_CHARS
2911 * @retval VWRN_TRAILING_SPACES
2912 * @retval VINF_SUCCESS
2913 * @retval VERR_NO_DIGITS
2914 *
2915 * @param pszValue Pointer to the string value.
2916 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2917 * @param uBase The base of the representation used.
2918 * If 0 the function will look for known prefixes before defaulting to 10.
2919 * @param pu16 Where to store the converted number. (optional)
2920 */
2921RTDECL(int) RTStrToUInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint16_t *pu16);
2922
2923/**
2924 * Converts a string representation of a number to a 16-bit unsigned number,
2925 * making sure the full string is converted.
2926 *
2927 * @returns iprt status code.
2928 * Warnings are used to indicate conversion problems.
2929 * @retval VWRN_NUMBER_TOO_BIG
2930 * @retval VWRN_NEGATIVE_UNSIGNED
2931 * @retval VINF_SUCCESS
2932 * @retval VERR_NO_DIGITS
2933 * @retval VERR_TRAILING_SPACES
2934 * @retval VERR_TRAILING_CHARS
2935 *
2936 * @param pszValue Pointer to the string value.
2937 * @param uBase The base of the representation used.
2938 * If 0 the function will look for known prefixes before defaulting to 10.
2939 * @param pu16 Where to store the converted number. (optional)
2940 */
2941RTDECL(int) RTStrToUInt16Full(const char *pszValue, unsigned uBase, uint16_t *pu16);
2942
2943/**
2944 * Converts a string representation of a number to a 16-bit unsigned number.
2945 * The base is guessed.
2946 *
2947 * @returns 16-bit unsigned number on success.
2948 * @returns 0 on failure.
2949 * @param pszValue Pointer to the string value.
2950 */
2951RTDECL(uint16_t) RTStrToUInt16(const char *pszValue);
2952
2953/**
2954 * Converts a string representation of a number to a 8-bit unsigned number.
2955 *
2956 * @returns iprt status code.
2957 * Warnings are used to indicate conversion problems.
2958 * @retval VWRN_NUMBER_TOO_BIG
2959 * @retval VWRN_NEGATIVE_UNSIGNED
2960 * @retval VWRN_TRAILING_CHARS
2961 * @retval VWRN_TRAILING_SPACES
2962 * @retval VINF_SUCCESS
2963 * @retval VERR_NO_DIGITS
2964 *
2965 * @param pszValue Pointer to the string value.
2966 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2967 * @param uBase The base of the representation used.
2968 * If 0 the function will look for known prefixes before defaulting to 10.
2969 * @param pu8 Where to store the converted number. (optional)
2970 */
2971RTDECL(int) RTStrToUInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint8_t *pu8);
2972
2973/**
2974 * Converts a string representation of a number to a 8-bit unsigned number,
2975 * making sure the full string is converted.
2976 *
2977 * @returns iprt status code.
2978 * Warnings are used to indicate conversion problems.
2979 * @retval VWRN_NUMBER_TOO_BIG
2980 * @retval VWRN_NEGATIVE_UNSIGNED
2981 * @retval VINF_SUCCESS
2982 * @retval VERR_NO_DIGITS
2983 * @retval VERR_TRAILING_SPACES
2984 * @retval VERR_TRAILING_CHARS
2985 *
2986 * @param pszValue Pointer to the string value.
2987 * @param uBase The base of the representation used.
2988 * If 0 the function will look for known prefixes before defaulting to 10.
2989 * @param pu8 Where to store the converted number. (optional)
2990 */
2991RTDECL(int) RTStrToUInt8Full(const char *pszValue, unsigned uBase, uint8_t *pu8);
2992
2993/**
2994 * Converts a string representation of a number to a 8-bit unsigned number.
2995 * The base is guessed.
2996 *
2997 * @returns 8-bit unsigned number on success.
2998 * @returns 0 on failure.
2999 * @param pszValue Pointer to the string value.
3000 */
3001RTDECL(uint8_t) RTStrToUInt8(const char *pszValue);
3002
3003/**
3004 * Converts a string representation of a number to a 64-bit signed number.
3005 *
3006 * @returns iprt status code.
3007 * Warnings are used to indicate conversion problems.
3008 * @retval VWRN_NUMBER_TOO_BIG
3009 * @retval VWRN_TRAILING_CHARS
3010 * @retval VWRN_TRAILING_SPACES
3011 * @retval VINF_SUCCESS
3012 * @retval VERR_NO_DIGITS
3013 *
3014 * @param pszValue Pointer to the string value.
3015 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
3016 * @param uBase The base of the representation used.
3017 * If 0 the function will look for known prefixes before defaulting to 10.
3018 * @param pi64 Where to store the converted number. (optional)
3019 */
3020RTDECL(int) RTStrToInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, int64_t *pi64);
3021
3022/**
3023 * Converts a string representation of a number to a 64-bit signed number,
3024 * making sure the full string is converted.
3025 *
3026 * @returns iprt status code.
3027 * Warnings are used to indicate conversion problems.
3028 * @retval VWRN_NUMBER_TOO_BIG
3029 * @retval VINF_SUCCESS
3030 * @retval VERR_TRAILING_CHARS
3031 * @retval VERR_TRAILING_SPACES
3032 * @retval VERR_NO_DIGITS
3033 *
3034 * @param pszValue Pointer to the string value.
3035 * @param uBase The base of the representation used.
3036 * If 0 the function will look for known prefixes before defaulting to 10.
3037 * @param pi64 Where to store the converted number. (optional)
3038 */
3039RTDECL(int) RTStrToInt64Full(const char *pszValue, unsigned uBase, int64_t *pi64);
3040
3041/**
3042 * Converts a string representation of a number to a 64-bit signed number.
3043 * The base is guessed.
3044 *
3045 * @returns 64-bit signed number on success.
3046 * @returns 0 on failure.
3047 * @param pszValue Pointer to the string value.
3048 */
3049RTDECL(int64_t) RTStrToInt64(const char *pszValue);
3050
3051/**
3052 * Converts a string representation of a number to a 32-bit signed number.
3053 *
3054 * @returns iprt status code.
3055 * Warnings are used to indicate conversion problems.
3056 * @retval VWRN_NUMBER_TOO_BIG
3057 * @retval VWRN_TRAILING_CHARS
3058 * @retval VWRN_TRAILING_SPACES
3059 * @retval VINF_SUCCESS
3060 * @retval VERR_NO_DIGITS
3061 *
3062 * @param pszValue Pointer to the string value.
3063 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
3064 * @param uBase The base of the representation used.
3065 * If 0 the function will look for known prefixes before defaulting to 10.
3066 * @param pi32 Where to store the converted number. (optional)
3067 */
3068RTDECL(int) RTStrToInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, int32_t *pi32);
3069
3070/**
3071 * Converts a string representation of a number to a 32-bit signed number,
3072 * making sure the full string is converted.
3073 *
3074 * @returns iprt status code.
3075 * Warnings are used to indicate conversion problems.
3076 * @retval VWRN_NUMBER_TOO_BIG
3077 * @retval VINF_SUCCESS
3078 * @retval VERR_TRAILING_CHARS
3079 * @retval VERR_TRAILING_SPACES
3080 * @retval VERR_NO_DIGITS
3081 *
3082 * @param pszValue Pointer to the string value.
3083 * @param uBase The base of the representation used.
3084 * If 0 the function will look for known prefixes before defaulting to 10.
3085 * @param pi32 Where to store the converted number. (optional)
3086 */
3087RTDECL(int) RTStrToInt32Full(const char *pszValue, unsigned uBase, int32_t *pi32);
3088
3089/**
3090 * Converts a string representation of a number to a 32-bit signed number.
3091 * The base is guessed.
3092 *
3093 * @returns 32-bit signed number on success.
3094 * @returns 0 on failure.
3095 * @param pszValue Pointer to the string value.
3096 */
3097RTDECL(int32_t) RTStrToInt32(const char *pszValue);
3098
3099/**
3100 * Converts a string representation of a number to a 16-bit signed number.
3101 *
3102 * @returns iprt status code.
3103 * Warnings are used to indicate conversion problems.
3104 * @retval VWRN_NUMBER_TOO_BIG
3105 * @retval VWRN_TRAILING_CHARS
3106 * @retval VWRN_TRAILING_SPACES
3107 * @retval VINF_SUCCESS
3108 * @retval VERR_NO_DIGITS
3109 *
3110 * @param pszValue Pointer to the string value.
3111 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
3112 * @param uBase The base of the representation used.
3113 * If 0 the function will look for known prefixes before defaulting to 10.
3114 * @param pi16 Where to store the converted number. (optional)
3115 */
3116RTDECL(int) RTStrToInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, int16_t *pi16);
3117
3118/**
3119 * Converts a string representation of a number to a 16-bit signed number,
3120 * making sure the full string is converted.
3121 *
3122 * @returns iprt status code.
3123 * Warnings are used to indicate conversion problems.
3124 * @retval VWRN_NUMBER_TOO_BIG
3125 * @retval VINF_SUCCESS
3126 * @retval VERR_TRAILING_CHARS
3127 * @retval VERR_TRAILING_SPACES
3128 * @retval VERR_NO_DIGITS
3129 *
3130 * @param pszValue Pointer to the string value.
3131 * @param uBase The base of the representation used.
3132 * If 0 the function will look for known prefixes before defaulting to 10.
3133 * @param pi16 Where to store the converted number. (optional)
3134 */
3135RTDECL(int) RTStrToInt16Full(const char *pszValue, unsigned uBase, int16_t *pi16);
3136
3137/**
3138 * Converts a string representation of a number to a 16-bit signed number.
3139 * The base is guessed.
3140 *
3141 * @returns 16-bit signed number on success.
3142 * @returns 0 on failure.
3143 * @param pszValue Pointer to the string value.
3144 */
3145RTDECL(int16_t) RTStrToInt16(const char *pszValue);
3146
3147/**
3148 * Converts a string representation of a number to a 8-bit signed number.
3149 *
3150 * @returns iprt status code.
3151 * Warnings are used to indicate conversion problems.
3152 * @retval VWRN_NUMBER_TOO_BIG
3153 * @retval VWRN_TRAILING_CHARS
3154 * @retval VWRN_TRAILING_SPACES
3155 * @retval VINF_SUCCESS
3156 * @retval VERR_NO_DIGITS
3157 *
3158 * @param pszValue Pointer to the string value.
3159 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
3160 * @param uBase The base of the representation used.
3161 * If 0 the function will look for known prefixes before defaulting to 10.
3162 * @param pi8 Where to store the converted number. (optional)
3163 */
3164RTDECL(int) RTStrToInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, int8_t *pi8);
3165
3166/**
3167 * Converts a string representation of a number to a 8-bit signed number,
3168 * making sure the full string is converted.
3169 *
3170 * @returns iprt status code.
3171 * Warnings are used to indicate conversion problems.
3172 * @retval VWRN_NUMBER_TOO_BIG
3173 * @retval VINF_SUCCESS
3174 * @retval VERR_TRAILING_CHARS
3175 * @retval VERR_TRAILING_SPACES
3176 * @retval VERR_NO_DIGITS
3177 *
3178 * @param pszValue Pointer to the string value.
3179 * @param uBase The base of the representation used.
3180 * If 0 the function will look for known prefixes before defaulting to 10.
3181 * @param pi8 Where to store the converted number. (optional)
3182 */
3183RTDECL(int) RTStrToInt8Full(const char *pszValue, unsigned uBase, int8_t *pi8);
3184
3185/**
3186 * Converts a string representation of a number to a 8-bit signed number.
3187 * The base is guessed.
3188 *
3189 * @returns 8-bit signed number on success.
3190 * @returns 0 on failure.
3191 * @param pszValue Pointer to the string value.
3192 */
3193RTDECL(int8_t) RTStrToInt8(const char *pszValue);
3194
3195/**
3196 * Formats a buffer stream as hex bytes.
3197 *
3198 * The default is no separating spaces or line breaks or anything.
3199 *
3200 * @returns IPRT status code.
3201 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
3202 * @retval VERR_BUFFER_OVERFLOW if the buffer is insufficent to hold the bytes.
3203 *
3204 * @param pszBuf Output string buffer.
3205 * @param cbBuf The size of the output buffer.
3206 * @param pv Pointer to the bytes to stringify.
3207 * @param cb The number of bytes to stringify.
3208 * @param fFlags Combination of RTSTRPRINTHEXBYTES_F_XXX values.
3209 * @sa RTUtf16PrintHexBytes.
3210 */
3211RTDECL(int) RTStrPrintHexBytes(char *pszBuf, size_t cbBuf, void const *pv, size_t cb, uint32_t fFlags);
3212/** @name RTSTRPRINTHEXBYTES_F_XXX - flags for RTStrPrintHexBytes and RTUtf16PritnHexBytes.
3213 * @{ */
3214/** Upper case hex digits, the default is lower case. */
3215#define RTSTRPRINTHEXBYTES_F_UPPER RT_BIT(0)
3216/** Add a space between each group. */
3217#define RTSTRPRINTHEXBYTES_F_SEP_SPACE RT_BIT(1)
3218/** Add a colon between each group. */
3219#define RTSTRPRINTHEXBYTES_F_SEP_COLON RT_BIT(2)
3220/** @} */
3221
3222/**
3223 * Converts a string of hex bytes back into binary data.
3224 *
3225 * @returns IPRT status code.
3226 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
3227 * @retval VERR_BUFFER_OVERFLOW if the string contains too many hex bytes.
3228 * @retval VERR_BUFFER_UNDERFLOW if there aren't enough hex bytes to fill up
3229 * the output buffer.
3230 * @retval VERR_UNEVEN_INPUT if the input contains a half byte.
3231 * @retval VERR_NO_DIGITS
3232 * @retval VWRN_TRAILING_CHARS
3233 * @retval VWRN_TRAILING_SPACES
3234 *
3235 * @param pszHex The string containing the hex bytes.
3236 * @param pv Output buffer.
3237 * @param cb The size of the output buffer.
3238 * @param fFlags RTSTRCONVERTHEXBYTES_F_XXX.
3239 */
3240RTDECL(int) RTStrConvertHexBytes(char const *pszHex, void *pv, size_t cb, uint32_t fFlags);
3241
3242/** @name RTSTRCONVERTHEXBYTES_F_XXX - Flags for RTStrConvertHexBytes() and RTStrConvertHexBytesEx().
3243 * @{ */
3244/** Accept colon as a byte separator. */
3245#define RTSTRCONVERTHEXBYTES_F_SEP_COLON RT_BIT(0)
3246/** @} */
3247
3248/**
3249 * Converts a string of hex bytes back into binary data, extended version.
3250 *
3251 * @returns IPRT status code.
3252 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
3253 * @retval VERR_BUFFER_OVERFLOW if the string contains too many hex bytes.
3254 * @retval VERR_BUFFER_UNDERFLOW if there aren't enough hex bytes to fill up
3255 * the output buffer and *pcbReturned is NULL.
3256 * @retval VINF_BUFFER_UNDERFLOW if there aren't enough hex bytes to fill up
3257 * the output buffer and *pcbReturned is not NULL, *pcbReturned holds
3258 * the actual number of bytes.
3259 * @retval VERR_UNEVEN_INPUT if the input contains a half byte.
3260 * @retval VERR_NO_DIGITS
3261 * @retval VWRN_TRAILING_CHARS
3262 * @retval VWRN_TRAILING_SPACES
3263 *
3264 * @param pszHex The string containing the hex bytes.
3265 * @param pv Output buffer.
3266 * @param cb The size of the output buffer.
3267 * @param fFlags RTSTRCONVERTHEXBYTES_F_XXX.
3268 * @param ppszNext Set to point at where we stopped decoding hex bytes.
3269 * Optional.
3270 * @param pcbReturned Where to return the number of bytes found. Optional.
3271 */
3272RTDECL(int) RTStrConvertHexBytesEx(char const *pszHex, void *pv, size_t cb, uint32_t fFlags,
3273 const char **ppszNext, size_t *pcbReturned);
3274
3275/** @} */
3276
3277
3278/** @defgroup rt_str_space Unique String Space
3279 * @{
3280 */
3281
3282/** Pointer to a string name space container node core. */
3283typedef struct RTSTRSPACECORE *PRTSTRSPACECORE;
3284/** Pointer to a pointer to a string name space container node core. */
3285typedef PRTSTRSPACECORE *PPRTSTRSPACECORE;
3286
3287/**
3288 * String name space container node core.
3289 */
3290typedef struct RTSTRSPACECORE
3291{
3292 /** Pointer to the left leaf node. Don't touch. */
3293 PRTSTRSPACECORE pLeft;
3294 /** Pointer to the left right node. Don't touch. */
3295 PRTSTRSPACECORE pRight;
3296 /** Pointer to the list of string with the same hash key value. Don't touch. */
3297 PRTSTRSPACECORE pList;
3298 /** Hash key. Don't touch. */
3299 uint32_t Key;
3300 /** Height of this tree: max(heigth(left), heigth(right)) + 1. Don't touch */
3301 unsigned char uchHeight;
3302 /** The string length. Read only! */
3303 size_t cchString;
3304 /** Pointer to the string. Read only! */
3305 const char *pszString;
3306} RTSTRSPACECORE;
3307
3308/** String space. (Initialize with NULL.) */
3309typedef PRTSTRSPACECORE RTSTRSPACE;
3310/** Pointer to a string space. */
3311typedef PPRTSTRSPACECORE PRTSTRSPACE;
3312
3313
3314/**
3315 * Inserts a string into a unique string space.
3316 *
3317 * @returns true on success.
3318 * @returns false if the string collided with an existing string.
3319 * @param pStrSpace The space to insert it into.
3320 * @param pStr The string node.
3321 */
3322RTDECL(bool) RTStrSpaceInsert(PRTSTRSPACE pStrSpace, PRTSTRSPACECORE pStr);
3323
3324/**
3325 * Removes a string from a unique string space.
3326 *
3327 * @returns Pointer to the removed string node.
3328 * @returns NULL if the string was not found in the string space.
3329 * @param pStrSpace The space to remove it from.
3330 * @param pszString The string to remove.
3331 */
3332RTDECL(PRTSTRSPACECORE) RTStrSpaceRemove(PRTSTRSPACE pStrSpace, const char *pszString);
3333
3334/**
3335 * Gets a string from a unique string space.
3336 *
3337 * @returns Pointer to the string node.
3338 * @returns NULL if the string was not found in the string space.
3339 * @param pStrSpace The space to get it from.
3340 * @param pszString The string to get.
3341 */
3342RTDECL(PRTSTRSPACECORE) RTStrSpaceGet(PRTSTRSPACE pStrSpace, const char *pszString);
3343
3344/**
3345 * Gets a string from a unique string space.
3346 *
3347 * @returns Pointer to the string node.
3348 * @returns NULL if the string was not found in the string space.
3349 * @param pStrSpace The space to get it from.
3350 * @param pszString The string to get.
3351 * @param cchMax The max string length to evaluate. Passing
3352 * RTSTR_MAX is ok and makes it behave just like
3353 * RTStrSpaceGet.
3354 */
3355RTDECL(PRTSTRSPACECORE) RTStrSpaceGetN(PRTSTRSPACE pStrSpace, const char *pszString, size_t cchMax);
3356
3357/**
3358 * Callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy().
3359 *
3360 * @returns 0 on continue.
3361 * @returns Non-zero to aborts the operation.
3362 * @param pStr The string node
3363 * @param pvUser The user specified argument.
3364 */
3365typedef DECLCALLBACKTYPE(int, FNRTSTRSPACECALLBACK,(PRTSTRSPACECORE pStr, void *pvUser));
3366/** Pointer to callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy(). */
3367typedef FNRTSTRSPACECALLBACK *PFNRTSTRSPACECALLBACK;
3368
3369/**
3370 * Destroys the string space.
3371 *
3372 * The caller supplies a callback which will be called for each of the string
3373 * nodes in for freeing their memory and other resources.
3374 *
3375 * @returns 0 or what ever non-zero return value pfnCallback returned
3376 * when aborting the destruction.
3377 * @param pStrSpace The space to destroy.
3378 * @param pfnCallback The callback.
3379 * @param pvUser The user argument.
3380 */
3381RTDECL(int) RTStrSpaceDestroy(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
3382
3383/**
3384 * Enumerates the string space.
3385 * The caller supplies a callback which will be called for each of
3386 * the string nodes.
3387 *
3388 * @returns 0 or what ever non-zero return value pfnCallback returned
3389 * when aborting the destruction.
3390 * @param pStrSpace The space to enumerate.
3391 * @param pfnCallback The callback.
3392 * @param pvUser The user argument.
3393 */
3394RTDECL(int) RTStrSpaceEnumerate(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
3395
3396/** @} */
3397
3398
3399/** @defgroup rt_str_hash Sting hashing
3400 * @{ */
3401
3402/**
3403 * Hashes the given string using algorithm \#1.
3404 *
3405 * @returns String hash.
3406 * @param pszString The string to hash.
3407 */
3408RTDECL(uint32_t) RTStrHash1(const char *pszString);
3409
3410/**
3411 * Hashes the given string using algorithm \#1.
3412 *
3413 * @returns String hash.
3414 * @param pszString The string to hash.
3415 * @param cchString The max length to hash. Hashing will stop if the
3416 * terminator character is encountered first. Passing
3417 * RTSTR_MAX is fine.
3418 */
3419RTDECL(uint32_t) RTStrHash1N(const char *pszString, size_t cchString);
3420
3421/**
3422 * Hashes the given strings as if they were concatenated using algorithm \#1.
3423 *
3424 * @returns String hash.
3425 * @param cPairs The number of string / length pairs in the
3426 * ellipsis.
3427 * @param ... List of string (const char *) and length
3428 * (size_t) pairs. Passing RTSTR_MAX as the size is
3429 * fine.
3430 */
3431RTDECL(uint32_t) RTStrHash1ExN(size_t cPairs, ...);
3432
3433/**
3434 * Hashes the given strings as if they were concatenated using algorithm \#1.
3435 *
3436 * @returns String hash.
3437 * @param cPairs The number of string / length pairs in the @a va.
3438 * @param va List of string (const char *) and length
3439 * (size_t) pairs. Passing RTSTR_MAX as the size is
3440 * fine.
3441 */
3442RTDECL(uint32_t) RTStrHash1ExNV(size_t cPairs, va_list va);
3443
3444/** @} */
3445
3446
3447/** @defgroup rt_str_mem Raw memory operations.
3448 *
3449 * @note Following the memchr/memcpy/memcmp/memset tradition and putting these
3450 * in the string.h header rather than in the mem.h one.
3451 *
3452 * @{ */
3453
3454/**
3455 * Searches @a pvHaystack for a 16-bit sized and aligned @a uNeedle.
3456 *
3457 * @returns Pointer to the first hit if found, NULL if not found.
3458 * @param pvHaystack The memory to search.
3459 * @param uNeedle The 16-bit value to find.
3460 * @param cbHaystack Size of the memory to search.
3461 * @sa memchr, RTStrMemFind32, RTStrMemFind64
3462 */
3463RTDECL(uint16_t *) RTStrMemFind16(const void *pvHaystack, uint16_t uNeedle, size_t cbHaystack);
3464
3465/**
3466 * Searches @a pvHaystack for a 32-bit sized and aligned @a uNeedle.
3467 *
3468 * @returns Pointer to the first hit if found, NULL if not found.
3469 * @param pvHaystack The memory to search.
3470 * @param uNeedle The 32-bit value to find.
3471 * @param cbHaystack Size of the memory to search.
3472 * @sa memchr, RTStrMemFind16, RTStrMemFind64
3473 */
3474RTDECL(uint32_t *) RTStrMemFind32(const void *pvHaystack, uint32_t uNeedle, size_t cbHaystack);
3475
3476/**
3477 * Searches @a pvHaystack for a 64-bit sized and aligned @a uNeedle.
3478 *
3479 * @returns Pointer to the first hit if found, NULL if not found.
3480 * @param pvHaystack The memory to search.
3481 * @param uNeedle The 64-bit value to find.
3482 * @param cbHaystack Size of the memory to search.
3483 * @sa memchr, RTStrMemFind16, RTStrMemFind32
3484 */
3485RTDECL(uint64_t *) RTStrMemFind64(const void *pvHaystack, uint64_t uNeedle, size_t cbHaystack);
3486
3487/** @} */
3488
3489
3490/** @} */
3491
3492RT_C_DECLS_END
3493
3494#endif /* !IPRT_INCLUDED_string_h */
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette