VirtualBox

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

Last change on this file since 97966 was 97887, checked in by vboxsync, 2 years ago

iprt/string.h: VAC++ tweak.

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