VirtualBox

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

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

Runtime: bugref:9955: Added conversion from console codepage to UTF-8 in Windows needed for proper handling the password containing national characters from console.

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

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