VirtualBox

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

Last change on this file since 76417 was 76408, checked in by vboxsync, 6 years ago

iprt/string.h: Dropped including utf16.h and let those who need it include it themselves. bugref:9344

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