VirtualBox

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

Last change on this file since 83168 was 83168, checked in by vboxsync, 5 years ago

iprt/string.h: Adding %RTeic and %RTeim format types for handling RTERRINFO more efficiently.

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