VirtualBox

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

Last change on this file since 70942 was 70092, checked in by vboxsync, 7 years ago

IPRT: Added RTStrNICmpAscii.

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