VirtualBox

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

Last change on this file was 108960, checked in by vboxsync, 3 weeks ago

iprt/string.h: RTStrPutCp/RTStrPutCpInternal doc update.

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