VirtualBox

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

Last change on this file since 102994 was 102994, checked in by vboxsync, 13 months ago

iprt: Add wrapper for strlcpy/strscpy functions, build fix, bugref:10584.

Another way around to suppress build error when strscpy return status
is not used. Also, turn macro into strlcpy for non-Linux systems,
so for them it will be no change.

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