VirtualBox

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

Last change on this file since 63108 was 62930, checked in by vboxsync, 9 years ago

RTUtf16PurgeEncoding: Optimized it a little, adding debug assertion for bad pairs.

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