VirtualBox

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

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

IPRT: Added RTStrFormatR64 and RTStrFormatR32 and split out the floating point formatting from strformatnum.cpp. bugref:9898

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 145.5 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 * Formats an 32-bit extended floating point number.
1912 *
1913 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1914 * @param pszBuf The output buffer.
1915 * @param cbBuf The size of the output buffer.
1916 * @param pr80Value The value to format.
1917 * @param cchWidth Width.
1918 * @param cchPrecision Precision.
1919 * @param fFlags Flags, RTSTR_F_XXX.
1920 */
1921RTDECL(ssize_t) RTStrFormatR32(char *pszBuf, size_t cbBuf, PCRTFLOAT32U pr32Value, signed int cchWidth,
1922 signed int cchPrecision, uint32_t fFlags);
1923
1924/**
1925 * Formats an 64-bit extended floating point number.
1926 *
1927 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1928 * @param pszBuf The output buffer.
1929 * @param cbBuf The size of the output buffer.
1930 * @param pr80Value The value to format.
1931 * @param cchWidth Width.
1932 * @param cchPrecision Precision.
1933 * @param fFlags Flags, RTSTR_F_XXX.
1934 */
1935RTDECL(ssize_t) RTStrFormatR64(char *pszBuf, size_t cbBuf, PCRTFLOAT64U pr64Value, signed int cchWidth,
1936 signed int cchPrecision, uint32_t fFlags);
1937
1938/**
1939 * Formats an 80-bit extended floating point number.
1940 *
1941 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1942 * @param pszBuf The output buffer.
1943 * @param cbBuf The size of the output buffer.
1944 * @param pr80Value The value to format.
1945 * @param cchWidth Width.
1946 * @param cchPrecision Precision.
1947 * @param fFlags Flags, RTSTR_F_XXX.
1948 */
1949RTDECL(ssize_t) RTStrFormatR80(char *pszBuf, size_t cbBuf, PCRTFLOAT80U pr80Value, signed int cchWidth,
1950 signed int cchPrecision, uint32_t fFlags);
1951
1952/**
1953 * Formats an 80-bit extended floating point number, version 2.
1954 *
1955 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1956 * @param pszBuf The output buffer.
1957 * @param cbBuf The size of the output buffer.
1958 * @param pr80Value The value to format.
1959 * @param cchWidth Width.
1960 * @param cchPrecision Precision.
1961 * @param fFlags Flags, RTSTR_F_XXX.
1962 */
1963RTDECL(ssize_t) RTStrFormatR80u2(char *pszBuf, size_t cbBuf, PCRTFLOAT80U2 pr80Value, signed int cchWidth,
1964 signed int cchPrecision, uint32_t fFlags);
1965
1966
1967
1968/**
1969 * Callback for formatting a type.
1970 *
1971 * This is registered using the RTStrFormatTypeRegister function and will
1972 * be called during string formatting to handle the specified %R[type].
1973 * The argument for this format type is assumed to be a pointer and it's
1974 * passed in the @a pvValue argument.
1975 *
1976 * @returns Length of the formatted output.
1977 * @param pfnOutput Output worker.
1978 * @param pvArgOutput Argument to the output worker.
1979 * @param pszType The type name.
1980 * @param pvValue The argument value.
1981 * @param cchWidth Width.
1982 * @param cchPrecision Precision.
1983 * @param fFlags Flags (NTFS_*).
1984 * @param pvUser The user argument.
1985 */
1986typedef DECLCALLBACKTYPE(size_t, FNRTSTRFORMATTYPE,(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
1987 const char *pszType, void const *pvValue,
1988 int cchWidth, int cchPrecision, unsigned fFlags,
1989 void *pvUser));
1990/** Pointer to a FNRTSTRFORMATTYPE. */
1991typedef FNRTSTRFORMATTYPE *PFNRTSTRFORMATTYPE;
1992
1993
1994/**
1995 * Register a format handler for a type.
1996 *
1997 * The format handler is used to handle '%R[type]' format types, where the argument
1998 * in the vector is a pointer value (a bit restrictive, but keeps it simple).
1999 *
2000 * The caller must ensure that no other thread will be making use of any of
2001 * the dynamic formatting type facilities simultaneously with this call.
2002 *
2003 * @returns IPRT status code.
2004 * @retval VINF_SUCCESS on success.
2005 * @retval VERR_ALREADY_EXISTS if the type has already been registered.
2006 * @retval VERR_TOO_MANY_OPEN_FILES if all the type slots has been allocated already.
2007 *
2008 * @param pszType The type name.
2009 * @param pfnHandler The handler address. See FNRTSTRFORMATTYPE for details.
2010 * @param pvUser The user argument to pass to the handler. See RTStrFormatTypeSetUser
2011 * for how to update this later.
2012 */
2013RTDECL(int) RTStrFormatTypeRegister(const char *pszType, PFNRTSTRFORMATTYPE pfnHandler, void *pvUser);
2014
2015/**
2016 * Deregisters a format type.
2017 *
2018 * The caller must ensure that no other thread will be making use of any of
2019 * the dynamic formatting type facilities simultaneously with this call.
2020 *
2021 * @returns IPRT status code.
2022 * @retval VINF_SUCCESS on success.
2023 * @retval VERR_FILE_NOT_FOUND if not found.
2024 *
2025 * @param pszType The type to deregister.
2026 */
2027RTDECL(int) RTStrFormatTypeDeregister(const char *pszType);
2028
2029/**
2030 * Sets the user argument for a type.
2031 *
2032 * This can be used if a user argument needs relocating in GC.
2033 *
2034 * @returns IPRT status code.
2035 * @retval VINF_SUCCESS on success.
2036 * @retval VERR_FILE_NOT_FOUND if not found.
2037 *
2038 * @param pszType The type to update.
2039 * @param pvUser The new user argument value.
2040 */
2041RTDECL(int) RTStrFormatTypeSetUser(const char *pszType, void *pvUser);
2042
2043
2044/**
2045 * String printf.
2046 *
2047 * @returns The length of the returned string (in pszBuffer) excluding the
2048 * terminator.
2049 * @param pszBuffer Output buffer.
2050 * @param cchBuffer Size of the output buffer.
2051 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2052 * @param args The format argument.
2053 *
2054 * @deprecated Use RTStrPrintf2V! Problematic return value on overflow.
2055 */
2056RTDECL(size_t) RTStrPrintfV(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(3, 0);
2057
2058/**
2059 * String printf.
2060 *
2061 * @returns The length of the returned string (in pszBuffer) excluding the
2062 * terminator.
2063 * @param pszBuffer Output buffer.
2064 * @param cchBuffer Size of the output buffer.
2065 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2066 * @param ... The format argument.
2067 *
2068 * @deprecated Use RTStrPrintf2! Problematic return value on overflow.
2069 */
2070RTDECL(size_t) RTStrPrintf(char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
2071
2072/**
2073 * String printf with custom formatting.
2074 *
2075 * @returns The length of the returned string (in pszBuffer) excluding the
2076 * terminator.
2077 * @param pfnFormat Pointer to handler function for the custom formats.
2078 * @param pvArg Argument to the pfnFormat function.
2079 * @param pszBuffer Output buffer.
2080 * @param cchBuffer Size of the output buffer.
2081 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2082 * @param args The format argument.
2083 *
2084 * @deprecated Use RTStrPrintf2ExV! Problematic return value on overflow.
2085 */
2086RTDECL(size_t) RTStrPrintfExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer,
2087 const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(5, 0);
2088
2089/**
2090 * String printf with custom formatting.
2091 *
2092 * @returns The length of the returned string (in pszBuffer) excluding the
2093 * terminator.
2094 * @param pfnFormat Pointer to handler function for the custom formats.
2095 * @param pvArg Argument to the pfnFormat function.
2096 * @param pszBuffer Output buffer.
2097 * @param cchBuffer Size of the output buffer.
2098 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2099 * @param ... The format argument.
2100 *
2101 * @deprecated Use RTStrPrintf2Ex! Problematic return value on overflow.
2102 */
2103RTDECL(size_t) RTStrPrintfEx(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer,
2104 const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(5, 6);
2105
2106/**
2107 * String printf, version 2.
2108 *
2109 * @returns On success, positive count of formatted character excluding the
2110 * terminator. On buffer overflow, negative number giving the required
2111 * buffer size (including terminator char).
2112 *
2113 * @param pszBuffer Output buffer.
2114 * @param cbBuffer Size of the output buffer.
2115 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2116 * @param args The format argument.
2117 */
2118RTDECL(ssize_t) RTStrPrintf2V(char *pszBuffer, size_t cbBuffer, const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(3, 0);
2119
2120/**
2121 * String printf, version 2.
2122 *
2123 * @returns On success, positive count of formatted character excluding the
2124 * terminator. On buffer overflow, negative number giving the required
2125 * buffer size (including terminator char).
2126 *
2127 * @param pszBuffer Output buffer.
2128 * @param cbBuffer Size of the output buffer.
2129 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2130 * @param ... The format argument.
2131 */
2132RTDECL(ssize_t) RTStrPrintf2(char *pszBuffer, size_t cbBuffer, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
2133
2134/**
2135 * String printf with custom formatting, version 2.
2136 *
2137 * @returns On success, positive count of formatted character excluding the
2138 * terminator. On buffer overflow, negative number giving the required
2139 * buffer size (including terminator char).
2140 *
2141 * @param pfnFormat Pointer to handler function for the custom formats.
2142 * @param pvArg Argument to the pfnFormat function.
2143 * @param pszBuffer Output buffer.
2144 * @param cbBuffer Size of the output buffer.
2145 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2146 * @param args The format argument.
2147 */
2148RTDECL(ssize_t) RTStrPrintf2ExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cbBuffer,
2149 const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(5, 0);
2150
2151/**
2152 * String printf with custom formatting, version 2.
2153 *
2154 * @returns On success, positive count of formatted character excluding the
2155 * terminator. On buffer overflow, negative number giving the required
2156 * buffer size (including terminator char).
2157 *
2158 * @param pfnFormat Pointer to handler function for the custom formats.
2159 * @param pvArg Argument to the pfnFormat function.
2160 * @param pszBuffer Output buffer.
2161 * @param cbBuffer Size of the output buffer.
2162 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2163 * @param ... The format argument.
2164 */
2165RTDECL(ssize_t) RTStrPrintf2Ex(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cbBuffer,
2166 const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(5, 6);
2167
2168/**
2169 * Allocating string printf (default tag).
2170 *
2171 * @returns The length of the string in the returned *ppszBuffer excluding the
2172 * terminator.
2173 * @returns -1 on failure.
2174 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
2175 * The buffer should be freed using RTStrFree().
2176 * On failure *ppszBuffer will be set to NULL.
2177 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2178 * @param args The format argument.
2179 */
2180#define RTStrAPrintfV(ppszBuffer, pszFormat, args) RTStrAPrintfVTag((ppszBuffer), (pszFormat), (args), RTSTR_TAG)
2181
2182/**
2183 * Allocating string printf (custom tag).
2184 *
2185 * @returns The length of the string in the returned *ppszBuffer excluding the
2186 * terminator.
2187 * @returns -1 on failure.
2188 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
2189 * The buffer should be freed using RTStrFree().
2190 * On failure *ppszBuffer will be set to NULL.
2191 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2192 * @param args The format argument.
2193 * @param pszTag Allocation tag used for statistics and such.
2194 */
2195RTDECL(int) RTStrAPrintfVTag(char **ppszBuffer, const char *pszFormat, va_list args, const char *pszTag) RT_IPRT_FORMAT_ATTR(2, 0);
2196
2197/**
2198 * Allocating string printf.
2199 *
2200 * @returns The length of the string in the returned *ppszBuffer excluding the
2201 * terminator.
2202 * @returns -1 on failure.
2203 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
2204 * The buffer should be freed using RTStrFree().
2205 * On failure *ppszBuffer will be set to NULL.
2206 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2207 * @param ... The format argument.
2208 */
2209DECLINLINE(int) RT_IPRT_FORMAT_ATTR(2, 3) RTStrAPrintf(char **ppszBuffer, const char *pszFormat, ...)
2210{
2211 int cbRet;
2212 va_list va;
2213 va_start(va, pszFormat);
2214 cbRet = RTStrAPrintfVTag(ppszBuffer, pszFormat, va, RTSTR_TAG);
2215 va_end(va);
2216 return cbRet;
2217}
2218
2219/**
2220 * Allocating string printf (custom tag).
2221 *
2222 * @returns The length of the string in the returned *ppszBuffer excluding the
2223 * terminator.
2224 * @returns -1 on failure.
2225 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
2226 * The buffer should be freed using RTStrFree().
2227 * On failure *ppszBuffer will be set to NULL.
2228 * @param pszTag Allocation tag used for statistics and such.
2229 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2230 * @param ... The format argument.
2231 */
2232DECLINLINE(int) RT_IPRT_FORMAT_ATTR(3, 4) RTStrAPrintfTag(char **ppszBuffer, const char *pszTag, const char *pszFormat, ...)
2233{
2234 int cbRet;
2235 va_list va;
2236 va_start(va, pszFormat);
2237 cbRet = RTStrAPrintfVTag(ppszBuffer, pszFormat, va, pszTag);
2238 va_end(va);
2239 return cbRet;
2240}
2241
2242/**
2243 * Allocating string printf, version 2.
2244 *
2245 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
2246 * memory.
2247 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2248 * @param args The format argument.
2249 */
2250#define RTStrAPrintf2V(pszFormat, args) RTStrAPrintf2VTag((pszFormat), (args), RTSTR_TAG)
2251
2252/**
2253 * Allocating string printf, version 2.
2254 *
2255 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
2256 * memory.
2257 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2258 * @param args The format argument.
2259 * @param pszTag Allocation tag used for statistics and such.
2260 */
2261RTDECL(char *) RTStrAPrintf2VTag(const char *pszFormat, va_list args, const char *pszTag) RT_IPRT_FORMAT_ATTR(1, 0);
2262
2263/**
2264 * Allocating string printf, version 2 (default tag).
2265 *
2266 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
2267 * memory.
2268 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2269 * @param ... The format argument.
2270 */
2271DECLINLINE(char *) RT_IPRT_FORMAT_ATTR(1, 2) RTStrAPrintf2(const char *pszFormat, ...)
2272{
2273 char *pszRet;
2274 va_list va;
2275 va_start(va, pszFormat);
2276 pszRet = RTStrAPrintf2VTag(pszFormat, va, RTSTR_TAG);
2277 va_end(va);
2278 return pszRet;
2279}
2280
2281/**
2282 * Allocating string printf, version 2 (custom tag).
2283 *
2284 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
2285 * memory.
2286 * @param pszTag Allocation tag used for statistics and such.
2287 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2288 * @param ... The format argument.
2289 */
2290DECLINLINE(char *) RT_IPRT_FORMAT_ATTR(2, 3) RTStrAPrintf2Tag(const char *pszTag, const char *pszFormat, ...)
2291{
2292 char *pszRet;
2293 va_list va;
2294 va_start(va, pszFormat);
2295 pszRet = RTStrAPrintf2VTag(pszFormat, va, pszTag);
2296 va_end(va);
2297 return pszRet;
2298}
2299
2300/**
2301 * Strips blankspaces from both ends of the string.
2302 *
2303 * @returns Pointer to first non-blank char in the string.
2304 * @param psz The string to strip.
2305 */
2306RTDECL(char *) RTStrStrip(char *psz);
2307
2308/**
2309 * Strips blankspaces from the start of the string.
2310 *
2311 * @returns Pointer to first non-blank char in the string.
2312 * @param psz The string to strip.
2313 */
2314RTDECL(char *) RTStrStripL(const char *psz);
2315
2316/**
2317 * Strips blankspaces from the end of the string.
2318 *
2319 * @returns psz.
2320 * @param psz The string to strip.
2321 */
2322RTDECL(char *) RTStrStripR(char *psz);
2323
2324/**
2325 * String copy with overflow handling.
2326 *
2327 * @retval VINF_SUCCESS on success.
2328 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2329 * buffer will contain as much of the string as it can hold, fully
2330 * terminated.
2331 *
2332 * @param pszDst The destination buffer.
2333 * @param cbDst The size of the destination buffer (in bytes).
2334 * @param pszSrc The source string. NULL is not OK.
2335 */
2336RTDECL(int) RTStrCopy(char *pszDst, size_t cbDst, const char *pszSrc);
2337
2338/**
2339 * String copy with overflow handling.
2340 *
2341 * @retval VINF_SUCCESS on success.
2342 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2343 * buffer will contain as much of the string as it can hold, fully
2344 * terminated.
2345 *
2346 * @param pszDst The destination buffer.
2347 * @param cbDst The size of the destination buffer (in bytes).
2348 * @param pszSrc The source string. NULL is not OK.
2349 * @param cchSrcMax The maximum number of chars (not code points) to
2350 * copy from the source string, not counting the
2351 * terminator as usual.
2352 */
2353RTDECL(int) RTStrCopyEx(char *pszDst, size_t cbDst, const char *pszSrc, size_t cchSrcMax);
2354
2355/**
2356 * String copy with overflow handling and buffer advancing.
2357 *
2358 * @retval VINF_SUCCESS on success.
2359 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2360 * buffer will contain as much of the string as it can hold, fully
2361 * terminated.
2362 *
2363 * @param ppszDst Pointer to the destination buffer pointer.
2364 * This will be advanced to the end of the copied
2365 * bytes (points at the terminator). This is also
2366 * updated on overflow.
2367 * @param pcbDst Pointer to the destination buffer size
2368 * variable. This will be updated in accord with
2369 * the buffer pointer.
2370 * @param pszSrc The source string. NULL is not OK.
2371 */
2372RTDECL(int) RTStrCopyP(char **ppszDst, size_t *pcbDst, const char *pszSrc);
2373
2374/**
2375 * String copy with overflow handling.
2376 *
2377 * @retval VINF_SUCCESS on success.
2378 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2379 * buffer will contain as much of the string as it can hold, fully
2380 * terminated.
2381 *
2382 * @param ppszDst Pointer to the destination buffer pointer.
2383 * This will be advanced to the end of the copied
2384 * bytes (points at the terminator). This is also
2385 * updated on overflow.
2386 * @param pcbDst Pointer to the destination buffer size
2387 * variable. This will be updated in accord with
2388 * the buffer pointer.
2389 * @param pszSrc The source string. NULL is not OK.
2390 * @param cchSrcMax The maximum number of chars (not code points) to
2391 * copy from the source string, not counting the
2392 * terminator as usual.
2393 */
2394RTDECL(int) RTStrCopyPEx(char **ppszDst, size_t *pcbDst, const char *pszSrc, size_t cchSrcMax);
2395
2396/**
2397 * String concatenation with overflow handling.
2398 *
2399 * @retval VINF_SUCCESS on success.
2400 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2401 * buffer will contain as much of the string as it can hold, fully
2402 * terminated.
2403 *
2404 * @param pszDst The destination buffer.
2405 * @param cbDst The size of the destination buffer (in bytes).
2406 * @param pszSrc The source string. NULL is not OK.
2407 */
2408RTDECL(int) RTStrCat(char *pszDst, size_t cbDst, const char *pszSrc);
2409
2410/**
2411 * String concatenation with overflow handling.
2412 *
2413 * @retval VINF_SUCCESS on success.
2414 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2415 * buffer will contain as much of the string as it can hold, fully
2416 * terminated.
2417 *
2418 * @param pszDst The destination buffer.
2419 * @param cbDst The size of the destination buffer (in bytes).
2420 * @param pszSrc The source string. NULL is not OK.
2421 * @param cchSrcMax The maximum number of chars (not code points) to
2422 * copy from the source string, not counting the
2423 * terminator as usual.
2424 */
2425RTDECL(int) RTStrCatEx(char *pszDst, size_t cbDst, const char *pszSrc, size_t cchSrcMax);
2426
2427/**
2428 * String concatenation with overflow handling.
2429 *
2430 * @retval VINF_SUCCESS on success.
2431 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2432 * buffer will contain as much of the string as it can hold, fully
2433 * terminated.
2434 *
2435 * @param ppszDst Pointer to the destination buffer pointer.
2436 * This will be advanced to the end of the copied
2437 * bytes (points at the terminator). This is also
2438 * updated on overflow.
2439 * @param pcbDst Pointer to the destination buffer size
2440 * variable. This will be updated in accord with
2441 * the buffer pointer.
2442 * @param pszSrc The source string. NULL is not OK.
2443 */
2444RTDECL(int) RTStrCatP(char **ppszDst, size_t *pcbDst, const char *pszSrc);
2445
2446/**
2447 * String concatenation with overflow handling and buffer advancing.
2448 *
2449 * @retval VINF_SUCCESS on success.
2450 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2451 * buffer will contain as much of the string as it can hold, fully
2452 * terminated.
2453 *
2454 * @param ppszDst Pointer to the destination buffer pointer.
2455 * This will be advanced to the end of the copied
2456 * bytes (points at the terminator). This is also
2457 * updated on overflow.
2458 * @param pcbDst Pointer to the destination buffer size
2459 * variable. This will be updated in accord with
2460 * the buffer pointer.
2461 * @param pszSrc The source string. NULL is not OK.
2462 * @param cchSrcMax The maximum number of chars (not code points) to
2463 * copy from the source string, not counting the
2464 * terminator as usual.
2465 */
2466RTDECL(int) RTStrCatPEx(char **ppszDst, size_t *pcbDst, const char *pszSrc, size_t cchSrcMax);
2467
2468/**
2469 * Performs a case sensitive string compare between two UTF-8 strings.
2470 *
2471 * Encoding errors are ignored by the current implementation. So, the only
2472 * difference between this and the CRT strcmp function is the handling of
2473 * NULL arguments.
2474 *
2475 * @returns < 0 if the first string less than the second string.
2476 * @returns 0 if the first string identical to the second string.
2477 * @returns > 0 if the first string greater than the second string.
2478 * @param psz1 First UTF-8 string. Null is allowed.
2479 * @param psz2 Second UTF-8 string. Null is allowed.
2480 */
2481RTDECL(int) RTStrCmp(const char *psz1, const char *psz2);
2482
2483/**
2484 * Performs a case sensitive string compare between two UTF-8 strings, given
2485 * a maximum string length.
2486 *
2487 * Encoding errors are ignored by the current implementation. So, the only
2488 * difference between this and the CRT strncmp function is the handling of
2489 * NULL arguments.
2490 *
2491 * @returns < 0 if the first string less than the second string.
2492 * @returns 0 if the first string identical to the second string.
2493 * @returns > 0 if the first string greater than the second string.
2494 * @param psz1 First UTF-8 string. Null is allowed.
2495 * @param psz2 Second UTF-8 string. Null is allowed.
2496 * @param cchMax The maximum string length
2497 */
2498RTDECL(int) RTStrNCmp(const char *psz1, const char *psz2, size_t cchMax);
2499
2500/**
2501 * Performs a case insensitive string compare between two UTF-8 strings.
2502 *
2503 * This is a simplified compare, as only the simplified lower/upper case folding
2504 * specified by the unicode specs are used. It does not consider character pairs
2505 * as they are used in some languages, just simple upper & lower case compares.
2506 *
2507 * The result is the difference between the mismatching codepoints after they
2508 * both have been lower cased.
2509 *
2510 * If the string encoding is invalid the function will assert (strict builds)
2511 * and use RTStrCmp for the remainder of the string.
2512 *
2513 * @returns < 0 if the first string less than the second string.
2514 * @returns 0 if the first string identical to the second string.
2515 * @returns > 0 if the first string greater than the second string.
2516 * @param psz1 First UTF-8 string. Null is allowed.
2517 * @param psz2 Second UTF-8 string. Null is allowed.
2518 */
2519RTDECL(int) RTStrICmp(const char *psz1, const char *psz2);
2520
2521/**
2522 * Performs a case insensitive string compare between two UTF-8 strings, given a
2523 * maximum string length.
2524 *
2525 * This is a simplified compare, as only the simplified lower/upper case folding
2526 * specified by the unicode specs are used. It does not consider character pairs
2527 * as they are used in some languages, just simple upper & lower case compares.
2528 *
2529 * The result is the difference between the mismatching codepoints after they
2530 * both have been lower cased.
2531 *
2532 * If the string encoding is invalid the function will assert (strict builds)
2533 * and use RTStrNCmp for the remainder of the string.
2534 *
2535 * @returns < 0 if the first string less than the second string.
2536 * @returns 0 if the first string identical to the second string.
2537 * @returns > 0 if the first string greater than the second string.
2538 * @param psz1 First UTF-8 string. Null is allowed.
2539 * @param psz2 Second UTF-8 string. Null is allowed.
2540 * @param cchMax Maximum string length
2541 */
2542RTDECL(int) RTStrNICmp(const char *psz1, const char *psz2, size_t cchMax);
2543
2544/**
2545 * Performs a case insensitive string compare between a UTF-8 string and a 7-bit
2546 * ASCII string.
2547 *
2548 * This is potentially faster than RTStrICmp and drags in less dependencies. It
2549 * is really handy for hardcoded inputs.
2550 *
2551 * If the string encoding is invalid the function will assert (strict builds)
2552 * and use RTStrCmp for the remainder of the string.
2553 *
2554 * @returns < 0 if the first string less than the second string.
2555 * @returns 0 if the first string identical to the second string.
2556 * @returns > 0 if the first string greater than the second string.
2557 * @param psz1 First UTF-8 string. Null is allowed.
2558 * @param psz2 Second string, 7-bit ASCII. Null is allowed.
2559 * @sa RTStrICmp, RTUtf16ICmpAscii
2560 */
2561RTDECL(int) RTStrICmpAscii(const char *psz1, const char *psz2);
2562
2563/**
2564 * Performs a case insensitive string compare between a UTF-8 string and a 7-bit
2565 * ASCII string, given a maximum string length.
2566 *
2567 * This is potentially faster than RTStrNICmp and drags in less dependencies.
2568 * It is really handy for hardcoded inputs.
2569 *
2570 * If the string encoding is invalid the function will assert (strict builds)
2571 * and use RTStrNCmp for the remainder of the string.
2572 *
2573 * @returns < 0 if the first string less than the second string.
2574 * @returns 0 if the first string identical to the second string.
2575 * @returns > 0 if the first string greater than the second string.
2576 * @param psz1 First UTF-8 string. Null is allowed.
2577 * @param psz2 Second string, 7-bit ASCII. Null is allowed.
2578 * @param cchMax Maximum string length
2579 * @sa RTStrNICmp, RTUtf16NICmpAscii
2580 */
2581RTDECL(int) RTStrNICmpAscii(const char *psz1, const char *psz2, size_t cchMax);
2582
2583/**
2584 * Checks whether @a pszString starts with @a pszStart.
2585 *
2586 * @returns true / false.
2587 * @param pszString The string to check.
2588 * @param pszStart The start string to check for.
2589 */
2590RTDECL(bool) RTStrStartsWith(const char *pszString, const char *pszStart);
2591
2592/**
2593 * Checks whether @a pszString starts with @a pszStart, case insensitive.
2594 *
2595 * @returns true / false.
2596 * @param pszString The string to check.
2597 * @param pszStart The start string to check for.
2598 */
2599RTDECL(bool) RTStrIStartsWith(const char *pszString, const char *pszStart);
2600
2601/**
2602 * Splits a string buffer with a given separator into separate strings.
2603 * If no separators are found, no strings are returned. Consequtive separators will be skipped.
2604 *
2605 * @returns iprt status code.
2606 * @param pcszStrings String buffer to split.
2607 * @param cbStrings Size (in bytes) of string buffer to split, including terminator.
2608 * @param pcszSeparator Separator to use / find for splitting strings.
2609 * @param ppapszStrings Where to return the allocated string array on success. Needs to be free'd by the caller.
2610 * @param pcStrings Where to return the number of split strings in \a ppapszStrings.
2611 */
2612RTDECL(int) RTStrSplit(const char *pcszStrings, size_t cbStrings,
2613 const char *pcszSeparator, char ***ppapszStrings, size_t *pcStrings);
2614
2615/**
2616 * Locates a case sensitive substring.
2617 *
2618 * If any of the two strings are NULL, then NULL is returned. If the needle is
2619 * an empty string, then the haystack is returned (i.e. matches anything).
2620 *
2621 * @returns Pointer to the first occurrence of the substring if found, NULL if
2622 * not.
2623 *
2624 * @param pszHaystack The string to search.
2625 * @param pszNeedle The substring to search for.
2626 *
2627 * @remarks The difference between this and strstr is the handling of NULL
2628 * pointers.
2629 */
2630RTDECL(char *) RTStrStr(const char *pszHaystack, const char *pszNeedle);
2631
2632/**
2633 * Locates a case insensitive substring.
2634 *
2635 * If any of the two strings are NULL, then NULL is returned. If the needle is
2636 * an empty string, then the haystack is returned (i.e. matches anything).
2637 *
2638 * @returns Pointer to the first occurrence of the substring if found, NULL if
2639 * not.
2640 *
2641 * @param pszHaystack The string to search.
2642 * @param pszNeedle The substring to search for.
2643 *
2644 */
2645RTDECL(char *) RTStrIStr(const char *pszHaystack, const char *pszNeedle);
2646
2647/**
2648 * Converts the string to lower case.
2649 *
2650 * @returns Pointer to the converted string.
2651 * @param psz The string to convert.
2652 */
2653RTDECL(char *) RTStrToLower(char *psz);
2654
2655/**
2656 * Converts the string to upper case.
2657 *
2658 * @returns Pointer to the converted string.
2659 * @param psz The string to convert.
2660 */
2661RTDECL(char *) RTStrToUpper(char *psz);
2662
2663/**
2664 * Checks if the string is case foldable, i.e. whether it would change if
2665 * subject to RTStrToLower or RTStrToUpper.
2666 *
2667 * @returns true / false
2668 * @param psz The string in question.
2669 */
2670RTDECL(bool) RTStrIsCaseFoldable(const char *psz);
2671
2672/**
2673 * Checks if the string is upper cased (no lower case chars in it).
2674 *
2675 * @returns true / false
2676 * @param psz The string in question.
2677 */
2678RTDECL(bool) RTStrIsUpperCased(const char *psz);
2679
2680/**
2681 * Checks if the string is lower cased (no upper case chars in it).
2682 *
2683 * @returns true / false
2684 * @param psz The string in question.
2685 */
2686RTDECL(bool) RTStrIsLowerCased(const char *psz);
2687
2688/**
2689 * Find the length of a zero-terminated byte string, given
2690 * a max string length.
2691 *
2692 * See also RTStrNLenEx.
2693 *
2694 * @returns The string length or cbMax. The returned length does not include
2695 * the zero terminator if it was found.
2696 *
2697 * @param pszString The string.
2698 * @param cchMax The max string length.
2699 */
2700RTDECL(size_t) RTStrNLen(const char *pszString, size_t cchMax);
2701
2702/**
2703 * Find the length of a zero-terminated byte string, given
2704 * a max string length.
2705 *
2706 * See also RTStrNLen.
2707 *
2708 * @returns IPRT status code.
2709 * @retval VINF_SUCCESS if the string has a length less than cchMax.
2710 * @retval VERR_BUFFER_OVERFLOW if the end of the string wasn't found
2711 * before cchMax was reached.
2712 *
2713 * @param pszString The string.
2714 * @param cchMax The max string length.
2715 * @param pcch Where to store the string length excluding the
2716 * terminator. This is set to cchMax if the terminator
2717 * isn't found.
2718 */
2719RTDECL(int) RTStrNLenEx(const char *pszString, size_t cchMax, size_t *pcch);
2720
2721/** The maximum size argument of a memchr call. */
2722#define RTSTR_MEMCHR_MAX ((~(size_t)0 >> 1) - 15)
2723
2724/**
2725 * Find the zero terminator in a string with a limited length.
2726 *
2727 * @returns Pointer to the zero terminator.
2728 * @returns NULL if the zero terminator was not found.
2729 *
2730 * @param pszString The string.
2731 * @param cchMax The max string length. RTSTR_MAX is fine.
2732 */
2733RTDECL(char *) RTStrEnd(char const *pszString, size_t cchMax);
2734
2735/**
2736 * Finds the offset at which a simple character first occurs in a string.
2737 *
2738 * @returns The offset of the first occurence or the terminator offset.
2739 * @param pszHaystack The string to search.
2740 * @param chNeedle The character to search for.
2741 */
2742DECLINLINE(size_t) RTStrOffCharOrTerm(const char *pszHaystack, char chNeedle)
2743{
2744 const char *psz = pszHaystack;
2745 char ch;
2746 while ( (ch = *psz) != chNeedle
2747 && ch != '\0')
2748 psz++;
2749 return (size_t)(psz - pszHaystack);
2750}
2751
2752/**
2753 * Matches a simple string pattern.
2754 *
2755 * @returns true if the string matches the pattern, otherwise false.
2756 *
2757 * @param pszPattern The pattern. Special chars are '*' and '?', where the
2758 * asterisk matches zero or more characters and question
2759 * mark matches exactly one character.
2760 * @param pszString The string to match against the pattern.
2761 */
2762RTDECL(bool) RTStrSimplePatternMatch(const char *pszPattern, const char *pszString);
2763
2764/**
2765 * Matches a simple string pattern, neither which needs to be zero terminated.
2766 *
2767 * This is identical to RTStrSimplePatternMatch except that you can optionally
2768 * specify the length of both the pattern and the string. The function will
2769 * stop when it hits a string terminator or either of the lengths.
2770 *
2771 * @returns true if the string matches the pattern, otherwise false.
2772 *
2773 * @param pszPattern The pattern. Special chars are '*' and '?', where the
2774 * asterisk matches zero or more characters and question
2775 * mark matches exactly one character.
2776 * @param cchPattern The pattern length. Pass RTSTR_MAX if you don't know the
2777 * length and wish to stop at the string terminator.
2778 * @param pszString The string to match against the pattern.
2779 * @param cchString The string length. Pass RTSTR_MAX if you don't know the
2780 * length and wish to match up to the string terminator.
2781 */
2782RTDECL(bool) RTStrSimplePatternNMatch(const char *pszPattern, size_t cchPattern,
2783 const char *pszString, size_t cchString);
2784
2785/**
2786 * Matches multiple patterns against a string.
2787 *
2788 * The patterns are separated by the pipe character (|).
2789 *
2790 * @returns true if the string matches the pattern, otherwise false.
2791 *
2792 * @param pszPatterns The patterns.
2793 * @param cchPatterns The lengths of the patterns to use. Pass RTSTR_MAX to
2794 * stop at the terminator.
2795 * @param pszString The string to match against the pattern.
2796 * @param cchString The string length. Pass RTSTR_MAX stop stop at the
2797 * terminator.
2798 * @param poffPattern Offset into the patterns string of the patttern that
2799 * matched. If no match, this will be set to RTSTR_MAX.
2800 * This is optional, NULL is fine.
2801 */
2802RTDECL(bool) RTStrSimplePatternMultiMatch(const char *pszPatterns, size_t cchPatterns,
2803 const char *pszString, size_t cchString,
2804 size_t *poffPattern);
2805
2806/**
2807 * Compares two version strings RTStrICmp fashion.
2808 *
2809 * The version string is split up into sections at punctuation, spaces,
2810 * underscores, dashes and plus signs. The sections are then split up into
2811 * numeric and string sub-sections. Finally, the sub-sections are compared
2812 * in a numeric or case insesntivie fashion depending on what they are.
2813 *
2814 * The following strings are considered to be equal: "1.0.0", "1.00.0", "1.0",
2815 * "1". These aren't: "1.0.0r993", "1.0", "1.0r993", "1.0_Beta3", "1.1"
2816 *
2817 * @returns < 0 if the first string less than the second string.
2818 * @returns 0 if the first string identical to the second string.
2819 * @returns > 0 if the first string greater than the second string.
2820 *
2821 * @param pszVer1 First version string to compare.
2822 * @param pszVer2 Second version string to compare first version with.
2823 */
2824RTDECL(int) RTStrVersionCompare(const char *pszVer1, const char *pszVer2);
2825
2826
2827/** @defgroup rt_str_conv String To/From Number Conversions
2828 * @{ */
2829
2830/**
2831 * Converts a string representation of a number to a 64-bit unsigned number.
2832 *
2833 * @returns iprt status code.
2834 * Warnings are used to indicate conversion problems.
2835 * @retval VWRN_NUMBER_TOO_BIG
2836 * @retval VWRN_NEGATIVE_UNSIGNED
2837 * @retval VWRN_TRAILING_CHARS
2838 * @retval VWRN_TRAILING_SPACES
2839 * @retval VINF_SUCCESS
2840 * @retval VERR_NO_DIGITS
2841 *
2842 * @param pszValue Pointer to the string value.
2843 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2844 * @param uBase The base of the representation used.
2845 * If 0 the function will look for known prefixes before defaulting to 10.
2846 * @param pu64 Where to store the converted number. (optional)
2847 */
2848RTDECL(int) RTStrToUInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint64_t *pu64);
2849
2850/**
2851 * Converts a string representation of a number to a 64-bit unsigned number,
2852 * making sure the full string is converted.
2853 *
2854 * @returns iprt status code.
2855 * Warnings are used to indicate conversion problems.
2856 * @retval VWRN_NUMBER_TOO_BIG
2857 * @retval VWRN_NEGATIVE_UNSIGNED
2858 * @retval VINF_SUCCESS
2859 * @retval VERR_NO_DIGITS
2860 * @retval VERR_TRAILING_SPACES
2861 * @retval VERR_TRAILING_CHARS
2862 *
2863 * @param pszValue Pointer to the string value.
2864 * @param uBase The base of the representation used.
2865 * If 0 the function will look for known prefixes before defaulting to 10.
2866 * @param pu64 Where to store the converted number. (optional)
2867 */
2868RTDECL(int) RTStrToUInt64Full(const char *pszValue, unsigned uBase, uint64_t *pu64);
2869
2870/**
2871 * Converts a string representation of a number to a 64-bit unsigned number.
2872 * The base is guessed.
2873 *
2874 * @returns 64-bit unsigned number on success.
2875 * @returns 0 on failure.
2876 * @param pszValue Pointer to the string value.
2877 */
2878RTDECL(uint64_t) RTStrToUInt64(const char *pszValue);
2879
2880/**
2881 * Converts a string representation of a number to a 32-bit unsigned number.
2882 *
2883 * @returns iprt status code.
2884 * Warnings are used to indicate conversion problems.
2885 * @retval VWRN_NUMBER_TOO_BIG
2886 * @retval VWRN_NEGATIVE_UNSIGNED
2887 * @retval VWRN_TRAILING_CHARS
2888 * @retval VWRN_TRAILING_SPACES
2889 * @retval VINF_SUCCESS
2890 * @retval VERR_NO_DIGITS
2891 *
2892 * @param pszValue Pointer to the string value.
2893 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2894 * @param uBase The base of the representation used.
2895 * If 0 the function will look for known prefixes before defaulting to 10.
2896 * @param pu32 Where to store the converted number. (optional)
2897 */
2898RTDECL(int) RTStrToUInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint32_t *pu32);
2899
2900/**
2901 * Converts a string representation of a number to a 32-bit unsigned number,
2902 * making sure the full string is converted.
2903 *
2904 * @returns iprt status code.
2905 * Warnings are used to indicate conversion problems.
2906 * @retval VWRN_NUMBER_TOO_BIG
2907 * @retval VWRN_NEGATIVE_UNSIGNED
2908 * @retval VINF_SUCCESS
2909 * @retval VERR_NO_DIGITS
2910 * @retval VERR_TRAILING_SPACES
2911 * @retval VERR_TRAILING_CHARS
2912 *
2913 * @param pszValue Pointer to the string value.
2914 * @param uBase The base of the representation used.
2915 * If 0 the function will look for known prefixes before defaulting to 10.
2916 * @param pu32 Where to store the converted number. (optional)
2917 */
2918RTDECL(int) RTStrToUInt32Full(const char *pszValue, unsigned uBase, uint32_t *pu32);
2919
2920/**
2921 * Converts a string representation of a number to a 32-bit unsigned number.
2922 * The base is guessed.
2923 *
2924 * @returns 32-bit unsigned number on success.
2925 * @returns 0 on failure.
2926 * @param pszValue Pointer to the string value.
2927 */
2928RTDECL(uint32_t) RTStrToUInt32(const char *pszValue);
2929
2930/**
2931 * Converts a string representation of a number to a 16-bit unsigned number.
2932 *
2933 * @returns iprt status code.
2934 * Warnings are used to indicate conversion problems.
2935 * @retval VWRN_NUMBER_TOO_BIG
2936 * @retval VWRN_NEGATIVE_UNSIGNED
2937 * @retval VWRN_TRAILING_CHARS
2938 * @retval VWRN_TRAILING_SPACES
2939 * @retval VINF_SUCCESS
2940 * @retval VERR_NO_DIGITS
2941 *
2942 * @param pszValue Pointer to the string value.
2943 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2944 * @param uBase The base of the representation used.
2945 * If 0 the function will look for known prefixes before defaulting to 10.
2946 * @param pu16 Where to store the converted number. (optional)
2947 */
2948RTDECL(int) RTStrToUInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint16_t *pu16);
2949
2950/**
2951 * Converts a string representation of a number to a 16-bit unsigned number,
2952 * making sure the full string is converted.
2953 *
2954 * @returns iprt status code.
2955 * Warnings are used to indicate conversion problems.
2956 * @retval VWRN_NUMBER_TOO_BIG
2957 * @retval VWRN_NEGATIVE_UNSIGNED
2958 * @retval VINF_SUCCESS
2959 * @retval VERR_NO_DIGITS
2960 * @retval VERR_TRAILING_SPACES
2961 * @retval VERR_TRAILING_CHARS
2962 *
2963 * @param pszValue Pointer to the string value.
2964 * @param uBase The base of the representation used.
2965 * If 0 the function will look for known prefixes before defaulting to 10.
2966 * @param pu16 Where to store the converted number. (optional)
2967 */
2968RTDECL(int) RTStrToUInt16Full(const char *pszValue, unsigned uBase, uint16_t *pu16);
2969
2970/**
2971 * Converts a string representation of a number to a 16-bit unsigned number.
2972 * The base is guessed.
2973 *
2974 * @returns 16-bit unsigned number on success.
2975 * @returns 0 on failure.
2976 * @param pszValue Pointer to the string value.
2977 */
2978RTDECL(uint16_t) RTStrToUInt16(const char *pszValue);
2979
2980/**
2981 * Converts a string representation of a number to a 8-bit unsigned number.
2982 *
2983 * @returns iprt status code.
2984 * Warnings are used to indicate conversion problems.
2985 * @retval VWRN_NUMBER_TOO_BIG
2986 * @retval VWRN_NEGATIVE_UNSIGNED
2987 * @retval VWRN_TRAILING_CHARS
2988 * @retval VWRN_TRAILING_SPACES
2989 * @retval VINF_SUCCESS
2990 * @retval VERR_NO_DIGITS
2991 *
2992 * @param pszValue Pointer to the string value.
2993 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2994 * @param uBase The base of the representation used.
2995 * If 0 the function will look for known prefixes before defaulting to 10.
2996 * @param pu8 Where to store the converted number. (optional)
2997 */
2998RTDECL(int) RTStrToUInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint8_t *pu8);
2999
3000/**
3001 * Converts a string representation of a number to a 8-bit unsigned number,
3002 * making sure the full string is converted.
3003 *
3004 * @returns iprt status code.
3005 * Warnings are used to indicate conversion problems.
3006 * @retval VWRN_NUMBER_TOO_BIG
3007 * @retval VWRN_NEGATIVE_UNSIGNED
3008 * @retval VINF_SUCCESS
3009 * @retval VERR_NO_DIGITS
3010 * @retval VERR_TRAILING_SPACES
3011 * @retval VERR_TRAILING_CHARS
3012 *
3013 * @param pszValue Pointer to the string value.
3014 * @param uBase The base of the representation used.
3015 * If 0 the function will look for known prefixes before defaulting to 10.
3016 * @param pu8 Where to store the converted number. (optional)
3017 */
3018RTDECL(int) RTStrToUInt8Full(const char *pszValue, unsigned uBase, uint8_t *pu8);
3019
3020/**
3021 * Converts a string representation of a number to a 8-bit unsigned number.
3022 * The base is guessed.
3023 *
3024 * @returns 8-bit unsigned number on success.
3025 * @returns 0 on failure.
3026 * @param pszValue Pointer to the string value.
3027 */
3028RTDECL(uint8_t) RTStrToUInt8(const char *pszValue);
3029
3030/**
3031 * Converts a string representation of a number to a 64-bit signed number.
3032 *
3033 * @returns iprt status code.
3034 * Warnings are used to indicate conversion problems.
3035 * @retval VWRN_NUMBER_TOO_BIG
3036 * @retval VWRN_TRAILING_CHARS
3037 * @retval VWRN_TRAILING_SPACES
3038 * @retval VINF_SUCCESS
3039 * @retval VERR_NO_DIGITS
3040 *
3041 * @param pszValue Pointer to the string value.
3042 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
3043 * @param uBase The base of the representation used.
3044 * If 0 the function will look for known prefixes before defaulting to 10.
3045 * @param pi64 Where to store the converted number. (optional)
3046 */
3047RTDECL(int) RTStrToInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, int64_t *pi64);
3048
3049/**
3050 * Converts a string representation of a number to a 64-bit signed number,
3051 * making sure the full string is converted.
3052 *
3053 * @returns iprt status code.
3054 * Warnings are used to indicate conversion problems.
3055 * @retval VWRN_NUMBER_TOO_BIG
3056 * @retval VINF_SUCCESS
3057 * @retval VERR_TRAILING_CHARS
3058 * @retval VERR_TRAILING_SPACES
3059 * @retval VERR_NO_DIGITS
3060 *
3061 * @param pszValue Pointer to the string value.
3062 * @param uBase The base of the representation used.
3063 * If 0 the function will look for known prefixes before defaulting to 10.
3064 * @param pi64 Where to store the converted number. (optional)
3065 */
3066RTDECL(int) RTStrToInt64Full(const char *pszValue, unsigned uBase, int64_t *pi64);
3067
3068/**
3069 * Converts a string representation of a number to a 64-bit signed number.
3070 * The base is guessed.
3071 *
3072 * @returns 64-bit signed number on success.
3073 * @returns 0 on failure.
3074 * @param pszValue Pointer to the string value.
3075 */
3076RTDECL(int64_t) RTStrToInt64(const char *pszValue);
3077
3078/**
3079 * Converts a string representation of a number to a 32-bit signed number.
3080 *
3081 * @returns iprt status code.
3082 * Warnings are used to indicate conversion problems.
3083 * @retval VWRN_NUMBER_TOO_BIG
3084 * @retval VWRN_TRAILING_CHARS
3085 * @retval VWRN_TRAILING_SPACES
3086 * @retval VINF_SUCCESS
3087 * @retval VERR_NO_DIGITS
3088 *
3089 * @param pszValue Pointer to the string value.
3090 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
3091 * @param uBase The base of the representation used.
3092 * If 0 the function will look for known prefixes before defaulting to 10.
3093 * @param pi32 Where to store the converted number. (optional)
3094 */
3095RTDECL(int) RTStrToInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, int32_t *pi32);
3096
3097/**
3098 * Converts a string representation of a number to a 32-bit signed number,
3099 * making sure the full string is converted.
3100 *
3101 * @returns iprt status code.
3102 * Warnings are used to indicate conversion problems.
3103 * @retval VWRN_NUMBER_TOO_BIG
3104 * @retval VINF_SUCCESS
3105 * @retval VERR_TRAILING_CHARS
3106 * @retval VERR_TRAILING_SPACES
3107 * @retval VERR_NO_DIGITS
3108 *
3109 * @param pszValue Pointer to the string value.
3110 * @param uBase The base of the representation used.
3111 * If 0 the function will look for known prefixes before defaulting to 10.
3112 * @param pi32 Where to store the converted number. (optional)
3113 */
3114RTDECL(int) RTStrToInt32Full(const char *pszValue, unsigned uBase, int32_t *pi32);
3115
3116/**
3117 * Converts a string representation of a number to a 32-bit signed number.
3118 * The base is guessed.
3119 *
3120 * @returns 32-bit signed number on success.
3121 * @returns 0 on failure.
3122 * @param pszValue Pointer to the string value.
3123 */
3124RTDECL(int32_t) RTStrToInt32(const char *pszValue);
3125
3126/**
3127 * Converts a string representation of a number to a 16-bit signed number.
3128 *
3129 * @returns iprt status code.
3130 * Warnings are used to indicate conversion problems.
3131 * @retval VWRN_NUMBER_TOO_BIG
3132 * @retval VWRN_TRAILING_CHARS
3133 * @retval VWRN_TRAILING_SPACES
3134 * @retval VINF_SUCCESS
3135 * @retval VERR_NO_DIGITS
3136 *
3137 * @param pszValue Pointer to the string value.
3138 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
3139 * @param uBase The base of the representation used.
3140 * If 0 the function will look for known prefixes before defaulting to 10.
3141 * @param pi16 Where to store the converted number. (optional)
3142 */
3143RTDECL(int) RTStrToInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, int16_t *pi16);
3144
3145/**
3146 * Converts a string representation of a number to a 16-bit signed number,
3147 * making sure the full string is converted.
3148 *
3149 * @returns iprt status code.
3150 * Warnings are used to indicate conversion problems.
3151 * @retval VWRN_NUMBER_TOO_BIG
3152 * @retval VINF_SUCCESS
3153 * @retval VERR_TRAILING_CHARS
3154 * @retval VERR_TRAILING_SPACES
3155 * @retval VERR_NO_DIGITS
3156 *
3157 * @param pszValue Pointer to the string value.
3158 * @param uBase The base of the representation used.
3159 * If 0 the function will look for known prefixes before defaulting to 10.
3160 * @param pi16 Where to store the converted number. (optional)
3161 */
3162RTDECL(int) RTStrToInt16Full(const char *pszValue, unsigned uBase, int16_t *pi16);
3163
3164/**
3165 * Converts a string representation of a number to a 16-bit signed number.
3166 * The base is guessed.
3167 *
3168 * @returns 16-bit signed number on success.
3169 * @returns 0 on failure.
3170 * @param pszValue Pointer to the string value.
3171 */
3172RTDECL(int16_t) RTStrToInt16(const char *pszValue);
3173
3174/**
3175 * Converts a string representation of a number to a 8-bit signed number.
3176 *
3177 * @returns iprt status code.
3178 * Warnings are used to indicate conversion problems.
3179 * @retval VWRN_NUMBER_TOO_BIG
3180 * @retval VWRN_TRAILING_CHARS
3181 * @retval VWRN_TRAILING_SPACES
3182 * @retval VINF_SUCCESS
3183 * @retval VERR_NO_DIGITS
3184 *
3185 * @param pszValue Pointer to the string value.
3186 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
3187 * @param uBase The base of the representation used.
3188 * If 0 the function will look for known prefixes before defaulting to 10.
3189 * @param pi8 Where to store the converted number. (optional)
3190 */
3191RTDECL(int) RTStrToInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, int8_t *pi8);
3192
3193/**
3194 * Converts a string representation of a number to a 8-bit signed number,
3195 * making sure the full string is converted.
3196 *
3197 * @returns iprt status code.
3198 * Warnings are used to indicate conversion problems.
3199 * @retval VWRN_NUMBER_TOO_BIG
3200 * @retval VINF_SUCCESS
3201 * @retval VERR_TRAILING_CHARS
3202 * @retval VERR_TRAILING_SPACES
3203 * @retval VERR_NO_DIGITS
3204 *
3205 * @param pszValue Pointer to the string value.
3206 * @param uBase The base of the representation used.
3207 * If 0 the function will look for known prefixes before defaulting to 10.
3208 * @param pi8 Where to store the converted number. (optional)
3209 */
3210RTDECL(int) RTStrToInt8Full(const char *pszValue, unsigned uBase, int8_t *pi8);
3211
3212/**
3213 * Converts a string representation of a number to a 8-bit signed number.
3214 * The base is guessed.
3215 *
3216 * @returns 8-bit signed number on success.
3217 * @returns 0 on failure.
3218 * @param pszValue Pointer to the string value.
3219 */
3220RTDECL(int8_t) RTStrToInt8(const char *pszValue);
3221
3222/**
3223 * Formats a buffer stream as hex bytes.
3224 *
3225 * The default is no separating spaces or line breaks or anything.
3226 *
3227 * @returns IPRT status code.
3228 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
3229 * @retval VERR_BUFFER_OVERFLOW if the buffer is insufficent to hold the bytes.
3230 *
3231 * @param pszBuf Output string buffer.
3232 * @param cbBuf The size of the output buffer.
3233 * @param pv Pointer to the bytes to stringify.
3234 * @param cb The number of bytes to stringify.
3235 * @param fFlags Combination of RTSTRPRINTHEXBYTES_F_XXX values.
3236 * @sa RTUtf16PrintHexBytes.
3237 */
3238RTDECL(int) RTStrPrintHexBytes(char *pszBuf, size_t cbBuf, void const *pv, size_t cb, uint32_t fFlags);
3239/** @name RTSTRPRINTHEXBYTES_F_XXX - flags for RTStrPrintHexBytes and RTUtf16PritnHexBytes.
3240 * @{ */
3241/** Upper case hex digits, the default is lower case. */
3242#define RTSTRPRINTHEXBYTES_F_UPPER RT_BIT(0)
3243/** Add a space between each group. */
3244#define RTSTRPRINTHEXBYTES_F_SEP_SPACE RT_BIT(1)
3245/** Add a colon between each group. */
3246#define RTSTRPRINTHEXBYTES_F_SEP_COLON RT_BIT(2)
3247/** @} */
3248
3249/**
3250 * Converts a string of hex bytes back into binary data.
3251 *
3252 * @returns IPRT status code.
3253 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
3254 * @retval VERR_BUFFER_OVERFLOW if the string contains too many hex bytes.
3255 * @retval VERR_BUFFER_UNDERFLOW if there aren't enough hex bytes to fill up
3256 * the output buffer.
3257 * @retval VERR_UNEVEN_INPUT if the input contains a half byte.
3258 * @retval VERR_NO_DIGITS
3259 * @retval VWRN_TRAILING_CHARS
3260 * @retval VWRN_TRAILING_SPACES
3261 *
3262 * @param pszHex The string containing the hex bytes.
3263 * @param pv Output buffer.
3264 * @param cb The size of the output buffer.
3265 * @param fFlags RTSTRCONVERTHEXBYTES_F_XXX.
3266 */
3267RTDECL(int) RTStrConvertHexBytes(char const *pszHex, void *pv, size_t cb, uint32_t fFlags);
3268
3269/** @name RTSTRCONVERTHEXBYTES_F_XXX - Flags for RTStrConvertHexBytes() and RTStrConvertHexBytesEx().
3270 * @{ */
3271/** Accept colon as a byte separator. */
3272#define RTSTRCONVERTHEXBYTES_F_SEP_COLON RT_BIT(0)
3273/** @} */
3274
3275/**
3276 * Converts a string of hex bytes back into binary data, extended version.
3277 *
3278 * @returns IPRT status code.
3279 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
3280 * @retval VERR_BUFFER_OVERFLOW if the string contains too many hex bytes.
3281 * @retval VERR_BUFFER_UNDERFLOW if there aren't enough hex bytes to fill up
3282 * the output buffer and *pcbReturned is NULL.
3283 * @retval VINF_BUFFER_UNDERFLOW if there aren't enough hex bytes to fill up
3284 * the output buffer and *pcbReturned is not NULL, *pcbReturned holds
3285 * the actual number of bytes.
3286 * @retval VERR_UNEVEN_INPUT if the input contains a half byte.
3287 * @retval VERR_NO_DIGITS
3288 * @retval VWRN_TRAILING_CHARS
3289 * @retval VWRN_TRAILING_SPACES
3290 *
3291 * @param pszHex The string containing the hex bytes.
3292 * @param pv Output buffer.
3293 * @param cb The size of the output buffer.
3294 * @param fFlags RTSTRCONVERTHEXBYTES_F_XXX.
3295 * @param ppszNext Set to point at where we stopped decoding hex bytes.
3296 * Optional.
3297 * @param pcbReturned Where to return the number of bytes found. Optional.
3298 */
3299RTDECL(int) RTStrConvertHexBytesEx(char const *pszHex, void *pv, size_t cb, uint32_t fFlags,
3300 const char **ppszNext, size_t *pcbReturned);
3301
3302/** @} */
3303
3304
3305/** @defgroup rt_str_space Unique String Space
3306 * @{
3307 */
3308
3309/** Pointer to a string name space container node core. */
3310typedef struct RTSTRSPACECORE *PRTSTRSPACECORE;
3311/** Pointer to a pointer to a string name space container node core. */
3312typedef PRTSTRSPACECORE *PPRTSTRSPACECORE;
3313
3314/**
3315 * String name space container node core.
3316 */
3317typedef struct RTSTRSPACECORE
3318{
3319 /** Pointer to the left leaf node. Don't touch. */
3320 PRTSTRSPACECORE pLeft;
3321 /** Pointer to the left right node. Don't touch. */
3322 PRTSTRSPACECORE pRight;
3323 /** Pointer to the list of string with the same hash key value. Don't touch. */
3324 PRTSTRSPACECORE pList;
3325 /** Hash key. Don't touch. */
3326 uint32_t Key;
3327 /** Height of this tree: max(heigth(left), heigth(right)) + 1. Don't touch */
3328 unsigned char uchHeight;
3329 /** The string length. Read only! */
3330 size_t cchString;
3331 /** Pointer to the string. Read only! */
3332 const char *pszString;
3333} RTSTRSPACECORE;
3334
3335/** String space. (Initialize with NULL.) */
3336typedef PRTSTRSPACECORE RTSTRSPACE;
3337/** Pointer to a string space. */
3338typedef PPRTSTRSPACECORE PRTSTRSPACE;
3339
3340
3341/**
3342 * Inserts a string into a unique string space.
3343 *
3344 * @returns true on success.
3345 * @returns false if the string collided with an existing string.
3346 * @param pStrSpace The space to insert it into.
3347 * @param pStr The string node.
3348 */
3349RTDECL(bool) RTStrSpaceInsert(PRTSTRSPACE pStrSpace, PRTSTRSPACECORE pStr);
3350
3351/**
3352 * Removes a string from a unique string space.
3353 *
3354 * @returns Pointer to the removed string node.
3355 * @returns NULL if the string was not found in the string space.
3356 * @param pStrSpace The space to remove it from.
3357 * @param pszString The string to remove.
3358 */
3359RTDECL(PRTSTRSPACECORE) RTStrSpaceRemove(PRTSTRSPACE pStrSpace, const char *pszString);
3360
3361/**
3362 * Gets a string from a unique string space.
3363 *
3364 * @returns Pointer to the string node.
3365 * @returns NULL if the string was not found in the string space.
3366 * @param pStrSpace The space to get it from.
3367 * @param pszString The string to get.
3368 */
3369RTDECL(PRTSTRSPACECORE) RTStrSpaceGet(PRTSTRSPACE pStrSpace, const char *pszString);
3370
3371/**
3372 * Gets a string from a unique string space.
3373 *
3374 * @returns Pointer to the string node.
3375 * @returns NULL if the string was not found in the string space.
3376 * @param pStrSpace The space to get it from.
3377 * @param pszString The string to get.
3378 * @param cchMax The max string length to evaluate. Passing
3379 * RTSTR_MAX is ok and makes it behave just like
3380 * RTStrSpaceGet.
3381 */
3382RTDECL(PRTSTRSPACECORE) RTStrSpaceGetN(PRTSTRSPACE pStrSpace, const char *pszString, size_t cchMax);
3383
3384/**
3385 * Callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy().
3386 *
3387 * @returns 0 on continue.
3388 * @returns Non-zero to aborts the operation.
3389 * @param pStr The string node
3390 * @param pvUser The user specified argument.
3391 */
3392typedef DECLCALLBACKTYPE(int, FNRTSTRSPACECALLBACK,(PRTSTRSPACECORE pStr, void *pvUser));
3393/** Pointer to callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy(). */
3394typedef FNRTSTRSPACECALLBACK *PFNRTSTRSPACECALLBACK;
3395
3396/**
3397 * Destroys the string space.
3398 *
3399 * The caller supplies a callback which will be called for each of the string
3400 * nodes in for freeing their memory and other resources.
3401 *
3402 * @returns 0 or what ever non-zero return value pfnCallback returned
3403 * when aborting the destruction.
3404 * @param pStrSpace The space to destroy.
3405 * @param pfnCallback The callback.
3406 * @param pvUser The user argument.
3407 */
3408RTDECL(int) RTStrSpaceDestroy(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
3409
3410/**
3411 * Enumerates the string space.
3412 * The caller supplies a callback which will be called for each of
3413 * the string nodes.
3414 *
3415 * @returns 0 or what ever non-zero return value pfnCallback returned
3416 * when aborting the destruction.
3417 * @param pStrSpace The space to enumerate.
3418 * @param pfnCallback The callback.
3419 * @param pvUser The user argument.
3420 */
3421RTDECL(int) RTStrSpaceEnumerate(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
3422
3423/** @} */
3424
3425
3426/** @defgroup rt_str_hash Sting hashing
3427 * @{ */
3428
3429/**
3430 * Hashes the given string using algorithm \#1.
3431 *
3432 * @returns String hash.
3433 * @param pszString The string to hash.
3434 */
3435RTDECL(uint32_t) RTStrHash1(const char *pszString);
3436
3437/**
3438 * Hashes the given string using algorithm \#1.
3439 *
3440 * @returns String hash.
3441 * @param pszString The string to hash.
3442 * @param cchString The max length to hash. Hashing will stop if the
3443 * terminator character is encountered first. Passing
3444 * RTSTR_MAX is fine.
3445 */
3446RTDECL(uint32_t) RTStrHash1N(const char *pszString, size_t cchString);
3447
3448/**
3449 * Hashes the given strings as if they were concatenated using algorithm \#1.
3450 *
3451 * @returns String hash.
3452 * @param cPairs The number of string / length pairs in the
3453 * ellipsis.
3454 * @param ... List of string (const char *) and length
3455 * (size_t) pairs. Passing RTSTR_MAX as the size is
3456 * fine.
3457 */
3458RTDECL(uint32_t) RTStrHash1ExN(size_t cPairs, ...);
3459
3460/**
3461 * Hashes the given strings as if they were concatenated using algorithm \#1.
3462 *
3463 * @returns String hash.
3464 * @param cPairs The number of string / length pairs in the @a va.
3465 * @param va List of string (const char *) and length
3466 * (size_t) pairs. Passing RTSTR_MAX as the size is
3467 * fine.
3468 */
3469RTDECL(uint32_t) RTStrHash1ExNV(size_t cPairs, va_list va);
3470
3471/** @} */
3472
3473
3474/** @defgroup rt_str_mem Raw memory operations.
3475 *
3476 * @note Following the memchr/memcpy/memcmp/memset tradition and putting these
3477 * in the string.h header rather than in the mem.h one.
3478 *
3479 * @{ */
3480
3481/**
3482 * Searches @a pvHaystack for a 16-bit sized and aligned @a uNeedle.
3483 *
3484 * @returns Pointer to the first hit if found, NULL if not found.
3485 * @param pvHaystack The memory to search.
3486 * @param uNeedle The 16-bit value to find.
3487 * @param cbHaystack Size of the memory to search.
3488 * @sa memchr, RTStrMemFind32, RTStrMemFind64
3489 */
3490RTDECL(uint16_t *) RTStrMemFind16(const void *pvHaystack, uint16_t uNeedle, size_t cbHaystack);
3491
3492/**
3493 * Searches @a pvHaystack for a 32-bit sized and aligned @a uNeedle.
3494 *
3495 * @returns Pointer to the first hit if found, NULL if not found.
3496 * @param pvHaystack The memory to search.
3497 * @param uNeedle The 32-bit value to find.
3498 * @param cbHaystack Size of the memory to search.
3499 * @sa memchr, RTStrMemFind16, RTStrMemFind64
3500 */
3501RTDECL(uint32_t *) RTStrMemFind32(const void *pvHaystack, uint32_t uNeedle, size_t cbHaystack);
3502
3503/**
3504 * Searches @a pvHaystack for a 64-bit sized and aligned @a uNeedle.
3505 *
3506 * @returns Pointer to the first hit if found, NULL if not found.
3507 * @param pvHaystack The memory to search.
3508 * @param uNeedle The 64-bit value to find.
3509 * @param cbHaystack Size of the memory to search.
3510 * @sa memchr, RTStrMemFind16, RTStrMemFind32
3511 */
3512RTDECL(uint64_t *) RTStrMemFind64(const void *pvHaystack, uint64_t uNeedle, size_t cbHaystack);
3513
3514/** @} */
3515
3516
3517/** @} */
3518
3519RT_C_DECLS_END
3520
3521#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