VirtualBox

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

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

iprt/string.h: Save 8 bytes in RTSTRSPACECORE on 64-bit systems.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette