VirtualBox

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

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

scm --update-copyright-year

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

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