VirtualBox

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

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

iprt/string.h: Adding %Rhcb + %Rhci for human readable size formatting (first is using binary prefix, latter is SI units), and adding %Rhub + %RThui for human readable non-size formatting.

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