VirtualBox

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

Last change on this file since 103275 was 103252, checked in by vboxsync, 16 months ago

IPRT/string: Added RTStrCopy2() as a convenient function.

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

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