VirtualBox

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

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

iprt/string.h: %RMpa - to percent-encode (a)ll reserved characters.

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