VirtualBox

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

Last change on this file since 80569 was 80565, checked in by vboxsync, 5 years ago

IPRT/string.h: Added hexformatting variants that takes an additional 64-bit offset/address argument to be used instead of the memory pointer: %RhXd, %RhXD, %RhXs Also changed space between the pointer and offset to a slash.

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