VirtualBox

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

Last change on this file since 97262 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 155.9 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/**
1949 * Formats an 80-bit extended floating point number.
1950 *
1951 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1952 * @param pszBuf The output buffer.
1953 * @param cbBuf The size of the output buffer.
1954 * @param pr80Value The value to format.
1955 * @param cchWidth Width.
1956 * @param cchPrecision Precision.
1957 * @param fFlags Flags, RTSTR_F_XXX.
1958 */
1959RTDECL(ssize_t) RTStrFormatR80(char *pszBuf, size_t cbBuf, PCRTFLOAT80U pr80Value, signed int cchWidth,
1960 signed int cchPrecision, uint32_t fFlags);
1961
1962/**
1963 * Formats an 80-bit extended floating point number, version 2.
1964 *
1965 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1966 * @param pszBuf The output buffer.
1967 * @param cbBuf The size of the output buffer.
1968 * @param pr80Value The value to format.
1969 * @param cchWidth Width.
1970 * @param cchPrecision Precision.
1971 * @param fFlags Flags, RTSTR_F_XXX.
1972 */
1973RTDECL(ssize_t) RTStrFormatR80u2(char *pszBuf, size_t cbBuf, PCRTFLOAT80U2 pr80Value, signed int cchWidth,
1974 signed int cchPrecision, uint32_t fFlags);
1975
1976
1977
1978/**
1979 * Callback for formatting a type.
1980 *
1981 * This is registered using the RTStrFormatTypeRegister function and will
1982 * be called during string formatting to handle the specified %R[type].
1983 * The argument for this format type is assumed to be a pointer and it's
1984 * passed in the @a pvValue argument.
1985 *
1986 * @returns Length of the formatted output.
1987 * @param pfnOutput Output worker.
1988 * @param pvArgOutput Argument to the output worker.
1989 * @param pszType The type name.
1990 * @param pvValue The argument value.
1991 * @param cchWidth Width.
1992 * @param cchPrecision Precision.
1993 * @param fFlags Flags (NTFS_*).
1994 * @param pvUser The user argument.
1995 */
1996typedef DECLCALLBACKTYPE(size_t, FNRTSTRFORMATTYPE,(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
1997 const char *pszType, void const *pvValue,
1998 int cchWidth, int cchPrecision, unsigned fFlags,
1999 void *pvUser));
2000/** Pointer to a FNRTSTRFORMATTYPE. */
2001typedef FNRTSTRFORMATTYPE *PFNRTSTRFORMATTYPE;
2002
2003
2004/**
2005 * Register a format handler for a type.
2006 *
2007 * The format handler is used to handle '%R[type]' format types, where the argument
2008 * in the vector is a pointer value (a bit restrictive, but keeps it simple).
2009 *
2010 * The caller must ensure that no other thread will be making use of any of
2011 * the dynamic formatting type facilities simultaneously with this call.
2012 *
2013 * @returns IPRT status code.
2014 * @retval VINF_SUCCESS on success.
2015 * @retval VERR_ALREADY_EXISTS if the type has already been registered.
2016 * @retval VERR_TOO_MANY_OPEN_FILES if all the type slots has been allocated already.
2017 *
2018 * @param pszType The type name.
2019 * @param pfnHandler The handler address. See FNRTSTRFORMATTYPE for details.
2020 * @param pvUser The user argument to pass to the handler. See RTStrFormatTypeSetUser
2021 * for how to update this later.
2022 */
2023RTDECL(int) RTStrFormatTypeRegister(const char *pszType, PFNRTSTRFORMATTYPE pfnHandler, void *pvUser);
2024
2025/**
2026 * Deregisters a format type.
2027 *
2028 * The caller must ensure that no other thread will be making use of any of
2029 * the dynamic formatting type facilities simultaneously with this call.
2030 *
2031 * @returns IPRT status code.
2032 * @retval VINF_SUCCESS on success.
2033 * @retval VERR_FILE_NOT_FOUND if not found.
2034 *
2035 * @param pszType The type to deregister.
2036 */
2037RTDECL(int) RTStrFormatTypeDeregister(const char *pszType);
2038
2039/**
2040 * Sets the user argument for a type.
2041 *
2042 * This can be used if a user argument needs relocating in GC.
2043 *
2044 * @returns IPRT status code.
2045 * @retval VINF_SUCCESS on success.
2046 * @retval VERR_FILE_NOT_FOUND if not found.
2047 *
2048 * @param pszType The type to update.
2049 * @param pvUser The new user argument value.
2050 */
2051RTDECL(int) RTStrFormatTypeSetUser(const char *pszType, void *pvUser);
2052
2053
2054/**
2055 * String printf.
2056 *
2057 * @returns The length of the returned string (in pszBuffer) excluding the
2058 * terminator.
2059 * @param pszBuffer Output buffer.
2060 * @param cchBuffer Size of the output buffer.
2061 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2062 * @param args The format argument.
2063 *
2064 * @deprecated Use RTStrPrintf2V! Problematic return value on overflow.
2065 */
2066RTDECL(size_t) RTStrPrintfV(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(3, 0);
2067
2068/**
2069 * String printf.
2070 *
2071 * @returns The length of the returned string (in pszBuffer) excluding the
2072 * terminator.
2073 * @param pszBuffer Output buffer.
2074 * @param cchBuffer Size of the output buffer.
2075 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2076 * @param ... The format argument.
2077 *
2078 * @deprecated Use RTStrPrintf2! Problematic return value on overflow.
2079 */
2080RTDECL(size_t) RTStrPrintf(char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
2081
2082/**
2083 * String printf with custom formatting.
2084 *
2085 * @returns The length of the returned string (in pszBuffer) excluding the
2086 * terminator.
2087 * @param pfnFormat Pointer to handler function for the custom formats.
2088 * @param pvArg Argument to the pfnFormat function.
2089 * @param pszBuffer Output buffer.
2090 * @param cchBuffer Size of the output buffer.
2091 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2092 * @param args The format argument.
2093 *
2094 * @deprecated Use RTStrPrintf2ExV! Problematic return value on overflow.
2095 */
2096RTDECL(size_t) RTStrPrintfExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer,
2097 const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(5, 0);
2098
2099/**
2100 * String printf with custom formatting.
2101 *
2102 * @returns The length of the returned string (in pszBuffer) excluding the
2103 * terminator.
2104 * @param pfnFormat Pointer to handler function for the custom formats.
2105 * @param pvArg Argument to the pfnFormat function.
2106 * @param pszBuffer Output buffer.
2107 * @param cchBuffer Size of the output buffer.
2108 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2109 * @param ... The format argument.
2110 *
2111 * @deprecated Use RTStrPrintf2Ex! Problematic return value on overflow.
2112 */
2113RTDECL(size_t) RTStrPrintfEx(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer,
2114 const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(5, 6);
2115
2116/**
2117 * String printf, version 2.
2118 *
2119 * @returns On success, positive count of formatted character excluding the
2120 * terminator. On buffer overflow, negative number giving the required
2121 * buffer size (including terminator char).
2122 *
2123 * @param pszBuffer Output buffer.
2124 * @param cbBuffer Size of the output buffer.
2125 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2126 * @param args The format argument.
2127 */
2128RTDECL(ssize_t) RTStrPrintf2V(char *pszBuffer, size_t cbBuffer, const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(3, 0);
2129
2130/**
2131 * String printf, version 2.
2132 *
2133 * @returns On success, positive count of formatted character excluding the
2134 * terminator. On buffer overflow, negative number giving the required
2135 * buffer size (including terminator char).
2136 *
2137 * @param pszBuffer Output buffer.
2138 * @param cbBuffer Size of the output buffer.
2139 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2140 * @param ... The format argument.
2141 */
2142RTDECL(ssize_t) RTStrPrintf2(char *pszBuffer, size_t cbBuffer, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
2143
2144/**
2145 * String printf with custom formatting, version 2.
2146 *
2147 * @returns On success, positive count of formatted character excluding the
2148 * terminator. On buffer overflow, negative number giving the required
2149 * buffer size (including terminator char).
2150 *
2151 * @param pfnFormat Pointer to handler function for the custom formats.
2152 * @param pvArg Argument to the pfnFormat function.
2153 * @param pszBuffer Output buffer.
2154 * @param cbBuffer Size of the output buffer.
2155 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2156 * @param args The format argument.
2157 */
2158RTDECL(ssize_t) RTStrPrintf2ExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cbBuffer,
2159 const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(5, 0);
2160
2161/**
2162 * String printf with custom formatting, version 2.
2163 *
2164 * @returns On success, positive count of formatted character excluding the
2165 * terminator. On buffer overflow, negative number giving the required
2166 * buffer size (including terminator char).
2167 *
2168 * @param pfnFormat Pointer to handler function for the custom formats.
2169 * @param pvArg Argument to the pfnFormat function.
2170 * @param pszBuffer Output buffer.
2171 * @param cbBuffer Size of the output buffer.
2172 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2173 * @param ... The format argument.
2174 */
2175RTDECL(ssize_t) RTStrPrintf2Ex(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cbBuffer,
2176 const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(5, 6);
2177
2178/**
2179 * Allocating string printf (default tag).
2180 *
2181 * @returns The length of the string in the returned *ppszBuffer excluding the
2182 * terminator.
2183 * @returns -1 on failure.
2184 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
2185 * The buffer should be freed using RTStrFree().
2186 * On failure *ppszBuffer will be set to NULL.
2187 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2188 * @param args The format argument.
2189 */
2190#define RTStrAPrintfV(ppszBuffer, pszFormat, args) RTStrAPrintfVTag((ppszBuffer), (pszFormat), (args), RTSTR_TAG)
2191
2192/**
2193 * Allocating string printf (custom tag).
2194 *
2195 * @returns The length of the string in the returned *ppszBuffer excluding the
2196 * terminator.
2197 * @returns -1 on failure.
2198 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
2199 * The buffer should be freed using RTStrFree().
2200 * On failure *ppszBuffer will be set to NULL.
2201 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2202 * @param args The format argument.
2203 * @param pszTag Allocation tag used for statistics and such.
2204 */
2205RTDECL(int) RTStrAPrintfVTag(char **ppszBuffer, const char *pszFormat, va_list args, const char *pszTag) RT_IPRT_FORMAT_ATTR(2, 0);
2206
2207/**
2208 * Allocating string printf.
2209 *
2210 * @returns The length of the string in the returned *ppszBuffer excluding the
2211 * terminator.
2212 * @returns -1 on failure.
2213 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
2214 * The buffer should be freed using RTStrFree().
2215 * On failure *ppszBuffer will be set to NULL.
2216 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2217 * @param ... The format argument.
2218 */
2219DECLINLINE(int) RT_IPRT_FORMAT_ATTR(2, 3) RTStrAPrintf(char **ppszBuffer, const char *pszFormat, ...)
2220{
2221 int cbRet;
2222 va_list va;
2223 va_start(va, pszFormat);
2224 cbRet = RTStrAPrintfVTag(ppszBuffer, pszFormat, va, RTSTR_TAG);
2225 va_end(va);
2226 return cbRet;
2227}
2228
2229/**
2230 * Allocating string printf (custom tag).
2231 *
2232 * @returns The length of the string in the returned *ppszBuffer excluding the
2233 * terminator.
2234 * @returns -1 on failure.
2235 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
2236 * The buffer should be freed using RTStrFree().
2237 * On failure *ppszBuffer will be set to NULL.
2238 * @param pszTag Allocation tag used for statistics and such.
2239 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2240 * @param ... The format argument.
2241 */
2242DECLINLINE(int) RT_IPRT_FORMAT_ATTR(3, 4) RTStrAPrintfTag(char **ppszBuffer, const char *pszTag, const char *pszFormat, ...)
2243{
2244 int cbRet;
2245 va_list va;
2246 va_start(va, pszFormat);
2247 cbRet = RTStrAPrintfVTag(ppszBuffer, pszFormat, va, pszTag);
2248 va_end(va);
2249 return cbRet;
2250}
2251
2252/**
2253 * Allocating string printf, version 2.
2254 *
2255 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
2256 * memory.
2257 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2258 * @param args The format argument.
2259 */
2260#define RTStrAPrintf2V(pszFormat, args) RTStrAPrintf2VTag((pszFormat), (args), RTSTR_TAG)
2261
2262/**
2263 * Allocating string printf, version 2.
2264 *
2265 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
2266 * memory.
2267 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2268 * @param args The format argument.
2269 * @param pszTag Allocation tag used for statistics and such.
2270 */
2271RTDECL(char *) RTStrAPrintf2VTag(const char *pszFormat, va_list args, const char *pszTag) RT_IPRT_FORMAT_ATTR(1, 0);
2272
2273/**
2274 * Allocating string printf, version 2 (default tag).
2275 *
2276 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
2277 * memory.
2278 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2279 * @param ... The format argument.
2280 */
2281DECLINLINE(char *) RT_IPRT_FORMAT_ATTR(1, 2) RTStrAPrintf2(const char *pszFormat, ...)
2282{
2283 char *pszRet;
2284 va_list va;
2285 va_start(va, pszFormat);
2286 pszRet = RTStrAPrintf2VTag(pszFormat, va, RTSTR_TAG);
2287 va_end(va);
2288 return pszRet;
2289}
2290
2291/**
2292 * Allocating string printf, version 2 (custom tag).
2293 *
2294 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
2295 * memory.
2296 * @param pszTag Allocation tag used for statistics and such.
2297 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2298 * @param ... The format argument.
2299 */
2300DECLINLINE(char *) RT_IPRT_FORMAT_ATTR(2, 3) RTStrAPrintf2Tag(const char *pszTag, const char *pszFormat, ...)
2301{
2302 char *pszRet;
2303 va_list va;
2304 va_start(va, pszFormat);
2305 pszRet = RTStrAPrintf2VTag(pszFormat, va, pszTag);
2306 va_end(va);
2307 return pszRet;
2308}
2309
2310/**
2311 * Strips blankspaces from both ends of the string.
2312 *
2313 * @returns Pointer to first non-blank char in the string.
2314 * @param psz The string to strip.
2315 */
2316RTDECL(char *) RTStrStrip(char *psz);
2317
2318/**
2319 * Strips blankspaces from the start of the string.
2320 *
2321 * @returns Pointer to first non-blank char in the string.
2322 * @param psz The string to strip.
2323 */
2324RTDECL(char *) RTStrStripL(const char *psz);
2325
2326/**
2327 * Strips blankspaces from the end of the string.
2328 *
2329 * @returns psz.
2330 * @param psz The string to strip.
2331 */
2332RTDECL(char *) RTStrStripR(char *psz);
2333
2334/**
2335 * String copy with overflow handling.
2336 *
2337 * @retval VINF_SUCCESS on success.
2338 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2339 * buffer will contain as much of the string as it can hold, fully
2340 * terminated.
2341 *
2342 * @param pszDst The destination buffer.
2343 * @param cbDst The size of the destination buffer (in bytes).
2344 * @param pszSrc The source string. NULL is not OK.
2345 */
2346RTDECL(int) RTStrCopy(char *pszDst, size_t cbDst, const char *pszSrc);
2347
2348/**
2349 * String copy with overflow handling.
2350 *
2351 * @retval VINF_SUCCESS on success.
2352 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2353 * buffer will contain as much of the string as it can hold, fully
2354 * terminated.
2355 *
2356 * @param pszDst The destination buffer.
2357 * @param cbDst The size of the destination buffer (in bytes).
2358 * @param pszSrc The source string. NULL is not OK.
2359 * @param cchSrcMax The maximum number of chars (not code points) to
2360 * copy from the source string, not counting the
2361 * terminator as usual.
2362 */
2363RTDECL(int) RTStrCopyEx(char *pszDst, size_t cbDst, const char *pszSrc, size_t cchSrcMax);
2364
2365/**
2366 * String copy with overflow handling and buffer advancing.
2367 *
2368 * @retval VINF_SUCCESS on success.
2369 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2370 * buffer will contain as much of the string as it can hold, fully
2371 * terminated.
2372 *
2373 * @param ppszDst Pointer to the destination buffer pointer.
2374 * This will be advanced to the end of the copied
2375 * bytes (points at the terminator). This is also
2376 * updated on overflow.
2377 * @param pcbDst Pointer to the destination buffer size
2378 * variable. This will be updated in accord with
2379 * the buffer pointer.
2380 * @param pszSrc The source string. NULL is not OK.
2381 */
2382RTDECL(int) RTStrCopyP(char **ppszDst, size_t *pcbDst, const char *pszSrc);
2383
2384/**
2385 * String copy with overflow handling.
2386 *
2387 * @retval VINF_SUCCESS on success.
2388 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2389 * buffer will contain as much of the string as it can hold, fully
2390 * terminated.
2391 *
2392 * @param ppszDst Pointer to the destination buffer pointer.
2393 * This will be advanced to the end of the copied
2394 * bytes (points at the terminator). This is also
2395 * updated on overflow.
2396 * @param pcbDst Pointer to the destination buffer size
2397 * variable. This will be updated in accord with
2398 * the buffer pointer.
2399 * @param pszSrc The source string. NULL is not OK.
2400 * @param cchSrcMax The maximum number of chars (not code points) to
2401 * copy from the source string, not counting the
2402 * terminator as usual.
2403 */
2404RTDECL(int) RTStrCopyPEx(char **ppszDst, size_t *pcbDst, const char *pszSrc, size_t cchSrcMax);
2405
2406/**
2407 * String concatenation with overflow handling.
2408 *
2409 * @retval VINF_SUCCESS on success.
2410 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2411 * buffer will contain as much of the string as it can hold, fully
2412 * terminated.
2413 *
2414 * @param pszDst The destination buffer.
2415 * @param cbDst The size of the destination buffer (in bytes).
2416 * @param pszSrc The source string. NULL is not OK.
2417 */
2418RTDECL(int) RTStrCat(char *pszDst, size_t cbDst, const char *pszSrc);
2419
2420/**
2421 * String concatenation with overflow handling.
2422 *
2423 * @retval VINF_SUCCESS on success.
2424 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2425 * buffer will contain as much of the string as it can hold, fully
2426 * terminated.
2427 *
2428 * @param pszDst The destination buffer.
2429 * @param cbDst The size of the destination buffer (in bytes).
2430 * @param pszSrc The source string. NULL is not OK.
2431 * @param cchSrcMax The maximum number of chars (not code points) to
2432 * copy from the source string, not counting the
2433 * terminator as usual.
2434 */
2435RTDECL(int) RTStrCatEx(char *pszDst, size_t cbDst, const char *pszSrc, size_t cchSrcMax);
2436
2437/**
2438 * String concatenation with overflow handling.
2439 *
2440 * @retval VINF_SUCCESS on success.
2441 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2442 * buffer will contain as much of the string as it can hold, fully
2443 * terminated.
2444 *
2445 * @param ppszDst Pointer to the destination buffer pointer.
2446 * This will be advanced to the end of the copied
2447 * bytes (points at the terminator). This is also
2448 * updated on overflow.
2449 * @param pcbDst Pointer to the destination buffer size
2450 * variable. This will be updated in accord with
2451 * the buffer pointer.
2452 * @param pszSrc The source string. NULL is not OK.
2453 */
2454RTDECL(int) RTStrCatP(char **ppszDst, size_t *pcbDst, const char *pszSrc);
2455
2456/**
2457 * String concatenation with overflow handling and buffer advancing.
2458 *
2459 * @retval VINF_SUCCESS on success.
2460 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2461 * buffer will contain as much of the string as it can hold, fully
2462 * terminated.
2463 *
2464 * @param ppszDst Pointer to the destination buffer pointer.
2465 * This will be advanced to the end of the copied
2466 * bytes (points at the terminator). This is also
2467 * updated on overflow.
2468 * @param pcbDst Pointer to the destination buffer size
2469 * variable. This will be updated in accord with
2470 * the buffer pointer.
2471 * @param pszSrc The source string. NULL is not OK.
2472 * @param cchSrcMax The maximum number of chars (not code points) to
2473 * copy from the source string, not counting the
2474 * terminator as usual.
2475 */
2476RTDECL(int) RTStrCatPEx(char **ppszDst, size_t *pcbDst, const char *pszSrc, size_t cchSrcMax);
2477
2478/**
2479 * Performs a case sensitive string compare between two UTF-8 strings.
2480 *
2481 * Encoding errors are ignored by the current implementation. So, the only
2482 * difference between this and the CRT strcmp function is the handling of
2483 * NULL arguments.
2484 *
2485 * @returns < 0 if the first string less than the second string.
2486 * @returns 0 if the first string identical to the second string.
2487 * @returns > 0 if the first string greater than the second string.
2488 * @param psz1 First UTF-8 string. Null is allowed.
2489 * @param psz2 Second UTF-8 string. Null is allowed.
2490 */
2491RTDECL(int) RTStrCmp(const char *psz1, const char *psz2);
2492
2493/**
2494 * Performs a case sensitive string compare between two UTF-8 strings, given
2495 * a maximum string length.
2496 *
2497 * Encoding errors are ignored by the current implementation. So, the only
2498 * difference between this and the CRT strncmp function is the handling of
2499 * NULL arguments.
2500 *
2501 * @returns < 0 if the first string less than the second string.
2502 * @returns 0 if the first string identical to the second string.
2503 * @returns > 0 if the first string greater than the second string.
2504 * @param psz1 First UTF-8 string. Null is allowed.
2505 * @param psz2 Second UTF-8 string. Null is allowed.
2506 * @param cchMax The maximum string length
2507 */
2508RTDECL(int) RTStrNCmp(const char *psz1, const char *psz2, size_t cchMax);
2509
2510/**
2511 * Performs a case insensitive string compare between two UTF-8 strings.
2512 *
2513 * This is a simplified compare, as only the simplified lower/upper case folding
2514 * specified by the unicode specs are used. It does not consider character pairs
2515 * as they are used in some languages, just simple upper & lower case compares.
2516 *
2517 * The result is the difference between the mismatching codepoints after they
2518 * both have been lower cased.
2519 *
2520 * If the string encoding is invalid the function will assert (strict builds)
2521 * and use RTStrCmp for the remainder of the string.
2522 *
2523 * @returns < 0 if the first string less than the second string.
2524 * @returns 0 if the first string identical to the second string.
2525 * @returns > 0 if the first string greater than the second string.
2526 * @param psz1 First UTF-8 string. Null is allowed.
2527 * @param psz2 Second UTF-8 string. Null is allowed.
2528 */
2529RTDECL(int) RTStrICmp(const char *psz1, const char *psz2);
2530
2531/**
2532 * Performs a case insensitive string compare between two UTF-8 strings, given a
2533 * maximum string length.
2534 *
2535 * This is a simplified compare, as only the simplified lower/upper case folding
2536 * specified by the unicode specs are used. It does not consider character pairs
2537 * as they are used in some languages, just simple upper & lower case compares.
2538 *
2539 * The result is the difference between the mismatching codepoints after they
2540 * both have been lower cased.
2541 *
2542 * If the string encoding is invalid the function will assert (strict builds)
2543 * and use RTStrNCmp for the remainder of the string.
2544 *
2545 * @returns < 0 if the first string less than the second string.
2546 * @returns 0 if the first string identical to the second string.
2547 * @returns > 0 if the first string greater than the second string.
2548 * @param psz1 First UTF-8 string. Null is allowed.
2549 * @param psz2 Second UTF-8 string. Null is allowed.
2550 * @param cchMax Maximum string length
2551 */
2552RTDECL(int) RTStrNICmp(const char *psz1, const char *psz2, size_t cchMax);
2553
2554/**
2555 * Performs a case insensitive string compare between a UTF-8 string and a 7-bit
2556 * ASCII string.
2557 *
2558 * This is potentially faster than RTStrICmp and drags in less dependencies. It
2559 * is really handy for hardcoded inputs.
2560 *
2561 * If the string encoding is invalid the function will assert (strict builds)
2562 * and use RTStrCmp for the remainder of the string.
2563 *
2564 * @returns < 0 if the first string less than the second string.
2565 * @returns 0 if the first string identical to the second string.
2566 * @returns > 0 if the first string greater than the second string.
2567 * @param psz1 First UTF-8 string. Null is allowed.
2568 * @param psz2 Second string, 7-bit ASCII. Null is allowed.
2569 * @sa RTStrICmp, RTUtf16ICmpAscii
2570 */
2571RTDECL(int) RTStrICmpAscii(const char *psz1, const char *psz2);
2572
2573/**
2574 * Performs a case insensitive string compare between a UTF-8 string and a 7-bit
2575 * ASCII string, given a maximum string length.
2576 *
2577 * This is potentially faster than RTStrNICmp and drags in less dependencies.
2578 * It is really handy for hardcoded inputs.
2579 *
2580 * If the string encoding is invalid the function will assert (strict builds)
2581 * and use RTStrNCmp for the remainder of the string.
2582 *
2583 * @returns < 0 if the first string less than the second string.
2584 * @returns 0 if the first string identical to the second string.
2585 * @returns > 0 if the first string greater than the second string.
2586 * @param psz1 First UTF-8 string. Null is allowed.
2587 * @param psz2 Second string, 7-bit ASCII. Null is allowed.
2588 * @param cchMax Maximum string length
2589 * @sa RTStrNICmp, RTUtf16NICmpAscii
2590 */
2591RTDECL(int) RTStrNICmpAscii(const char *psz1, const char *psz2, size_t cchMax);
2592
2593/**
2594 * Checks whether @a pszString starts with @a pszStart.
2595 *
2596 * @returns true / false.
2597 * @param pszString The string to check.
2598 * @param pszStart The start string to check for.
2599 */
2600RTDECL(bool) RTStrStartsWith(const char *pszString, const char *pszStart);
2601
2602/**
2603 * Checks whether @a pszString starts with @a pszStart, case insensitive.
2604 *
2605 * @returns true / false.
2606 * @param pszString The string to check.
2607 * @param pszStart The start string to check for.
2608 */
2609RTDECL(bool) RTStrIStartsWith(const char *pszString, const char *pszStart);
2610
2611/**
2612 * Splits a string buffer with a given separator into separate strings.
2613 * If no separators are found, no strings are returned. Consequtive separators will be skipped.
2614 *
2615 * @returns iprt status code.
2616 * @param pcszStrings String buffer to split.
2617 * @param cbStrings Size (in bytes) of string buffer to split, including terminator.
2618 * @param pcszSeparator Separator to use / find for splitting strings.
2619 * @param ppapszStrings Where to return the allocated string array on success. Needs to be free'd by the caller.
2620 * @param pcStrings Where to return the number of split strings in \a ppapszStrings.
2621 */
2622RTDECL(int) RTStrSplit(const char *pcszStrings, size_t cbStrings,
2623 const char *pcszSeparator, char ***ppapszStrings, size_t *pcStrings);
2624
2625/**
2626 * Locates a case sensitive substring.
2627 *
2628 * If any of the two strings are NULL, then NULL is returned. If the needle is
2629 * an empty string, then the haystack is returned (i.e. matches anything).
2630 *
2631 * @returns Pointer to the first occurrence of the substring if found, NULL if
2632 * not.
2633 *
2634 * @param pszHaystack The string to search.
2635 * @param pszNeedle The substring to search for.
2636 *
2637 * @remarks The difference between this and strstr is the handling of NULL
2638 * pointers.
2639 */
2640RTDECL(char *) RTStrStr(const char *pszHaystack, const char *pszNeedle);
2641
2642/**
2643 * Locates a case insensitive substring.
2644 *
2645 * If any of the two strings are NULL, then NULL is returned. If the needle is
2646 * an empty string, then the haystack is returned (i.e. matches anything).
2647 *
2648 * @returns Pointer to the first occurrence of the substring if found, NULL if
2649 * not.
2650 *
2651 * @param pszHaystack The string to search.
2652 * @param pszNeedle The substring to search for.
2653 *
2654 */
2655RTDECL(char *) RTStrIStr(const char *pszHaystack, const char *pszNeedle);
2656
2657/**
2658 * Converts the string to lower case.
2659 *
2660 * @returns Pointer to the converted string.
2661 * @param psz The string to convert.
2662 */
2663RTDECL(char *) RTStrToLower(char *psz);
2664
2665/**
2666 * Converts the string to upper case.
2667 *
2668 * @returns Pointer to the converted string.
2669 * @param psz The string to convert.
2670 */
2671RTDECL(char *) RTStrToUpper(char *psz);
2672
2673/**
2674 * Checks if the string is case foldable, i.e. whether it would change if
2675 * subject to RTStrToLower or RTStrToUpper.
2676 *
2677 * @returns true / false
2678 * @param psz The string in question.
2679 */
2680RTDECL(bool) RTStrIsCaseFoldable(const char *psz);
2681
2682/**
2683 * Checks if the string is upper cased (no lower case chars in it).
2684 *
2685 * @returns true / false
2686 * @param psz The string in question.
2687 */
2688RTDECL(bool) RTStrIsUpperCased(const char *psz);
2689
2690/**
2691 * Checks if the string is lower cased (no upper case chars in it).
2692 *
2693 * @returns true / false
2694 * @param psz The string in question.
2695 */
2696RTDECL(bool) RTStrIsLowerCased(const char *psz);
2697
2698/**
2699 * Find the length of a zero-terminated byte string, given
2700 * a max string length.
2701 *
2702 * See also RTStrNLenEx.
2703 *
2704 * @returns The string length or cbMax. The returned length does not include
2705 * the zero terminator if it was found.
2706 *
2707 * @param pszString The string.
2708 * @param cchMax The max string length.
2709 */
2710RTDECL(size_t) RTStrNLen(const char *pszString, size_t cchMax);
2711
2712/**
2713 * Find the length of a zero-terminated byte string, given
2714 * a max string length.
2715 *
2716 * See also RTStrNLen.
2717 *
2718 * @returns IPRT status code.
2719 * @retval VINF_SUCCESS if the string has a length less than cchMax.
2720 * @retval VERR_BUFFER_OVERFLOW if the end of the string wasn't found
2721 * before cchMax was reached.
2722 *
2723 * @param pszString The string.
2724 * @param cchMax The max string length.
2725 * @param pcch Where to store the string length excluding the
2726 * terminator. This is set to cchMax if the terminator
2727 * isn't found.
2728 */
2729RTDECL(int) RTStrNLenEx(const char *pszString, size_t cchMax, size_t *pcch);
2730
2731/** The maximum size argument of a memchr call. */
2732#define RTSTR_MEMCHR_MAX ((~(size_t)0 >> 1) - 15)
2733
2734/**
2735 * Find the zero terminator in a string with a limited length.
2736 *
2737 * @returns Pointer to the zero terminator.
2738 * @returns NULL if the zero terminator was not found.
2739 *
2740 * @param pszString The string.
2741 * @param cchMax The max string length. RTSTR_MAX is fine.
2742 */
2743RTDECL(char *) RTStrEnd(char const *pszString, size_t cchMax);
2744
2745/**
2746 * Finds the offset at which a simple character first occurs in a string.
2747 *
2748 * @returns The offset of the first occurence or the terminator offset.
2749 * @param pszHaystack The string to search.
2750 * @param chNeedle The character to search for.
2751 */
2752DECLINLINE(size_t) RTStrOffCharOrTerm(const char *pszHaystack, char chNeedle)
2753{
2754 const char *psz = pszHaystack;
2755 char ch;
2756 while ( (ch = *psz) != chNeedle
2757 && ch != '\0')
2758 psz++;
2759 return (size_t)(psz - pszHaystack);
2760}
2761
2762/**
2763 * Matches a simple string pattern.
2764 *
2765 * @returns true if the string matches the pattern, otherwise false.
2766 *
2767 * @param pszPattern The pattern. Special chars are '*' and '?', where the
2768 * asterisk matches zero or more characters and question
2769 * mark matches exactly one character.
2770 * @param pszString The string to match against the pattern.
2771 */
2772RTDECL(bool) RTStrSimplePatternMatch(const char *pszPattern, const char *pszString);
2773
2774/**
2775 * Matches a simple string pattern, neither which needs to be zero terminated.
2776 *
2777 * This is identical to RTStrSimplePatternMatch except that you can optionally
2778 * specify the length of both the pattern and the string. The function will
2779 * stop when it hits a string terminator or either of the lengths.
2780 *
2781 * @returns true if the string matches the pattern, otherwise false.
2782 *
2783 * @param pszPattern The pattern. Special chars are '*' and '?', where the
2784 * asterisk matches zero or more characters and question
2785 * mark matches exactly one character.
2786 * @param cchPattern The pattern length. Pass RTSTR_MAX if you don't know the
2787 * length and wish to stop at the string terminator.
2788 * @param pszString The string to match against the pattern.
2789 * @param cchString The string length. Pass RTSTR_MAX if you don't know the
2790 * length and wish to match up to the string terminator.
2791 */
2792RTDECL(bool) RTStrSimplePatternNMatch(const char *pszPattern, size_t cchPattern,
2793 const char *pszString, size_t cchString);
2794
2795/**
2796 * Matches multiple patterns against a string.
2797 *
2798 * The patterns are separated by the pipe character (|).
2799 *
2800 * @returns true if the string matches the pattern, otherwise false.
2801 *
2802 * @param pszPatterns The patterns.
2803 * @param cchPatterns The lengths of the patterns to use. Pass RTSTR_MAX to
2804 * stop at the terminator.
2805 * @param pszString The string to match against the pattern.
2806 * @param cchString The string length. Pass RTSTR_MAX stop stop at the
2807 * terminator.
2808 * @param poffPattern Offset into the patterns string of the patttern that
2809 * matched. If no match, this will be set to RTSTR_MAX.
2810 * This is optional, NULL is fine.
2811 */
2812RTDECL(bool) RTStrSimplePatternMultiMatch(const char *pszPatterns, size_t cchPatterns,
2813 const char *pszString, size_t cchString,
2814 size_t *poffPattern);
2815
2816/**
2817 * Compares two version strings RTStrICmp fashion.
2818 *
2819 * The version string is split up into sections at punctuation, spaces,
2820 * underscores, dashes and plus signs. The sections are then split up into
2821 * numeric and string sub-sections. Finally, the sub-sections are compared
2822 * in a numeric or case insesntivie fashion depending on what they are.
2823 *
2824 * The following strings are considered to be equal: "1.0.0", "1.00.0", "1.0",
2825 * "1". These aren't: "1.0.0r993", "1.0", "1.0r993", "1.0_Beta3", "1.1"
2826 *
2827 * @returns < 0 if the first string less than the second string.
2828 * @returns 0 if the first string identical to the second string.
2829 * @returns > 0 if the first string greater than the second string.
2830 *
2831 * @param pszVer1 First version string to compare.
2832 * @param pszVer2 Second version string to compare first version with.
2833 */
2834RTDECL(int) RTStrVersionCompare(const char *pszVer1, const char *pszVer2);
2835
2836
2837/** @defgroup rt_str_conv String To/From Number Conversions
2838 * @{ */
2839
2840/**
2841 * Converts a string representation of a number to a 64-bit unsigned number.
2842 *
2843 * @returns iprt status code.
2844 * Warnings are used to indicate conversion problems.
2845 * @retval VWRN_NUMBER_TOO_BIG
2846 * @retval VWRN_NEGATIVE_UNSIGNED
2847 * @retval VWRN_TRAILING_CHARS
2848 * @retval VWRN_TRAILING_SPACES
2849 * @retval VINF_SUCCESS
2850 * @retval VERR_NO_DIGITS
2851 *
2852 * @param pszValue Pointer to the string value.
2853 * @param ppszNext Where to store the pointer to the first char
2854 * following the number. (Optional)
2855 * @param uBaseAndMaxLen The low byte is the base of the representation, the
2856 * upper 24 bits are the max length to parse. If the base
2857 * is zero the function will look for known prefixes before
2858 * defaulting to 10. A max length of zero means no length
2859 * restriction.
2860 * @param pu64 Where to store the converted number. (optional)
2861 */
2862RTDECL(int) RTStrToUInt64Ex(const char *pszValue, char **ppszNext, unsigned uBaseAndMaxLen, uint64_t *pu64);
2863
2864/**
2865 * Converts a string representation of a number to a 64-bit unsigned number,
2866 * making sure the full string is converted.
2867 *
2868 * @returns iprt status code.
2869 * Warnings are used to indicate conversion problems.
2870 * @retval VWRN_NUMBER_TOO_BIG
2871 * @retval VWRN_NEGATIVE_UNSIGNED
2872 * @retval VINF_SUCCESS
2873 * @retval VERR_NO_DIGITS
2874 * @retval VERR_TRAILING_SPACES
2875 * @retval VERR_TRAILING_CHARS
2876 *
2877 * @param pszValue Pointer to the string value.
2878 * @param uBaseAndMaxLen The low byte is the base of the representation, the
2879 * upper 24 bits are the max length to parse. If the base
2880 * is zero the function will look for known prefixes before
2881 * defaulting to 10. A max length of zero means no length
2882 * restriction.
2883 * @param pu64 Where to store the converted number. (optional)
2884 */
2885RTDECL(int) RTStrToUInt64Full(const char *pszValue, unsigned uBaseAndMaxLen, uint64_t *pu64);
2886
2887/**
2888 * Converts a string representation of a number to a 64-bit unsigned number.
2889 * The base is guessed.
2890 *
2891 * @returns 64-bit unsigned number on success.
2892 * @returns 0 on failure.
2893 * @param pszValue Pointer to the string value.
2894 */
2895RTDECL(uint64_t) RTStrToUInt64(const char *pszValue);
2896
2897/**
2898 * Converts a string representation of a number to a 32-bit unsigned number.
2899 *
2900 * @returns iprt status code.
2901 * Warnings are used to indicate conversion problems.
2902 * @retval VWRN_NUMBER_TOO_BIG
2903 * @retval VWRN_NEGATIVE_UNSIGNED
2904 * @retval VWRN_TRAILING_CHARS
2905 * @retval VWRN_TRAILING_SPACES
2906 * @retval VINF_SUCCESS
2907 * @retval VERR_NO_DIGITS
2908 *
2909 * @param pszValue Pointer to the string value.
2910 * @param ppszNext Where to store the pointer to the first char
2911 * following the number. (Optional)
2912 * @param uBaseAndMaxLen The low byte is the base of the representation, the
2913 * upper 24 bits are the max length to parse. If the base
2914 * is zero the function will look for known prefixes before
2915 * defaulting to 10. A max length of zero means no length
2916 * restriction.
2917 * @param pu32 Where to store the converted number. (optional)
2918 */
2919RTDECL(int) RTStrToUInt32Ex(const char *pszValue, char **ppszNext, unsigned uBaseAndMaxLen, uint32_t *pu32);
2920
2921/**
2922 * Converts a string representation of a number to a 32-bit unsigned number,
2923 * making sure the full string is converted.
2924 *
2925 * @returns iprt status code.
2926 * Warnings are used to indicate conversion problems.
2927 * @retval VWRN_NUMBER_TOO_BIG
2928 * @retval VWRN_NEGATIVE_UNSIGNED
2929 * @retval VINF_SUCCESS
2930 * @retval VERR_NO_DIGITS
2931 * @retval VERR_TRAILING_SPACES
2932 * @retval VERR_TRAILING_CHARS
2933 *
2934 * @param pszValue Pointer to the string value.
2935 * @param uBaseAndMaxLen The low byte is the base of the representation, the
2936 * upper 24 bits are the max length to parse. If the base
2937 * is zero the function will look for known prefixes before
2938 * defaulting to 10. A max length of zero means no length
2939 * restriction.
2940 * @param pu32 Where to store the converted number. (optional)
2941 */
2942RTDECL(int) RTStrToUInt32Full(const char *pszValue, unsigned uBaseAndMaxLen, uint32_t *pu32);
2943
2944/**
2945 * Converts a string representation of a number to a 32-bit unsigned number.
2946 * The base is guessed.
2947 *
2948 * @returns 32-bit unsigned number on success.
2949 * @returns 0 on failure.
2950 * @param pszValue Pointer to the string value.
2951 */
2952RTDECL(uint32_t) RTStrToUInt32(const char *pszValue);
2953
2954/**
2955 * Converts a string representation of a number to a 16-bit unsigned number.
2956 *
2957 * @returns iprt status code.
2958 * Warnings are used to indicate conversion problems.
2959 * @retval VWRN_NUMBER_TOO_BIG
2960 * @retval VWRN_NEGATIVE_UNSIGNED
2961 * @retval VWRN_TRAILING_CHARS
2962 * @retval VWRN_TRAILING_SPACES
2963 * @retval VINF_SUCCESS
2964 * @retval VERR_NO_DIGITS
2965 *
2966 * @param pszValue Pointer to the string value.
2967 * @param ppszNext Where to store the pointer to the first char
2968 * following the number. (Optional)
2969 * @param uBaseAndMaxLen The low byte is the base of the representation, the
2970 * upper 24 bits are the max length to parse. If the base
2971 * is zero the function will look for known prefixes before
2972 * defaulting to 10. A max length of zero means no length
2973 * restriction.
2974 * @param pu16 Where to store the converted number. (optional)
2975 */
2976RTDECL(int) RTStrToUInt16Ex(const char *pszValue, char **ppszNext, unsigned uBaseAndMaxLen, uint16_t *pu16);
2977
2978/**
2979 * Converts a string representation of a number to a 16-bit unsigned number,
2980 * making sure the full string is converted.
2981 *
2982 * @returns iprt status code.
2983 * Warnings are used to indicate conversion problems.
2984 * @retval VWRN_NUMBER_TOO_BIG
2985 * @retval VWRN_NEGATIVE_UNSIGNED
2986 * @retval VINF_SUCCESS
2987 * @retval VERR_NO_DIGITS
2988 * @retval VERR_TRAILING_SPACES
2989 * @retval VERR_TRAILING_CHARS
2990 *
2991 * @param pszValue Pointer to the string value.
2992 * @param uBaseAndMaxLen The low byte is the base of the representation, the
2993 * upper 24 bits are the max length to parse. If the base
2994 * is zero the function will look for known prefixes before
2995 * defaulting to 10. A max length of zero means no length
2996 * restriction.
2997 * @param pu16 Where to store the converted number. (optional)
2998 */
2999RTDECL(int) RTStrToUInt16Full(const char *pszValue, unsigned uBaseAndMaxLen, uint16_t *pu16);
3000
3001/**
3002 * Converts a string representation of a number to a 16-bit unsigned number.
3003 * The base is guessed.
3004 *
3005 * @returns 16-bit unsigned number on success.
3006 * @returns 0 on failure.
3007 * @param pszValue Pointer to the string value.
3008 */
3009RTDECL(uint16_t) RTStrToUInt16(const char *pszValue);
3010
3011/**
3012 * Converts a string representation of a number to a 8-bit unsigned number.
3013 *
3014 * @returns iprt status code.
3015 * Warnings are used to indicate conversion problems.
3016 * @retval VWRN_NUMBER_TOO_BIG
3017 * @retval VWRN_NEGATIVE_UNSIGNED
3018 * @retval VWRN_TRAILING_CHARS
3019 * @retval VWRN_TRAILING_SPACES
3020 * @retval VINF_SUCCESS
3021 * @retval VERR_NO_DIGITS
3022 *
3023 * @param pszValue Pointer to the string value.
3024 * @param ppszNext Where to store the pointer to the first char
3025 * following the number. (Optional)
3026 * @param uBaseAndMaxLen The low byte is the base of the representation, the
3027 * upper 24 bits are the max length to parse. If the base
3028 * is zero the function will look for known prefixes before
3029 * defaulting to 10. A max length of zero means no length
3030 * restriction.
3031 * @param pu8 Where to store the converted number. (optional)
3032 */
3033RTDECL(int) RTStrToUInt8Ex(const char *pszValue, char **ppszNext, unsigned uBaseAndMaxLen, uint8_t *pu8);
3034
3035/**
3036 * Converts a string representation of a number to a 8-bit unsigned number,
3037 * making sure the full string is converted.
3038 *
3039 * @returns iprt status code.
3040 * Warnings are used to indicate conversion problems.
3041 * @retval VWRN_NUMBER_TOO_BIG
3042 * @retval VWRN_NEGATIVE_UNSIGNED
3043 * @retval VINF_SUCCESS
3044 * @retval VERR_NO_DIGITS
3045 * @retval VERR_TRAILING_SPACES
3046 * @retval VERR_TRAILING_CHARS
3047 *
3048 * @param pszValue Pointer to the string value.
3049 * @param uBaseAndMaxLen The low byte is the base of the representation, the
3050 * upper 24 bits are the max length to parse. If the base
3051 * is zero the function will look for known prefixes before
3052 * defaulting to 10. A max length of zero means no length
3053 * restriction.
3054 * @param pu8 Where to store the converted number. (optional)
3055 */
3056RTDECL(int) RTStrToUInt8Full(const char *pszValue, unsigned uBaseAndMaxLen, uint8_t *pu8);
3057
3058/**
3059 * Converts a string representation of a number to a 8-bit unsigned number.
3060 * The base is guessed.
3061 *
3062 * @returns 8-bit unsigned number on success.
3063 * @returns 0 on failure.
3064 * @param pszValue Pointer to the string value.
3065 */
3066RTDECL(uint8_t) RTStrToUInt8(const char *pszValue);
3067
3068/**
3069 * Converts a string representation of a number to a 64-bit signed number.
3070 *
3071 * @returns iprt status code.
3072 * Warnings are used to indicate conversion problems.
3073 * @retval VWRN_NUMBER_TOO_BIG
3074 * @retval VWRN_TRAILING_CHARS
3075 * @retval VWRN_TRAILING_SPACES
3076 * @retval VINF_SUCCESS
3077 * @retval VERR_NO_DIGITS
3078 *
3079 * @param pszValue Pointer to the string value.
3080 * @param ppszNext Where to store the pointer to the first char
3081 * following the number. (Optional)
3082 * @param uBaseAndMaxLen The low byte is the base of the representation, the
3083 * upper 24 bits are the max length to parse. If the base
3084 * is zero the function will look for known prefixes before
3085 * defaulting to 10. A max length of zero means no length
3086 * restriction.
3087 * @param pi64 Where to store the converted number. (optional)
3088 */
3089RTDECL(int) RTStrToInt64Ex(const char *pszValue, char **ppszNext, unsigned uBaseAndMaxLen, int64_t *pi64);
3090
3091/**
3092 * Converts a string representation of a number to a 64-bit signed number,
3093 * making sure the full string is converted.
3094 *
3095 * @returns iprt status code.
3096 * Warnings are used to indicate conversion problems.
3097 * @retval VWRN_NUMBER_TOO_BIG
3098 * @retval VINF_SUCCESS
3099 * @retval VERR_TRAILING_CHARS
3100 * @retval VERR_TRAILING_SPACES
3101 * @retval VERR_NO_DIGITS
3102 *
3103 * @param pszValue Pointer to the string value.
3104 * @param uBaseAndMaxLen The low byte is the base of the representation, the
3105 * upper 24 bits are the max length to parse. If the base
3106 * is zero the function will look for known prefixes before
3107 * defaulting to 10. A max length of zero means no length
3108 * restriction.
3109 * @param pi64 Where to store the converted number. (optional)
3110 */
3111RTDECL(int) RTStrToInt64Full(const char *pszValue, unsigned uBaseAndMaxLen, int64_t *pi64);
3112
3113/**
3114 * Converts a string representation of a number to a 64-bit signed number.
3115 * The base is guessed.
3116 *
3117 * @returns 64-bit signed number on success.
3118 * @returns 0 on failure.
3119 * @param pszValue Pointer to the string value.
3120 */
3121RTDECL(int64_t) RTStrToInt64(const char *pszValue);
3122
3123/**
3124 * Converts a string representation of a number to a 32-bit signed number.
3125 *
3126 * @returns iprt status code.
3127 * Warnings are used to indicate conversion problems.
3128 * @retval VWRN_NUMBER_TOO_BIG
3129 * @retval VWRN_TRAILING_CHARS
3130 * @retval VWRN_TRAILING_SPACES
3131 * @retval VINF_SUCCESS
3132 * @retval VERR_NO_DIGITS
3133 *
3134 * @param pszValue Pointer to the string value.
3135 * @param ppszNext Where to store the pointer to the first char
3136 * following the number. (Optional)
3137 * @param uBaseAndMaxLen The low byte is the base of the representation, the
3138 * upper 24 bits are the max length to parse. If the base
3139 * is zero the function will look for known prefixes before
3140 * defaulting to 10. A max length of zero means no length
3141 * restriction.
3142 * @param pi32 Where to store the converted number. (optional)
3143 */
3144RTDECL(int) RTStrToInt32Ex(const char *pszValue, char **ppszNext, unsigned uBaseAndMaxLen, int32_t *pi32);
3145
3146/**
3147 * Converts a string representation of a number to a 32-bit signed number,
3148 * making sure the full string is converted.
3149 *
3150 * @returns iprt status code.
3151 * Warnings are used to indicate conversion problems.
3152 * @retval VWRN_NUMBER_TOO_BIG
3153 * @retval VINF_SUCCESS
3154 * @retval VERR_TRAILING_CHARS
3155 * @retval VERR_TRAILING_SPACES
3156 * @retval VERR_NO_DIGITS
3157 *
3158 * @param pszValue Pointer to the string value.
3159 * @param uBaseAndMaxLen The low byte is the base of the representation, the
3160 * upper 24 bits are the max length to parse. If the base
3161 * is zero the function will look for known prefixes before
3162 * defaulting to 10. A max length of zero means no length
3163 * restriction.
3164 * @param pi32 Where to store the converted number. (optional)
3165 */
3166RTDECL(int) RTStrToInt32Full(const char *pszValue, unsigned uBaseAndMaxLen, int32_t *pi32);
3167
3168/**
3169 * Converts a string representation of a number to a 32-bit signed number.
3170 * The base is guessed.
3171 *
3172 * @returns 32-bit signed number on success.
3173 * @returns 0 on failure.
3174 * @param pszValue Pointer to the string value.
3175 */
3176RTDECL(int32_t) RTStrToInt32(const char *pszValue);
3177
3178/**
3179 * Converts a string representation of a number to a 16-bit signed number.
3180 *
3181 * @returns iprt status code.
3182 * Warnings are used to indicate conversion problems.
3183 * @retval VWRN_NUMBER_TOO_BIG
3184 * @retval VWRN_TRAILING_CHARS
3185 * @retval VWRN_TRAILING_SPACES
3186 * @retval VINF_SUCCESS
3187 * @retval VERR_NO_DIGITS
3188 *
3189 * @param pszValue Pointer to the string value.
3190 * @param ppszNext Where to store the pointer to the first char
3191 * following the number. (Optional)
3192 * @param uBaseAndMaxLen The low byte is the base of the representation, the
3193 * upper 24 bits are the max length to parse. If the base
3194 * is zero the function will look for known prefixes before
3195 * defaulting to 10. A max length of zero means no length
3196 * restriction.
3197 * @param pi16 Where to store the converted number. (optional)
3198 */
3199RTDECL(int) RTStrToInt16Ex(const char *pszValue, char **ppszNext, unsigned uBaseAndMaxLen, int16_t *pi16);
3200
3201/**
3202 * Converts a string representation of a number to a 16-bit signed number,
3203 * making sure the full string is converted.
3204 *
3205 * @returns iprt status code.
3206 * Warnings are used to indicate conversion problems.
3207 * @retval VWRN_NUMBER_TOO_BIG
3208 * @retval VINF_SUCCESS
3209 * @retval VERR_TRAILING_CHARS
3210 * @retval VERR_TRAILING_SPACES
3211 * @retval VERR_NO_DIGITS
3212 *
3213 * @param pszValue Pointer to the string value.
3214 * @param uBaseAndMaxLen The low byte is the base of the representation, the
3215 * upper 24 bits are the max length to parse. If the base
3216 * is zero the function will look for known prefixes before
3217 * defaulting to 10. A max length of zero means no length
3218 * restriction.
3219 * @param pi16 Where to store the converted number. (optional)
3220 */
3221RTDECL(int) RTStrToInt16Full(const char *pszValue, unsigned uBaseAndMaxLen, int16_t *pi16);
3222
3223/**
3224 * Converts a string representation of a number to a 16-bit signed number.
3225 * The base is guessed.
3226 *
3227 * @returns 16-bit signed number on success.
3228 * @returns 0 on failure.
3229 * @param pszValue Pointer to the string value.
3230 */
3231RTDECL(int16_t) RTStrToInt16(const char *pszValue);
3232
3233/**
3234 * Converts a string representation of a number to a 8-bit signed number.
3235 *
3236 * @returns iprt status code.
3237 * Warnings are used to indicate conversion problems.
3238 * @retval VWRN_NUMBER_TOO_BIG
3239 * @retval VWRN_TRAILING_CHARS
3240 * @retval VWRN_TRAILING_SPACES
3241 * @retval VINF_SUCCESS
3242 * @retval VERR_NO_DIGITS
3243 *
3244 * @param pszValue Pointer to the string value.
3245 * @param ppszNext Where to store the pointer to the first char
3246 * following the number. (Optional)
3247 * @param uBaseAndMaxLen The low byte is the base of the representation, the
3248 * upper 24 bits are the max length to parse. If the base
3249 * is zero the function will look for known prefixes before
3250 * defaulting to 10. A max length of zero means no length
3251 * restriction.
3252 * @param pi8 Where to store the converted number. (optional)
3253 */
3254RTDECL(int) RTStrToInt8Ex(const char *pszValue, char **ppszNext, unsigned uBaseAndMaxLen, int8_t *pi8);
3255
3256/**
3257 * Converts a string representation of a number to a 8-bit signed number,
3258 * making sure the full string is converted.
3259 *
3260 * @returns iprt status code.
3261 * Warnings are used to indicate conversion problems.
3262 * @retval VWRN_NUMBER_TOO_BIG
3263 * @retval VINF_SUCCESS
3264 * @retval VERR_TRAILING_CHARS
3265 * @retval VERR_TRAILING_SPACES
3266 * @retval VERR_NO_DIGITS
3267 *
3268 * @param pszValue Pointer to the string value.
3269 * @param uBaseAndMaxLen The low byte is the base of the representation, the
3270 * upper 24 bits are the max length to parse. If the base
3271 * is zero the function will look for known prefixes before
3272 * defaulting to 10. A max length of zero means no length
3273 * restriction.
3274 * @param pi8 Where to store the converted number. (optional)
3275 */
3276RTDECL(int) RTStrToInt8Full(const char *pszValue, unsigned uBaseAndMaxLen, int8_t *pi8);
3277
3278/**
3279 * Converts a string representation of a number to a 8-bit signed number.
3280 * The base is guessed.
3281 *
3282 * @returns 8-bit signed number on success.
3283 * @returns 0 on failure.
3284 * @param pszValue Pointer to the string value.
3285 */
3286RTDECL(int8_t) RTStrToInt8(const char *pszValue);
3287
3288
3289/**
3290 * Converts a string to long double floating point, extended edition.
3291 *
3292 * Please note that long double can be double precision, extended precision, or
3293 * quad precision floating point depending on the platform and architecture. See
3294 * RT_COMPILER_WITH_128BIT_LONG_DOUBLE and RT_COMPILER_WITH_80BIT_LONG_DOUBLE.
3295 *
3296 * @returns IPRT status code.
3297 * @retval VERR_NO_DIGITS if no valid digits found.
3298 * @retval VWRN_FLOAT_UNDERFLOW on underflow with denormal/subnormal return
3299 * value
3300 * @retval VERR_FLOAT_UNDERFLOW on underflow, value set to +/- zero.
3301 * @retval VERR_FLOAT_OVERFLOW on overflow, value set to +/- infinity.
3302 * @retval VWRN_TRAILING_CHARS
3303 * @retval VWRN_TRAILING_SPACES
3304 *
3305 * @param pszValue The string to parse.
3306 * @param ppszNext Where to store the pointer to the first char following
3307 * the number. Optional.
3308 * @param cchMax Max number of character to parse. Zero means unlimited.
3309 * @param plrd Where to return the number. Optional.
3310 *
3311 * @note This code isn't entirely perfect yet. It could exhibit rounding
3312 * differences compared to strtold & the compiler, and extreme value
3313 * may overflow/underflow prematurely depending on the build config.
3314 */
3315RTDECL(int) RTStrToLongDoubleEx(const char *pszValue, char **ppszNext, size_t cchMax, long double *plrd);
3316
3317/**
3318 * Converts a string to double precision floating point, extended edition.
3319 *
3320 * @returns IPRT status code.
3321 * @retval VERR_NO_DIGITS if no valid digits found.
3322 * @retval VWRN_FLOAT_UNDERFLOW on underflow with denormal/subnormal return
3323 * value
3324 * @retval VERR_FLOAT_UNDERFLOW on underflow, value set to +/- zero.
3325 * @retval VERR_FLOAT_OVERFLOW on overflow, value set to +/- infinity.
3326 * @retval VWRN_TRAILING_CHARS
3327 * @retval VWRN_TRAILING_SPACES
3328 *
3329 * @param pszValue The string to parse.
3330 * @param ppszNext Where to store the pointer to the first char following
3331 * the number. Optional.
3332 * @param cchMax Max number of character to parse. Zero means unlimited.
3333 * @param prd Where to return the number. Optional.
3334 *
3335 * @note This code isn't entirely perfect yet. It could exhibit rounding
3336 * differences compared to strtold & the compiler, and extreme value
3337 * may overflow/underflow prematurely depending on the build config.
3338 */
3339RTDECL(int) RTStrToDoubleEx(const char *pszValue, char **ppszNext, size_t cchMax, double *prd);
3340
3341/**
3342 * Converts a string to single precision floating point, extended edition.
3343 *
3344 * @returns IPRT status code.
3345 * @retval VERR_NO_DIGITS if no valid digits found.
3346 * @retval VWRN_FLOAT_UNDERFLOW on underflow with denormal/subnormal return
3347 * value
3348 * @retval VERR_FLOAT_UNDERFLOW on underflow, value set to +/- zero.
3349 * @retval VERR_FLOAT_OVERFLOW on overflow, value set to +/- infinity.
3350 * @retval VWRN_TRAILING_CHARS
3351 * @retval VWRN_TRAILING_SPACES
3352 *
3353 * @param pszValue The string to parse.
3354 * @param ppszNext Where to store the pointer to the first char following
3355 * the number. Optional.
3356 * @param cchMax Max number of character to parse. Zero means unlimited.
3357 * @param pr Where to return the number. Optional.
3358 *
3359 * @note This code isn't entirely perfect yet. It could exhibit rounding
3360 * differences compared to strtold & the compiler, and extreme value
3361 * may overflow/underflow prematurely depending on the build config.
3362 */
3363RTDECL(int) RTStrToFloatEx(const char *pszValue, char **ppszNext, size_t cchMax, float *pr);
3364
3365
3366/**
3367 * Gets a long double NaN.
3368 *
3369 * @returns NaN value.
3370 * @param pszTag Optional NaN tag for modifying the NaN value. We
3371 * recognizes a string of hex digits for inserting into the
3372 * fraction part. This may be followed 'quiet' or
3373 * 'signaling', ignoring case and requiring at only the
3374 * first character. The two components may be separated by
3375 * zero or more '_' characters. Any other stuff in the tag
3376 * will be ignored.
3377 *
3378 * If the tag is empty or we cannot grok any of it, we'll
3379 * return a default quiet NaN.
3380 * @param fPositive Whether the NaN value should be positive or negative
3381 * (for what that's worth).
3382 */
3383RTDECL(long double) RTStrNanLongDouble(const char *pszTag, bool fPositive);
3384
3385/**
3386 * Gets a double NaN.
3387 *
3388 * @returns NaN value.
3389 * @param pszTag Optional NaN tag for modifying the NaN value. We
3390 * recognizes a string of hex digits for inserting into the
3391 * fraction part. This may be followed 'quiet' or
3392 * 'signaling', ignoring case and requiring at only the
3393 * first character. The two components may be separated by
3394 * zero or more '_' characters. Any other stuff in the tag
3395 * will be ignored.
3396 *
3397 * If the tag is empty or we cannot grok any of it, we'll
3398 * return a default quiet NaN.
3399 * @param fPositive Whether the NaN value should be positive or negative
3400 * (for what that's worth).
3401 */
3402RTDECL(double) RTStrNanDouble(const char *pszTag, bool fPositive);
3403
3404/**
3405 * Gets a float NaN.
3406 *
3407 * @returns NaN value.
3408 * @param pszTag Optional NaN tag for modifying the NaN value. We
3409 * recognizes a string of hex digits for inserting into the
3410 * fraction part. This may be followed 'quiet' or
3411 * 'signaling', ignoring case and requiring at only the
3412 * first character. The two components may be separated by
3413 * zero or more '_' characters. Any other stuff in the tag
3414 * will be ignored.
3415 *
3416 * If the tag is empty or we cannot grok any of it, we'll
3417 * return a default quiet NaN.
3418 * @param fPositive Whether the NaN value should be positive or negative
3419 * (for what that's worth).
3420 */
3421RTDECL(float) RTStrNanFloat(const char *pszTag, bool fPositive);
3422
3423/**
3424 * Formats a buffer stream as hex bytes.
3425 *
3426 * The default is no separating spaces or line breaks or anything.
3427 *
3428 * @returns IPRT status code.
3429 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
3430 * @retval VERR_BUFFER_OVERFLOW if the buffer is insufficent to hold the bytes.
3431 *
3432 * @param pszBuf Output string buffer.
3433 * @param cbBuf The size of the output buffer.
3434 * @param pv Pointer to the bytes to stringify.
3435 * @param cb The number of bytes to stringify.
3436 * @param fFlags Combination of RTSTRPRINTHEXBYTES_F_XXX values.
3437 * @sa RTUtf16PrintHexBytes.
3438 */
3439RTDECL(int) RTStrPrintHexBytes(char *pszBuf, size_t cbBuf, void const *pv, size_t cb, uint32_t fFlags);
3440/** @name RTSTRPRINTHEXBYTES_F_XXX - flags for RTStrPrintHexBytes and RTUtf16PritnHexBytes.
3441 * @{ */
3442/** Upper case hex digits, the default is lower case. */
3443#define RTSTRPRINTHEXBYTES_F_UPPER RT_BIT(0)
3444/** Add a space between each group. */
3445#define RTSTRPRINTHEXBYTES_F_SEP_SPACE RT_BIT(1)
3446/** Add a colon between each group. */
3447#define RTSTRPRINTHEXBYTES_F_SEP_COLON RT_BIT(2)
3448/** @} */
3449
3450/**
3451 * Converts a string of hex bytes back into binary data.
3452 *
3453 * @returns IPRT status code.
3454 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
3455 * @retval VERR_BUFFER_OVERFLOW if the string contains too many hex bytes.
3456 * @retval VERR_BUFFER_UNDERFLOW if there aren't enough hex bytes to fill up
3457 * the output buffer.
3458 * @retval VERR_UNEVEN_INPUT if the input contains a half byte.
3459 * @retval VERR_NO_DIGITS
3460 * @retval VWRN_TRAILING_CHARS
3461 * @retval VWRN_TRAILING_SPACES
3462 *
3463 * @param pszHex The string containing the hex bytes.
3464 * @param pv Output buffer.
3465 * @param cb The size of the output buffer.
3466 * @param fFlags RTSTRCONVERTHEXBYTES_F_XXX.
3467 */
3468RTDECL(int) RTStrConvertHexBytes(char const *pszHex, void *pv, size_t cb, uint32_t fFlags);
3469
3470/** @name RTSTRCONVERTHEXBYTES_F_XXX - Flags for RTStrConvertHexBytes() and RTStrConvertHexBytesEx().
3471 * @{ */
3472/** Accept colon as a byte separator. */
3473#define RTSTRCONVERTHEXBYTES_F_SEP_COLON RT_BIT(0)
3474/** @} */
3475
3476/**
3477 * Converts a string of hex bytes back into binary data, extended version.
3478 *
3479 * @returns IPRT status code.
3480 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
3481 * @retval VERR_BUFFER_OVERFLOW if the string contains too many hex bytes.
3482 * @retval VERR_BUFFER_UNDERFLOW if there aren't enough hex bytes to fill up
3483 * the output buffer and *pcbReturned is NULL.
3484 * @retval VINF_BUFFER_UNDERFLOW if there aren't enough hex bytes to fill up
3485 * the output buffer and *pcbReturned is not NULL, *pcbReturned holds
3486 * the actual number of bytes.
3487 * @retval VERR_UNEVEN_INPUT if the input contains a half byte.
3488 * @retval VERR_NO_DIGITS
3489 * @retval VWRN_TRAILING_CHARS
3490 * @retval VWRN_TRAILING_SPACES
3491 *
3492 * @param pszHex The string containing the hex bytes.
3493 * @param pv Output buffer.
3494 * @param cb The size of the output buffer.
3495 * @param fFlags RTSTRCONVERTHEXBYTES_F_XXX.
3496 * @param ppszNext Set to point at where we stopped decoding hex bytes.
3497 * Optional.
3498 * @param pcbReturned Where to return the number of bytes found. Optional.
3499 */
3500RTDECL(int) RTStrConvertHexBytesEx(char const *pszHex, void *pv, size_t cb, uint32_t fFlags,
3501 const char **ppszNext, size_t *pcbReturned);
3502
3503/** @} */
3504
3505
3506/** @defgroup rt_str_space Unique String Space
3507 * @{
3508 */
3509
3510/** Pointer to a string name space container node core. */
3511typedef struct RTSTRSPACECORE *PRTSTRSPACECORE;
3512/** Pointer to a pointer to a string name space container node core. */
3513typedef PRTSTRSPACECORE *PPRTSTRSPACECORE;
3514
3515/**
3516 * String name space container node core.
3517 */
3518typedef struct RTSTRSPACECORE
3519{
3520 /** Pointer to the left leaf node. Don't touch. */
3521 PRTSTRSPACECORE pLeft;
3522 /** Pointer to the left right node. Don't touch. */
3523 PRTSTRSPACECORE pRight;
3524 /** Pointer to the list of string with the same hash key value. Don't touch. */
3525 PRTSTRSPACECORE pList;
3526 /** Hash key. Don't touch. */
3527 uint32_t Key;
3528 /** Height of this tree: max(heigth(left), heigth(right)) + 1. Don't touch */
3529 unsigned char uchHeight;
3530 /** The string length. Read only! */
3531 size_t cchString;
3532 /** Pointer to the string. Read only! */
3533 const char *pszString;
3534} RTSTRSPACECORE;
3535
3536/** String space. (Initialize with NULL.) */
3537typedef PRTSTRSPACECORE RTSTRSPACE;
3538/** Pointer to a string space. */
3539typedef PPRTSTRSPACECORE PRTSTRSPACE;
3540
3541
3542/**
3543 * Inserts a string into a unique string space.
3544 *
3545 * @returns true on success.
3546 * @returns false if the string collided with an existing string.
3547 * @param pStrSpace The space to insert it into.
3548 * @param pStr The string node.
3549 */
3550RTDECL(bool) RTStrSpaceInsert(PRTSTRSPACE pStrSpace, PRTSTRSPACECORE pStr);
3551
3552/**
3553 * Removes a string from a unique string space.
3554 *
3555 * @returns Pointer to the removed string node.
3556 * @returns NULL if the string was not found in the string space.
3557 * @param pStrSpace The space to remove it from.
3558 * @param pszString The string to remove.
3559 */
3560RTDECL(PRTSTRSPACECORE) RTStrSpaceRemove(PRTSTRSPACE pStrSpace, const char *pszString);
3561
3562/**
3563 * Gets a string from a unique string space.
3564 *
3565 * @returns Pointer to the string node.
3566 * @returns NULL if the string was not found in the string space.
3567 * @param pStrSpace The space to get it from.
3568 * @param pszString The string to get.
3569 */
3570RTDECL(PRTSTRSPACECORE) RTStrSpaceGet(PRTSTRSPACE pStrSpace, const char *pszString);
3571
3572/**
3573 * Gets a string from a unique string space.
3574 *
3575 * @returns Pointer to the string node.
3576 * @returns NULL if the string was not found in the string space.
3577 * @param pStrSpace The space to get it from.
3578 * @param pszString The string to get.
3579 * @param cchMax The max string length to evaluate. Passing
3580 * RTSTR_MAX is ok and makes it behave just like
3581 * RTStrSpaceGet.
3582 */
3583RTDECL(PRTSTRSPACECORE) RTStrSpaceGetN(PRTSTRSPACE pStrSpace, const char *pszString, size_t cchMax);
3584
3585/**
3586 * Callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy().
3587 *
3588 * @returns 0 on continue.
3589 * @returns Non-zero to aborts the operation.
3590 * @param pStr The string node
3591 * @param pvUser The user specified argument.
3592 */
3593typedef DECLCALLBACKTYPE(int, FNRTSTRSPACECALLBACK,(PRTSTRSPACECORE pStr, void *pvUser));
3594/** Pointer to callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy(). */
3595typedef FNRTSTRSPACECALLBACK *PFNRTSTRSPACECALLBACK;
3596
3597/**
3598 * Destroys the string space.
3599 *
3600 * The caller supplies a callback which will be called for each of the string
3601 * nodes in for freeing their memory and other resources.
3602 *
3603 * @returns 0 or what ever non-zero return value pfnCallback returned
3604 * when aborting the destruction.
3605 * @param pStrSpace The space to destroy.
3606 * @param pfnCallback The callback.
3607 * @param pvUser The user argument.
3608 */
3609RTDECL(int) RTStrSpaceDestroy(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
3610
3611/**
3612 * Enumerates the string space.
3613 * The caller supplies a callback which will be called for each of
3614 * the string nodes.
3615 *
3616 * @returns 0 or what ever non-zero return value pfnCallback returned
3617 * when aborting the destruction.
3618 * @param pStrSpace The space to enumerate.
3619 * @param pfnCallback The callback.
3620 * @param pvUser The user argument.
3621 */
3622RTDECL(int) RTStrSpaceEnumerate(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
3623
3624/** @} */
3625
3626
3627/** @defgroup rt_str_hash Sting hashing
3628 * @{ */
3629
3630/**
3631 * Hashes the given string using algorithm \#1.
3632 *
3633 * @returns String hash.
3634 * @param pszString The string to hash.
3635 */
3636RTDECL(uint32_t) RTStrHash1(const char *pszString);
3637
3638/**
3639 * Hashes the given string using algorithm \#1.
3640 *
3641 * @returns String hash.
3642 * @param pszString The string to hash.
3643 * @param cchString The max length to hash. Hashing will stop if the
3644 * terminator character is encountered first. Passing
3645 * RTSTR_MAX is fine.
3646 */
3647RTDECL(uint32_t) RTStrHash1N(const char *pszString, size_t cchString);
3648
3649/**
3650 * Hashes the given strings as if they were concatenated using algorithm \#1.
3651 *
3652 * @returns String hash.
3653 * @param cPairs The number of string / length pairs in the
3654 * ellipsis.
3655 * @param ... List of string (const char *) and length
3656 * (size_t) pairs. Passing RTSTR_MAX as the size is
3657 * fine.
3658 */
3659RTDECL(uint32_t) RTStrHash1ExN(size_t cPairs, ...);
3660
3661/**
3662 * Hashes the given strings as if they were concatenated using algorithm \#1.
3663 *
3664 * @returns String hash.
3665 * @param cPairs The number of string / length pairs in the @a va.
3666 * @param va List of string (const char *) and length
3667 * (size_t) pairs. Passing RTSTR_MAX as the size is
3668 * fine.
3669 */
3670RTDECL(uint32_t) RTStrHash1ExNV(size_t cPairs, va_list va);
3671
3672/** @} */
3673
3674
3675/** @defgroup rt_str_mem Raw memory operations.
3676 *
3677 * @note Following the memchr/memcpy/memcmp/memset tradition and putting these
3678 * in the string.h header rather than in the mem.h one.
3679 *
3680 * @{ */
3681
3682/**
3683 * Searches @a pvHaystack for a 16-bit sized and aligned @a uNeedle.
3684 *
3685 * @returns Pointer to the first hit if found, NULL if not found.
3686 * @param pvHaystack The memory to search.
3687 * @param uNeedle The 16-bit value to find.
3688 * @param cbHaystack Size of the memory to search.
3689 * @sa memchr, RTStrMemFind32, RTStrMemFind64
3690 */
3691RTDECL(uint16_t *) RTStrMemFind16(const void *pvHaystack, uint16_t uNeedle, size_t cbHaystack);
3692
3693/**
3694 * Searches @a pvHaystack for a 32-bit sized and aligned @a uNeedle.
3695 *
3696 * @returns Pointer to the first hit if found, NULL if not found.
3697 * @param pvHaystack The memory to search.
3698 * @param uNeedle The 32-bit value to find.
3699 * @param cbHaystack Size of the memory to search.
3700 * @sa memchr, RTStrMemFind16, RTStrMemFind64
3701 */
3702RTDECL(uint32_t *) RTStrMemFind32(const void *pvHaystack, uint32_t uNeedle, size_t cbHaystack);
3703
3704/**
3705 * Searches @a pvHaystack for a 64-bit sized and aligned @a uNeedle.
3706 *
3707 * @returns Pointer to the first hit if found, NULL if not found.
3708 * @param pvHaystack The memory to search.
3709 * @param uNeedle The 64-bit value to find.
3710 * @param cbHaystack Size of the memory to search.
3711 * @sa memchr, RTStrMemFind16, RTStrMemFind32
3712 */
3713RTDECL(uint64_t *) RTStrMemFind64(const void *pvHaystack, uint64_t uNeedle, size_t cbHaystack);
3714
3715/** @} */
3716
3717
3718/** @} */
3719
3720RT_C_DECLS_END
3721
3722#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