VirtualBox

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

Last change on this file since 102989 was 102989, checked in by vboxsync, 10 months ago

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

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