VirtualBox

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

Last change on this file since 101357 was 101357, checked in by vboxsync, 14 months ago

iprt: string.h: Move unfortified memcpy wrapper for Linux kernel code into common place, bugref:10209.

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

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