VirtualBox

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

Last change on this file since 86780 was 85463, checked in by vboxsync, 4 years ago

FE/Qt: bugref:9686, bugref:9510. Small cleanup

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