VirtualBox

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

Last change on this file since 66882 was 66882, checked in by vboxsync, 8 years ago

iprt: Added RTUINT256U and RTUINT512U. Hacked up simple formatting methods for each.

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