VirtualBox

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

Last change on this file since 96169 was 96169, checked in by vboxsync, 2 years ago

iprt/string.h: doxygen fix. bugref:10261

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