VirtualBox

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

Last change on this file since 68716 was 67769, checked in by vboxsync, 7 years ago

iprt/string.h: removed C++ hack for the 'new' keyword. We don't need it anymore as we never compile Linux R0 code with a C++ compiler. Furthermore the hack did not always work, for instance not for replacing %[new] in cmpxchg.h.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 131.4 KB
Line 
1/** @file
2 * IPRT - String Manipulation.
3 */
4
5/*
6 * Copyright (C) 2006-2016 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 * - \%Rhxs - Takes a pointer to the memory to be displayed as a hex string,
1442 * i.e. a series of space separated bytes formatted as two digit hex value.
1443 * Use the precision to specify the length. Default length is 16 bytes.
1444 * The width, if specified, is ignored.
1445 * - \%Rrc - Takes an integer iprt status code as argument. Will insert the
1446 * status code define corresponding to the iprt status code.
1447 * - \%Rrs - Takes an integer iprt status code as argument. Will insert the
1448 * short description of the specified status code.
1449 * - \%Rrf - Takes an integer iprt status code as argument. Will insert the
1450 * full description of the specified status code.
1451 * - \%Rra - Takes an integer iprt status code as argument. Will insert the
1452 * status code define + full description.
1453 * - \%Rwc - Takes a long Windows error code as argument. Will insert the status
1454 * code define corresponding to the Windows error code.
1455 * - \%Rwf - Takes a long Windows error code as argument. Will insert the
1456 * full description of the specified status code.
1457 * - \%Rwa - Takes a long Windows error code as argument. Will insert the
1458 * error code define + full description.
1459 *
1460 * - \%Rhrc - Takes a COM/XPCOM status code as argument. Will insert the status
1461 * code define corresponding to the Windows error code.
1462 * - \%Rhrf - Takes a COM/XPCOM status code as argument. Will insert the
1463 * full description of the specified status code.
1464 * - \%Rhra - Takes a COM/XPCOM error code as argument. Will insert the
1465 * error code define + full description.
1466 *
1467 * - \%Rfn - Pretty printing of a function or method. It drops the
1468 * return code and parameter list.
1469 * - \%Rbn - Prints the base name. For dropping the path in
1470 * order to save space when printing a path name.
1471 *
1472 * - \%lRbs - Same as \%ls except inlut is big endian UTF-16.
1473 *
1474 * On other platforms, \%Rw? simply prints the argument in a form of 0xXXXXXXXX.
1475 *
1476 *
1477 * Group 4, structure dumpers:
1478 * - \%RDtimespec - Takes a PCRTTIMESPEC.
1479 *
1480 *
1481 * Group 5, XML / HTML escapers:
1482 * - \%RMas - Takes a string pointer (const char *) and outputs
1483 * it as an attribute value with the proper escaping.
1484 * This typically ends up in double quotes.
1485 *
1486 * - \%RMes - Takes a string pointer (const char *) and outputs
1487 * it as an element with the necessary escaping.
1488 *
1489 * Group 6, CPU Architecture Register dumpers:
1490 * - \%RAx86[reg] - Takes a 64-bit register value if the register is
1491 * 64-bit or smaller. Check the code wrt which
1492 * registers are implemented.
1493 *
1494 */
1495
1496#ifndef DECLARED_FNRTSTROUTPUT /* duplicated in iprt/log.h */
1497# define DECLARED_FNRTSTROUTPUT
1498/**
1499 * Output callback.
1500 *
1501 * @returns number of bytes written.
1502 * @param pvArg User argument.
1503 * @param pachChars Pointer to an array of utf-8 characters.
1504 * @param cbChars Number of bytes in the character array pointed to by pachChars.
1505 */
1506typedef DECLCALLBACK(size_t) FNRTSTROUTPUT(void *pvArg, const char *pachChars, size_t cbChars);
1507/** Pointer to callback function. */
1508typedef FNRTSTROUTPUT *PFNRTSTROUTPUT;
1509#endif
1510
1511/** @name Format flag.
1512 * These are used by RTStrFormat extensions and RTStrFormatNumber, mind
1513 * that not all flags makes sense to both of the functions.
1514 * @{ */
1515#define RTSTR_F_CAPITAL 0x0001
1516#define RTSTR_F_LEFT 0x0002
1517#define RTSTR_F_ZEROPAD 0x0004
1518#define RTSTR_F_SPECIAL 0x0008
1519#define RTSTR_F_VALSIGNED 0x0010
1520#define RTSTR_F_PLUS 0x0020
1521#define RTSTR_F_BLANK 0x0040
1522#define RTSTR_F_WIDTH 0x0080
1523#define RTSTR_F_PRECISION 0x0100
1524#define RTSTR_F_THOUSAND_SEP 0x0200
1525#define RTSTR_F_OBFUSCATE_PTR 0x0400
1526
1527#define RTSTR_F_BIT_MASK 0xf800
1528#define RTSTR_F_8BIT 0x0800
1529#define RTSTR_F_16BIT 0x1000
1530#define RTSTR_F_32BIT 0x2000
1531#define RTSTR_F_64BIT 0x4000
1532#define RTSTR_F_128BIT 0x8000
1533/** @} */
1534
1535/** @def RTSTR_GET_BIT_FLAG
1536 * Gets the bit flag for the specified type.
1537 */
1538#define RTSTR_GET_BIT_FLAG(type) \
1539 ( sizeof(type) * 8 == 32 ? RTSTR_F_32BIT \
1540 : sizeof(type) * 8 == 64 ? RTSTR_F_64BIT \
1541 : sizeof(type) * 8 == 16 ? RTSTR_F_16BIT \
1542 : sizeof(type) * 8 == 8 ? RTSTR_F_8BIT \
1543 : sizeof(type) * 8 == 128 ? RTSTR_F_128BIT \
1544 : 0)
1545
1546
1547/**
1548 * Callback to format non-standard format specifiers.
1549 *
1550 * @returns The number of bytes formatted.
1551 * @param pvArg Formatter argument.
1552 * @param pfnOutput Pointer to output function.
1553 * @param pvArgOutput Argument for the output function.
1554 * @param ppszFormat Pointer to the format string pointer. Advance this till the char
1555 * after the format specifier.
1556 * @param pArgs Pointer to the argument list. Use this to fetch the arguments.
1557 * @param cchWidth Format Width. -1 if not specified.
1558 * @param cchPrecision Format Precision. -1 if not specified.
1559 * @param fFlags Flags (RTSTR_NTFS_*).
1560 * @param chArgSize The argument size specifier, 'l' or 'L'.
1561 */
1562typedef DECLCALLBACK(size_t) FNSTRFORMAT(void *pvArg, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
1563 const char **ppszFormat, va_list *pArgs, int cchWidth,
1564 int cchPrecision, unsigned fFlags, char chArgSize);
1565/** Pointer to a FNSTRFORMAT() function. */
1566typedef FNSTRFORMAT *PFNSTRFORMAT;
1567
1568
1569/**
1570 * Partial implementation of a printf like formatter.
1571 * It doesn't do everything correct, and there is no floating point support.
1572 * However, it supports custom formats by the means of a format callback.
1573 *
1574 * @returns number of bytes formatted.
1575 * @param pfnOutput Output worker.
1576 * Called in two ways. Normally with a string and its length.
1577 * For termination, it's called with NULL for string, 0 for length.
1578 * @param pvArgOutput Argument to the output worker.
1579 * @param pfnFormat Custom format worker.
1580 * @param pvArgFormat Argument to the format worker.
1581 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1582 * @param InArgs Argument list.
1583 */
1584RTDECL(size_t) RTStrFormatV(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat,
1585 const char *pszFormat, va_list InArgs) RT_IPRT_FORMAT_ATTR(5, 0);
1586
1587/**
1588 * Partial implementation of a printf like formatter.
1589 *
1590 * It doesn't do everything correct, and there is no floating point support.
1591 * However, it supports custom formats by the means of a format callback.
1592 *
1593 * @returns number of bytes formatted.
1594 * @param pfnOutput Output worker.
1595 * Called in two ways. Normally with a string and its length.
1596 * For termination, it's called with NULL for string, 0 for length.
1597 * @param pvArgOutput Argument to the output worker.
1598 * @param pfnFormat Custom format worker.
1599 * @param pvArgFormat Argument to the format worker.
1600 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1601 * @param ... Argument list.
1602 */
1603RTDECL(size_t) RTStrFormat(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat,
1604 const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(5, 6);
1605
1606/**
1607 * Formats an integer number according to the parameters.
1608 *
1609 * @returns Length of the formatted number.
1610 * @param psz Pointer to output string buffer of sufficient size.
1611 * @param u64Value Value to format.
1612 * @param uiBase Number representation base.
1613 * @param cchWidth Width.
1614 * @param cchPrecision Precision.
1615 * @param fFlags Flags, RTSTR_F_XXX.
1616 */
1617RTDECL(int) RTStrFormatNumber(char *psz, uint64_t u64Value, unsigned int uiBase, signed int cchWidth, signed int cchPrecision,
1618 unsigned int fFlags);
1619
1620/**
1621 * Formats an unsigned 8-bit number.
1622 *
1623 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1624 * @param pszBuf The output buffer.
1625 * @param cbBuf The size of the output buffer.
1626 * @param u8Value The 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(ssize_t) RTStrFormatU8(char *pszBuf, size_t cbBuf, uint8_t u8Value, unsigned int uiBase,
1633 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1634
1635/**
1636 * Formats an unsigned 16-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 u16Value 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) RTStrFormatU16(char *pszBuf, size_t cbBuf, uint16_t u16Value, unsigned int uiBase,
1648 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1649
1650/**
1651 * Formats an unsigned 32-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 u32Value 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) RTStrFormatU32(char *pszBuf, size_t cbBuf, uint32_t u32Value, unsigned int uiBase,
1663 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1664
1665/**
1666 * Formats an unsigned 64-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 u64Value 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) RTStrFormatU64(char *pszBuf, size_t cbBuf, uint64_t u64Value, unsigned int uiBase,
1678 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1679
1680/**
1681 * Formats an unsigned 128-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 pu128Value 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 * @remarks The current implementation is limited to base 16 and doesn't do
1692 * width or precision and probably ignores few flags too.
1693 */
1694RTDECL(ssize_t) RTStrFormatU128(char *pszBuf, size_t cbBuf, PCRTUINT128U pu128Value, unsigned int uiBase,
1695 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1696
1697/**
1698 * Formats an unsigned 256-bit number.
1699 *
1700 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1701 * @param pszBuf The output buffer.
1702 * @param cbBuf The size of the output buffer.
1703 * @param pu256Value The value to format.
1704 * @param uiBase Number representation base.
1705 * @param cchWidth Width.
1706 * @param cchPrecision Precision.
1707 * @param fFlags Flags, RTSTR_F_XXX.
1708 * @remarks The current implementation is limited to base 16 and doesn't do
1709 * width or precision and probably ignores few flags too.
1710 */
1711RTDECL(ssize_t) RTStrFormatU256(char *pszBuf, size_t cbBuf, PCRTUINT256U pu256Value, unsigned int uiBase,
1712 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1713
1714/**
1715 * Formats an unsigned 512-bit number.
1716 *
1717 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1718 * @param pszBuf The output buffer.
1719 * @param cbBuf The size of the output buffer.
1720 * @param pu512Value The value to format.
1721 * @param uiBase Number representation base.
1722 * @param cchWidth Width.
1723 * @param cchPrecision Precision.
1724 * @param fFlags Flags, RTSTR_F_XXX.
1725 * @remarks The current implementation is limited to base 16 and doesn't do
1726 * width or precision and probably ignores few flags too.
1727 */
1728RTDECL(ssize_t) RTStrFormatU512(char *pszBuf, size_t cbBuf, PCRTUINT512U pu512Value, unsigned int uiBase,
1729 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1730
1731
1732/**
1733 * Formats an 80-bit extended floating point number.
1734 *
1735 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1736 * @param pszBuf The output buffer.
1737 * @param cbBuf The size of the output buffer.
1738 * @param pr80Value The value to format.
1739 * @param cchWidth Width.
1740 * @param cchPrecision Precision.
1741 * @param fFlags Flags, RTSTR_F_XXX.
1742 */
1743RTDECL(ssize_t) RTStrFormatR80(char *pszBuf, size_t cbBuf, PCRTFLOAT80U pr80Value, signed int cchWidth,
1744 signed int cchPrecision, uint32_t fFlags);
1745
1746/**
1747 * Formats an 80-bit extended floating point number, version 2.
1748 *
1749 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1750 * @param pszBuf The output buffer.
1751 * @param cbBuf The size of the output buffer.
1752 * @param pr80Value The value to format.
1753 * @param cchWidth Width.
1754 * @param cchPrecision Precision.
1755 * @param fFlags Flags, RTSTR_F_XXX.
1756 */
1757RTDECL(ssize_t) RTStrFormatR80u2(char *pszBuf, size_t cbBuf, PCRTFLOAT80U2 pr80Value, signed int cchWidth,
1758 signed int cchPrecision, uint32_t fFlags);
1759
1760
1761
1762/**
1763 * Callback for formatting a type.
1764 *
1765 * This is registered using the RTStrFormatTypeRegister function and will
1766 * be called during string formatting to handle the specified %R[type].
1767 * The argument for this format type is assumed to be a pointer and it's
1768 * passed in the @a pvValue argument.
1769 *
1770 * @returns Length of the formatted output.
1771 * @param pfnOutput Output worker.
1772 * @param pvArgOutput Argument to the output worker.
1773 * @param pszType The type name.
1774 * @param pvValue The argument value.
1775 * @param cchWidth Width.
1776 * @param cchPrecision Precision.
1777 * @param fFlags Flags (NTFS_*).
1778 * @param pvUser The user argument.
1779 */
1780typedef DECLCALLBACK(size_t) FNRTSTRFORMATTYPE(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
1781 const char *pszType, void const *pvValue,
1782 int cchWidth, int cchPrecision, unsigned fFlags,
1783 void *pvUser);
1784/** Pointer to a FNRTSTRFORMATTYPE. */
1785typedef FNRTSTRFORMATTYPE *PFNRTSTRFORMATTYPE;
1786
1787
1788/**
1789 * Register a format handler for a type.
1790 *
1791 * The format handler is used to handle '%R[type]' format types, where the argument
1792 * in the vector is a pointer value (a bit restrictive, but keeps it simple).
1793 *
1794 * The caller must ensure that no other thread will be making use of any of
1795 * the dynamic formatting type facilities simultaneously with this call.
1796 *
1797 * @returns IPRT status code.
1798 * @retval VINF_SUCCESS on success.
1799 * @retval VERR_ALREADY_EXISTS if the type has already been registered.
1800 * @retval VERR_TOO_MANY_OPEN_FILES if all the type slots has been allocated already.
1801 *
1802 * @param pszType The type name.
1803 * @param pfnHandler The handler address. See FNRTSTRFORMATTYPE for details.
1804 * @param pvUser The user argument to pass to the handler. See RTStrFormatTypeSetUser
1805 * for how to update this later.
1806 */
1807RTDECL(int) RTStrFormatTypeRegister(const char *pszType, PFNRTSTRFORMATTYPE pfnHandler, void *pvUser);
1808
1809/**
1810 * Deregisters a format type.
1811 *
1812 * The caller must ensure that no other thread will be making use of any of
1813 * the dynamic formatting type facilities simultaneously with this call.
1814 *
1815 * @returns IPRT status code.
1816 * @retval VINF_SUCCESS on success.
1817 * @retval VERR_FILE_NOT_FOUND if not found.
1818 *
1819 * @param pszType The type to deregister.
1820 */
1821RTDECL(int) RTStrFormatTypeDeregister(const char *pszType);
1822
1823/**
1824 * Sets the user argument for a type.
1825 *
1826 * This can be used if a user argument needs relocating in GC.
1827 *
1828 * @returns IPRT status code.
1829 * @retval VINF_SUCCESS on success.
1830 * @retval VERR_FILE_NOT_FOUND if not found.
1831 *
1832 * @param pszType The type to update.
1833 * @param pvUser The new user argument value.
1834 */
1835RTDECL(int) RTStrFormatTypeSetUser(const char *pszType, void *pvUser);
1836
1837
1838/**
1839 * String printf.
1840 *
1841 * @returns The length of the returned string (in pszBuffer) excluding the
1842 * terminator.
1843 * @param pszBuffer Output buffer.
1844 * @param cchBuffer Size of the output buffer.
1845 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1846 * @param args The format argument.
1847 *
1848 * @deprecated Use RTStrPrintf2V! Problematic return value on overflow.
1849 */
1850RTDECL(size_t) RTStrPrintfV(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(3, 0);
1851
1852/**
1853 * String printf.
1854 *
1855 * @returns The length of the returned string (in pszBuffer) excluding the
1856 * terminator.
1857 * @param pszBuffer Output buffer.
1858 * @param cchBuffer Size of the output buffer.
1859 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1860 * @param ... The format argument.
1861 *
1862 * @deprecated Use RTStrPrintf2! Problematic return value on overflow.
1863 */
1864RTDECL(size_t) RTStrPrintf(char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
1865
1866/**
1867 * String printf with custom formatting.
1868 *
1869 * @returns The length of the returned string (in pszBuffer) excluding the
1870 * terminator.
1871 * @param pfnFormat Pointer to handler function for the custom formats.
1872 * @param pvArg Argument to the pfnFormat function.
1873 * @param pszBuffer Output buffer.
1874 * @param cchBuffer Size of the output buffer.
1875 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1876 * @param args The format argument.
1877 *
1878 * @deprecated Use RTStrPrintf2ExV! Problematic return value on overflow.
1879 */
1880RTDECL(size_t) RTStrPrintfExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer,
1881 const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(5, 0);
1882
1883/**
1884 * String printf with custom formatting.
1885 *
1886 * @returns The length of the returned string (in pszBuffer) excluding the
1887 * terminator.
1888 * @param pfnFormat Pointer to handler function for the custom formats.
1889 * @param pvArg Argument to the pfnFormat function.
1890 * @param pszBuffer Output buffer.
1891 * @param cchBuffer Size of the output buffer.
1892 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1893 * @param ... The format argument.
1894 *
1895 * @deprecated Use RTStrPrintf2Ex! Problematic return value on overflow.
1896 */
1897RTDECL(size_t) RTStrPrintfEx(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer,
1898 const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(5, 6);
1899
1900/**
1901 * String printf, version 2.
1902 *
1903 * @returns On success, positive count of formatted character excluding the
1904 * terminator. On buffer overflow, negative number giving the required
1905 * buffer size (including terminator char).
1906 *
1907 * @param pszBuffer Output buffer.
1908 * @param cbBuffer Size of the output buffer.
1909 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1910 * @param args The format argument.
1911 */
1912RTDECL(ssize_t) RTStrPrintf2V(char *pszBuffer, size_t cbBuffer, const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(3, 0);
1913
1914/**
1915 * String printf, version 2.
1916 *
1917 * @returns On success, positive count of formatted character excluding the
1918 * terminator. On buffer overflow, negative number giving the required
1919 * buffer size (including terminator char).
1920 *
1921 * @param pszBuffer Output buffer.
1922 * @param cbBuffer Size of the output buffer.
1923 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1924 * @param ... The format argument.
1925 */
1926RTDECL(ssize_t) RTStrPrintf2(char *pszBuffer, size_t cbBuffer, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
1927
1928/**
1929 * String printf with custom formatting, version 2.
1930 *
1931 * @returns On success, positive count of formatted character excluding the
1932 * terminator. On buffer overflow, negative number giving the required
1933 * buffer size (including terminator char).
1934 *
1935 * @param pfnFormat Pointer to handler function for the custom formats.
1936 * @param pvArg Argument to the pfnFormat function.
1937 * @param pszBuffer Output buffer.
1938 * @param cbBuffer Size of the output buffer.
1939 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1940 * @param args The format argument.
1941 */
1942RTDECL(ssize_t) RTStrPrintf2ExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cbBuffer,
1943 const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(5, 0);
1944
1945/**
1946 * String printf with custom formatting, version 2.
1947 *
1948 * @returns On success, positive count of formatted character excluding the
1949 * terminator. On buffer overflow, negative number giving the required
1950 * buffer size (including terminator char).
1951 *
1952 * @param pfnFormat Pointer to handler function for the custom formats.
1953 * @param pvArg Argument to the pfnFormat function.
1954 * @param pszBuffer Output buffer.
1955 * @param cbBuffer Size of the output buffer.
1956 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1957 * @param ... The format argument.
1958 */
1959RTDECL(ssize_t) RTStrPrintf2Ex(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cbBuffer,
1960 const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(5, 6);
1961
1962/**
1963 * Allocating string printf (default tag).
1964 *
1965 * @returns The length of the string in the returned *ppszBuffer excluding the
1966 * terminator.
1967 * @returns -1 on failure.
1968 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
1969 * The buffer should be freed using RTStrFree().
1970 * On failure *ppszBuffer will be set to NULL.
1971 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1972 * @param args The format argument.
1973 */
1974#define RTStrAPrintfV(ppszBuffer, pszFormat, args) RTStrAPrintfVTag((ppszBuffer), (pszFormat), (args), RTSTR_TAG)
1975
1976/**
1977 * Allocating string printf (custom tag).
1978 *
1979 * @returns The length of the string in the returned *ppszBuffer excluding the
1980 * terminator.
1981 * @returns -1 on failure.
1982 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
1983 * The buffer should be freed using RTStrFree().
1984 * On failure *ppszBuffer will be set to NULL.
1985 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1986 * @param args The format argument.
1987 * @param pszTag Allocation tag used for statistics and such.
1988 */
1989RTDECL(int) RTStrAPrintfVTag(char **ppszBuffer, const char *pszFormat, va_list args, const char *pszTag) RT_IPRT_FORMAT_ATTR(2, 0);
1990
1991/**
1992 * Allocating string printf.
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 ... The format argument.
2002 */
2003DECLINLINE(int) RT_IPRT_FORMAT_ATTR(2, 3) RTStrAPrintf(char **ppszBuffer, const char *pszFormat, ...)
2004{
2005 int cbRet;
2006 va_list va;
2007 va_start(va, pszFormat);
2008 cbRet = RTStrAPrintfVTag(ppszBuffer, pszFormat, va, RTSTR_TAG);
2009 va_end(va);
2010 return cbRet;
2011}
2012
2013/**
2014 * Allocating string printf (custom tag).
2015 *
2016 * @returns The length of the string in the returned *ppszBuffer excluding the
2017 * terminator.
2018 * @returns -1 on failure.
2019 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
2020 * The buffer should be freed using RTStrFree().
2021 * On failure *ppszBuffer will be set to NULL.
2022 * @param pszTag Allocation tag used for statistics and such.
2023 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2024 * @param ... The format argument.
2025 */
2026DECLINLINE(int) RT_IPRT_FORMAT_ATTR(3, 4) RTStrAPrintfTag(char **ppszBuffer, const char *pszTag, const char *pszFormat, ...)
2027{
2028 int cbRet;
2029 va_list va;
2030 va_start(va, pszFormat);
2031 cbRet = RTStrAPrintfVTag(ppszBuffer, pszFormat, va, pszTag);
2032 va_end(va);
2033 return cbRet;
2034}
2035
2036/**
2037 * Allocating string printf, version 2.
2038 *
2039 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
2040 * memory.
2041 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2042 * @param args The format argument.
2043 */
2044#define RTStrAPrintf2V(pszFormat, args) RTStrAPrintf2VTag((pszFormat), (args), RTSTR_TAG)
2045
2046/**
2047 * Allocating string printf, version 2.
2048 *
2049 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
2050 * memory.
2051 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2052 * @param args The format argument.
2053 * @param pszTag Allocation tag used for statistics and such.
2054 */
2055RTDECL(char *) RTStrAPrintf2VTag(const char *pszFormat, va_list args, const char *pszTag) RT_IPRT_FORMAT_ATTR(1, 0);
2056
2057/**
2058 * Allocating string printf, version 2 (default tag).
2059 *
2060 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
2061 * memory.
2062 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2063 * @param ... The format argument.
2064 */
2065DECLINLINE(char *) RT_IPRT_FORMAT_ATTR(1, 2) RTStrAPrintf2(const char *pszFormat, ...)
2066{
2067 char *pszRet;
2068 va_list va;
2069 va_start(va, pszFormat);
2070 pszRet = RTStrAPrintf2VTag(pszFormat, va, RTSTR_TAG);
2071 va_end(va);
2072 return pszRet;
2073}
2074
2075/**
2076 * Allocating string printf, version 2 (custom tag).
2077 *
2078 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
2079 * memory.
2080 * @param pszTag Allocation tag used for statistics and such.
2081 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2082 * @param ... The format argument.
2083 */
2084DECLINLINE(char *) RT_IPRT_FORMAT_ATTR(2, 3) RTStrAPrintf2Tag(const char *pszTag, const char *pszFormat, ...)
2085{
2086 char *pszRet;
2087 va_list va;
2088 va_start(va, pszFormat);
2089 pszRet = RTStrAPrintf2VTag(pszFormat, va, pszTag);
2090 va_end(va);
2091 return pszRet;
2092}
2093
2094/**
2095 * Strips blankspaces from both ends of the string.
2096 *
2097 * @returns Pointer to first non-blank char in the string.
2098 * @param psz The string to strip.
2099 */
2100RTDECL(char *) RTStrStrip(char *psz);
2101
2102/**
2103 * Strips blankspaces from the start of the string.
2104 *
2105 * @returns Pointer to first non-blank char in the string.
2106 * @param psz The string to strip.
2107 */
2108RTDECL(char *) RTStrStripL(const char *psz);
2109
2110/**
2111 * Strips blankspaces from the end of the string.
2112 *
2113 * @returns psz.
2114 * @param psz The string to strip.
2115 */
2116RTDECL(char *) RTStrStripR(char *psz);
2117
2118/**
2119 * String copy with overflow handling.
2120 *
2121 * @retval VINF_SUCCESS on success.
2122 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2123 * buffer will contain as much of the string as it can hold, fully
2124 * terminated.
2125 *
2126 * @param pszDst The destination buffer.
2127 * @param cbDst The size of the destination buffer (in bytes).
2128 * @param pszSrc The source string. NULL is not OK.
2129 */
2130RTDECL(int) RTStrCopy(char *pszDst, size_t cbDst, const char *pszSrc);
2131
2132/**
2133 * String copy with overflow handling.
2134 *
2135 * @retval VINF_SUCCESS on success.
2136 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2137 * buffer will contain as much of the string as it can hold, fully
2138 * terminated.
2139 *
2140 * @param pszDst The destination buffer.
2141 * @param cbDst The size of the destination buffer (in bytes).
2142 * @param pszSrc The source string. NULL is not OK.
2143 * @param cchSrcMax The maximum number of chars (not code points) to
2144 * copy from the source string, not counting the
2145 * terminator as usual.
2146 */
2147RTDECL(int) RTStrCopyEx(char *pszDst, size_t cbDst, const char *pszSrc, size_t cchSrcMax);
2148
2149/**
2150 * String copy with overflow handling and buffer advancing.
2151 *
2152 * @retval VINF_SUCCESS on success.
2153 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2154 * buffer will contain as much of the string as it can hold, fully
2155 * terminated.
2156 *
2157 * @param ppszDst Pointer to the destination buffer pointer.
2158 * This will be advanced to the end of the copied
2159 * bytes (points at the terminator). This is also
2160 * updated on overflow.
2161 * @param pcbDst Pointer to the destination buffer size
2162 * variable. This will be updated in accord with
2163 * the buffer pointer.
2164 * @param pszSrc The source string. NULL is not OK.
2165 */
2166RTDECL(int) RTStrCopyP(char **ppszDst, size_t *pcbDst, const char *pszSrc);
2167
2168/**
2169 * String copy with overflow handling.
2170 *
2171 * @retval VINF_SUCCESS on success.
2172 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2173 * buffer will contain as much of the string as it can hold, fully
2174 * terminated.
2175 *
2176 * @param ppszDst Pointer to the destination buffer pointer.
2177 * This will be advanced to the end of the copied
2178 * bytes (points at the terminator). This is also
2179 * updated on overflow.
2180 * @param pcbDst Pointer to the destination buffer size
2181 * variable. This will be updated in accord with
2182 * the buffer pointer.
2183 * @param pszSrc The source string. NULL is not OK.
2184 * @param cchSrcMax The maximum number of chars (not code points) to
2185 * copy from the source string, not counting the
2186 * terminator as usual.
2187 */
2188RTDECL(int) RTStrCopyPEx(char **ppszDst, size_t *pcbDst, const char *pszSrc, size_t cchSrcMax);
2189
2190/**
2191 * String concatenation with overflow handling.
2192 *
2193 * @retval VINF_SUCCESS on success.
2194 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2195 * buffer will contain as much of the string as it can hold, fully
2196 * terminated.
2197 *
2198 * @param pszDst The destination buffer.
2199 * @param cbDst The size of the destination buffer (in bytes).
2200 * @param pszSrc The source string. NULL is not OK.
2201 */
2202RTDECL(int) RTStrCat(char *pszDst, size_t cbDst, const char *pszSrc);
2203
2204/**
2205 * String concatenation with overflow handling.
2206 *
2207 * @retval VINF_SUCCESS on success.
2208 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2209 * buffer will contain as much of the string as it can hold, fully
2210 * terminated.
2211 *
2212 * @param pszDst The destination buffer.
2213 * @param cbDst The size of the destination buffer (in bytes).
2214 * @param pszSrc The source string. NULL is not OK.
2215 * @param cchSrcMax The maximum number of chars (not code points) to
2216 * copy from the source string, not counting the
2217 * terminator as usual.
2218 */
2219RTDECL(int) RTStrCatEx(char *pszDst, size_t cbDst, const char *pszSrc, size_t cchSrcMax);
2220
2221/**
2222 * String concatenation with overflow handling.
2223 *
2224 * @retval VINF_SUCCESS on success.
2225 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2226 * buffer will contain as much of the string as it can hold, fully
2227 * terminated.
2228 *
2229 * @param ppszDst Pointer to the destination buffer pointer.
2230 * This will be advanced to the end of the copied
2231 * bytes (points at the terminator). This is also
2232 * updated on overflow.
2233 * @param pcbDst Pointer to the destination buffer size
2234 * variable. This will be updated in accord with
2235 * the buffer pointer.
2236 * @param pszSrc The source string. NULL is not OK.
2237 */
2238RTDECL(int) RTStrCatP(char **ppszDst, size_t *pcbDst, const char *pszSrc);
2239
2240/**
2241 * String concatenation with overflow handling and buffer advancing.
2242 *
2243 * @retval VINF_SUCCESS on success.
2244 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2245 * buffer will contain as much of the string as it can hold, fully
2246 * terminated.
2247 *
2248 * @param ppszDst Pointer to the destination buffer pointer.
2249 * This will be advanced to the end of the copied
2250 * bytes (points at the terminator). This is also
2251 * updated on overflow.
2252 * @param pcbDst Pointer to the destination buffer size
2253 * variable. This will be updated in accord with
2254 * the buffer pointer.
2255 * @param pszSrc The source string. NULL is not OK.
2256 * @param cchSrcMax The maximum number of chars (not code points) to
2257 * copy from the source string, not counting the
2258 * terminator as usual.
2259 */
2260RTDECL(int) RTStrCatPEx(char **ppszDst, size_t *pcbDst, const char *pszSrc, size_t cchSrcMax);
2261
2262/**
2263 * Performs a case sensitive string compare between two UTF-8 strings.
2264 *
2265 * Encoding errors are ignored by the current implementation. So, the only
2266 * difference between this and the CRT strcmp function is the handling of
2267 * NULL arguments.
2268 *
2269 * @returns < 0 if the first string less than the second string.
2270 * @returns 0 if the first string identical to the second string.
2271 * @returns > 0 if the first string greater than the second string.
2272 * @param psz1 First UTF-8 string. Null is allowed.
2273 * @param psz2 Second UTF-8 string. Null is allowed.
2274 */
2275RTDECL(int) RTStrCmp(const char *psz1, const char *psz2);
2276
2277/**
2278 * Performs a case sensitive string compare between two UTF-8 strings, given
2279 * a maximum string length.
2280 *
2281 * Encoding errors are ignored by the current implementation. So, the only
2282 * difference between this and the CRT strncmp function is the handling of
2283 * NULL arguments.
2284 *
2285 * @returns < 0 if the first string less than the second string.
2286 * @returns 0 if the first string identical to the second string.
2287 * @returns > 0 if the first string greater than the second string.
2288 * @param psz1 First UTF-8 string. Null is allowed.
2289 * @param psz2 Second UTF-8 string. Null is allowed.
2290 * @param cchMax The maximum string length
2291 */
2292RTDECL(int) RTStrNCmp(const char *psz1, const char *psz2, size_t cchMax);
2293
2294/**
2295 * Performs a case insensitive string compare between two UTF-8 strings.
2296 *
2297 * This is a simplified compare, as only the simplified lower/upper case folding
2298 * specified by the unicode specs are used. It does not consider character pairs
2299 * as they are used in some languages, just simple upper & lower case compares.
2300 *
2301 * The result is the difference between the mismatching codepoints after they
2302 * both have been lower cased.
2303 *
2304 * If the string encoding is invalid the function will assert (strict builds)
2305 * and use RTStrCmp for the remainder of the string.
2306 *
2307 * @returns < 0 if the first string less than the second string.
2308 * @returns 0 if the first string identical to the second string.
2309 * @returns > 0 if the first string greater than the second string.
2310 * @param psz1 First UTF-8 string. Null is allowed.
2311 * @param psz2 Second UTF-8 string. Null is allowed.
2312 */
2313RTDECL(int) RTStrICmp(const char *psz1, const char *psz2);
2314
2315/**
2316 * Performs a case insensitive string compare between two UTF-8 strings, given a
2317 * maximum string length.
2318 *
2319 * This is a simplified compare, as only the simplified lower/upper case folding
2320 * specified by the unicode specs are used. It does not consider character pairs
2321 * as they are used in some languages, just simple upper & lower case compares.
2322 *
2323 * The result is the difference between the mismatching codepoints after they
2324 * both have been lower cased.
2325 *
2326 * If the string encoding is invalid the function will assert (strict builds)
2327 * and use RTStrCmp for the remainder of the string.
2328 *
2329 * @returns < 0 if the first string less than the second string.
2330 * @returns 0 if the first string identical to the second string.
2331 * @returns > 0 if the first string greater than the second string.
2332 * @param psz1 First UTF-8 string. Null is allowed.
2333 * @param psz2 Second UTF-8 string. Null is allowed.
2334 * @param cchMax Maximum string length
2335 */
2336RTDECL(int) RTStrNICmp(const char *psz1, const char *psz2, size_t cchMax);
2337
2338/**
2339 * Performs a case insensitive string compare between a UTF-8 string and a 7-bit
2340 * ASCII string.
2341 *
2342 * This is potentially faster than RTStrICmp and drags in less dependencies. It
2343 * is really handy for hardcoded inputs.
2344 *
2345 * If the string encoding is invalid the function will assert (strict builds)
2346 * and use RTStrCmp for the remainder of the string.
2347 *
2348 * @returns < 0 if the first string less than the second string.
2349 * @returns 0 if the first string identical to the second string.
2350 * @returns > 0 if the first string greater than the second string.
2351 * @param psz1 First UTF-8 string. Null is allowed.
2352 * @param psz2 Second string, 7-bit ASCII. Null is allowed.
2353 * @sa RTUtf16ICmpAscii
2354 */
2355RTDECL(int) RTStrICmpAscii(const char *psz1, const char *psz2);
2356
2357/**
2358 * Checks whether @a pszString starts with @a pszStart.
2359 *
2360 * @returns true / false.
2361 * @param pszString The string to check.
2362 * @param pszStart The start string to check for.
2363 */
2364RTDECL(int) RTStrStartsWith(const char *pszString, const char *pszStart);
2365
2366/**
2367 * Checks whether @a pszString starts with @a pszStart, case insensitive.
2368 *
2369 * @returns true / false.
2370 * @param pszString The string to check.
2371 * @param pszStart The start string to check for.
2372 */
2373RTDECL(int) RTStrIStartsWith(const char *pszString, const char *pszStart);
2374
2375/**
2376 * Locates a case sensitive substring.
2377 *
2378 * If any of the two strings are NULL, then NULL is returned. If the needle is
2379 * an empty string, then the haystack is returned (i.e. matches anything).
2380 *
2381 * @returns Pointer to the first occurrence of the substring if found, NULL if
2382 * not.
2383 *
2384 * @param pszHaystack The string to search.
2385 * @param pszNeedle The substring to search for.
2386 *
2387 * @remarks The difference between this and strstr is the handling of NULL
2388 * pointers.
2389 */
2390RTDECL(char *) RTStrStr(const char *pszHaystack, const char *pszNeedle);
2391
2392/**
2393 * Locates a case insensitive substring.
2394 *
2395 * If any of the two strings are NULL, then NULL is returned. If the needle is
2396 * an empty string, then the haystack is returned (i.e. matches anything).
2397 *
2398 * @returns Pointer to the first occurrence of the substring if found, NULL if
2399 * not.
2400 *
2401 * @param pszHaystack The string to search.
2402 * @param pszNeedle The substring to search for.
2403 *
2404 */
2405RTDECL(char *) RTStrIStr(const char *pszHaystack, const char *pszNeedle);
2406
2407/**
2408 * Converts the string to lower case.
2409 *
2410 * @returns Pointer to the converted string.
2411 * @param psz The string to convert.
2412 */
2413RTDECL(char *) RTStrToLower(char *psz);
2414
2415/**
2416 * Converts the string to upper case.
2417 *
2418 * @returns Pointer to the converted string.
2419 * @param psz The string to convert.
2420 */
2421RTDECL(char *) RTStrToUpper(char *psz);
2422
2423/**
2424 * Checks if the string is case foldable, i.e. whether it would change if
2425 * subject to RTStrToLower or RTStrToUpper.
2426 *
2427 * @returns true / false
2428 * @param psz The string in question.
2429 */
2430RTDECL(bool) RTStrIsCaseFoldable(const char *psz);
2431
2432/**
2433 * Checks if the string is upper cased (no lower case chars in it).
2434 *
2435 * @returns true / false
2436 * @param psz The string in question.
2437 */
2438RTDECL(bool) RTStrIsUpperCased(const char *psz);
2439
2440/**
2441 * Checks if the string is lower cased (no upper case chars in it).
2442 *
2443 * @returns true / false
2444 * @param psz The string in question.
2445 */
2446RTDECL(bool) RTStrIsLowerCased(const char *psz);
2447
2448/**
2449 * Find the length of a zero-terminated byte string, given
2450 * a max string length.
2451 *
2452 * See also RTStrNLenEx.
2453 *
2454 * @returns The string length or cbMax. The returned length does not include
2455 * the zero terminator if it was found.
2456 *
2457 * @param pszString The string.
2458 * @param cchMax The max string length.
2459 */
2460RTDECL(size_t) RTStrNLen(const char *pszString, size_t cchMax);
2461
2462/**
2463 * Find the length of a zero-terminated byte string, given
2464 * a max string length.
2465 *
2466 * See also RTStrNLen.
2467 *
2468 * @returns IPRT status code.
2469 * @retval VINF_SUCCESS if the string has a length less than cchMax.
2470 * @retval VERR_BUFFER_OVERFLOW if the end of the string wasn't found
2471 * before cchMax was reached.
2472 *
2473 * @param pszString The string.
2474 * @param cchMax The max string length.
2475 * @param pcch Where to store the string length excluding the
2476 * terminator. This is set to cchMax if the terminator
2477 * isn't found.
2478 */
2479RTDECL(int) RTStrNLenEx(const char *pszString, size_t cchMax, size_t *pcch);
2480
2481RT_C_DECLS_END
2482
2483/** The maximum size argument of a memchr call. */
2484#define RTSTR_MEMCHR_MAX ((~(size_t)0 >> 1) - 15)
2485
2486/**
2487 * Find the zero terminator in a string with a limited length.
2488 *
2489 * @returns Pointer to the zero terminator.
2490 * @returns NULL if the zero terminator was not found.
2491 *
2492 * @param pszString The string.
2493 * @param cchMax The max string length. RTSTR_MAX is fine.
2494 */
2495#if defined(__cplusplus) && !defined(DOXYGEN_RUNNING)
2496DECLINLINE(char const *) RTStrEnd(char const *pszString, size_t cchMax)
2497{
2498 /* Avoid potential issues with memchr seen in glibc.
2499 * See sysdeps/x86_64/memchr.S in glibc versions older than 2.11 */
2500 while (cchMax > RTSTR_MEMCHR_MAX)
2501 {
2502 char const *pszRet = (char const *)memchr(pszString, '\0', RTSTR_MEMCHR_MAX);
2503 if (RT_LIKELY(pszRet))
2504 return pszRet;
2505 pszString += RTSTR_MEMCHR_MAX;
2506 cchMax -= RTSTR_MEMCHR_MAX;
2507 }
2508 return (char const *)memchr(pszString, '\0', cchMax);
2509}
2510
2511DECLINLINE(char *) RTStrEnd(char *pszString, size_t cchMax)
2512#else
2513DECLINLINE(char *) RTStrEnd(const char *pszString, size_t cchMax)
2514#endif
2515{
2516 /* Avoid potential issues with memchr seen in glibc.
2517 * See sysdeps/x86_64/memchr.S in glibc versions older than 2.11 */
2518 while (cchMax > RTSTR_MEMCHR_MAX)
2519 {
2520 char *pszRet = (char *)memchr(pszString, '\0', RTSTR_MEMCHR_MAX);
2521 if (RT_LIKELY(pszRet))
2522 return pszRet;
2523 pszString += RTSTR_MEMCHR_MAX;
2524 cchMax -= RTSTR_MEMCHR_MAX;
2525 }
2526 return (char *)memchr(pszString, '\0', cchMax);
2527}
2528
2529RT_C_DECLS_BEGIN
2530
2531/**
2532 * Finds the offset at which a simple character first occurs in a string.
2533 *
2534 * @returns The offset of the first occurence or the terminator offset.
2535 * @param pszHaystack The string to search.
2536 * @param chNeedle The character to search for.
2537 */
2538DECLINLINE(size_t) RTStrOffCharOrTerm(const char *pszHaystack, char chNeedle)
2539{
2540 const char *psz = pszHaystack;
2541 char ch;
2542 while ( (ch = *psz) != chNeedle
2543 && ch != '\0')
2544 psz++;
2545 return psz - pszHaystack;
2546}
2547
2548
2549/**
2550 * Matches a simple string pattern.
2551 *
2552 * @returns true if the string matches the pattern, otherwise false.
2553 *
2554 * @param pszPattern The pattern. Special chars are '*' and '?', where the
2555 * asterisk matches zero or more characters and question
2556 * mark matches exactly one character.
2557 * @param pszString The string to match against the pattern.
2558 */
2559RTDECL(bool) RTStrSimplePatternMatch(const char *pszPattern, const char *pszString);
2560
2561/**
2562 * Matches a simple string pattern, neither which needs to be zero terminated.
2563 *
2564 * This is identical to RTStrSimplePatternMatch except that you can optionally
2565 * specify the length of both the pattern and the string. The function will
2566 * stop when it hits a string terminator or either of the lengths.
2567 *
2568 * @returns true if the string matches the pattern, otherwise false.
2569 *
2570 * @param pszPattern The pattern. Special chars are '*' and '?', where the
2571 * asterisk matches zero or more characters and question
2572 * mark matches exactly one character.
2573 * @param cchPattern The pattern length. Pass RTSTR_MAX if you don't know the
2574 * length and wish to stop at the string terminator.
2575 * @param pszString The string to match against the pattern.
2576 * @param cchString The string length. Pass RTSTR_MAX if you don't know the
2577 * length and wish to match up to the string terminator.
2578 */
2579RTDECL(bool) RTStrSimplePatternNMatch(const char *pszPattern, size_t cchPattern,
2580 const char *pszString, size_t cchString);
2581
2582/**
2583 * Matches multiple patterns against a string.
2584 *
2585 * The patterns are separated by the pipe character (|).
2586 *
2587 * @returns true if the string matches the pattern, otherwise false.
2588 *
2589 * @param pszPatterns The patterns.
2590 * @param cchPatterns The lengths of the patterns to use. Pass RTSTR_MAX to
2591 * stop at the terminator.
2592 * @param pszString The string to match against the pattern.
2593 * @param cchString The string length. Pass RTSTR_MAX stop stop at the
2594 * terminator.
2595 * @param poffPattern Offset into the patterns string of the patttern that
2596 * matched. If no match, this will be set to RTSTR_MAX.
2597 * This is optional, NULL is fine.
2598 */
2599RTDECL(bool) RTStrSimplePatternMultiMatch(const char *pszPatterns, size_t cchPatterns,
2600 const char *pszString, size_t cchString,
2601 size_t *poffPattern);
2602
2603/**
2604 * Compares two version strings RTStrICmp fashion.
2605 *
2606 * The version string is split up into sections at punctuation, spaces,
2607 * underscores, dashes and plus signs. The sections are then split up into
2608 * numeric and string sub-sections. Finally, the sub-sections are compared
2609 * in a numeric or case insesntivie fashion depending on what they are.
2610 *
2611 * The following strings are considered to be equal: "1.0.0", "1.00.0", "1.0",
2612 * "1". These aren't: "1.0.0r993", "1.0", "1.0r993", "1.0_Beta3", "1.1"
2613 *
2614 * @returns < 0 if the first string less than the second string.
2615 * @returns 0 if the first string identical to the second string.
2616 * @returns > 0 if the first string greater than the second string.
2617 *
2618 * @param pszVer1 First version string to compare.
2619 * @param pszVer2 Second version string to compare first version with.
2620 */
2621RTDECL(int) RTStrVersionCompare(const char *pszVer1, const char *pszVer2);
2622
2623
2624/** @defgroup rt_str_conv String To/From Number Conversions
2625 * @{ */
2626
2627/**
2628 * Converts a string representation of a number to a 64-bit unsigned number.
2629 *
2630 * @returns iprt status code.
2631 * Warnings are used to indicate conversion problems.
2632 * @retval VWRN_NUMBER_TOO_BIG
2633 * @retval VWRN_NEGATIVE_UNSIGNED
2634 * @retval VWRN_TRAILING_CHARS
2635 * @retval VWRN_TRAILING_SPACES
2636 * @retval VINF_SUCCESS
2637 * @retval VERR_NO_DIGITS
2638 *
2639 * @param pszValue Pointer to the string value.
2640 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2641 * @param uBase The base of the representation used.
2642 * If 0 the function will look for known prefixes before defaulting to 10.
2643 * @param pu64 Where to store the converted number. (optional)
2644 */
2645RTDECL(int) RTStrToUInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint64_t *pu64);
2646
2647/**
2648 * Converts a string representation of a number to a 64-bit unsigned number,
2649 * making sure the full string is converted.
2650 *
2651 * @returns iprt status code.
2652 * Warnings are used to indicate conversion problems.
2653 * @retval VWRN_NUMBER_TOO_BIG
2654 * @retval VWRN_NEGATIVE_UNSIGNED
2655 * @retval VINF_SUCCESS
2656 * @retval VERR_NO_DIGITS
2657 * @retval VERR_TRAILING_SPACES
2658 * @retval VERR_TRAILING_CHARS
2659 *
2660 * @param pszValue Pointer to the string value.
2661 * @param uBase The base of the representation used.
2662 * If 0 the function will look for known prefixes before defaulting to 10.
2663 * @param pu64 Where to store the converted number. (optional)
2664 */
2665RTDECL(int) RTStrToUInt64Full(const char *pszValue, unsigned uBase, uint64_t *pu64);
2666
2667/**
2668 * Converts a string representation of a number to a 64-bit unsigned number.
2669 * The base is guessed.
2670 *
2671 * @returns 64-bit unsigned number on success.
2672 * @returns 0 on failure.
2673 * @param pszValue Pointer to the string value.
2674 */
2675RTDECL(uint64_t) RTStrToUInt64(const char *pszValue);
2676
2677/**
2678 * Converts a string representation of a number to a 32-bit unsigned number.
2679 *
2680 * @returns iprt status code.
2681 * Warnings are used to indicate conversion problems.
2682 * @retval VWRN_NUMBER_TOO_BIG
2683 * @retval VWRN_NEGATIVE_UNSIGNED
2684 * @retval VWRN_TRAILING_CHARS
2685 * @retval VWRN_TRAILING_SPACES
2686 * @retval VINF_SUCCESS
2687 * @retval VERR_NO_DIGITS
2688 *
2689 * @param pszValue Pointer to the string value.
2690 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2691 * @param uBase The base of the representation used.
2692 * If 0 the function will look for known prefixes before defaulting to 10.
2693 * @param pu32 Where to store the converted number. (optional)
2694 */
2695RTDECL(int) RTStrToUInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint32_t *pu32);
2696
2697/**
2698 * Converts a string representation of a number to a 32-bit unsigned number,
2699 * making sure the full string is converted.
2700 *
2701 * @returns iprt status code.
2702 * Warnings are used to indicate conversion problems.
2703 * @retval VWRN_NUMBER_TOO_BIG
2704 * @retval VWRN_NEGATIVE_UNSIGNED
2705 * @retval VINF_SUCCESS
2706 * @retval VERR_NO_DIGITS
2707 * @retval VERR_TRAILING_SPACES
2708 * @retval VERR_TRAILING_CHARS
2709 *
2710 * @param pszValue Pointer to the string value.
2711 * @param uBase The base of the representation used.
2712 * If 0 the function will look for known prefixes before defaulting to 10.
2713 * @param pu32 Where to store the converted number. (optional)
2714 */
2715RTDECL(int) RTStrToUInt32Full(const char *pszValue, unsigned uBase, uint32_t *pu32);
2716
2717/**
2718 * Converts a string representation of a number to a 64-bit unsigned number.
2719 * The base is guessed.
2720 *
2721 * @returns 32-bit unsigned number on success.
2722 * @returns 0 on failure.
2723 * @param pszValue Pointer to the string value.
2724 */
2725RTDECL(uint32_t) RTStrToUInt32(const char *pszValue);
2726
2727/**
2728 * Converts a string representation of a number to a 16-bit unsigned number.
2729 *
2730 * @returns iprt status code.
2731 * Warnings are used to indicate conversion problems.
2732 * @retval VWRN_NUMBER_TOO_BIG
2733 * @retval VWRN_NEGATIVE_UNSIGNED
2734 * @retval VWRN_TRAILING_CHARS
2735 * @retval VWRN_TRAILING_SPACES
2736 * @retval VINF_SUCCESS
2737 * @retval VERR_NO_DIGITS
2738 *
2739 * @param pszValue Pointer to the string value.
2740 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2741 * @param uBase The base of the representation used.
2742 * If 0 the function will look for known prefixes before defaulting to 10.
2743 * @param pu16 Where to store the converted number. (optional)
2744 */
2745RTDECL(int) RTStrToUInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint16_t *pu16);
2746
2747/**
2748 * Converts a string representation of a number to a 16-bit unsigned number,
2749 * making sure the full string is converted.
2750 *
2751 * @returns iprt status code.
2752 * Warnings are used to indicate conversion problems.
2753 * @retval VWRN_NUMBER_TOO_BIG
2754 * @retval VWRN_NEGATIVE_UNSIGNED
2755 * @retval VINF_SUCCESS
2756 * @retval VERR_NO_DIGITS
2757 * @retval VERR_TRAILING_SPACES
2758 * @retval VERR_TRAILING_CHARS
2759 *
2760 * @param pszValue Pointer to the string value.
2761 * @param uBase The base of the representation used.
2762 * If 0 the function will look for known prefixes before defaulting to 10.
2763 * @param pu16 Where to store the converted number. (optional)
2764 */
2765RTDECL(int) RTStrToUInt16Full(const char *pszValue, unsigned uBase, uint16_t *pu16);
2766
2767/**
2768 * Converts a string representation of a number to a 16-bit unsigned number.
2769 * The base is guessed.
2770 *
2771 * @returns 16-bit unsigned number on success.
2772 * @returns 0 on failure.
2773 * @param pszValue Pointer to the string value.
2774 */
2775RTDECL(uint16_t) RTStrToUInt16(const char *pszValue);
2776
2777/**
2778 * Converts a string representation of a number to a 8-bit unsigned number.
2779 *
2780 * @returns iprt status code.
2781 * Warnings are used to indicate conversion problems.
2782 * @retval VWRN_NUMBER_TOO_BIG
2783 * @retval VWRN_NEGATIVE_UNSIGNED
2784 * @retval VWRN_TRAILING_CHARS
2785 * @retval VWRN_TRAILING_SPACES
2786 * @retval VINF_SUCCESS
2787 * @retval VERR_NO_DIGITS
2788 *
2789 * @param pszValue Pointer to the string value.
2790 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2791 * @param uBase The base of the representation used.
2792 * If 0 the function will look for known prefixes before defaulting to 10.
2793 * @param pu8 Where to store the converted number. (optional)
2794 */
2795RTDECL(int) RTStrToUInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint8_t *pu8);
2796
2797/**
2798 * Converts a string representation of a number to a 8-bit unsigned number,
2799 * making sure the full string is converted.
2800 *
2801 * @returns iprt status code.
2802 * Warnings are used to indicate conversion problems.
2803 * @retval VWRN_NUMBER_TOO_BIG
2804 * @retval VWRN_NEGATIVE_UNSIGNED
2805 * @retval VINF_SUCCESS
2806 * @retval VERR_NO_DIGITS
2807 * @retval VERR_TRAILING_SPACES
2808 * @retval VERR_TRAILING_CHARS
2809 *
2810 * @param pszValue Pointer to the string value.
2811 * @param uBase The base of the representation used.
2812 * If 0 the function will look for known prefixes before defaulting to 10.
2813 * @param pu8 Where to store the converted number. (optional)
2814 */
2815RTDECL(int) RTStrToUInt8Full(const char *pszValue, unsigned uBase, uint8_t *pu8);
2816
2817/**
2818 * Converts a string representation of a number to a 8-bit unsigned number.
2819 * The base is guessed.
2820 *
2821 * @returns 8-bit unsigned number on success.
2822 * @returns 0 on failure.
2823 * @param pszValue Pointer to the string value.
2824 */
2825RTDECL(uint8_t) RTStrToUInt8(const char *pszValue);
2826
2827/**
2828 * Converts a string representation of a number to a 64-bit signed number.
2829 *
2830 * @returns iprt status code.
2831 * Warnings are used to indicate conversion problems.
2832 * @retval VWRN_NUMBER_TOO_BIG
2833 * @retval VWRN_TRAILING_CHARS
2834 * @retval VWRN_TRAILING_SPACES
2835 * @retval VINF_SUCCESS
2836 * @retval VERR_NO_DIGITS
2837 *
2838 * @param pszValue Pointer to the string value.
2839 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2840 * @param uBase The base of the representation used.
2841 * If 0 the function will look for known prefixes before defaulting to 10.
2842 * @param pi64 Where to store the converted number. (optional)
2843 */
2844RTDECL(int) RTStrToInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, int64_t *pi64);
2845
2846/**
2847 * Converts a string representation of a number to a 64-bit signed number,
2848 * making sure the full string is converted.
2849 *
2850 * @returns iprt status code.
2851 * Warnings are used to indicate conversion problems.
2852 * @retval VWRN_NUMBER_TOO_BIG
2853 * @retval VINF_SUCCESS
2854 * @retval VERR_TRAILING_CHARS
2855 * @retval VERR_TRAILING_SPACES
2856 * @retval VERR_NO_DIGITS
2857 *
2858 * @param pszValue Pointer to the string value.
2859 * @param uBase The base of the representation used.
2860 * If 0 the function will look for known prefixes before defaulting to 10.
2861 * @param pi64 Where to store the converted number. (optional)
2862 */
2863RTDECL(int) RTStrToInt64Full(const char *pszValue, unsigned uBase, int64_t *pi64);
2864
2865/**
2866 * Converts a string representation of a number to a 64-bit signed number.
2867 * The base is guessed.
2868 *
2869 * @returns 64-bit signed number on success.
2870 * @returns 0 on failure.
2871 * @param pszValue Pointer to the string value.
2872 */
2873RTDECL(int64_t) RTStrToInt64(const char *pszValue);
2874
2875/**
2876 * Converts a string representation of a number to a 32-bit signed number.
2877 *
2878 * @returns iprt status code.
2879 * Warnings are used to indicate conversion problems.
2880 * @retval VWRN_NUMBER_TOO_BIG
2881 * @retval VWRN_TRAILING_CHARS
2882 * @retval VWRN_TRAILING_SPACES
2883 * @retval VINF_SUCCESS
2884 * @retval VERR_NO_DIGITS
2885 *
2886 * @param pszValue Pointer to the string value.
2887 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2888 * @param uBase The base of the representation used.
2889 * If 0 the function will look for known prefixes before defaulting to 10.
2890 * @param pi32 Where to store the converted number. (optional)
2891 */
2892RTDECL(int) RTStrToInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, int32_t *pi32);
2893
2894/**
2895 * Converts a string representation of a number to a 32-bit signed number,
2896 * making sure the full string is converted.
2897 *
2898 * @returns iprt status code.
2899 * Warnings are used to indicate conversion problems.
2900 * @retval VWRN_NUMBER_TOO_BIG
2901 * @retval VINF_SUCCESS
2902 * @retval VERR_TRAILING_CHARS
2903 * @retval VERR_TRAILING_SPACES
2904 * @retval VERR_NO_DIGITS
2905 *
2906 * @param pszValue Pointer to the string value.
2907 * @param uBase The base of the representation used.
2908 * If 0 the function will look for known prefixes before defaulting to 10.
2909 * @param pi32 Where to store the converted number. (optional)
2910 */
2911RTDECL(int) RTStrToInt32Full(const char *pszValue, unsigned uBase, int32_t *pi32);
2912
2913/**
2914 * Converts a string representation of a number to a 32-bit signed number.
2915 * The base is guessed.
2916 *
2917 * @returns 32-bit signed number on success.
2918 * @returns 0 on failure.
2919 * @param pszValue Pointer to the string value.
2920 */
2921RTDECL(int32_t) RTStrToInt32(const char *pszValue);
2922
2923/**
2924 * Converts a string representation of a number to a 16-bit signed number.
2925 *
2926 * @returns iprt status code.
2927 * Warnings are used to indicate conversion problems.
2928 * @retval VWRN_NUMBER_TOO_BIG
2929 * @retval VWRN_TRAILING_CHARS
2930 * @retval VWRN_TRAILING_SPACES
2931 * @retval VINF_SUCCESS
2932 * @retval VERR_NO_DIGITS
2933 *
2934 * @param pszValue Pointer to the string value.
2935 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2936 * @param uBase The base of the representation used.
2937 * If 0 the function will look for known prefixes before defaulting to 10.
2938 * @param pi16 Where to store the converted number. (optional)
2939 */
2940RTDECL(int) RTStrToInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, int16_t *pi16);
2941
2942/**
2943 * Converts a string representation of a number to a 16-bit signed number,
2944 * making sure the full string is converted.
2945 *
2946 * @returns iprt status code.
2947 * Warnings are used to indicate conversion problems.
2948 * @retval VWRN_NUMBER_TOO_BIG
2949 * @retval VINF_SUCCESS
2950 * @retval VERR_TRAILING_CHARS
2951 * @retval VERR_TRAILING_SPACES
2952 * @retval VERR_NO_DIGITS
2953 *
2954 * @param pszValue Pointer to the string value.
2955 * @param uBase The base of the representation used.
2956 * If 0 the function will look for known prefixes before defaulting to 10.
2957 * @param pi16 Where to store the converted number. (optional)
2958 */
2959RTDECL(int) RTStrToInt16Full(const char *pszValue, unsigned uBase, int16_t *pi16);
2960
2961/**
2962 * Converts a string representation of a number to a 16-bit signed number.
2963 * The base is guessed.
2964 *
2965 * @returns 16-bit signed number on success.
2966 * @returns 0 on failure.
2967 * @param pszValue Pointer to the string value.
2968 */
2969RTDECL(int16_t) RTStrToInt16(const char *pszValue);
2970
2971/**
2972 * Converts a string representation of a number to a 8-bit signed number.
2973 *
2974 * @returns iprt status code.
2975 * Warnings are used to indicate conversion problems.
2976 * @retval VWRN_NUMBER_TOO_BIG
2977 * @retval VWRN_TRAILING_CHARS
2978 * @retval VWRN_TRAILING_SPACES
2979 * @retval VINF_SUCCESS
2980 * @retval VERR_NO_DIGITS
2981 *
2982 * @param pszValue Pointer to the string value.
2983 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2984 * @param uBase The base of the representation used.
2985 * If 0 the function will look for known prefixes before defaulting to 10.
2986 * @param pi8 Where to store the converted number. (optional)
2987 */
2988RTDECL(int) RTStrToInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, int8_t *pi8);
2989
2990/**
2991 * Converts a string representation of a number to a 8-bit signed number,
2992 * making sure the full string is converted.
2993 *
2994 * @returns iprt status code.
2995 * Warnings are used to indicate conversion problems.
2996 * @retval VWRN_NUMBER_TOO_BIG
2997 * @retval VINF_SUCCESS
2998 * @retval VERR_TRAILING_CHARS
2999 * @retval VERR_TRAILING_SPACES
3000 * @retval VERR_NO_DIGITS
3001 *
3002 * @param pszValue Pointer to the string value.
3003 * @param uBase The base of the representation used.
3004 * If 0 the function will look for known prefixes before defaulting to 10.
3005 * @param pi8 Where to store the converted number. (optional)
3006 */
3007RTDECL(int) RTStrToInt8Full(const char *pszValue, unsigned uBase, int8_t *pi8);
3008
3009/**
3010 * Converts a string representation of a number to a 8-bit signed number.
3011 * The base is guessed.
3012 *
3013 * @returns 8-bit signed number on success.
3014 * @returns 0 on failure.
3015 * @param pszValue Pointer to the string value.
3016 */
3017RTDECL(int8_t) RTStrToInt8(const char *pszValue);
3018
3019/**
3020 * Formats a buffer stream as hex bytes.
3021 *
3022 * The default is no separating spaces or line breaks or anything.
3023 *
3024 * @returns IPRT status code.
3025 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
3026 * @retval VERR_BUFFER_OVERFLOW if the buffer is insufficent to hold the bytes.
3027 *
3028 * @param pszBuf Output string buffer.
3029 * @param cbBuf The size of the output buffer.
3030 * @param pv Pointer to the bytes to stringify.
3031 * @param cb The number of bytes to stringify.
3032 * @param fFlags Combination of RTSTRPRINTHEXBYTES_F_XXX values.
3033 * @sa RTUtf16PrintHexBytes.
3034 */
3035RTDECL(int) RTStrPrintHexBytes(char *pszBuf, size_t cbBuf, void const *pv, size_t cb, uint32_t fFlags);
3036/** @name RTSTRPRINTHEXBYTES_F_XXX - flags for RTStrPrintHexBytes and RTUtf16PritnHexBytes.
3037 * @{ */
3038/** Upper case hex digits, the default is lower case. */
3039#define RTSTRPRINTHEXBYTES_F_UPPER RT_BIT(0)
3040/** Add a space between each group. */
3041#define RTSTRPRINTHEXBYTES_F_SEP_SPACE RT_BIT(1)
3042/** Add a colon between each group. */
3043#define RTSTRPRINTHEXBYTES_F_SEP_COLON RT_BIT(2)
3044/** @} */
3045
3046/**
3047 * Converts a string of hex bytes back into binary data.
3048 *
3049 * @returns IPRT status code.
3050 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
3051 * @retval VERR_BUFFER_OVERFLOW if the string contains too many hex bytes.
3052 * @retval VERR_BUFFER_UNDERFLOW if there aren't enough hex bytes to fill up
3053 * the output buffer.
3054 * @retval VERR_UNEVEN_INPUT if the input contains a half byte.
3055 * @retval VERR_NO_DIGITS
3056 * @retval VWRN_TRAILING_CHARS
3057 * @retval VWRN_TRAILING_SPACES
3058 *
3059 * @param pszHex The string containing the hex bytes.
3060 * @param pv Output buffer.
3061 * @param cb The size of the output buffer.
3062 * @param fFlags Must be zero, reserved for future use.
3063 */
3064RTDECL(int) RTStrConvertHexBytes(char const *pszHex, void *pv, size_t cb, uint32_t fFlags);
3065
3066/** @} */
3067
3068
3069/** @defgroup rt_str_space Unique String Space
3070 * @{
3071 */
3072
3073/** Pointer to a string name space container node core. */
3074typedef struct RTSTRSPACECORE *PRTSTRSPACECORE;
3075/** Pointer to a pointer to a string name space container node core. */
3076typedef PRTSTRSPACECORE *PPRTSTRSPACECORE;
3077
3078/**
3079 * String name space container node core.
3080 */
3081typedef struct RTSTRSPACECORE
3082{
3083 /** Hash key. Don't touch. */
3084 uint32_t Key;
3085 /** Pointer to the left leaf node. Don't touch. */
3086 PRTSTRSPACECORE pLeft;
3087 /** Pointer to the left right node. Don't touch. */
3088 PRTSTRSPACECORE pRight;
3089 /** Pointer to the list of string with the same key. Don't touch. */
3090 PRTSTRSPACECORE pList;
3091 /** Height of this tree: max(heigth(left), heigth(right)) + 1. Don't touch */
3092 unsigned char uchHeight;
3093 /** The string length. Read only! */
3094 size_t cchString;
3095 /** Pointer to the string. Read only! */
3096 const char *pszString;
3097} RTSTRSPACECORE;
3098
3099/** String space. (Initialize with NULL.) */
3100typedef PRTSTRSPACECORE RTSTRSPACE;
3101/** Pointer to a string space. */
3102typedef PPRTSTRSPACECORE PRTSTRSPACE;
3103
3104
3105/**
3106 * Inserts a string into a unique string space.
3107 *
3108 * @returns true on success.
3109 * @returns false if the string collided with an existing string.
3110 * @param pStrSpace The space to insert it into.
3111 * @param pStr The string node.
3112 */
3113RTDECL(bool) RTStrSpaceInsert(PRTSTRSPACE pStrSpace, PRTSTRSPACECORE pStr);
3114
3115/**
3116 * Removes a string from a unique string space.
3117 *
3118 * @returns Pointer to the removed string node.
3119 * @returns NULL if the string was not found in the string space.
3120 * @param pStrSpace The space to remove it from.
3121 * @param pszString The string to remove.
3122 */
3123RTDECL(PRTSTRSPACECORE) RTStrSpaceRemove(PRTSTRSPACE pStrSpace, const char *pszString);
3124
3125/**
3126 * Gets a string from a unique string space.
3127 *
3128 * @returns Pointer to the string node.
3129 * @returns NULL if the string was not found in the string space.
3130 * @param pStrSpace The space to get it from.
3131 * @param pszString The string to get.
3132 */
3133RTDECL(PRTSTRSPACECORE) RTStrSpaceGet(PRTSTRSPACE pStrSpace, const char *pszString);
3134
3135/**
3136 * Gets a string from a unique string space.
3137 *
3138 * @returns Pointer to the string node.
3139 * @returns NULL if the string was not found in the string space.
3140 * @param pStrSpace The space to get it from.
3141 * @param pszString The string to get.
3142 * @param cchMax The max string length to evaluate. Passing
3143 * RTSTR_MAX is ok and makes it behave just like
3144 * RTStrSpaceGet.
3145 */
3146RTDECL(PRTSTRSPACECORE) RTStrSpaceGetN(PRTSTRSPACE pStrSpace, const char *pszString, size_t cchMax);
3147
3148/**
3149 * Callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy().
3150 *
3151 * @returns 0 on continue.
3152 * @returns Non-zero to aborts the operation.
3153 * @param pStr The string node
3154 * @param pvUser The user specified argument.
3155 */
3156typedef DECLCALLBACK(int) FNRTSTRSPACECALLBACK(PRTSTRSPACECORE pStr, void *pvUser);
3157/** Pointer to callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy(). */
3158typedef FNRTSTRSPACECALLBACK *PFNRTSTRSPACECALLBACK;
3159
3160/**
3161 * Destroys the string space.
3162 *
3163 * The caller supplies a callback which will be called for each of the string
3164 * nodes in for freeing their memory and other resources.
3165 *
3166 * @returns 0 or what ever non-zero return value pfnCallback returned
3167 * when aborting the destruction.
3168 * @param pStrSpace The space to destroy.
3169 * @param pfnCallback The callback.
3170 * @param pvUser The user argument.
3171 */
3172RTDECL(int) RTStrSpaceDestroy(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
3173
3174/**
3175 * Enumerates the string space.
3176 * The caller supplies a callback which will be called for each of
3177 * the string nodes.
3178 *
3179 * @returns 0 or what ever non-zero return value pfnCallback returned
3180 * when aborting the destruction.
3181 * @param pStrSpace The space to enumerate.
3182 * @param pfnCallback The callback.
3183 * @param pvUser The user argument.
3184 */
3185RTDECL(int) RTStrSpaceEnumerate(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
3186
3187/** @} */
3188
3189
3190/** @defgroup rt_str_hash Sting hashing
3191 * @{ */
3192
3193/**
3194 * Hashes the given string using algorithm \#1.
3195 *
3196 * @returns String hash.
3197 * @param pszString The string to hash.
3198 */
3199RTDECL(uint32_t) RTStrHash1(const char *pszString);
3200
3201/**
3202 * Hashes the given string using algorithm \#1.
3203 *
3204 * @returns String hash.
3205 * @param pszString The string to hash.
3206 * @param cchString The max length to hash. Hashing will stop if the
3207 * terminator character is encountered first. Passing
3208 * RTSTR_MAX is fine.
3209 */
3210RTDECL(uint32_t) RTStrHash1N(const char *pszString, size_t cchString);
3211
3212/**
3213 * Hashes the given strings as if they were concatenated using algorithm \#1.
3214 *
3215 * @returns String hash.
3216 * @param cPairs The number of string / length pairs in the
3217 * ellipsis.
3218 * @param ... List of string (const char *) and length
3219 * (size_t) pairs. Passing RTSTR_MAX as the size is
3220 * fine.
3221 */
3222RTDECL(uint32_t) RTStrHash1ExN(size_t cPairs, ...);
3223
3224/**
3225 * Hashes the given strings as if they were concatenated using algorithm \#1.
3226 *
3227 * @returns String hash.
3228 * @param cPairs The number of string / length pairs in the @a va.
3229 * @param va List of string (const char *) and length
3230 * (size_t) pairs. Passing RTSTR_MAX as the size is
3231 * fine.
3232 */
3233RTDECL(uint32_t) RTStrHash1ExNV(size_t cPairs, va_list va);
3234
3235/** @} */
3236
3237/** @} */
3238
3239RT_C_DECLS_END
3240
3241#endif
3242
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