VirtualBox

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

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

IPRT/process-creation-posix.cpp: Try to dynamically resolve crypt_r on linux. [build fix] ticketref:18682

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 135.4 KB
Line 
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.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef IPRT_INCLUDED_string_h
27#define IPRT_INCLUDED_string_h
28#ifndef RT_WITHOUT_PRAGMA_ONCE
29# pragma once
30#endif
31
32#include <iprt/cdefs.h>
33#include <iprt/types.h>
34#include <iprt/assert.h>
35#include <iprt/stdarg.h>
36#include <iprt/errcore.h> /* for VINF_SUCCESS */
37#if defined(RT_OS_LINUX) && defined(__KERNEL__)
38 /* no C++ hacks ('new' etc) here anymore! */
39# include <linux/string.h>
40
41#elif defined(IN_XF86_MODULE) && !defined(NO_ANSIC)
42 RT_C_DECLS_BEGIN
43# include "xf86_ansic.h"
44 RT_C_DECLS_END
45
46#elif defined(RT_OS_FREEBSD) && defined(_KERNEL)
47 RT_C_DECLS_BEGIN
48# include <sys/libkern.h>
49 RT_C_DECLS_END
50
51#elif defined(RT_OS_NETBSD) && defined(_KERNEL)
52 RT_C_DECLS_BEGIN
53# include <lib/libkern/libkern.h>
54 RT_C_DECLS_END
55
56#elif defined(RT_OS_SOLARIS) && defined(_KERNEL)
57 /*
58 * Same case as with FreeBSD kernel:
59 * The string.h stuff clashes with sys/system.h
60 * ffs = find first set bit.
61 */
62# define ffs ffs_string_h
63# include <string.h>
64# undef ffs
65# undef strpbrk
66
67#else
68# include <string.h>
69#endif
70
71/*
72 * Supply prototypes for standard string functions provided by
73 * IPRT instead of the operating environment.
74 */
75#if defined(RT_OS_DARWIN) && defined(KERNEL)
76RT_C_DECLS_BEGIN
77void *memchr(const void *pv, int ch, size_t cb);
78char *strpbrk(const char *pszStr, const char *pszChars);
79RT_C_DECLS_END
80#endif
81
82#if defined(RT_OS_FREEBSD) && defined(_KERNEL)
83RT_C_DECLS_BEGIN
84char *strpbrk(const char *pszStr, const char *pszChars);
85RT_C_DECLS_END
86#endif
87
88#if defined(RT_OS_NETBSD) && defined(_KERNEL)
89RT_C_DECLS_BEGIN
90char *strpbrk(const char *pszStr, const char *pszChars);
91RT_C_DECLS_END
92#endif
93
94#if (!defined(RT_OS_LINUX) || !defined(_GNU_SOURCE)) \
95 && (!defined(RT_OS_OS2) || !defined(_GNU_SOURCE)) \
96 && !defined(RT_OS_FREEBSD) \
97 && !defined(RT_OS_NETBSD)
98RT_C_DECLS_BEGIN
99void *memrchr(const void *pv, int ch, size_t cb);
100RT_C_DECLS_END
101#endif
102
103
104/** @def RT_USE_RTC_3629
105 * When defined the UTF-8 range will stop at 0x10ffff. If not defined, the
106 * range stops at 0x7fffffff.
107 * @remarks Must be defined both when building and using the IPRT. */
108#ifdef DOXYGEN_RUNNING
109# define RT_USE_RTC_3629
110#endif
111
112
113/**
114 * Byte zero the specified object.
115 *
116 * This will use sizeof(Obj) to figure the size and will call memset, bzero
117 * or some compiler intrinsic to perform the actual zeroing.
118 *
119 * @param Obj The object to zero. Make sure to dereference pointers.
120 *
121 * @remarks Because the macro may use memset it has been placed in string.h
122 * instead of cdefs.h to avoid build issues because someone forgot
123 * to include this header.
124 *
125 * @ingroup grp_rt_cdefs
126 */
127#define RT_ZERO(Obj) RT_BZERO(&(Obj), sizeof(Obj))
128
129/**
130 * Byte zero the specified memory area.
131 *
132 * This will call memset, bzero or some compiler intrinsic to clear the
133 * specified bytes of memory.
134 *
135 * @param pv Pointer to the memory.
136 * @param cb The number of bytes to clear. Please, don't pass 0.
137 *
138 * @remarks Because the macro may use memset it has been placed in string.h
139 * instead of cdefs.h to avoid build issues because someone forgot
140 * to include this header.
141 *
142 * @ingroup grp_rt_cdefs
143 */
144#define RT_BZERO(pv, cb) do { memset((pv), 0, cb); } while (0)
145
146
147/**
148 * For copying a volatile variable to a non-volatile one.
149 * @param a_Dst The non-volatile destination variable.
150 * @param a_VolatileSrc The volatile source variable / dereferenced pointer.
151 */
152#define RT_COPY_VOLATILE(a_Dst, a_VolatileSrc) \
153 do { \
154 void const volatile *a_pvVolatileSrc_BCopy_Volatile = &(a_VolatileSrc); \
155 AssertCompile(sizeof(a_Dst) == sizeof(a_VolatileSrc)); \
156 memcpy(&(a_Dst), (void const *)a_pvVolatileSrc_BCopy_Volatile, sizeof(a_Dst)); \
157 } while (0)
158
159/**
160 * For copy a number of bytes from a volatile buffer to a non-volatile one.
161 *
162 * @param a_pDst Pointer to the destination buffer.
163 * @param a_pVolatileSrc Pointer to the volatile source buffer.
164 * @param a_cbToCopy Number of bytes to copy.
165 */
166#define RT_BCOPY_VOLATILE(a_pDst, a_pVolatileSrc, a_cbToCopy) \
167 do { \
168 void const volatile *a_pvVolatileSrc_BCopy_Volatile = (a_pVolatileSrc); \
169 memcpy((a_pDst), (void const *)a_pvVolatileSrc_BCopy_Volatile, (a_cbToCopy)); \
170 } while (0)
171
172
173/** @defgroup grp_rt_str RTStr - String Manipulation
174 * Mostly UTF-8 related helpers where the standard string functions won't do.
175 * @ingroup grp_rt
176 * @{
177 */
178
179RT_C_DECLS_BEGIN
180
181
182/**
183 * The maximum string length.
184 */
185#define RTSTR_MAX (~(size_t)0)
186
187
188/** @def RTSTR_TAG
189 * The default allocation tag used by the RTStr allocation APIs.
190 *
191 * When not defined before the inclusion of iprt/string.h, this will default to
192 * the pointer to the current file name. The string API will make of use of
193 * this as pointer to a volatile but read-only string.
194 */
195#if !defined(RTSTR_TAG) || defined(DOXYGEN_RUNNING)
196# define RTSTR_TAG (__FILE__)
197#endif
198
199
200#ifdef IN_RING3
201
202/**
203 * Allocates tmp buffer with default tag, translates pszString from UTF8 to
204 * current codepage.
205 *
206 * @returns iprt status code.
207 * @param ppszString Receives pointer of allocated native CP string.
208 * The returned pointer must be freed using RTStrFree().
209 * @param pszString UTF-8 string to convert.
210 */
211#define RTStrUtf8ToCurrentCP(ppszString, pszString) RTStrUtf8ToCurrentCPTag((ppszString), (pszString), RTSTR_TAG)
212
213/**
214 * Allocates tmp buffer with custom tag, translates pszString from 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 * - \%Rhci - SI variant of \%Rhcb, fraction is rounded.
1480 * - \%Rhub - Human readable number formatting, using
1481 * binary unit prefixes. Takes a 64-bit unsigned
1482 * integer as input. Does one decimal point by
1483 * default, can do 0-3 via precision field. No
1484 * rounding when calculating fraction.
1485 * - \%Rhui - SI variant of \%Rhub, fraction is rounded.
1486 *
1487 * - \%Rrc - Takes an integer iprt status code as argument. Will insert the
1488 * status code define corresponding to the iprt status code.
1489 * - \%Rrs - Takes an integer iprt status code as argument. Will insert the
1490 * short description of the specified status code.
1491 * - \%Rrf - Takes an integer iprt status code as argument. Will insert the
1492 * full description of the specified status code.
1493 * - \%Rra - Takes an integer iprt status code as argument. Will insert the
1494 * status code define + full description.
1495 * - \%Rwc - Takes a long Windows error code as argument. Will insert the status
1496 * code define corresponding to the Windows error code.
1497 * - \%Rwf - Takes a long Windows error code as argument. Will insert the
1498 * full description of the specified status code.
1499 * - \%Rwa - Takes a long Windows error code as argument. Will insert the
1500 * error code define + full description.
1501 *
1502 * - \%Rhrc - Takes a COM/XPCOM status code as argument. Will insert the status
1503 * code define corresponding to the Windows error code.
1504 * - \%Rhrf - Takes a COM/XPCOM status code as argument. Will insert the
1505 * full description of the specified status code.
1506 * - \%Rhra - Takes a COM/XPCOM error code as argument. Will insert the
1507 * error code define + full description.
1508 *
1509 * - \%Rfn - Pretty printing of a function or method. It drops the
1510 * return code and parameter list.
1511 * - \%Rbn - Prints the base name. For dropping the path in
1512 * order to save space when printing a path name.
1513 *
1514 * - \%lRbs - Same as \%ls except inlut is big endian UTF-16.
1515 *
1516 * On other platforms, \%Rw? simply prints the argument in a form of 0xXXXXXXXX.
1517 *
1518 *
1519 * Group 4, structure dumpers:
1520 * - \%RDtimespec - Takes a PCRTTIMESPEC.
1521 *
1522 *
1523 * Group 5, XML / HTML, JSON and URI escapers:
1524 * - \%RMas - Takes a string pointer (const char *) and outputs
1525 * it as an attribute value with the proper escaping.
1526 * This typically ends up in double quotes.
1527 *
1528 * - \%RMes - Takes a string pointer (const char *) and outputs
1529 * it as an element with the necessary escaping.
1530 *
1531 * - \%RMjs - Takes a string pointer (const char *) and outputs
1532 * it in quotes with proper JSON escaping.
1533 *
1534 * - \%RMpa - Takes a string pointer (const char *) and outputs
1535 * it percent-encoded (RFC-3986). All reserved characters
1536 * are encoded.
1537 *
1538 * - \%RMpf - Takes a string pointer (const char *) and outputs
1539 * it percent-encoded (RFC-3986), form style. This
1540 * means '+' is used to escape space (' ') and '%2B'
1541 * is used to escape '+'.
1542 *
1543 * - \%RMpp - Takes a string pointer (const char *) and outputs
1544 * it percent-encoded (RFC-3986), path style. This
1545 * means '/' will not be escaped.
1546 *
1547 * - \%RMpq - Takes a string pointer (const char *) and outputs
1548 * it percent-encoded (RFC-3986), query style. This
1549 * means '+' will not be escaped.
1550 *
1551 *
1552 * Group 6, CPU Architecture Register dumpers:
1553 * - \%RAx86[reg] - Takes a 64-bit register value if the register is
1554 * 64-bit or smaller. Check the code wrt which
1555 * registers are implemented.
1556 *
1557 */
1558
1559#ifndef DECLARED_FNRTSTROUTPUT /* duplicated in iprt/log.h */
1560# define DECLARED_FNRTSTROUTPUT
1561/**
1562 * Output callback.
1563 *
1564 * @returns number of bytes written.
1565 * @param pvArg User argument.
1566 * @param pachChars Pointer to an array of utf-8 characters.
1567 * @param cbChars Number of bytes in the character array pointed to by pachChars.
1568 */
1569typedef DECLCALLBACK(size_t) FNRTSTROUTPUT(void *pvArg, const char *pachChars, size_t cbChars);
1570/** Pointer to callback function. */
1571typedef FNRTSTROUTPUT *PFNRTSTROUTPUT;
1572#endif
1573
1574/** @name Format flag.
1575 * These are used by RTStrFormat extensions and RTStrFormatNumber, mind
1576 * that not all flags makes sense to both of the functions.
1577 * @{ */
1578#define RTSTR_F_CAPITAL 0x0001
1579#define RTSTR_F_LEFT 0x0002
1580#define RTSTR_F_ZEROPAD 0x0004
1581#define RTSTR_F_SPECIAL 0x0008
1582#define RTSTR_F_VALSIGNED 0x0010
1583#define RTSTR_F_PLUS 0x0020
1584#define RTSTR_F_BLANK 0x0040
1585#define RTSTR_F_WIDTH 0x0080
1586#define RTSTR_F_PRECISION 0x0100
1587#define RTSTR_F_THOUSAND_SEP 0x0200
1588#define RTSTR_F_OBFUSCATE_PTR 0x0400
1589
1590#define RTSTR_F_BIT_MASK 0xf800
1591#define RTSTR_F_8BIT 0x0800
1592#define RTSTR_F_16BIT 0x1000
1593#define RTSTR_F_32BIT 0x2000
1594#define RTSTR_F_64BIT 0x4000
1595#define RTSTR_F_128BIT 0x8000
1596/** @} */
1597
1598/** @def RTSTR_GET_BIT_FLAG
1599 * Gets the bit flag for the specified type.
1600 */
1601#define RTSTR_GET_BIT_FLAG(type) \
1602 ( sizeof(type) * 8 == 32 ? RTSTR_F_32BIT \
1603 : sizeof(type) * 8 == 64 ? RTSTR_F_64BIT \
1604 : sizeof(type) * 8 == 16 ? RTSTR_F_16BIT \
1605 : sizeof(type) * 8 == 8 ? RTSTR_F_8BIT \
1606 : sizeof(type) * 8 == 128 ? RTSTR_F_128BIT \
1607 : 0)
1608
1609
1610/**
1611 * Callback to format non-standard format specifiers.
1612 *
1613 * @returns The number of bytes formatted.
1614 * @param pvArg Formatter argument.
1615 * @param pfnOutput Pointer to output function.
1616 * @param pvArgOutput Argument for the output function.
1617 * @param ppszFormat Pointer to the format string pointer. Advance this till the char
1618 * after the format specifier.
1619 * @param pArgs Pointer to the argument list. Use this to fetch the arguments.
1620 * @param cchWidth Format Width. -1 if not specified.
1621 * @param cchPrecision Format Precision. -1 if not specified.
1622 * @param fFlags Flags (RTSTR_NTFS_*).
1623 * @param chArgSize The argument size specifier, 'l' or 'L'.
1624 */
1625typedef DECLCALLBACK(size_t) FNSTRFORMAT(void *pvArg, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
1626 const char **ppszFormat, va_list *pArgs, int cchWidth,
1627 int cchPrecision, unsigned fFlags, char chArgSize);
1628/** Pointer to a FNSTRFORMAT() function. */
1629typedef FNSTRFORMAT *PFNSTRFORMAT;
1630
1631
1632/**
1633 * Partial implementation of a printf like formatter.
1634 * It doesn't do everything correct, and there is no floating point support.
1635 * However, it supports custom formats by the means of a format callback.
1636 *
1637 * @returns number of bytes formatted.
1638 * @param pfnOutput Output worker.
1639 * Called in two ways. Normally with a string and its length.
1640 * For termination, it's called with NULL for string, 0 for length.
1641 * @param pvArgOutput Argument to the output worker.
1642 * @param pfnFormat Custom format worker.
1643 * @param pvArgFormat Argument to the format worker.
1644 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1645 * @param InArgs Argument list.
1646 */
1647RTDECL(size_t) RTStrFormatV(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat,
1648 const char *pszFormat, va_list InArgs) RT_IPRT_FORMAT_ATTR(5, 0);
1649
1650/**
1651 * Partial implementation of a printf like formatter.
1652 *
1653 * It doesn't do everything correct, and there is no floating point support.
1654 * However, it supports custom formats by the means of a format callback.
1655 *
1656 * @returns number of bytes formatted.
1657 * @param pfnOutput Output worker.
1658 * Called in two ways. Normally with a string and its length.
1659 * For termination, it's called with NULL for string, 0 for length.
1660 * @param pvArgOutput Argument to the output worker.
1661 * @param pfnFormat Custom format worker.
1662 * @param pvArgFormat Argument to the format worker.
1663 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1664 * @param ... Argument list.
1665 */
1666RTDECL(size_t) RTStrFormat(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat,
1667 const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(5, 6);
1668
1669/**
1670 * Formats an integer number according to the parameters.
1671 *
1672 * @returns Length of the formatted number.
1673 * @param psz Pointer to output string buffer of sufficient size.
1674 * @param u64Value Value to format.
1675 * @param uiBase Number representation base.
1676 * @param cchWidth Width.
1677 * @param cchPrecision Precision.
1678 * @param fFlags Flags, RTSTR_F_XXX.
1679 */
1680RTDECL(int) RTStrFormatNumber(char *psz, uint64_t u64Value, unsigned int uiBase, signed int cchWidth, signed int cchPrecision,
1681 unsigned int fFlags);
1682
1683/**
1684 * Formats an unsigned 8-bit number.
1685 *
1686 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1687 * @param pszBuf The output buffer.
1688 * @param cbBuf The size of the output buffer.
1689 * @param u8Value The value to format.
1690 * @param uiBase Number representation base.
1691 * @param cchWidth Width.
1692 * @param cchPrecision Precision.
1693 * @param fFlags Flags, RTSTR_F_XXX.
1694 */
1695RTDECL(ssize_t) RTStrFormatU8(char *pszBuf, size_t cbBuf, uint8_t u8Value, unsigned int uiBase,
1696 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1697
1698/**
1699 * Formats an unsigned 16-bit number.
1700 *
1701 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1702 * @param pszBuf The output buffer.
1703 * @param cbBuf The size of the output buffer.
1704 * @param u16Value The value to format.
1705 * @param uiBase Number representation base.
1706 * @param cchWidth Width.
1707 * @param cchPrecision Precision.
1708 * @param fFlags Flags, RTSTR_F_XXX.
1709 */
1710RTDECL(ssize_t) RTStrFormatU16(char *pszBuf, size_t cbBuf, uint16_t u16Value, unsigned int uiBase,
1711 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1712
1713/**
1714 * Formats an unsigned 32-bit number.
1715 *
1716 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1717 * @param pszBuf The output buffer.
1718 * @param cbBuf The size of the output buffer.
1719 * @param u32Value The value to format.
1720 * @param uiBase Number representation base.
1721 * @param cchWidth Width.
1722 * @param cchPrecision Precision.
1723 * @param fFlags Flags, RTSTR_F_XXX.
1724 */
1725RTDECL(ssize_t) RTStrFormatU32(char *pszBuf, size_t cbBuf, uint32_t u32Value, unsigned int uiBase,
1726 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1727
1728/**
1729 * Formats an unsigned 64-bit number.
1730 *
1731 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1732 * @param pszBuf The output buffer.
1733 * @param cbBuf The size of the output buffer.
1734 * @param u64Value The value to format.
1735 * @param uiBase Number representation base.
1736 * @param cchWidth Width.
1737 * @param cchPrecision Precision.
1738 * @param fFlags Flags, RTSTR_F_XXX.
1739 */
1740RTDECL(ssize_t) RTStrFormatU64(char *pszBuf, size_t cbBuf, uint64_t u64Value, unsigned int uiBase,
1741 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1742
1743/**
1744 * Formats an unsigned 128-bit number.
1745 *
1746 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1747 * @param pszBuf The output buffer.
1748 * @param cbBuf The size of the output buffer.
1749 * @param pu128Value The value to format.
1750 * @param uiBase Number representation base.
1751 * @param cchWidth Width.
1752 * @param cchPrecision Precision.
1753 * @param fFlags Flags, RTSTR_F_XXX.
1754 * @remarks The current implementation is limited to base 16 and doesn't do
1755 * width or precision and probably ignores few flags too.
1756 */
1757RTDECL(ssize_t) RTStrFormatU128(char *pszBuf, size_t cbBuf, PCRTUINT128U pu128Value, unsigned int uiBase,
1758 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1759
1760/**
1761 * Formats an unsigned 256-bit number.
1762 *
1763 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1764 * @param pszBuf The output buffer.
1765 * @param cbBuf The size of the output buffer.
1766 * @param pu256Value The value to format.
1767 * @param uiBase Number representation base.
1768 * @param cchWidth Width.
1769 * @param cchPrecision Precision.
1770 * @param fFlags Flags, RTSTR_F_XXX.
1771 * @remarks The current implementation is limited to base 16 and doesn't do
1772 * width or precision and probably ignores few flags too.
1773 */
1774RTDECL(ssize_t) RTStrFormatU256(char *pszBuf, size_t cbBuf, PCRTUINT256U pu256Value, unsigned int uiBase,
1775 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1776
1777/**
1778 * Formats an unsigned 512-bit number.
1779 *
1780 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1781 * @param pszBuf The output buffer.
1782 * @param cbBuf The size of the output buffer.
1783 * @param pu512Value The value to format.
1784 * @param uiBase Number representation base.
1785 * @param cchWidth Width.
1786 * @param cchPrecision Precision.
1787 * @param fFlags Flags, RTSTR_F_XXX.
1788 * @remarks The current implementation is limited to base 16 and doesn't do
1789 * width or precision and probably ignores few flags too.
1790 */
1791RTDECL(ssize_t) RTStrFormatU512(char *pszBuf, size_t cbBuf, PCRTUINT512U pu512Value, unsigned int uiBase,
1792 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1793
1794
1795/**
1796 * Formats an 80-bit extended floating point number.
1797 *
1798 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1799 * @param pszBuf The output buffer.
1800 * @param cbBuf The size of the output buffer.
1801 * @param pr80Value The value to format.
1802 * @param cchWidth Width.
1803 * @param cchPrecision Precision.
1804 * @param fFlags Flags, RTSTR_F_XXX.
1805 */
1806RTDECL(ssize_t) RTStrFormatR80(char *pszBuf, size_t cbBuf, PCRTFLOAT80U pr80Value, signed int cchWidth,
1807 signed int cchPrecision, uint32_t fFlags);
1808
1809/**
1810 * Formats an 80-bit extended floating point number, version 2.
1811 *
1812 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1813 * @param pszBuf The output buffer.
1814 * @param cbBuf The size of the output buffer.
1815 * @param pr80Value The value to format.
1816 * @param cchWidth Width.
1817 * @param cchPrecision Precision.
1818 * @param fFlags Flags, RTSTR_F_XXX.
1819 */
1820RTDECL(ssize_t) RTStrFormatR80u2(char *pszBuf, size_t cbBuf, PCRTFLOAT80U2 pr80Value, signed int cchWidth,
1821 signed int cchPrecision, uint32_t fFlags);
1822
1823
1824
1825/**
1826 * Callback for formatting a type.
1827 *
1828 * This is registered using the RTStrFormatTypeRegister function and will
1829 * be called during string formatting to handle the specified %R[type].
1830 * The argument for this format type is assumed to be a pointer and it's
1831 * passed in the @a pvValue argument.
1832 *
1833 * @returns Length of the formatted output.
1834 * @param pfnOutput Output worker.
1835 * @param pvArgOutput Argument to the output worker.
1836 * @param pszType The type name.
1837 * @param pvValue The argument value.
1838 * @param cchWidth Width.
1839 * @param cchPrecision Precision.
1840 * @param fFlags Flags (NTFS_*).
1841 * @param pvUser The user argument.
1842 */
1843typedef DECLCALLBACK(size_t) FNRTSTRFORMATTYPE(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
1844 const char *pszType, void const *pvValue,
1845 int cchWidth, int cchPrecision, unsigned fFlags,
1846 void *pvUser);
1847/** Pointer to a FNRTSTRFORMATTYPE. */
1848typedef FNRTSTRFORMATTYPE *PFNRTSTRFORMATTYPE;
1849
1850
1851/**
1852 * Register a format handler for a type.
1853 *
1854 * The format handler is used to handle '%R[type]' format types, where the argument
1855 * in the vector is a pointer value (a bit restrictive, but keeps it simple).
1856 *
1857 * The caller must ensure that no other thread will be making use of any of
1858 * the dynamic formatting type facilities simultaneously with this call.
1859 *
1860 * @returns IPRT status code.
1861 * @retval VINF_SUCCESS on success.
1862 * @retval VERR_ALREADY_EXISTS if the type has already been registered.
1863 * @retval VERR_TOO_MANY_OPEN_FILES if all the type slots has been allocated already.
1864 *
1865 * @param pszType The type name.
1866 * @param pfnHandler The handler address. See FNRTSTRFORMATTYPE for details.
1867 * @param pvUser The user argument to pass to the handler. See RTStrFormatTypeSetUser
1868 * for how to update this later.
1869 */
1870RTDECL(int) RTStrFormatTypeRegister(const char *pszType, PFNRTSTRFORMATTYPE pfnHandler, void *pvUser);
1871
1872/**
1873 * Deregisters a format type.
1874 *
1875 * The caller must ensure that no other thread will be making use of any of
1876 * the dynamic formatting type facilities simultaneously with this call.
1877 *
1878 * @returns IPRT status code.
1879 * @retval VINF_SUCCESS on success.
1880 * @retval VERR_FILE_NOT_FOUND if not found.
1881 *
1882 * @param pszType The type to deregister.
1883 */
1884RTDECL(int) RTStrFormatTypeDeregister(const char *pszType);
1885
1886/**
1887 * Sets the user argument for a type.
1888 *
1889 * This can be used if a user argument needs relocating in GC.
1890 *
1891 * @returns IPRT status code.
1892 * @retval VINF_SUCCESS on success.
1893 * @retval VERR_FILE_NOT_FOUND if not found.
1894 *
1895 * @param pszType The type to update.
1896 * @param pvUser The new user argument value.
1897 */
1898RTDECL(int) RTStrFormatTypeSetUser(const char *pszType, void *pvUser);
1899
1900
1901/**
1902 * String printf.
1903 *
1904 * @returns The length of the returned string (in pszBuffer) excluding the
1905 * terminator.
1906 * @param pszBuffer Output buffer.
1907 * @param cchBuffer Size of the output buffer.
1908 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1909 * @param args The format argument.
1910 *
1911 * @deprecated Use RTStrPrintf2V! Problematic return value on overflow.
1912 */
1913RTDECL(size_t) RTStrPrintfV(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(3, 0);
1914
1915/**
1916 * String printf.
1917 *
1918 * @returns The length of the returned string (in pszBuffer) excluding the
1919 * terminator.
1920 * @param pszBuffer Output buffer.
1921 * @param cchBuffer Size of the output buffer.
1922 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1923 * @param ... The format argument.
1924 *
1925 * @deprecated Use RTStrPrintf2! Problematic return value on overflow.
1926 */
1927RTDECL(size_t) RTStrPrintf(char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
1928
1929/**
1930 * String printf with custom formatting.
1931 *
1932 * @returns The length of the returned string (in pszBuffer) excluding the
1933 * terminator.
1934 * @param pfnFormat Pointer to handler function for the custom formats.
1935 * @param pvArg Argument to the pfnFormat function.
1936 * @param pszBuffer Output buffer.
1937 * @param cchBuffer Size of the output buffer.
1938 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1939 * @param args The format argument.
1940 *
1941 * @deprecated Use RTStrPrintf2ExV! Problematic return value on overflow.
1942 */
1943RTDECL(size_t) RTStrPrintfExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer,
1944 const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(5, 0);
1945
1946/**
1947 * String printf with custom formatting.
1948 *
1949 * @returns The length of the returned string (in pszBuffer) excluding the
1950 * terminator.
1951 * @param pfnFormat Pointer to handler function for the custom formats.
1952 * @param pvArg Argument to the pfnFormat function.
1953 * @param pszBuffer Output buffer.
1954 * @param cchBuffer Size of the output buffer.
1955 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1956 * @param ... The format argument.
1957 *
1958 * @deprecated Use RTStrPrintf2Ex! Problematic return value on overflow.
1959 */
1960RTDECL(size_t) RTStrPrintfEx(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer,
1961 const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(5, 6);
1962
1963/**
1964 * String printf, version 2.
1965 *
1966 * @returns On success, positive count of formatted character excluding the
1967 * terminator. On buffer overflow, negative number giving the required
1968 * buffer size (including terminator char).
1969 *
1970 * @param pszBuffer Output buffer.
1971 * @param cbBuffer Size of the output buffer.
1972 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1973 * @param args The format argument.
1974 */
1975RTDECL(ssize_t) RTStrPrintf2V(char *pszBuffer, size_t cbBuffer, const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(3, 0);
1976
1977/**
1978 * String printf, version 2.
1979 *
1980 * @returns On success, positive count of formatted character excluding the
1981 * terminator. On buffer overflow, negative number giving the required
1982 * buffer size (including terminator char).
1983 *
1984 * @param pszBuffer Output buffer.
1985 * @param cbBuffer Size of the output buffer.
1986 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1987 * @param ... The format argument.
1988 */
1989RTDECL(ssize_t) RTStrPrintf2(char *pszBuffer, size_t cbBuffer, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
1990
1991/**
1992 * String printf with custom formatting, version 2.
1993 *
1994 * @returns On success, positive count of formatted character excluding the
1995 * terminator. On buffer overflow, negative number giving the required
1996 * buffer size (including terminator char).
1997 *
1998 * @param pfnFormat Pointer to handler function for the custom formats.
1999 * @param pvArg Argument to the pfnFormat function.
2000 * @param pszBuffer Output buffer.
2001 * @param cbBuffer Size of the output buffer.
2002 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2003 * @param args The format argument.
2004 */
2005RTDECL(ssize_t) RTStrPrintf2ExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cbBuffer,
2006 const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(5, 0);
2007
2008/**
2009 * String printf with custom formatting, version 2.
2010 *
2011 * @returns On success, positive count of formatted character excluding the
2012 * terminator. On buffer overflow, negative number giving the required
2013 * buffer size (including terminator char).
2014 *
2015 * @param pfnFormat Pointer to handler function for the custom formats.
2016 * @param pvArg Argument to the pfnFormat function.
2017 * @param pszBuffer Output buffer.
2018 * @param cbBuffer Size of the output buffer.
2019 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2020 * @param ... The format argument.
2021 */
2022RTDECL(ssize_t) RTStrPrintf2Ex(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cbBuffer,
2023 const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(5, 6);
2024
2025/**
2026 * Allocating string printf (default tag).
2027 *
2028 * @returns The length of the string in the returned *ppszBuffer excluding the
2029 * terminator.
2030 * @returns -1 on failure.
2031 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
2032 * The buffer should be freed using RTStrFree().
2033 * On failure *ppszBuffer will be set to NULL.
2034 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2035 * @param args The format argument.
2036 */
2037#define RTStrAPrintfV(ppszBuffer, pszFormat, args) RTStrAPrintfVTag((ppszBuffer), (pszFormat), (args), RTSTR_TAG)
2038
2039/**
2040 * Allocating string printf (custom tag).
2041 *
2042 * @returns The length of the string in the returned *ppszBuffer excluding the
2043 * terminator.
2044 * @returns -1 on failure.
2045 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
2046 * The buffer should be freed using RTStrFree().
2047 * On failure *ppszBuffer will be set to NULL.
2048 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2049 * @param args The format argument.
2050 * @param pszTag Allocation tag used for statistics and such.
2051 */
2052RTDECL(int) RTStrAPrintfVTag(char **ppszBuffer, const char *pszFormat, va_list args, const char *pszTag) RT_IPRT_FORMAT_ATTR(2, 0);
2053
2054/**
2055 * Allocating string printf.
2056 *
2057 * @returns The length of the string in the returned *ppszBuffer excluding the
2058 * terminator.
2059 * @returns -1 on failure.
2060 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
2061 * The buffer should be freed using RTStrFree().
2062 * On failure *ppszBuffer will be set to NULL.
2063 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2064 * @param ... The format argument.
2065 */
2066DECLINLINE(int) RT_IPRT_FORMAT_ATTR(2, 3) RTStrAPrintf(char **ppszBuffer, const char *pszFormat, ...)
2067{
2068 int cbRet;
2069 va_list va;
2070 va_start(va, pszFormat);
2071 cbRet = RTStrAPrintfVTag(ppszBuffer, pszFormat, va, RTSTR_TAG);
2072 va_end(va);
2073 return cbRet;
2074}
2075
2076/**
2077 * Allocating string printf (custom tag).
2078 *
2079 * @returns The length of the string in the returned *ppszBuffer excluding the
2080 * terminator.
2081 * @returns -1 on failure.
2082 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
2083 * The buffer should be freed using RTStrFree().
2084 * On failure *ppszBuffer will be set to NULL.
2085 * @param pszTag Allocation tag used for statistics and such.
2086 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2087 * @param ... The format argument.
2088 */
2089DECLINLINE(int) RT_IPRT_FORMAT_ATTR(3, 4) RTStrAPrintfTag(char **ppszBuffer, const char *pszTag, const char *pszFormat, ...)
2090{
2091 int cbRet;
2092 va_list va;
2093 va_start(va, pszFormat);
2094 cbRet = RTStrAPrintfVTag(ppszBuffer, pszFormat, va, pszTag);
2095 va_end(va);
2096 return cbRet;
2097}
2098
2099/**
2100 * Allocating string printf, version 2.
2101 *
2102 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
2103 * memory.
2104 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2105 * @param args The format argument.
2106 */
2107#define RTStrAPrintf2V(pszFormat, args) RTStrAPrintf2VTag((pszFormat), (args), RTSTR_TAG)
2108
2109/**
2110 * Allocating string printf, version 2.
2111 *
2112 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
2113 * memory.
2114 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2115 * @param args The format argument.
2116 * @param pszTag Allocation tag used for statistics and such.
2117 */
2118RTDECL(char *) RTStrAPrintf2VTag(const char *pszFormat, va_list args, const char *pszTag) RT_IPRT_FORMAT_ATTR(1, 0);
2119
2120/**
2121 * Allocating string printf, version 2 (default tag).
2122 *
2123 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
2124 * memory.
2125 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2126 * @param ... The format argument.
2127 */
2128DECLINLINE(char *) RT_IPRT_FORMAT_ATTR(1, 2) RTStrAPrintf2(const char *pszFormat, ...)
2129{
2130 char *pszRet;
2131 va_list va;
2132 va_start(va, pszFormat);
2133 pszRet = RTStrAPrintf2VTag(pszFormat, va, RTSTR_TAG);
2134 va_end(va);
2135 return pszRet;
2136}
2137
2138/**
2139 * Allocating string printf, version 2 (custom tag).
2140 *
2141 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
2142 * memory.
2143 * @param pszTag Allocation tag used for statistics and such.
2144 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
2145 * @param ... The format argument.
2146 */
2147DECLINLINE(char *) RT_IPRT_FORMAT_ATTR(2, 3) RTStrAPrintf2Tag(const char *pszTag, const char *pszFormat, ...)
2148{
2149 char *pszRet;
2150 va_list va;
2151 va_start(va, pszFormat);
2152 pszRet = RTStrAPrintf2VTag(pszFormat, va, pszTag);
2153 va_end(va);
2154 return pszRet;
2155}
2156
2157/**
2158 * Strips blankspaces from both ends of the string.
2159 *
2160 * @returns Pointer to first non-blank char in the string.
2161 * @param psz The string to strip.
2162 */
2163RTDECL(char *) RTStrStrip(char *psz);
2164
2165/**
2166 * Strips blankspaces from the start of the string.
2167 *
2168 * @returns Pointer to first non-blank char in the string.
2169 * @param psz The string to strip.
2170 */
2171RTDECL(char *) RTStrStripL(const char *psz);
2172
2173/**
2174 * Strips blankspaces from the end of the string.
2175 *
2176 * @returns psz.
2177 * @param psz The string to strip.
2178 */
2179RTDECL(char *) RTStrStripR(char *psz);
2180
2181/**
2182 * String copy with overflow handling.
2183 *
2184 * @retval VINF_SUCCESS on success.
2185 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2186 * buffer will contain as much of the string as it can hold, fully
2187 * terminated.
2188 *
2189 * @param pszDst The destination buffer.
2190 * @param cbDst The size of the destination buffer (in bytes).
2191 * @param pszSrc The source string. NULL is not OK.
2192 */
2193RTDECL(int) RTStrCopy(char *pszDst, size_t cbDst, const char *pszSrc);
2194
2195/**
2196 * String copy with overflow handling.
2197 *
2198 * @retval VINF_SUCCESS on success.
2199 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2200 * buffer will contain as much of the string as it can hold, fully
2201 * terminated.
2202 *
2203 * @param pszDst The destination buffer.
2204 * @param cbDst The size of the destination buffer (in bytes).
2205 * @param pszSrc The source string. NULL is not OK.
2206 * @param cchSrcMax The maximum number of chars (not code points) to
2207 * copy from the source string, not counting the
2208 * terminator as usual.
2209 */
2210RTDECL(int) RTStrCopyEx(char *pszDst, size_t cbDst, const char *pszSrc, size_t cchSrcMax);
2211
2212/**
2213 * String copy with overflow handling and buffer advancing.
2214 *
2215 * @retval VINF_SUCCESS on success.
2216 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2217 * buffer will contain as much of the string as it can hold, fully
2218 * terminated.
2219 *
2220 * @param ppszDst Pointer to the destination buffer pointer.
2221 * This will be advanced to the end of the copied
2222 * bytes (points at the terminator). This is also
2223 * updated on overflow.
2224 * @param pcbDst Pointer to the destination buffer size
2225 * variable. This will be updated in accord with
2226 * the buffer pointer.
2227 * @param pszSrc The source string. NULL is not OK.
2228 */
2229RTDECL(int) RTStrCopyP(char **ppszDst, size_t *pcbDst, const char *pszSrc);
2230
2231/**
2232 * String copy with overflow handling.
2233 *
2234 * @retval VINF_SUCCESS on success.
2235 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2236 * buffer will contain as much of the string as it can hold, fully
2237 * terminated.
2238 *
2239 * @param ppszDst Pointer to the destination buffer pointer.
2240 * This will be advanced to the end of the copied
2241 * bytes (points at the terminator). This is also
2242 * updated on overflow.
2243 * @param pcbDst Pointer to the destination buffer size
2244 * variable. This will be updated in accord with
2245 * the buffer pointer.
2246 * @param pszSrc The source string. NULL is not OK.
2247 * @param cchSrcMax The maximum number of chars (not code points) to
2248 * copy from the source string, not counting the
2249 * terminator as usual.
2250 */
2251RTDECL(int) RTStrCopyPEx(char **ppszDst, size_t *pcbDst, const char *pszSrc, size_t cchSrcMax);
2252
2253/**
2254 * String concatenation with overflow handling.
2255 *
2256 * @retval VINF_SUCCESS on success.
2257 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2258 * buffer will contain as much of the string as it can hold, fully
2259 * terminated.
2260 *
2261 * @param pszDst The destination buffer.
2262 * @param cbDst The size of the destination buffer (in bytes).
2263 * @param pszSrc The source string. NULL is not OK.
2264 */
2265RTDECL(int) RTStrCat(char *pszDst, size_t cbDst, const char *pszSrc);
2266
2267/**
2268 * String concatenation with overflow handling.
2269 *
2270 * @retval VINF_SUCCESS on success.
2271 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2272 * buffer will contain as much of the string as it can hold, fully
2273 * terminated.
2274 *
2275 * @param pszDst The destination buffer.
2276 * @param cbDst The size of the destination buffer (in bytes).
2277 * @param pszSrc The source string. NULL is not OK.
2278 * @param cchSrcMax The maximum number of chars (not code points) to
2279 * copy from the source string, not counting the
2280 * terminator as usual.
2281 */
2282RTDECL(int) RTStrCatEx(char *pszDst, size_t cbDst, const char *pszSrc, size_t cchSrcMax);
2283
2284/**
2285 * String concatenation with overflow handling.
2286 *
2287 * @retval VINF_SUCCESS on success.
2288 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2289 * buffer will contain as much of the string as it can hold, fully
2290 * terminated.
2291 *
2292 * @param ppszDst Pointer to the destination buffer pointer.
2293 * This will be advanced to the end of the copied
2294 * bytes (points at the terminator). This is also
2295 * updated on overflow.
2296 * @param pcbDst Pointer to the destination buffer size
2297 * variable. This will be updated in accord with
2298 * the buffer pointer.
2299 * @param pszSrc The source string. NULL is not OK.
2300 */
2301RTDECL(int) RTStrCatP(char **ppszDst, size_t *pcbDst, const char *pszSrc);
2302
2303/**
2304 * String concatenation with overflow handling and buffer advancing.
2305 *
2306 * @retval VINF_SUCCESS on success.
2307 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2308 * buffer will contain as much of the string as it can hold, fully
2309 * terminated.
2310 *
2311 * @param ppszDst Pointer to the destination buffer pointer.
2312 * This will be advanced to the end of the copied
2313 * bytes (points at the terminator). This is also
2314 * updated on overflow.
2315 * @param pcbDst Pointer to the destination buffer size
2316 * variable. This will be updated in accord with
2317 * the buffer pointer.
2318 * @param pszSrc The source string. NULL is not OK.
2319 * @param cchSrcMax The maximum number of chars (not code points) to
2320 * copy from the source string, not counting the
2321 * terminator as usual.
2322 */
2323RTDECL(int) RTStrCatPEx(char **ppszDst, size_t *pcbDst, const char *pszSrc, size_t cchSrcMax);
2324
2325/**
2326 * Performs a case sensitive string compare between two UTF-8 strings.
2327 *
2328 * Encoding errors are ignored by the current implementation. So, the only
2329 * difference between this and the CRT strcmp function is the handling of
2330 * NULL arguments.
2331 *
2332 * @returns < 0 if the first string less than the second string.
2333 * @returns 0 if the first string identical to the second string.
2334 * @returns > 0 if the first string greater than the second string.
2335 * @param psz1 First UTF-8 string. Null is allowed.
2336 * @param psz2 Second UTF-8 string. Null is allowed.
2337 */
2338RTDECL(int) RTStrCmp(const char *psz1, const char *psz2);
2339
2340/**
2341 * Performs a case sensitive string compare between two UTF-8 strings, given
2342 * a maximum string length.
2343 *
2344 * Encoding errors are ignored by the current implementation. So, the only
2345 * difference between this and the CRT strncmp function is the handling of
2346 * NULL arguments.
2347 *
2348 * @returns < 0 if the first string less than the second string.
2349 * @returns 0 if the first string identical to the second string.
2350 * @returns > 0 if the first string greater than the second string.
2351 * @param psz1 First UTF-8 string. Null is allowed.
2352 * @param psz2 Second UTF-8 string. Null is allowed.
2353 * @param cchMax The maximum string length
2354 */
2355RTDECL(int) RTStrNCmp(const char *psz1, const char *psz2, size_t cchMax);
2356
2357/**
2358 * Performs a case insensitive string compare between two UTF-8 strings.
2359 *
2360 * This is a simplified compare, as only the simplified lower/upper case folding
2361 * specified by the unicode specs are used. It does not consider character pairs
2362 * as they are used in some languages, just simple upper & lower case compares.
2363 *
2364 * The result is the difference between the mismatching codepoints after they
2365 * both have been lower cased.
2366 *
2367 * If the string encoding is invalid the function will assert (strict builds)
2368 * and use RTStrCmp for the remainder of the string.
2369 *
2370 * @returns < 0 if the first string less than the second string.
2371 * @returns 0 if the first string identical to the second string.
2372 * @returns > 0 if the first string greater than the second string.
2373 * @param psz1 First UTF-8 string. Null is allowed.
2374 * @param psz2 Second UTF-8 string. Null is allowed.
2375 */
2376RTDECL(int) RTStrICmp(const char *psz1, const char *psz2);
2377
2378/**
2379 * Performs a case insensitive string compare between two UTF-8 strings, given a
2380 * maximum string length.
2381 *
2382 * This is a simplified compare, as only the simplified lower/upper case folding
2383 * specified by the unicode specs are used. It does not consider character pairs
2384 * as they are used in some languages, just simple upper & lower case compares.
2385 *
2386 * The result is the difference between the mismatching codepoints after they
2387 * both have been lower cased.
2388 *
2389 * If the string encoding is invalid the function will assert (strict builds)
2390 * and use RTStrNCmp for the remainder of the string.
2391 *
2392 * @returns < 0 if the first string less than the second string.
2393 * @returns 0 if the first string identical to the second string.
2394 * @returns > 0 if the first string greater than the second string.
2395 * @param psz1 First UTF-8 string. Null is allowed.
2396 * @param psz2 Second UTF-8 string. Null is allowed.
2397 * @param cchMax Maximum string length
2398 */
2399RTDECL(int) RTStrNICmp(const char *psz1, const char *psz2, size_t cchMax);
2400
2401/**
2402 * Performs a case insensitive string compare between a UTF-8 string and a 7-bit
2403 * ASCII string.
2404 *
2405 * This is potentially faster than RTStrICmp and drags in less dependencies. It
2406 * is really handy for hardcoded inputs.
2407 *
2408 * If the string encoding is invalid the function will assert (strict builds)
2409 * and use RTStrCmp for the remainder of the string.
2410 *
2411 * @returns < 0 if the first string less than the second string.
2412 * @returns 0 if the first string identical to the second string.
2413 * @returns > 0 if the first string greater than the second string.
2414 * @param psz1 First UTF-8 string. Null is allowed.
2415 * @param psz2 Second string, 7-bit ASCII. Null is allowed.
2416 * @sa RTStrICmp, RTUtf16ICmpAscii
2417 */
2418RTDECL(int) RTStrICmpAscii(const char *psz1, const char *psz2);
2419
2420/**
2421 * Performs a case insensitive string compare between a UTF-8 string and a 7-bit
2422 * ASCII string, given a maximum string length.
2423 *
2424 * This is potentially faster than RTStrNICmp and drags in less dependencies.
2425 * It is really handy for hardcoded inputs.
2426 *
2427 * If the string encoding is invalid the function will assert (strict builds)
2428 * and use RTStrNCmp for the remainder of the string.
2429 *
2430 * @returns < 0 if the first string less than the second string.
2431 * @returns 0 if the first string identical to the second string.
2432 * @returns > 0 if the first string greater than the second string.
2433 * @param psz1 First UTF-8 string. Null is allowed.
2434 * @param psz2 Second string, 7-bit ASCII. Null is allowed.
2435 * @param cchMax Maximum string length
2436 * @sa RTStrNICmp, RTUtf16NICmpAscii
2437 */
2438RTDECL(int) RTStrNICmpAscii(const char *psz1, const char *psz2, size_t cchMax);
2439
2440/**
2441 * Checks whether @a pszString starts with @a pszStart.
2442 *
2443 * @returns true / false.
2444 * @param pszString The string to check.
2445 * @param pszStart The start string to check for.
2446 */
2447RTDECL(int) RTStrStartsWith(const char *pszString, const char *pszStart);
2448
2449/**
2450 * Checks whether @a pszString starts with @a pszStart, case insensitive.
2451 *
2452 * @returns true / false.
2453 * @param pszString The string to check.
2454 * @param pszStart The start string to check for.
2455 */
2456RTDECL(int) RTStrIStartsWith(const char *pszString, const char *pszStart);
2457
2458/**
2459 * Locates a case sensitive substring.
2460 *
2461 * If any of the two strings are NULL, then NULL is returned. If the needle is
2462 * an empty string, then the haystack is returned (i.e. matches anything).
2463 *
2464 * @returns Pointer to the first occurrence of the substring if found, NULL if
2465 * not.
2466 *
2467 * @param pszHaystack The string to search.
2468 * @param pszNeedle The substring to search for.
2469 *
2470 * @remarks The difference between this and strstr is the handling of NULL
2471 * pointers.
2472 */
2473RTDECL(char *) RTStrStr(const char *pszHaystack, const char *pszNeedle);
2474
2475/**
2476 * Locates a case insensitive substring.
2477 *
2478 * If any of the two strings are NULL, then NULL is returned. If the needle is
2479 * an empty string, then the haystack is returned (i.e. matches anything).
2480 *
2481 * @returns Pointer to the first occurrence of the substring if found, NULL if
2482 * not.
2483 *
2484 * @param pszHaystack The string to search.
2485 * @param pszNeedle The substring to search for.
2486 *
2487 */
2488RTDECL(char *) RTStrIStr(const char *pszHaystack, const char *pszNeedle);
2489
2490/**
2491 * Converts the string to lower case.
2492 *
2493 * @returns Pointer to the converted string.
2494 * @param psz The string to convert.
2495 */
2496RTDECL(char *) RTStrToLower(char *psz);
2497
2498/**
2499 * Converts the string to upper case.
2500 *
2501 * @returns Pointer to the converted string.
2502 * @param psz The string to convert.
2503 */
2504RTDECL(char *) RTStrToUpper(char *psz);
2505
2506/**
2507 * Checks if the string is case foldable, i.e. whether it would change if
2508 * subject to RTStrToLower or RTStrToUpper.
2509 *
2510 * @returns true / false
2511 * @param psz The string in question.
2512 */
2513RTDECL(bool) RTStrIsCaseFoldable(const char *psz);
2514
2515/**
2516 * Checks if the string is upper cased (no lower case chars in it).
2517 *
2518 * @returns true / false
2519 * @param psz The string in question.
2520 */
2521RTDECL(bool) RTStrIsUpperCased(const char *psz);
2522
2523/**
2524 * Checks if the string is lower cased (no upper case chars in it).
2525 *
2526 * @returns true / false
2527 * @param psz The string in question.
2528 */
2529RTDECL(bool) RTStrIsLowerCased(const char *psz);
2530
2531/**
2532 * Find the length of a zero-terminated byte string, given
2533 * a max string length.
2534 *
2535 * See also RTStrNLenEx.
2536 *
2537 * @returns The string length or cbMax. The returned length does not include
2538 * the zero terminator if it was found.
2539 *
2540 * @param pszString The string.
2541 * @param cchMax The max string length.
2542 */
2543RTDECL(size_t) RTStrNLen(const char *pszString, size_t cchMax);
2544
2545/**
2546 * Find the length of a zero-terminated byte string, given
2547 * a max string length.
2548 *
2549 * See also RTStrNLen.
2550 *
2551 * @returns IPRT status code.
2552 * @retval VINF_SUCCESS if the string has a length less than cchMax.
2553 * @retval VERR_BUFFER_OVERFLOW if the end of the string wasn't found
2554 * before cchMax was reached.
2555 *
2556 * @param pszString The string.
2557 * @param cchMax The max string length.
2558 * @param pcch Where to store the string length excluding the
2559 * terminator. This is set to cchMax if the terminator
2560 * isn't found.
2561 */
2562RTDECL(int) RTStrNLenEx(const char *pszString, size_t cchMax, size_t *pcch);
2563
2564RT_C_DECLS_END
2565
2566/** The maximum size argument of a memchr call. */
2567#define RTSTR_MEMCHR_MAX ((~(size_t)0 >> 1) - 15)
2568
2569/**
2570 * Find the zero terminator in a string with a limited length.
2571 *
2572 * @returns Pointer to the zero terminator.
2573 * @returns NULL if the zero terminator was not found.
2574 *
2575 * @param pszString The string.
2576 * @param cchMax The max string length. RTSTR_MAX is fine.
2577 */
2578#if defined(__cplusplus) && !defined(DOXYGEN_RUNNING)
2579DECLINLINE(char const *) RTStrEnd(char const *pszString, size_t cchMax)
2580{
2581 /* Avoid potential issues with memchr seen in glibc.
2582 * See sysdeps/x86_64/memchr.S in glibc versions older than 2.11 */
2583 while (cchMax > RTSTR_MEMCHR_MAX)
2584 {
2585 char const *pszRet = (char const *)memchr(pszString, '\0', RTSTR_MEMCHR_MAX);
2586 if (RT_LIKELY(pszRet))
2587 return pszRet;
2588 pszString += RTSTR_MEMCHR_MAX;
2589 cchMax -= RTSTR_MEMCHR_MAX;
2590 }
2591 return (char const *)memchr(pszString, '\0', cchMax);
2592}
2593
2594DECLINLINE(char *) RTStrEnd(char *pszString, size_t cchMax)
2595#else
2596DECLINLINE(char *) RTStrEnd(const char *pszString, size_t cchMax)
2597#endif
2598{
2599 /* Avoid potential issues with memchr seen in glibc.
2600 * See sysdeps/x86_64/memchr.S in glibc versions older than 2.11 */
2601 while (cchMax > RTSTR_MEMCHR_MAX)
2602 {
2603 char *pszRet = (char *)memchr(pszString, '\0', RTSTR_MEMCHR_MAX);
2604 if (RT_LIKELY(pszRet))
2605 return pszRet;
2606 pszString += RTSTR_MEMCHR_MAX;
2607 cchMax -= RTSTR_MEMCHR_MAX;
2608 }
2609 return (char *)memchr(pszString, '\0', cchMax);
2610}
2611
2612RT_C_DECLS_BEGIN
2613
2614/**
2615 * Finds the offset at which a simple character first occurs in a string.
2616 *
2617 * @returns The offset of the first occurence or the terminator offset.
2618 * @param pszHaystack The string to search.
2619 * @param chNeedle The character to search for.
2620 */
2621DECLINLINE(size_t) RTStrOffCharOrTerm(const char *pszHaystack, char chNeedle)
2622{
2623 const char *psz = pszHaystack;
2624 char ch;
2625 while ( (ch = *psz) != chNeedle
2626 && ch != '\0')
2627 psz++;
2628 return psz - pszHaystack;
2629}
2630
2631
2632/**
2633 * Matches a simple string pattern.
2634 *
2635 * @returns true if the string matches the pattern, otherwise false.
2636 *
2637 * @param pszPattern The pattern. Special chars are '*' and '?', where the
2638 * asterisk matches zero or more characters and question
2639 * mark matches exactly one character.
2640 * @param pszString The string to match against the pattern.
2641 */
2642RTDECL(bool) RTStrSimplePatternMatch(const char *pszPattern, const char *pszString);
2643
2644/**
2645 * Matches a simple string pattern, neither which needs to be zero terminated.
2646 *
2647 * This is identical to RTStrSimplePatternMatch except that you can optionally
2648 * specify the length of both the pattern and the string. The function will
2649 * stop when it hits a string terminator or either of the lengths.
2650 *
2651 * @returns true if the string matches the pattern, otherwise false.
2652 *
2653 * @param pszPattern The pattern. Special chars are '*' and '?', where the
2654 * asterisk matches zero or more characters and question
2655 * mark matches exactly one character.
2656 * @param cchPattern The pattern length. Pass RTSTR_MAX if you don't know the
2657 * length and wish to stop at the string terminator.
2658 * @param pszString The string to match against the pattern.
2659 * @param cchString The string length. Pass RTSTR_MAX if you don't know the
2660 * length and wish to match up to the string terminator.
2661 */
2662RTDECL(bool) RTStrSimplePatternNMatch(const char *pszPattern, size_t cchPattern,
2663 const char *pszString, size_t cchString);
2664
2665/**
2666 * Matches multiple patterns against a string.
2667 *
2668 * The patterns are separated by the pipe character (|).
2669 *
2670 * @returns true if the string matches the pattern, otherwise false.
2671 *
2672 * @param pszPatterns The patterns.
2673 * @param cchPatterns The lengths of the patterns to use. Pass RTSTR_MAX to
2674 * stop at the terminator.
2675 * @param pszString The string to match against the pattern.
2676 * @param cchString The string length. Pass RTSTR_MAX stop stop at the
2677 * terminator.
2678 * @param poffPattern Offset into the patterns string of the patttern that
2679 * matched. If no match, this will be set to RTSTR_MAX.
2680 * This is optional, NULL is fine.
2681 */
2682RTDECL(bool) RTStrSimplePatternMultiMatch(const char *pszPatterns, size_t cchPatterns,
2683 const char *pszString, size_t cchString,
2684 size_t *poffPattern);
2685
2686/**
2687 * Compares two version strings RTStrICmp fashion.
2688 *
2689 * The version string is split up into sections at punctuation, spaces,
2690 * underscores, dashes and plus signs. The sections are then split up into
2691 * numeric and string sub-sections. Finally, the sub-sections are compared
2692 * in a numeric or case insesntivie fashion depending on what they are.
2693 *
2694 * The following strings are considered to be equal: "1.0.0", "1.00.0", "1.0",
2695 * "1". These aren't: "1.0.0r993", "1.0", "1.0r993", "1.0_Beta3", "1.1"
2696 *
2697 * @returns < 0 if the first string less than the second string.
2698 * @returns 0 if the first string identical to the second string.
2699 * @returns > 0 if the first string greater than the second string.
2700 *
2701 * @param pszVer1 First version string to compare.
2702 * @param pszVer2 Second version string to compare first version with.
2703 */
2704RTDECL(int) RTStrVersionCompare(const char *pszVer1, const char *pszVer2);
2705
2706
2707/** @defgroup rt_str_conv String To/From Number Conversions
2708 * @{ */
2709
2710/**
2711 * Converts a string representation of a number to a 64-bit unsigned number.
2712 *
2713 * @returns iprt status code.
2714 * Warnings are used to indicate conversion problems.
2715 * @retval VWRN_NUMBER_TOO_BIG
2716 * @retval VWRN_NEGATIVE_UNSIGNED
2717 * @retval VWRN_TRAILING_CHARS
2718 * @retval VWRN_TRAILING_SPACES
2719 * @retval VINF_SUCCESS
2720 * @retval VERR_NO_DIGITS
2721 *
2722 * @param pszValue Pointer to the string value.
2723 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2724 * @param uBase The base of the representation used.
2725 * If 0 the function will look for known prefixes before defaulting to 10.
2726 * @param pu64 Where to store the converted number. (optional)
2727 */
2728RTDECL(int) RTStrToUInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint64_t *pu64);
2729
2730/**
2731 * Converts a string representation of a number to a 64-bit unsigned number,
2732 * making sure the full string is converted.
2733 *
2734 * @returns iprt status code.
2735 * Warnings are used to indicate conversion problems.
2736 * @retval VWRN_NUMBER_TOO_BIG
2737 * @retval VWRN_NEGATIVE_UNSIGNED
2738 * @retval VINF_SUCCESS
2739 * @retval VERR_NO_DIGITS
2740 * @retval VERR_TRAILING_SPACES
2741 * @retval VERR_TRAILING_CHARS
2742 *
2743 * @param pszValue Pointer to the string value.
2744 * @param uBase The base of the representation used.
2745 * If 0 the function will look for known prefixes before defaulting to 10.
2746 * @param pu64 Where to store the converted number. (optional)
2747 */
2748RTDECL(int) RTStrToUInt64Full(const char *pszValue, unsigned uBase, uint64_t *pu64);
2749
2750/**
2751 * Converts a string representation of a number to a 64-bit unsigned number.
2752 * The base is guessed.
2753 *
2754 * @returns 64-bit unsigned number on success.
2755 * @returns 0 on failure.
2756 * @param pszValue Pointer to the string value.
2757 */
2758RTDECL(uint64_t) RTStrToUInt64(const char *pszValue);
2759
2760/**
2761 * Converts a string representation of a number to a 32-bit unsigned number.
2762 *
2763 * @returns iprt status code.
2764 * Warnings are used to indicate conversion problems.
2765 * @retval VWRN_NUMBER_TOO_BIG
2766 * @retval VWRN_NEGATIVE_UNSIGNED
2767 * @retval VWRN_TRAILING_CHARS
2768 * @retval VWRN_TRAILING_SPACES
2769 * @retval VINF_SUCCESS
2770 * @retval VERR_NO_DIGITS
2771 *
2772 * @param pszValue Pointer to the string value.
2773 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2774 * @param uBase The base of the representation used.
2775 * If 0 the function will look for known prefixes before defaulting to 10.
2776 * @param pu32 Where to store the converted number. (optional)
2777 */
2778RTDECL(int) RTStrToUInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint32_t *pu32);
2779
2780/**
2781 * Converts a string representation of a number to a 32-bit unsigned number,
2782 * making sure the full string is converted.
2783 *
2784 * @returns iprt status code.
2785 * Warnings are used to indicate conversion problems.
2786 * @retval VWRN_NUMBER_TOO_BIG
2787 * @retval VWRN_NEGATIVE_UNSIGNED
2788 * @retval VINF_SUCCESS
2789 * @retval VERR_NO_DIGITS
2790 * @retval VERR_TRAILING_SPACES
2791 * @retval VERR_TRAILING_CHARS
2792 *
2793 * @param pszValue Pointer to the string value.
2794 * @param uBase The base of the representation used.
2795 * If 0 the function will look for known prefixes before defaulting to 10.
2796 * @param pu32 Where to store the converted number. (optional)
2797 */
2798RTDECL(int) RTStrToUInt32Full(const char *pszValue, unsigned uBase, uint32_t *pu32);
2799
2800/**
2801 * Converts a string representation of a number to a 32-bit unsigned number.
2802 * The base is guessed.
2803 *
2804 * @returns 32-bit unsigned number on success.
2805 * @returns 0 on failure.
2806 * @param pszValue Pointer to the string value.
2807 */
2808RTDECL(uint32_t) RTStrToUInt32(const char *pszValue);
2809
2810/**
2811 * Converts a string representation of a number to a 16-bit unsigned number.
2812 *
2813 * @returns iprt status code.
2814 * Warnings are used to indicate conversion problems.
2815 * @retval VWRN_NUMBER_TOO_BIG
2816 * @retval VWRN_NEGATIVE_UNSIGNED
2817 * @retval VWRN_TRAILING_CHARS
2818 * @retval VWRN_TRAILING_SPACES
2819 * @retval VINF_SUCCESS
2820 * @retval VERR_NO_DIGITS
2821 *
2822 * @param pszValue Pointer to the string value.
2823 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2824 * @param uBase The base of the representation used.
2825 * If 0 the function will look for known prefixes before defaulting to 10.
2826 * @param pu16 Where to store the converted number. (optional)
2827 */
2828RTDECL(int) RTStrToUInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint16_t *pu16);
2829
2830/**
2831 * Converts a string representation of a number to a 16-bit unsigned number,
2832 * making sure the full string is converted.
2833 *
2834 * @returns iprt status code.
2835 * Warnings are used to indicate conversion problems.
2836 * @retval VWRN_NUMBER_TOO_BIG
2837 * @retval VWRN_NEGATIVE_UNSIGNED
2838 * @retval VINF_SUCCESS
2839 * @retval VERR_NO_DIGITS
2840 * @retval VERR_TRAILING_SPACES
2841 * @retval VERR_TRAILING_CHARS
2842 *
2843 * @param pszValue Pointer to the string value.
2844 * @param uBase The base of the representation used.
2845 * If 0 the function will look for known prefixes before defaulting to 10.
2846 * @param pu16 Where to store the converted number. (optional)
2847 */
2848RTDECL(int) RTStrToUInt16Full(const char *pszValue, unsigned uBase, uint16_t *pu16);
2849
2850/**
2851 * Converts a string representation of a number to a 16-bit unsigned number.
2852 * The base is guessed.
2853 *
2854 * @returns 16-bit unsigned number on success.
2855 * @returns 0 on failure.
2856 * @param pszValue Pointer to the string value.
2857 */
2858RTDECL(uint16_t) RTStrToUInt16(const char *pszValue);
2859
2860/**
2861 * Converts a string representation of a number to a 8-bit unsigned number.
2862 *
2863 * @returns iprt status code.
2864 * Warnings are used to indicate conversion problems.
2865 * @retval VWRN_NUMBER_TOO_BIG
2866 * @retval VWRN_NEGATIVE_UNSIGNED
2867 * @retval VWRN_TRAILING_CHARS
2868 * @retval VWRN_TRAILING_SPACES
2869 * @retval VINF_SUCCESS
2870 * @retval VERR_NO_DIGITS
2871 *
2872 * @param pszValue Pointer to the string value.
2873 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2874 * @param uBase The base of the representation used.
2875 * If 0 the function will look for known prefixes before defaulting to 10.
2876 * @param pu8 Where to store the converted number. (optional)
2877 */
2878RTDECL(int) RTStrToUInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint8_t *pu8);
2879
2880/**
2881 * Converts a string representation of a number to a 8-bit unsigned number,
2882 * making sure the full string is converted.
2883 *
2884 * @returns iprt status code.
2885 * Warnings are used to indicate conversion problems.
2886 * @retval VWRN_NUMBER_TOO_BIG
2887 * @retval VWRN_NEGATIVE_UNSIGNED
2888 * @retval VINF_SUCCESS
2889 * @retval VERR_NO_DIGITS
2890 * @retval VERR_TRAILING_SPACES
2891 * @retval VERR_TRAILING_CHARS
2892 *
2893 * @param pszValue Pointer to the string value.
2894 * @param uBase The base of the representation used.
2895 * If 0 the function will look for known prefixes before defaulting to 10.
2896 * @param pu8 Where to store the converted number. (optional)
2897 */
2898RTDECL(int) RTStrToUInt8Full(const char *pszValue, unsigned uBase, uint8_t *pu8);
2899
2900/**
2901 * Converts a string representation of a number to a 8-bit unsigned number.
2902 * The base is guessed.
2903 *
2904 * @returns 8-bit unsigned number on success.
2905 * @returns 0 on failure.
2906 * @param pszValue Pointer to the string value.
2907 */
2908RTDECL(uint8_t) RTStrToUInt8(const char *pszValue);
2909
2910/**
2911 * Converts a string representation of a number to a 64-bit signed number.
2912 *
2913 * @returns iprt status code.
2914 * Warnings are used to indicate conversion problems.
2915 * @retval VWRN_NUMBER_TOO_BIG
2916 * @retval VWRN_TRAILING_CHARS
2917 * @retval VWRN_TRAILING_SPACES
2918 * @retval VINF_SUCCESS
2919 * @retval VERR_NO_DIGITS
2920 *
2921 * @param pszValue Pointer to the string value.
2922 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2923 * @param uBase The base of the representation used.
2924 * If 0 the function will look for known prefixes before defaulting to 10.
2925 * @param pi64 Where to store the converted number. (optional)
2926 */
2927RTDECL(int) RTStrToInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, int64_t *pi64);
2928
2929/**
2930 * Converts a string representation of a number to a 64-bit signed number,
2931 * making sure the full string is converted.
2932 *
2933 * @returns iprt status code.
2934 * Warnings are used to indicate conversion problems.
2935 * @retval VWRN_NUMBER_TOO_BIG
2936 * @retval VINF_SUCCESS
2937 * @retval VERR_TRAILING_CHARS
2938 * @retval VERR_TRAILING_SPACES
2939 * @retval VERR_NO_DIGITS
2940 *
2941 * @param pszValue Pointer to the string value.
2942 * @param uBase The base of the representation used.
2943 * If 0 the function will look for known prefixes before defaulting to 10.
2944 * @param pi64 Where to store the converted number. (optional)
2945 */
2946RTDECL(int) RTStrToInt64Full(const char *pszValue, unsigned uBase, int64_t *pi64);
2947
2948/**
2949 * Converts a string representation of a number to a 64-bit signed number.
2950 * The base is guessed.
2951 *
2952 * @returns 64-bit signed number on success.
2953 * @returns 0 on failure.
2954 * @param pszValue Pointer to the string value.
2955 */
2956RTDECL(int64_t) RTStrToInt64(const char *pszValue);
2957
2958/**
2959 * Converts a string representation of a number to a 32-bit signed number.
2960 *
2961 * @returns iprt status code.
2962 * Warnings are used to indicate conversion problems.
2963 * @retval VWRN_NUMBER_TOO_BIG
2964 * @retval VWRN_TRAILING_CHARS
2965 * @retval VWRN_TRAILING_SPACES
2966 * @retval VINF_SUCCESS
2967 * @retval VERR_NO_DIGITS
2968 *
2969 * @param pszValue Pointer to the string value.
2970 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2971 * @param uBase The base of the representation used.
2972 * If 0 the function will look for known prefixes before defaulting to 10.
2973 * @param pi32 Where to store the converted number. (optional)
2974 */
2975RTDECL(int) RTStrToInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, int32_t *pi32);
2976
2977/**
2978 * Converts a string representation of a number to a 32-bit signed number,
2979 * making sure the full string is converted.
2980 *
2981 * @returns iprt status code.
2982 * Warnings are used to indicate conversion problems.
2983 * @retval VWRN_NUMBER_TOO_BIG
2984 * @retval VINF_SUCCESS
2985 * @retval VERR_TRAILING_CHARS
2986 * @retval VERR_TRAILING_SPACES
2987 * @retval VERR_NO_DIGITS
2988 *
2989 * @param pszValue Pointer to the string value.
2990 * @param uBase The base of the representation used.
2991 * If 0 the function will look for known prefixes before defaulting to 10.
2992 * @param pi32 Where to store the converted number. (optional)
2993 */
2994RTDECL(int) RTStrToInt32Full(const char *pszValue, unsigned uBase, int32_t *pi32);
2995
2996/**
2997 * Converts a string representation of a number to a 32-bit signed number.
2998 * The base is guessed.
2999 *
3000 * @returns 32-bit signed number on success.
3001 * @returns 0 on failure.
3002 * @param pszValue Pointer to the string value.
3003 */
3004RTDECL(int32_t) RTStrToInt32(const char *pszValue);
3005
3006/**
3007 * Converts a string representation of a number to a 16-bit signed number.
3008 *
3009 * @returns iprt status code.
3010 * Warnings are used to indicate conversion problems.
3011 * @retval VWRN_NUMBER_TOO_BIG
3012 * @retval VWRN_TRAILING_CHARS
3013 * @retval VWRN_TRAILING_SPACES
3014 * @retval VINF_SUCCESS
3015 * @retval VERR_NO_DIGITS
3016 *
3017 * @param pszValue Pointer to the string value.
3018 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
3019 * @param uBase The base of the representation used.
3020 * If 0 the function will look for known prefixes before defaulting to 10.
3021 * @param pi16 Where to store the converted number. (optional)
3022 */
3023RTDECL(int) RTStrToInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, int16_t *pi16);
3024
3025/**
3026 * Converts a string representation of a number to a 16-bit signed number,
3027 * making sure the full string is converted.
3028 *
3029 * @returns iprt status code.
3030 * Warnings are used to indicate conversion problems.
3031 * @retval VWRN_NUMBER_TOO_BIG
3032 * @retval VINF_SUCCESS
3033 * @retval VERR_TRAILING_CHARS
3034 * @retval VERR_TRAILING_SPACES
3035 * @retval VERR_NO_DIGITS
3036 *
3037 * @param pszValue Pointer to the string value.
3038 * @param uBase The base of the representation used.
3039 * If 0 the function will look for known prefixes before defaulting to 10.
3040 * @param pi16 Where to store the converted number. (optional)
3041 */
3042RTDECL(int) RTStrToInt16Full(const char *pszValue, unsigned uBase, int16_t *pi16);
3043
3044/**
3045 * Converts a string representation of a number to a 16-bit signed number.
3046 * The base is guessed.
3047 *
3048 * @returns 16-bit signed number on success.
3049 * @returns 0 on failure.
3050 * @param pszValue Pointer to the string value.
3051 */
3052RTDECL(int16_t) RTStrToInt16(const char *pszValue);
3053
3054/**
3055 * Converts a string representation of a number to a 8-bit signed number.
3056 *
3057 * @returns iprt status code.
3058 * Warnings are used to indicate conversion problems.
3059 * @retval VWRN_NUMBER_TOO_BIG
3060 * @retval VWRN_TRAILING_CHARS
3061 * @retval VWRN_TRAILING_SPACES
3062 * @retval VINF_SUCCESS
3063 * @retval VERR_NO_DIGITS
3064 *
3065 * @param pszValue Pointer to the string value.
3066 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
3067 * @param uBase The base of the representation used.
3068 * If 0 the function will look for known prefixes before defaulting to 10.
3069 * @param pi8 Where to store the converted number. (optional)
3070 */
3071RTDECL(int) RTStrToInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, int8_t *pi8);
3072
3073/**
3074 * Converts a string representation of a number to a 8-bit signed number,
3075 * making sure the full string is converted.
3076 *
3077 * @returns iprt status code.
3078 * Warnings are used to indicate conversion problems.
3079 * @retval VWRN_NUMBER_TOO_BIG
3080 * @retval VINF_SUCCESS
3081 * @retval VERR_TRAILING_CHARS
3082 * @retval VERR_TRAILING_SPACES
3083 * @retval VERR_NO_DIGITS
3084 *
3085 * @param pszValue Pointer to the string value.
3086 * @param uBase The base of the representation used.
3087 * If 0 the function will look for known prefixes before defaulting to 10.
3088 * @param pi8 Where to store the converted number. (optional)
3089 */
3090RTDECL(int) RTStrToInt8Full(const char *pszValue, unsigned uBase, int8_t *pi8);
3091
3092/**
3093 * Converts a string representation of a number to a 8-bit signed number.
3094 * The base is guessed.
3095 *
3096 * @returns 8-bit signed number on success.
3097 * @returns 0 on failure.
3098 * @param pszValue Pointer to the string value.
3099 */
3100RTDECL(int8_t) RTStrToInt8(const char *pszValue);
3101
3102/**
3103 * Formats a buffer stream as hex bytes.
3104 *
3105 * The default is no separating spaces or line breaks or anything.
3106 *
3107 * @returns IPRT status code.
3108 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
3109 * @retval VERR_BUFFER_OVERFLOW if the buffer is insufficent to hold the bytes.
3110 *
3111 * @param pszBuf Output string buffer.
3112 * @param cbBuf The size of the output buffer.
3113 * @param pv Pointer to the bytes to stringify.
3114 * @param cb The number of bytes to stringify.
3115 * @param fFlags Combination of RTSTRPRINTHEXBYTES_F_XXX values.
3116 * @sa RTUtf16PrintHexBytes.
3117 */
3118RTDECL(int) RTStrPrintHexBytes(char *pszBuf, size_t cbBuf, void const *pv, size_t cb, uint32_t fFlags);
3119/** @name RTSTRPRINTHEXBYTES_F_XXX - flags for RTStrPrintHexBytes and RTUtf16PritnHexBytes.
3120 * @{ */
3121/** Upper case hex digits, the default is lower case. */
3122#define RTSTRPRINTHEXBYTES_F_UPPER RT_BIT(0)
3123/** Add a space between each group. */
3124#define RTSTRPRINTHEXBYTES_F_SEP_SPACE RT_BIT(1)
3125/** Add a colon between each group. */
3126#define RTSTRPRINTHEXBYTES_F_SEP_COLON RT_BIT(2)
3127/** @} */
3128
3129/**
3130 * Converts a string of hex bytes back into binary data.
3131 *
3132 * @returns IPRT status code.
3133 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
3134 * @retval VERR_BUFFER_OVERFLOW if the string contains too many hex bytes.
3135 * @retval VERR_BUFFER_UNDERFLOW if there aren't enough hex bytes to fill up
3136 * the output buffer.
3137 * @retval VERR_UNEVEN_INPUT if the input contains a half byte.
3138 * @retval VERR_NO_DIGITS
3139 * @retval VWRN_TRAILING_CHARS
3140 * @retval VWRN_TRAILING_SPACES
3141 *
3142 * @param pszHex The string containing the hex bytes.
3143 * @param pv Output buffer.
3144 * @param cb The size of the output buffer.
3145 * @param fFlags Must be zero, reserved for future use.
3146 */
3147RTDECL(int) RTStrConvertHexBytes(char const *pszHex, void *pv, size_t cb, uint32_t fFlags);
3148
3149/** @} */
3150
3151
3152/** @defgroup rt_str_space Unique String Space
3153 * @{
3154 */
3155
3156/** Pointer to a string name space container node core. */
3157typedef struct RTSTRSPACECORE *PRTSTRSPACECORE;
3158/** Pointer to a pointer to a string name space container node core. */
3159typedef PRTSTRSPACECORE *PPRTSTRSPACECORE;
3160
3161/**
3162 * String name space container node core.
3163 */
3164typedef struct RTSTRSPACECORE
3165{
3166 /** Pointer to the left leaf node. Don't touch. */
3167 PRTSTRSPACECORE pLeft;
3168 /** Pointer to the left right node. Don't touch. */
3169 PRTSTRSPACECORE pRight;
3170 /** Pointer to the list of string with the same hash key value. Don't touch. */
3171 PRTSTRSPACECORE pList;
3172 /** Hash key. Don't touch. */
3173 uint32_t Key;
3174 /** Height of this tree: max(heigth(left), heigth(right)) + 1. Don't touch */
3175 unsigned char uchHeight;
3176 /** The string length. Read only! */
3177 size_t cchString;
3178 /** Pointer to the string. Read only! */
3179 const char *pszString;
3180} RTSTRSPACECORE;
3181
3182/** String space. (Initialize with NULL.) */
3183typedef PRTSTRSPACECORE RTSTRSPACE;
3184/** Pointer to a string space. */
3185typedef PPRTSTRSPACECORE PRTSTRSPACE;
3186
3187
3188/**
3189 * Inserts a string into a unique string space.
3190 *
3191 * @returns true on success.
3192 * @returns false if the string collided with an existing string.
3193 * @param pStrSpace The space to insert it into.
3194 * @param pStr The string node.
3195 */
3196RTDECL(bool) RTStrSpaceInsert(PRTSTRSPACE pStrSpace, PRTSTRSPACECORE pStr);
3197
3198/**
3199 * Removes a string from a unique string space.
3200 *
3201 * @returns Pointer to the removed string node.
3202 * @returns NULL if the string was not found in the string space.
3203 * @param pStrSpace The space to remove it from.
3204 * @param pszString The string to remove.
3205 */
3206RTDECL(PRTSTRSPACECORE) RTStrSpaceRemove(PRTSTRSPACE pStrSpace, const char *pszString);
3207
3208/**
3209 * Gets a string from a unique string space.
3210 *
3211 * @returns Pointer to the string node.
3212 * @returns NULL if the string was not found in the string space.
3213 * @param pStrSpace The space to get it from.
3214 * @param pszString The string to get.
3215 */
3216RTDECL(PRTSTRSPACECORE) RTStrSpaceGet(PRTSTRSPACE pStrSpace, const char *pszString);
3217
3218/**
3219 * Gets a string from a unique string space.
3220 *
3221 * @returns Pointer to the string node.
3222 * @returns NULL if the string was not found in the string space.
3223 * @param pStrSpace The space to get it from.
3224 * @param pszString The string to get.
3225 * @param cchMax The max string length to evaluate. Passing
3226 * RTSTR_MAX is ok and makes it behave just like
3227 * RTStrSpaceGet.
3228 */
3229RTDECL(PRTSTRSPACECORE) RTStrSpaceGetN(PRTSTRSPACE pStrSpace, const char *pszString, size_t cchMax);
3230
3231/**
3232 * Callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy().
3233 *
3234 * @returns 0 on continue.
3235 * @returns Non-zero to aborts the operation.
3236 * @param pStr The string node
3237 * @param pvUser The user specified argument.
3238 */
3239typedef DECLCALLBACK(int) FNRTSTRSPACECALLBACK(PRTSTRSPACECORE pStr, void *pvUser);
3240/** Pointer to callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy(). */
3241typedef FNRTSTRSPACECALLBACK *PFNRTSTRSPACECALLBACK;
3242
3243/**
3244 * Destroys the string space.
3245 *
3246 * The caller supplies a callback which will be called for each of the string
3247 * nodes in for freeing their memory and other resources.
3248 *
3249 * @returns 0 or what ever non-zero return value pfnCallback returned
3250 * when aborting the destruction.
3251 * @param pStrSpace The space to destroy.
3252 * @param pfnCallback The callback.
3253 * @param pvUser The user argument.
3254 */
3255RTDECL(int) RTStrSpaceDestroy(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
3256
3257/**
3258 * Enumerates the string space.
3259 * The caller supplies a callback which will be called for each of
3260 * the string nodes.
3261 *
3262 * @returns 0 or what ever non-zero return value pfnCallback returned
3263 * when aborting the destruction.
3264 * @param pStrSpace The space to enumerate.
3265 * @param pfnCallback The callback.
3266 * @param pvUser The user argument.
3267 */
3268RTDECL(int) RTStrSpaceEnumerate(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
3269
3270/** @} */
3271
3272
3273/** @defgroup rt_str_hash Sting hashing
3274 * @{ */
3275
3276/**
3277 * Hashes the given string using algorithm \#1.
3278 *
3279 * @returns String hash.
3280 * @param pszString The string to hash.
3281 */
3282RTDECL(uint32_t) RTStrHash1(const char *pszString);
3283
3284/**
3285 * Hashes the given string using algorithm \#1.
3286 *
3287 * @returns String hash.
3288 * @param pszString The string to hash.
3289 * @param cchString The max length to hash. Hashing will stop if the
3290 * terminator character is encountered first. Passing
3291 * RTSTR_MAX is fine.
3292 */
3293RTDECL(uint32_t) RTStrHash1N(const char *pszString, size_t cchString);
3294
3295/**
3296 * Hashes the given strings as if they were concatenated using algorithm \#1.
3297 *
3298 * @returns String hash.
3299 * @param cPairs The number of string / length pairs in the
3300 * ellipsis.
3301 * @param ... List of string (const char *) and length
3302 * (size_t) pairs. Passing RTSTR_MAX as the size is
3303 * fine.
3304 */
3305RTDECL(uint32_t) RTStrHash1ExN(size_t cPairs, ...);
3306
3307/**
3308 * Hashes the given strings as if they were concatenated using algorithm \#1.
3309 *
3310 * @returns String hash.
3311 * @param cPairs The number of string / length pairs in the @a va.
3312 * @param va List of string (const char *) and length
3313 * (size_t) pairs. Passing RTSTR_MAX as the size is
3314 * fine.
3315 */
3316RTDECL(uint32_t) RTStrHash1ExNV(size_t cPairs, va_list va);
3317
3318/** @} */
3319
3320/** @} */
3321
3322RT_C_DECLS_END
3323
3324#endif /* !IPRT_INCLUDED_string_h */
3325
Note: See TracBrowser for help on using the repository browser.

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