VirtualBox

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

最後變更 在這個檔案從80496是 80496,由 vboxsync 提交於 6 年 前

IPRT/string.h: Added added two more human readable size formatting variants for skipping the 'i' in 'KiB' and such. Made the human readable size formatting use the space format flag to specify a space between the value and the unit.

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

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