VirtualBox

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

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

RTStrPurgeEncoding: 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. Multi-byte characters will
730 * be replaced byte by byte.
731 *
732 * @returns The number of code points replaced. In the case of an incorrectly
733 * encoded string -1 will be returned, and the string is not completely
734 * processed. In the case of puszValidPairs having an odd number of
735 * code points, -1 will be also return but without any modification to
736 * the string.
737 * @param psz The string to sanitise.
738 * @param puszValidPairs A zero-terminated array of pairs of Unicode points.
739 * Each pair is the start and end point of a range,
740 * and the union of these ranges forms the white list.
741 * @param chReplacement The ASCII replacement character.
742 */
743RTDECL(ssize_t) RTStrPurgeComplementSet(char *psz, PCRTUNICP puszValidPairs, char chReplacement);
744
745/**
746 * Gets the number of code points the string is made up of, excluding
747 * the terminator.
748 *
749 *
750 * @returns Number of code points (RTUNICP).
751 * @returns 0 if the string was incorrectly encoded.
752 * @param psz The string.
753 */
754RTDECL(size_t) RTStrUniLen(const char *psz);
755
756/**
757 * Gets the number of code points the string is made up of, excluding
758 * the terminator.
759 *
760 * This function will validate the string, and incorrectly encoded UTF-8
761 * strings will be rejected.
762 *
763 * @returns iprt status code.
764 * @param psz The string.
765 * @param cch The max string length. Use RTSTR_MAX to process the entire string.
766 * @param pcuc Where to store the code point count.
767 * This is undefined on failure.
768 */
769RTDECL(int) RTStrUniLenEx(const char *psz, size_t cch, size_t *pcuc);
770
771/**
772 * Translate a UTF-8 string into an unicode string (i.e. RTUNICPs), allocating the string buffer.
773 *
774 * @returns iprt status code.
775 * @param pszString UTF-8 string to convert.
776 * @param ppUniString Receives pointer to the allocated unicode string.
777 * The returned string must be freed using RTUniFree().
778 */
779RTDECL(int) RTStrToUni(const char *pszString, PRTUNICP *ppUniString);
780
781/**
782 * Translates pszString from UTF-8 to an array of code points, allocating the result
783 * array if requested.
784 *
785 * @returns iprt status code.
786 * @param pszString UTF-8 string to convert.
787 * @param cchString The maximum size in chars (the type) to convert. The conversion stop
788 * when it reaches cchString or the string terminator ('\\0').
789 * Use RTSTR_MAX to translate the entire string.
790 * @param ppaCps If cCps is non-zero, this must either be pointing to pointer to
791 * a buffer of the specified size, or pointer to a NULL pointer.
792 * If *ppusz is NULL or cCps is zero a buffer of at least cCps items
793 * will be allocated to hold the translated string.
794 * If a buffer was requested it must be freed using RTUtf16Free().
795 * @param cCps The number of code points in the unicode string. This includes the terminator.
796 * @param pcCps Where to store the length of the translated string,
797 * excluding the terminator. (Optional)
798 *
799 * This may be set under some error conditions,
800 * however, only for VERR_BUFFER_OVERFLOW and
801 * VERR_NO_STR_MEMORY will it contain a valid string
802 * length that can be used to resize the buffer.
803 */
804RTDECL(int) RTStrToUniEx(const char *pszString, size_t cchString, PRTUNICP *ppaCps, size_t cCps, size_t *pcCps);
805
806/**
807 * Calculates the length of the string in RTUTF16 items.
808 *
809 * This function will validate the string, and incorrectly encoded UTF-8
810 * strings will be rejected. The primary purpose of this function is to
811 * help allocate buffers for RTStrToUtf16Ex of the correct size. For most
812 * other purposes RTStrCalcUtf16LenEx() should be used.
813 *
814 * @returns Number of RTUTF16 items.
815 * @returns 0 if the string was incorrectly encoded.
816 * @param psz The string.
817 */
818RTDECL(size_t) RTStrCalcUtf16Len(const char *psz);
819
820/**
821 * Calculates the length of the string in RTUTF16 items.
822 *
823 * This function will validate the string, and incorrectly encoded UTF-8
824 * strings will be rejected.
825 *
826 * @returns iprt status code.
827 * @param psz The string.
828 * @param cch The max string length. Use RTSTR_MAX to process the entire string.
829 * @param pcwc Where to store the string length. Optional.
830 * This is undefined on failure.
831 */
832RTDECL(int) RTStrCalcUtf16LenEx(const char *psz, size_t cch, size_t *pcwc);
833
834/**
835 * Translate a UTF-8 string into a UTF-16 allocating the result buffer (default
836 * tag).
837 *
838 * @returns iprt status code.
839 * @param pszString UTF-8 string to convert.
840 * @param ppwszString Receives pointer to the allocated UTF-16 string.
841 * The returned string must be freed using RTUtf16Free().
842 */
843#define RTStrToUtf16(pszString, ppwszString) RTStrToUtf16Tag((pszString), (ppwszString), RTSTR_TAG)
844
845/**
846 * Translate a UTF-8 string into a UTF-16 allocating the result buffer (custom
847 * tag).
848 *
849 * @returns iprt status code.
850 * @param pszString UTF-8 string to convert.
851 * @param ppwszString Receives pointer to the allocated UTF-16 string.
852 * The returned string must be freed using RTUtf16Free().
853 * @param pszTag Allocation tag used for statistics and such.
854 */
855RTDECL(int) RTStrToUtf16Tag(const char *pszString, PRTUTF16 *ppwszString, const char *pszTag);
856
857/**
858 * Translates pszString from UTF-8 to UTF-16, allocating the result buffer if requested.
859 *
860 * @returns iprt status code.
861 * @param pszString UTF-8 string to convert.
862 * @param cchString The maximum size in chars (the type) to convert. The conversion stop
863 * when it reaches cchString or the string terminator ('\\0').
864 * Use RTSTR_MAX to translate the entire string.
865 * @param ppwsz If cwc is non-zero, this must either be pointing to pointer to
866 * a buffer of the specified size, or pointer to a NULL pointer.
867 * If *ppwsz is NULL or cwc is zero a buffer of at least cwc items
868 * will be allocated to hold the translated string.
869 * If a buffer was requested it must be freed using RTUtf16Free().
870 * @param cwc The buffer size in RTUTF16s. This includes the terminator.
871 * @param pcwc Where to store the length of the translated string,
872 * excluding the terminator. (Optional)
873 *
874 * This may be set under some error conditions,
875 * however, only for VERR_BUFFER_OVERFLOW and
876 * VERR_NO_STR_MEMORY will it contain a valid string
877 * length that can be used to resize the buffer.
878 */
879#define RTStrToUtf16Ex(pszString, cchString, ppwsz, cwc, pcwc) \
880 RTStrToUtf16ExTag((pszString), (cchString), (ppwsz), (cwc), (pcwc), RTSTR_TAG)
881
882/**
883 * Translates pszString from UTF-8 to UTF-16, allocating the result buffer if
884 * requested (custom tag).
885 *
886 * @returns iprt status code.
887 * @param pszString UTF-8 string to convert.
888 * @param cchString The maximum size in chars (the type) to convert. The conversion stop
889 * when it reaches cchString or the string terminator ('\\0').
890 * Use RTSTR_MAX to translate the entire string.
891 * @param ppwsz If cwc is non-zero, this must either be pointing to pointer to
892 * a buffer of the specified size, or pointer to a NULL pointer.
893 * If *ppwsz is NULL or cwc is zero a buffer of at least cwc items
894 * will be allocated to hold the translated string.
895 * If a buffer was requested it must be freed using RTUtf16Free().
896 * @param cwc The buffer size in RTUTF16s. This includes the terminator.
897 * @param pcwc Where to store the length of the translated string,
898 * excluding the terminator. (Optional)
899 *
900 * This may be set under some error conditions,
901 * however, only for VERR_BUFFER_OVERFLOW and
902 * VERR_NO_STR_MEMORY will it contain a valid string
903 * length that can be used to resize the buffer.
904 * @param pszTag Allocation tag used for statistics and such.
905 */
906RTDECL(int) RTStrToUtf16ExTag(const char *pszString, size_t cchString, PRTUTF16 *ppwsz, size_t cwc, size_t *pcwc, const char *pszTag);
907
908
909/**
910 * Calculates the length of the string in Latin-1 characters.
911 *
912 * This function will validate the string, and incorrectly encoded UTF-8
913 * strings as well as string with codepoints outside the latin-1 range will be
914 * rejected. The primary purpose of this function is to help allocate buffers
915 * for RTStrToLatin1Ex of the correct size. For most other purposes
916 * RTStrCalcLatin1LenEx() should be used.
917 *
918 * @returns Number of Latin-1 characters.
919 * @returns 0 if the string was incorrectly encoded.
920 * @param psz The string.
921 */
922RTDECL(size_t) RTStrCalcLatin1Len(const char *psz);
923
924/**
925 * Calculates the length of the string in Latin-1 characters.
926 *
927 * This function will validate the string, and incorrectly encoded UTF-8
928 * strings as well as string with codepoints outside the latin-1 range will be
929 * rejected.
930 *
931 * @returns iprt status code.
932 * @param psz The string.
933 * @param cch The max string length. Use RTSTR_MAX to process the
934 * entire string.
935 * @param pcch Where to store the string length. Optional.
936 * This is undefined on failure.
937 */
938RTDECL(int) RTStrCalcLatin1LenEx(const char *psz, size_t cch, size_t *pcch);
939
940/**
941 * Translate a UTF-8 string into a Latin-1 allocating the result buffer (default
942 * tag).
943 *
944 * @returns iprt status code.
945 * @param pszString UTF-8 string to convert.
946 * @param ppszString Receives pointer to the allocated Latin-1 string.
947 * The returned string must be freed using RTStrFree().
948 */
949#define RTStrToLatin1(pszString, ppszString) RTStrToLatin1Tag((pszString), (ppszString), RTSTR_TAG)
950
951/**
952 * Translate a UTF-8 string into a Latin-1 allocating the result buffer (custom
953 * tag).
954 *
955 * @returns iprt status code.
956 * @param pszString UTF-8 string to convert.
957 * @param ppszString Receives pointer to the allocated Latin-1 string.
958 * The returned string must be freed using RTStrFree().
959 * @param pszTag Allocation tag used for statistics and such.
960 */
961RTDECL(int) RTStrToLatin1Tag(const char *pszString, char **ppszString, const char *pszTag);
962
963/**
964 * Translates pszString from UTF-8 to Latin-1, allocating the result buffer if requested.
965 *
966 * @returns iprt status code.
967 * @param pszString UTF-8 string to convert.
968 * @param cchString The maximum size in chars (the type) to convert.
969 * The conversion stop when it reaches cchString or
970 * the string terminator ('\\0'). Use RTSTR_MAX to
971 * translate the entire string.
972 * @param ppsz If cch is non-zero, this must either be pointing to
973 * pointer to a buffer of the specified size, or
974 * pointer to a NULL pointer. If *ppsz is NULL or cch
975 * is zero a buffer of at least cch items will be
976 * allocated to hold the translated string. If a
977 * buffer was requested it must be freed using
978 * RTStrFree().
979 * @param cch The buffer size in bytes. This includes the
980 * terminator.
981 * @param pcch Where to store the length of the translated string,
982 * excluding the terminator. (Optional)
983 *
984 * This may be set under some error conditions,
985 * however, only for VERR_BUFFER_OVERFLOW and
986 * VERR_NO_STR_MEMORY will it contain a valid string
987 * length that can be used to resize the buffer.
988 */
989#define RTStrToLatin1Ex(pszString, cchString, ppsz, cch, pcch) \
990 RTStrToLatin1ExTag((pszString), (cchString), (ppsz), (cch), (pcch), RTSTR_TAG)
991
992/**
993 * Translates pszString from UTF-8 to Latin1, allocating the result buffer if
994 * requested (custom tag).
995 *
996 * @returns iprt status code.
997 * @param pszString UTF-8 string to convert.
998 * @param cchString The maximum size in chars (the type) to convert.
999 * The conversion stop when it reaches cchString or
1000 * the string terminator ('\\0'). Use RTSTR_MAX to
1001 * translate the entire string.
1002 * @param ppsz If cch is non-zero, this must either be pointing to
1003 * pointer to a buffer of the specified size, or
1004 * pointer to a NULL pointer. If *ppsz is NULL or cch
1005 * is zero a buffer of at least cch items will be
1006 * allocated to hold the translated string. If a
1007 * buffer was requested it must be freed using
1008 * RTStrFree().
1009 * @param cch The buffer size in bytes. This includes the
1010 * terminator.
1011 * @param pcch Where to store the length of the translated string,
1012 * excluding the terminator. (Optional)
1013 *
1014 * This may be set under some error conditions,
1015 * however, only for VERR_BUFFER_OVERFLOW and
1016 * VERR_NO_STR_MEMORY will it contain a valid string
1017 * length that can be used to resize the buffer.
1018 * @param pszTag Allocation tag used for statistics and such.
1019 */
1020RTDECL(int) RTStrToLatin1ExTag(const char *pszString, size_t cchString, char **ppsz, size_t cch, size_t *pcch, const char *pszTag);
1021
1022/**
1023 * Get the unicode code point at the given string position.
1024 *
1025 * @returns unicode code point.
1026 * @returns RTUNICP_INVALID if the encoding is invalid.
1027 * @param psz The string.
1028 */
1029RTDECL(RTUNICP) RTStrGetCpInternal(const char *psz);
1030
1031/**
1032 * Get the unicode code point at the given string position.
1033 *
1034 * @returns iprt status code
1035 * @returns VERR_INVALID_UTF8_ENCODING if the encoding is invalid.
1036 * @param ppsz The string cursor.
1037 * This is advanced one character forward on failure.
1038 * @param pCp Where to store the unicode code point.
1039 * Stores RTUNICP_INVALID if the encoding is invalid.
1040 */
1041RTDECL(int) RTStrGetCpExInternal(const char **ppsz, PRTUNICP pCp);
1042
1043/**
1044 * Get the unicode code point at the given string position for a string of a
1045 * given length.
1046 *
1047 * @returns iprt status code
1048 * @retval VERR_INVALID_UTF8_ENCODING if the encoding is invalid.
1049 * @retval VERR_END_OF_STRING if *pcch is 0. *pCp is set to RTUNICP_INVALID.
1050 *
1051 * @param ppsz The string.
1052 * @param pcch Pointer to the length of the string. This will be
1053 * decremented by the size of the code point.
1054 * @param pCp Where to store the unicode code point.
1055 * Stores RTUNICP_INVALID if the encoding is invalid.
1056 */
1057RTDECL(int) RTStrGetCpNExInternal(const char **ppsz, size_t *pcch, PRTUNICP pCp);
1058
1059/**
1060 * Put the unicode code point at the given string position
1061 * and return the pointer to the char following it.
1062 *
1063 * This function will not consider anything at or following the
1064 * buffer area pointed to by psz. It is therefore not suitable for
1065 * inserting code points into a string, only appending/overwriting.
1066 *
1067 * @returns pointer to the char following the written code point.
1068 * @param psz The string.
1069 * @param CodePoint The code point to write.
1070 * This should not be RTUNICP_INVALID or any other
1071 * character out of the UTF-8 range.
1072 *
1073 * @remark This is a worker function for RTStrPutCp().
1074 *
1075 */
1076RTDECL(char *) RTStrPutCpInternal(char *psz, RTUNICP CodePoint);
1077
1078/**
1079 * Get the unicode code point at the given string position.
1080 *
1081 * @returns unicode code point.
1082 * @returns RTUNICP_INVALID if the encoding is invalid.
1083 * @param psz The string.
1084 *
1085 * @remark We optimize this operation by using an inline function for
1086 * the most frequent and simplest sequence, the rest is
1087 * handled by RTStrGetCpInternal().
1088 */
1089DECLINLINE(RTUNICP) RTStrGetCp(const char *psz)
1090{
1091 const unsigned char uch = *(const unsigned char *)psz;
1092 if (!(uch & RT_BIT(7)))
1093 return uch;
1094 return RTStrGetCpInternal(psz);
1095}
1096
1097/**
1098 * Get the unicode code point at the given string position.
1099 *
1100 * @returns iprt status code.
1101 * @param ppsz Pointer to the string pointer. This will be updated to
1102 * point to the char following the current code point.
1103 * This is advanced one character forward on failure.
1104 * @param pCp Where to store the code point.
1105 * RTUNICP_INVALID is stored here on failure.
1106 *
1107 * @remark We optimize this operation by using an inline function for
1108 * the most frequent and simplest sequence, the rest is
1109 * handled by RTStrGetCpExInternal().
1110 */
1111DECLINLINE(int) RTStrGetCpEx(const char **ppsz, PRTUNICP pCp)
1112{
1113 const unsigned char uch = **(const unsigned char **)ppsz;
1114 if (!(uch & RT_BIT(7)))
1115 {
1116 (*ppsz)++;
1117 *pCp = uch;
1118 return VINF_SUCCESS;
1119 }
1120 return RTStrGetCpExInternal(ppsz, pCp);
1121}
1122
1123/**
1124 * Get the unicode code point at the given string position for a string of a
1125 * given maximum length.
1126 *
1127 * @returns iprt status code.
1128 * @retval VERR_INVALID_UTF8_ENCODING if the encoding is invalid.
1129 * @retval VERR_END_OF_STRING if *pcch is 0. *pCp is set to RTUNICP_INVALID.
1130 *
1131 * @param ppsz Pointer to the string pointer. This will be updated to
1132 * point to the char following the current code point.
1133 * @param pcch Pointer to the maximum string length. This will be
1134 * decremented by the size of the code point found.
1135 * @param pCp Where to store the code point.
1136 * RTUNICP_INVALID is stored here on failure.
1137 *
1138 * @remark We optimize this operation by using an inline function for
1139 * the most frequent and simplest sequence, the rest is
1140 * handled by RTStrGetCpNExInternal().
1141 */
1142DECLINLINE(int) RTStrGetCpNEx(const char **ppsz, size_t *pcch, PRTUNICP pCp)
1143{
1144 if (RT_LIKELY(*pcch != 0))
1145 {
1146 const unsigned char uch = **(const unsigned char **)ppsz;
1147 if (!(uch & RT_BIT(7)))
1148 {
1149 (*ppsz)++;
1150 (*pcch)--;
1151 *pCp = uch;
1152 return VINF_SUCCESS;
1153 }
1154 }
1155 return RTStrGetCpNExInternal(ppsz, pcch, pCp);
1156}
1157
1158/**
1159 * Get the UTF-8 size in characters of a given Unicode code point.
1160 *
1161 * The code point is expected to be a valid Unicode one, but not necessarily in
1162 * the range supported by UTF-8.
1163 *
1164 * @returns The number of chars (bytes) required to encode the code point, or
1165 * zero if there is no UTF-8 encoding.
1166 * @param CodePoint The unicode code point.
1167 */
1168DECLINLINE(size_t) RTStrCpSize(RTUNICP CodePoint)
1169{
1170 if (CodePoint < 0x00000080)
1171 return 1;
1172 if (CodePoint < 0x00000800)
1173 return 2;
1174 if (CodePoint < 0x00010000)
1175 return 3;
1176#ifdef RT_USE_RTC_3629
1177 if (CodePoint < 0x00011000)
1178 return 4;
1179#else
1180 if (CodePoint < 0x00200000)
1181 return 4;
1182 if (CodePoint < 0x04000000)
1183 return 5;
1184 if (CodePoint < 0x7fffffff)
1185 return 6;
1186#endif
1187 return 0;
1188}
1189
1190/**
1191 * Put the unicode code point at the given string position
1192 * and return the pointer to the char following it.
1193 *
1194 * This function will not consider anything at or following the
1195 * buffer area pointed to by psz. It is therefore not suitable for
1196 * inserting code points into a string, only appending/overwriting.
1197 *
1198 * @returns pointer to the char following the written code point.
1199 * @param psz The string.
1200 * @param CodePoint The code point to write.
1201 * This should not be RTUNICP_INVALID or any other
1202 * character out of the UTF-8 range.
1203 *
1204 * @remark We optimize this operation by using an inline function for
1205 * the most frequent and simplest sequence, the rest is
1206 * handled by RTStrPutCpInternal().
1207 */
1208DECLINLINE(char *) RTStrPutCp(char *psz, RTUNICP CodePoint)
1209{
1210 if (CodePoint < 0x80)
1211 {
1212 *psz++ = (unsigned char)CodePoint;
1213 return psz;
1214 }
1215 return RTStrPutCpInternal(psz, CodePoint);
1216}
1217
1218/**
1219 * Skips ahead, past the current code point.
1220 *
1221 * @returns Pointer to the char after the current code point.
1222 * @param psz Pointer to the current code point.
1223 * @remark This will not move the next valid code point, only past the current one.
1224 */
1225DECLINLINE(char *) RTStrNextCp(const char *psz)
1226{
1227 RTUNICP Cp;
1228 RTStrGetCpEx(&psz, &Cp);
1229 return (char *)psz;
1230}
1231
1232/**
1233 * Skips back to the previous code point.
1234 *
1235 * @returns Pointer to the char before the current code point.
1236 * @returns pszStart on failure.
1237 * @param pszStart Pointer to the start of the string.
1238 * @param psz Pointer to the current code point.
1239 */
1240RTDECL(char *) RTStrPrevCp(const char *pszStart, const char *psz);
1241
1242
1243/** @page pg_rt_str_format The IPRT Format Strings
1244 *
1245 * IPRT implements most of the commonly used format types and flags with the
1246 * exception of floating point which is completely missing. In addition IPRT
1247 * provides a number of IPRT specific format types for the IPRT typedefs and
1248 * other useful things. Note that several of these extensions are similar to
1249 * \%p and doesn't care much if you try add formating flags/width/precision.
1250 *
1251 *
1252 * Group 0a, The commonly used format types:
1253 * - \%s - Takes a pointer to a zero terminated string (UTF-8) and
1254 * prints it with the optionally adjustment (width, -) and
1255 * length restriction (precision).
1256 * - \%ls - Same as \%s except that the input is UTF-16 (output UTF-8).
1257 * - \%Ls - Same as \%s except that the input is UCS-32 (output UTF-8).
1258 * - \%S - Same as \%s, used to convert to current codeset but this is
1259 * now done by the streams code. Deprecated, use \%s.
1260 * - \%lS - Ditto. Deprecated, use \%ls.
1261 * - \%LS - Ditto. Deprecated, use \%Ls.
1262 * - \%c - Takes a char and prints it.
1263 * - \%d - Takes a signed integer and prints it as decimal. Thousand
1264 * separator (\'), zero padding (0), adjustment (-+), width,
1265 * precision
1266 * - \%i - Same as \%d.
1267 * - \%u - Takes an unsigned integer and prints it as decimal. Thousand
1268 * separator (\'), zero padding (0), adjustment (-+), width,
1269 * precision
1270 * - \%x - Takes an unsigned integer and prints it as lowercased
1271 * hexadecimal. The special hash (\#) flag causes a '0x'
1272 * prefixed to be printed. Zero padding (0), adjustment (-+),
1273 * width, precision.
1274 * - \%X - Same as \%x except that it is uppercased.
1275 * - \%o - Takes an unsigned (?) integer and prints it as octal. Zero
1276 * padding (0), adjustment (-+), width, precision.
1277 * - \%p - Takes a pointer (void technically) and prints it. Zero
1278 * padding (0), adjustment (-+), width, precision.
1279 *
1280 * The \%d, \%i, \%u, \%x, \%X and \%o format types support the following
1281 * argument type specifiers:
1282 * - \%ll - long long (uint64_t).
1283 * - \%L - long long (uint64_t).
1284 * - \%l - long (uint32_t, uint64_t)
1285 * - \%h - short (int16_t).
1286 * - \%hh - char (int8_t).
1287 * - \%H - char (int8_t).
1288 * - \%z - size_t.
1289 * - \%j - intmax_t (int64_t).
1290 * - \%t - ptrdiff_t.
1291 * The type in parentheses is typical sizes, however when printing those types
1292 * you are better off using the special group 2 format types below (\%RX32 and
1293 * such).
1294 *
1295 *
1296 * Group 0b, IPRT format tricks:
1297 * - %M - Replaces the format string, takes a string pointer.
1298 * - %N - Nested formatting, takes a pointer to a format string
1299 * followed by the pointer to a va_list variable. The va_list
1300 * variable will not be modified and the caller must do va_end()
1301 * on it. Make sure the va_list variable is NOT in a parameter
1302 * list or some gcc versions/targets may get it all wrong.
1303 *
1304 *
1305 * Group 1, the basic runtime typedefs (excluding those which obviously are
1306 * pointer):
1307 * - \%RTbool - Takes a bool value and prints 'true', 'false', or '!%d!'.
1308 * - \%RTfile - Takes a #RTFILE value.
1309 * - \%RTfmode - Takes a #RTFMODE value.
1310 * - \%RTfoff - Takes a #RTFOFF value.
1311 * - \%RTfp16 - Takes a #RTFAR16 value.
1312 * - \%RTfp32 - Takes a #RTFAR32 value.
1313 * - \%RTfp64 - Takes a #RTFAR64 value.
1314 * - \%RTgid - Takes a #RTGID value.
1315 * - \%RTino - Takes a #RTINODE value.
1316 * - \%RTint - Takes a #RTINT value.
1317 * - \%RTiop - Takes a #RTIOPORT value.
1318 * - \%RTldrm - Takes a #RTLDRMOD value.
1319 * - \%RTmac - Takes a #PCRTMAC pointer.
1320 * - \%RTnaddr - Takes a #PCRTNETADDR value.
1321 * - \%RTnaipv4 - Takes a #RTNETADDRIPV4 value.
1322 * - \%RTnaipv6 - Takes a #PCRTNETADDRIPV6 value.
1323 * - \%RTnthrd - Takes a #RTNATIVETHREAD value.
1324 * - \%RTnthrd - Takes a #RTNATIVETHREAD value.
1325 * - \%RTproc - Takes a #RTPROCESS value.
1326 * - \%RTptr - Takes a #RTINTPTR or #RTUINTPTR value (but not void *).
1327 * - \%RTreg - Takes a #RTCCUINTREG value.
1328 * - \%RTsel - Takes a #RTSEL value.
1329 * - \%RTsem - Takes a #RTSEMEVENT, #RTSEMEVENTMULTI, #RTSEMMUTEX, #RTSEMFASTMUTEX, or #RTSEMRW value.
1330 * - \%RTsock - Takes a #RTSOCKET value.
1331 * - \%RTthrd - Takes a #RTTHREAD value.
1332 * - \%RTuid - Takes a #RTUID value.
1333 * - \%RTuint - Takes a #RTUINT value.
1334 * - \%RTunicp - Takes a #RTUNICP value.
1335 * - \%RTutf16 - Takes a #RTUTF16 value.
1336 * - \%RTuuid - Takes a #PCRTUUID and will print the UUID as a string.
1337 * - \%RTxuint - Takes a #RTUINT or #RTINT value, formatting it as hex.
1338 * - \%RGi - Takes a #RTGCINT value.
1339 * - \%RGp - Takes a #RTGCPHYS value.
1340 * - \%RGr - Takes a #RTGCUINTREG value.
1341 * - \%RGu - Takes a #RTGCUINT value.
1342 * - \%RGv - Takes a #RTGCPTR, #RTGCINTPTR or #RTGCUINTPTR value.
1343 * - \%RGx - Takes a #RTGCUINT or #RTGCINT value, formatting it as hex.
1344 * - \%RHi - Takes a #RTHCINT value.
1345 * - \%RHp - Takes a #RTHCPHYS value.
1346 * - \%RHr - Takes a #RTHCUINTREG value.
1347 * - \%RHu - Takes a #RTHCUINT value.
1348 * - \%RHv - Takes a #RTHCPTR, #RTHCINTPTR or #RTHCUINTPTR value.
1349 * - \%RHx - Takes a #RTHCUINT or #RTHCINT value, formatting it as hex.
1350 * - \%RRv - Takes a #RTRCPTR, #RTRCINTPTR or #RTRCUINTPTR value.
1351 * - \%RCi - Takes a #RTINT value.
1352 * - \%RCp - Takes a #RTCCPHYS value.
1353 * - \%RCr - Takes a #RTCCUINTREG value.
1354 * - \%RCu - Takes a #RTUINT value.
1355 * - \%RCv - Takes a #uintptr_t, #intptr_t, void * value.
1356 * - \%RCx - Takes a #RTUINT or #RTINT value, formatting it as hex.
1357 *
1358 *
1359 * Group 2, the generic integer types which are prefered over relying on what
1360 * bit-count a 'long', 'short', or 'long long' has on a platform. This are
1361 * highly prefered for the [u]intXX_t kind of types:
1362 * - \%RI[8|16|32|64] - Signed integer value of the specifed bit count.
1363 * - \%RU[8|16|32|64] - Unsigned integer value of the specifed bit count.
1364 * - \%RX[8|16|32|64] - Hexadecimal integer value of the specifed bit count.
1365 *
1366 *
1367 * Group 3, hex dumpers and other complex stuff which requires more than simple
1368 * formatting:
1369 * - \%Rhxd - Takes a pointer to the memory which is to be dumped in typical
1370 * hex format. Use the precision to specify the length, and the width to
1371 * set the number of bytes per line. Default width and precision is 16.
1372 * - \%Rhxs - Takes a pointer to the memory to be displayed as a hex string,
1373 * i.e. a series of space separated bytes formatted as two digit hex value.
1374 * Use the precision to specify the length. Default length is 16 bytes.
1375 * The width, if specified, is ignored.
1376 * - \%Rrc - Takes an integer iprt status code as argument. Will insert the
1377 * status code define corresponding to the iprt status code.
1378 * - \%Rrs - Takes an integer iprt status code as argument. Will insert the
1379 * short description of the specified status code.
1380 * - \%Rrf - Takes an integer iprt status code as argument. Will insert the
1381 * full description of the specified status code.
1382 * - \%Rra - Takes an integer iprt status code as argument. Will insert the
1383 * status code define + full description.
1384 * - \%Rwc - Takes a long Windows error code as argument. Will insert the status
1385 * code define corresponding to the Windows error code.
1386 * - \%Rwf - Takes a long Windows error code as argument. Will insert the
1387 * full description of the specified status code.
1388 * - \%Rwa - Takes a long Windows error code as argument. Will insert the
1389 * error code define + full description.
1390 *
1391 * - \%Rhrc - Takes a COM/XPCOM status code as argument. Will insert the status
1392 * code define corresponding to the Windows error code.
1393 * - \%Rhrf - Takes a COM/XPCOM status code as argument. Will insert the
1394 * full description of the specified status code.
1395 * - \%Rhra - Takes a COM/XPCOM error code as argument. Will insert the
1396 * error code define + full description.
1397 *
1398 * - \%Rfn - Pretty printing of a function or method. It drops the
1399 * return code and parameter list.
1400 * - \%Rbn - Prints the base name. For dropping the path in
1401 * order to save space when printing a path name.
1402 *
1403 * On other platforms, \%Rw? simply prints the argument in a form of 0xXXXXXXXX.
1404 *
1405 *
1406 * Group 4, structure dumpers:
1407 * - \%RDtimespec - Takes a PCRTTIMESPEC.
1408 *
1409 *
1410 * Group 5, XML / HTML escapers:
1411 * - \%RMas - Takes a string pointer (const char *) and outputs
1412 * it as an attribute value with the proper escaping.
1413 * This typically ends up in double quotes.
1414 *
1415 * - \%RMes - Takes a string pointer (const char *) and outputs
1416 * it as an element with the necessary escaping.
1417 *
1418 * Group 6, CPU Architecture Register dumpers:
1419 * - \%RAx86[reg] - Takes a 64-bit register value if the register is
1420 * 64-bit or smaller. Check the code wrt which
1421 * registers are implemented.
1422 *
1423 */
1424
1425#ifndef DECLARED_FNRTSTROUTPUT /* duplicated in iprt/log.h */
1426# define DECLARED_FNRTSTROUTPUT
1427/**
1428 * Output callback.
1429 *
1430 * @returns number of bytes written.
1431 * @param pvArg User argument.
1432 * @param pachChars Pointer to an array of utf-8 characters.
1433 * @param cbChars Number of bytes in the character array pointed to by pachChars.
1434 */
1435typedef DECLCALLBACK(size_t) FNRTSTROUTPUT(void *pvArg, const char *pachChars, size_t cbChars);
1436/** Pointer to callback function. */
1437typedef FNRTSTROUTPUT *PFNRTSTROUTPUT;
1438#endif
1439
1440/** @name Format flag.
1441 * These are used by RTStrFormat extensions and RTStrFormatNumber, mind
1442 * that not all flags makes sense to both of the functions.
1443 * @{ */
1444#define RTSTR_F_CAPITAL 0x0001
1445#define RTSTR_F_LEFT 0x0002
1446#define RTSTR_F_ZEROPAD 0x0004
1447#define RTSTR_F_SPECIAL 0x0008
1448#define RTSTR_F_VALSIGNED 0x0010
1449#define RTSTR_F_PLUS 0x0020
1450#define RTSTR_F_BLANK 0x0040
1451#define RTSTR_F_WIDTH 0x0080
1452#define RTSTR_F_PRECISION 0x0100
1453#define RTSTR_F_THOUSAND_SEP 0x0200
1454
1455#define RTSTR_F_BIT_MASK 0xf800
1456#define RTSTR_F_8BIT 0x0800
1457#define RTSTR_F_16BIT 0x1000
1458#define RTSTR_F_32BIT 0x2000
1459#define RTSTR_F_64BIT 0x4000
1460#define RTSTR_F_128BIT 0x8000
1461/** @} */
1462
1463/** @def RTSTR_GET_BIT_FLAG
1464 * Gets the bit flag for the specified type.
1465 */
1466#define RTSTR_GET_BIT_FLAG(type) \
1467 ( sizeof(type) * 8 == 32 ? RTSTR_F_32BIT \
1468 : sizeof(type) * 8 == 64 ? RTSTR_F_64BIT \
1469 : sizeof(type) * 8 == 16 ? RTSTR_F_16BIT \
1470 : sizeof(type) * 8 == 8 ? RTSTR_F_8BIT \
1471 : sizeof(type) * 8 == 128 ? RTSTR_F_128BIT \
1472 : 0)
1473
1474
1475/**
1476 * Callback to format non-standard format specifiers.
1477 *
1478 * @returns The number of bytes formatted.
1479 * @param pvArg Formatter argument.
1480 * @param pfnOutput Pointer to output function.
1481 * @param pvArgOutput Argument for the output function.
1482 * @param ppszFormat Pointer to the format string pointer. Advance this till the char
1483 * after the format specifier.
1484 * @param pArgs Pointer to the argument list. Use this to fetch the arguments.
1485 * @param cchWidth Format Width. -1 if not specified.
1486 * @param cchPrecision Format Precision. -1 if not specified.
1487 * @param fFlags Flags (RTSTR_NTFS_*).
1488 * @param chArgSize The argument size specifier, 'l' or 'L'.
1489 */
1490typedef DECLCALLBACK(size_t) FNSTRFORMAT(void *pvArg, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
1491 const char **ppszFormat, va_list *pArgs, int cchWidth,
1492 int cchPrecision, unsigned fFlags, char chArgSize);
1493/** Pointer to a FNSTRFORMAT() function. */
1494typedef FNSTRFORMAT *PFNSTRFORMAT;
1495
1496
1497/**
1498 * Partial implementation of a printf like formatter.
1499 * It doesn't do everything correct, and there is no floating point support.
1500 * However, it supports custom formats by the means of a format callback.
1501 *
1502 * @returns number of bytes formatted.
1503 * @param pfnOutput Output worker.
1504 * Called in two ways. Normally with a string and its length.
1505 * For termination, it's called with NULL for string, 0 for length.
1506 * @param pvArgOutput Argument to the output worker.
1507 * @param pfnFormat Custom format worker.
1508 * @param pvArgFormat Argument to the format worker.
1509 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1510 * @param InArgs Argument list.
1511 */
1512RTDECL(size_t) RTStrFormatV(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat,
1513 const char *pszFormat, va_list InArgs) RT_IPRT_FORMAT_ATTR(5, 0);
1514
1515/**
1516 * Partial implementation of a printf like formatter.
1517 * It doesn't do everything correct, and there is no floating point support.
1518 * However, it supports custom formats by the means of a format callback.
1519 *
1520 * @returns number of bytes formatted.
1521 * @param pfnOutput Output worker.
1522 * Called in two ways. Normally with a string and its length.
1523 * For termination, it's called with NULL for string, 0 for length.
1524 * @param pvArgOutput Argument to the output worker.
1525 * @param pfnFormat Custom format worker.
1526 * @param pvArgFormat Argument to the format worker.
1527 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1528 * @param ... Argument list.
1529 */
1530RTDECL(size_t) RTStrFormat(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat,
1531 const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(5, 6);
1532
1533/**
1534 * Formats an integer number according to the parameters.
1535 *
1536 * @returns Length of the formatted number.
1537 * @param psz Pointer to output string buffer of sufficient size.
1538 * @param u64Value Value to format.
1539 * @param uiBase Number representation base.
1540 * @param cchWidth Width.
1541 * @param cchPrecision Precision.
1542 * @param fFlags Flags, RTSTR_F_XXX.
1543 */
1544RTDECL(int) RTStrFormatNumber(char *psz, uint64_t u64Value, unsigned int uiBase, signed int cchWidth, signed int cchPrecision,
1545 unsigned int fFlags);
1546
1547/**
1548 * Formats an unsigned 8-bit number.
1549 *
1550 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1551 * @param pszBuf The output buffer.
1552 * @param cbBuf The size of the output buffer.
1553 * @param u8Value The value to format.
1554 * @param uiBase Number representation base.
1555 * @param cchWidth Width.
1556 * @param cchPrecision Precision.
1557 * @param fFlags Flags, RTSTR_F_XXX.
1558 */
1559RTDECL(ssize_t) RTStrFormatU8(char *pszBuf, size_t cbBuf, uint8_t u8Value, unsigned int uiBase,
1560 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1561
1562/**
1563 * Formats an unsigned 16-bit number.
1564 *
1565 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1566 * @param pszBuf The output buffer.
1567 * @param cbBuf The size of the output buffer.
1568 * @param u16Value The value to format.
1569 * @param uiBase Number representation base.
1570 * @param cchWidth Width.
1571 * @param cchPrecision Precision.
1572 * @param fFlags Flags, RTSTR_F_XXX.
1573 */
1574RTDECL(ssize_t) RTStrFormatU16(char *pszBuf, size_t cbBuf, uint16_t u16Value, unsigned int uiBase,
1575 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1576
1577/**
1578 * Formats an unsigned 32-bit number.
1579 *
1580 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1581 * @param pszBuf The output buffer.
1582 * @param cbBuf The size of the output buffer.
1583 * @param u32Value The value to format.
1584 * @param uiBase Number representation base.
1585 * @param cchWidth Width.
1586 * @param cchPrecision Precision.
1587 * @param fFlags Flags, RTSTR_F_XXX.
1588 */
1589RTDECL(ssize_t) RTStrFormatU32(char *pszBuf, size_t cbBuf, uint32_t u32Value, unsigned int uiBase,
1590 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1591
1592/**
1593 * Formats an unsigned 64-bit number.
1594 *
1595 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1596 * @param pszBuf The output buffer.
1597 * @param cbBuf The size of the output buffer.
1598 * @param u64Value The value to format.
1599 * @param uiBase Number representation base.
1600 * @param cchWidth Width.
1601 * @param cchPrecision Precision.
1602 * @param fFlags Flags, RTSTR_F_XXX.
1603 */
1604RTDECL(ssize_t) RTStrFormatU64(char *pszBuf, size_t cbBuf, uint64_t u64Value, unsigned int uiBase,
1605 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1606
1607/**
1608 * Formats an unsigned 128-bit number.
1609 *
1610 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1611 * @param pszBuf The output buffer.
1612 * @param cbBuf The size of the output buffer.
1613 * @param pu128Value The value to format.
1614 * @param uiBase Number representation base.
1615 * @param cchWidth Width.
1616 * @param cchPrecision Precision.
1617 * @param fFlags Flags, RTSTR_F_XXX.
1618 */
1619RTDECL(ssize_t) RTStrFormatU128(char *pszBuf, size_t cbBuf, PCRTUINT128U pu128Value, unsigned int uiBase,
1620 signed int cchWidth, signed int cchPrecision, uint32_t fFlags);
1621
1622/**
1623 * Formats an 80-bit extended floating point number.
1624 *
1625 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1626 * @param pszBuf The output buffer.
1627 * @param cbBuf The size of the output buffer.
1628 * @param pr80Value The value to format.
1629 * @param cchWidth Width.
1630 * @param cchPrecision Precision.
1631 * @param fFlags Flags, RTSTR_F_XXX.
1632 */
1633RTDECL(ssize_t) RTStrFormatR80(char *pszBuf, size_t cbBuf, PCRTFLOAT80U pr80Value, signed int cchWidth,
1634 signed int cchPrecision, uint32_t fFlags);
1635
1636/**
1637 * Formats an 80-bit extended floating point number, version 2.
1638 *
1639 * @returns The length of the formatted number or VERR_BUFFER_OVERFLOW.
1640 * @param pszBuf The output buffer.
1641 * @param cbBuf The size of the output buffer.
1642 * @param pr80Value The value to format.
1643 * @param cchWidth Width.
1644 * @param cchPrecision Precision.
1645 * @param fFlags Flags, RTSTR_F_XXX.
1646 */
1647RTDECL(ssize_t) RTStrFormatR80u2(char *pszBuf, size_t cbBuf, PCRTFLOAT80U2 pr80Value, signed int cchWidth,
1648 signed int cchPrecision, uint32_t fFlags);
1649
1650
1651
1652/**
1653 * Callback for formatting a type.
1654 *
1655 * This is registered using the RTStrFormatTypeRegister function and will
1656 * be called during string formatting to handle the specified %R[type].
1657 * The argument for this format type is assumed to be a pointer and it's
1658 * passed in the @a pvValue argument.
1659 *
1660 * @returns Length of the formatted output.
1661 * @param pfnOutput Output worker.
1662 * @param pvArgOutput Argument to the output worker.
1663 * @param pszType The type name.
1664 * @param pvValue The argument value.
1665 * @param cchWidth Width.
1666 * @param cchPrecision Precision.
1667 * @param fFlags Flags (NTFS_*).
1668 * @param pvUser The user argument.
1669 */
1670typedef DECLCALLBACK(size_t) FNRTSTRFORMATTYPE(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
1671 const char *pszType, void const *pvValue,
1672 int cchWidth, int cchPrecision, unsigned fFlags,
1673 void *pvUser);
1674/** Pointer to a FNRTSTRFORMATTYPE. */
1675typedef FNRTSTRFORMATTYPE *PFNRTSTRFORMATTYPE;
1676
1677
1678/**
1679 * Register a format handler for a type.
1680 *
1681 * The format handler is used to handle '%R[type]' format types, where the argument
1682 * in the vector is a pointer value (a bit restrictive, but keeps it simple).
1683 *
1684 * The caller must ensure that no other thread will be making use of any of
1685 * the dynamic formatting type facilities simultaneously with this call.
1686 *
1687 * @returns IPRT status code.
1688 * @retval VINF_SUCCESS on success.
1689 * @retval VERR_ALREADY_EXISTS if the type has already been registered.
1690 * @retval VERR_TOO_MANY_OPEN_FILES if all the type slots has been allocated already.
1691 *
1692 * @param pszType The type name.
1693 * @param pfnHandler The handler address. See FNRTSTRFORMATTYPE for details.
1694 * @param pvUser The user argument to pass to the handler. See RTStrFormatTypeSetUser
1695 * for how to update this later.
1696 */
1697RTDECL(int) RTStrFormatTypeRegister(const char *pszType, PFNRTSTRFORMATTYPE pfnHandler, void *pvUser);
1698
1699/**
1700 * Deregisters a format type.
1701 *
1702 * The caller must ensure that no other thread will be making use of any of
1703 * the dynamic formatting type facilities simultaneously with this call.
1704 *
1705 * @returns IPRT status code.
1706 * @retval VINF_SUCCESS on success.
1707 * @retval VERR_FILE_NOT_FOUND if not found.
1708 *
1709 * @param pszType The type to deregister.
1710 */
1711RTDECL(int) RTStrFormatTypeDeregister(const char *pszType);
1712
1713/**
1714 * Sets the user argument for a type.
1715 *
1716 * This can be used if a user argument needs relocating in GC.
1717 *
1718 * @returns IPRT status code.
1719 * @retval VINF_SUCCESS on success.
1720 * @retval VERR_FILE_NOT_FOUND if not found.
1721 *
1722 * @param pszType The type to update.
1723 * @param pvUser The new user argument value.
1724 */
1725RTDECL(int) RTStrFormatTypeSetUser(const char *pszType, void *pvUser);
1726
1727
1728/**
1729 * String printf.
1730 *
1731 * @returns The length of the returned string (in pszBuffer) excluding the
1732 * terminator.
1733 * @param pszBuffer Output buffer.
1734 * @param cchBuffer Size of the output buffer.
1735 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1736 * @param args The format argument.
1737 */
1738RTDECL(size_t) RTStrPrintfV(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(3, 0);
1739
1740/**
1741 * String printf.
1742 *
1743 * @returns The length of the returned string (in pszBuffer) excluding the
1744 * terminator.
1745 * @param pszBuffer Output buffer.
1746 * @param cchBuffer Size of the output buffer.
1747 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1748 * @param ... The format argument.
1749 */
1750RTDECL(size_t) RTStrPrintf(char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);
1751
1752
1753/**
1754 * String printf with custom formatting.
1755 *
1756 * @returns The length of the returned string (in pszBuffer) excluding the
1757 * terminator.
1758 * @param pfnFormat Pointer to handler function for the custom formats.
1759 * @param pvArg Argument to the pfnFormat function.
1760 * @param pszBuffer Output buffer.
1761 * @param cchBuffer Size of the output buffer.
1762 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1763 * @param args The format argument.
1764 */
1765RTDECL(size_t) RTStrPrintfExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer,
1766 const char *pszFormat, va_list args) RT_IPRT_FORMAT_ATTR(5, 0);
1767
1768/**
1769 * String printf with custom formatting.
1770 *
1771 * @returns The length of the returned string (in pszBuffer) excluding the
1772 * terminator.
1773 * @param pfnFormat Pointer to handler function for the custom formats.
1774 * @param pvArg Argument to the pfnFormat function.
1775 * @param pszBuffer Output buffer.
1776 * @param cchBuffer Size of the output buffer.
1777 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1778 * @param ... The format argument.
1779 */
1780RTDECL(size_t) RTStrPrintfEx(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer,
1781 const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(5, 6);
1782
1783
1784/**
1785 * Allocating string printf (default tag).
1786 *
1787 * @returns The length of the string in the returned *ppszBuffer excluding the
1788 * terminator.
1789 * @returns -1 on failure.
1790 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
1791 * The buffer should be freed using RTStrFree().
1792 * On failure *ppszBuffer will be set to NULL.
1793 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1794 * @param args The format argument.
1795 */
1796#define RTStrAPrintfV(ppszBuffer, pszFormat, args) RTStrAPrintfVTag((ppszBuffer), (pszFormat), (args), RTSTR_TAG)
1797
1798/**
1799 * Allocating string printf (custom tag).
1800 *
1801 * @returns The length of the string in the returned *ppszBuffer excluding the
1802 * terminator.
1803 * @returns -1 on failure.
1804 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
1805 * The buffer should be freed using RTStrFree().
1806 * On failure *ppszBuffer will be set to NULL.
1807 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1808 * @param args The format argument.
1809 * @param pszTag Allocation tag used for statistics and such.
1810 */
1811RTDECL(int) RTStrAPrintfVTag(char **ppszBuffer, const char *pszFormat, va_list args, const char *pszTag) RT_IPRT_FORMAT_ATTR(2, 0);
1812
1813/**
1814 * Allocating string printf.
1815 *
1816 * @returns The length of the string in the returned *ppszBuffer excluding the
1817 * terminator.
1818 * @returns -1 on failure.
1819 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
1820 * The buffer should be freed using RTStrFree().
1821 * On failure *ppszBuffer will be set to NULL.
1822 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1823 * @param ... The format argument.
1824 */
1825DECLINLINE(int) RT_IPRT_FORMAT_ATTR(2, 3) RTStrAPrintf(char **ppszBuffer, const char *pszFormat, ...)
1826{
1827 int cbRet;
1828 va_list va;
1829 va_start(va, pszFormat);
1830 cbRet = RTStrAPrintfVTag(ppszBuffer, pszFormat, va, RTSTR_TAG);
1831 va_end(va);
1832 return cbRet;
1833}
1834
1835/**
1836 * Allocating string printf (custom tag).
1837 *
1838 * @returns The length of the string in the returned *ppszBuffer excluding the
1839 * terminator.
1840 * @returns -1 on failure.
1841 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
1842 * The buffer should be freed using RTStrFree().
1843 * On failure *ppszBuffer will be set to NULL.
1844 * @param pszTag Allocation tag used for statistics and such.
1845 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1846 * @param ... The format argument.
1847 */
1848DECLINLINE(int) RT_IPRT_FORMAT_ATTR(3, 4) RTStrAPrintfTag(char **ppszBuffer, const char *pszTag, const char *pszFormat, ...)
1849{
1850 int cbRet;
1851 va_list va;
1852 va_start(va, pszFormat);
1853 cbRet = RTStrAPrintfVTag(ppszBuffer, pszFormat, va, pszTag);
1854 va_end(va);
1855 return cbRet;
1856}
1857
1858/**
1859 * Allocating string printf, version 2.
1860 *
1861 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
1862 * memory.
1863 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1864 * @param args The format argument.
1865 */
1866#define RTStrAPrintf2V(pszFormat, args) RTStrAPrintf2VTag((pszFormat), (args), RTSTR_TAG)
1867
1868/**
1869 * Allocating string printf, version 2.
1870 *
1871 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
1872 * memory.
1873 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1874 * @param args The format argument.
1875 * @param pszTag Allocation tag used for statistics and such.
1876 */
1877RTDECL(char *) RTStrAPrintf2VTag(const char *pszFormat, va_list args, const char *pszTag) RT_IPRT_FORMAT_ATTR(1, 0);
1878
1879/**
1880 * Allocating string printf, version 2 (default tag).
1881 *
1882 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
1883 * memory.
1884 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1885 * @param ... The format argument.
1886 */
1887DECLINLINE(char *) RT_IPRT_FORMAT_ATTR(1, 2) RTStrAPrintf2(const char *pszFormat, ...)
1888{
1889 char *pszRet;
1890 va_list va;
1891 va_start(va, pszFormat);
1892 pszRet = RTStrAPrintf2VTag(pszFormat, va, RTSTR_TAG);
1893 va_end(va);
1894 return pszRet;
1895}
1896
1897/**
1898 * Allocating string printf, version 2 (custom tag).
1899 *
1900 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
1901 * memory.
1902 * @param pszTag Allocation tag used for statistics and such.
1903 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1904 * @param ... The format argument.
1905 */
1906DECLINLINE(char *) RT_IPRT_FORMAT_ATTR(2, 3) RTStrAPrintf2Tag(const char *pszTag, const char *pszFormat, ...)
1907{
1908 char *pszRet;
1909 va_list va;
1910 va_start(va, pszFormat);
1911 pszRet = RTStrAPrintf2VTag(pszFormat, va, pszTag);
1912 va_end(va);
1913 return pszRet;
1914}
1915
1916/**
1917 * Strips blankspaces from both ends of the string.
1918 *
1919 * @returns Pointer to first non-blank char in the string.
1920 * @param psz The string to strip.
1921 */
1922RTDECL(char *) RTStrStrip(char *psz);
1923
1924/**
1925 * Strips blankspaces from the start of the string.
1926 *
1927 * @returns Pointer to first non-blank char in the string.
1928 * @param psz The string to strip.
1929 */
1930RTDECL(char *) RTStrStripL(const char *psz);
1931
1932/**
1933 * Strips blankspaces from the end of the string.
1934 *
1935 * @returns psz.
1936 * @param psz The string to strip.
1937 */
1938RTDECL(char *) RTStrStripR(char *psz);
1939
1940/**
1941 * String copy with overflow handling.
1942 *
1943 * @retval VINF_SUCCESS on success.
1944 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
1945 * buffer will contain as much of the string as it can hold, fully
1946 * terminated.
1947 *
1948 * @param pszDst The destination buffer.
1949 * @param cbDst The size of the destination buffer (in bytes).
1950 * @param pszSrc The source string. NULL is not OK.
1951 */
1952RTDECL(int) RTStrCopy(char *pszDst, size_t cbDst, const char *pszSrc);
1953
1954/**
1955 * String copy with overflow handling.
1956 *
1957 * @retval VINF_SUCCESS on success.
1958 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
1959 * buffer will contain as much of the string as it can hold, fully
1960 * terminated.
1961 *
1962 * @param pszDst The destination buffer.
1963 * @param cbDst The size of the destination buffer (in bytes).
1964 * @param pszSrc The source string. NULL is not OK.
1965 * @param cchSrcMax The maximum number of chars (not code points) to
1966 * copy from the source string, not counting the
1967 * terminator as usual.
1968 */
1969RTDECL(int) RTStrCopyEx(char *pszDst, size_t cbDst, const char *pszSrc, size_t cchSrcMax);
1970
1971/**
1972 * String copy with overflow handling and buffer advancing.
1973 *
1974 * @retval VINF_SUCCESS on success.
1975 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
1976 * buffer will contain as much of the string as it can hold, fully
1977 * terminated.
1978 *
1979 * @param ppszDst Pointer to the destination buffer pointer.
1980 * This will be advanced to the end of the copied
1981 * bytes (points at the terminator). This is also
1982 * updated on overflow.
1983 * @param pcbDst Pointer to the destination buffer size
1984 * variable. This will be updated in accord with
1985 * the buffer pointer.
1986 * @param pszSrc The source string. NULL is not OK.
1987 */
1988RTDECL(int) RTStrCopyP(char **ppszDst, size_t *pcbDst, const char *pszSrc);
1989
1990/**
1991 * String copy with overflow handling.
1992 *
1993 * @retval VINF_SUCCESS on success.
1994 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
1995 * buffer will contain as much of the string as it can hold, fully
1996 * terminated.
1997 *
1998 * @param ppszDst Pointer to the destination buffer pointer.
1999 * This will be advanced to the end of the copied
2000 * bytes (points at the terminator). This is also
2001 * updated on overflow.
2002 * @param pcbDst Pointer to the destination buffer size
2003 * variable. This will be updated in accord with
2004 * the buffer pointer.
2005 * @param pszSrc The source string. NULL is not OK.
2006 * @param cchSrcMax The maximum number of chars (not code points) to
2007 * copy from the source string, not counting the
2008 * terminator as usual.
2009 */
2010RTDECL(int) RTStrCopyPEx(char **ppszDst, size_t *pcbDst, const char *pszSrc, size_t cchSrcMax);
2011
2012/**
2013 * String concatenation with overflow handling.
2014 *
2015 * @retval VINF_SUCCESS on success.
2016 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2017 * buffer will contain as much of the string as it can hold, fully
2018 * terminated.
2019 *
2020 * @param pszDst The destination buffer.
2021 * @param cbDst The size of the destination buffer (in bytes).
2022 * @param pszSrc The source string. NULL is not OK.
2023 */
2024RTDECL(int) RTStrCat(char *pszDst, size_t cbDst, const char *pszSrc);
2025
2026/**
2027 * String concatenation with overflow handling.
2028 *
2029 * @retval VINF_SUCCESS on success.
2030 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2031 * buffer will contain as much of the string as it can hold, fully
2032 * terminated.
2033 *
2034 * @param pszDst The destination buffer.
2035 * @param cbDst The size of the destination buffer (in bytes).
2036 * @param pszSrc The source string. NULL is not OK.
2037 * @param cchSrcMax The maximum number of chars (not code points) to
2038 * copy from the source string, not counting the
2039 * terminator as usual.
2040 */
2041RTDECL(int) RTStrCatEx(char *pszDst, size_t cbDst, const char *pszSrc, size_t cchSrcMax);
2042
2043/**
2044 * String concatenation with overflow handling.
2045 *
2046 * @retval VINF_SUCCESS on success.
2047 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2048 * buffer will contain as much of the string as it can hold, fully
2049 * terminated.
2050 *
2051 * @param ppszDst Pointer to the destination buffer pointer.
2052 * This will be advanced to the end of the copied
2053 * bytes (points at the terminator). This is also
2054 * updated on overflow.
2055 * @param pcbDst Pointer to the destination buffer size
2056 * variable. This will be updated in accord with
2057 * the buffer pointer.
2058 * @param pszSrc The source string. NULL is not OK.
2059 */
2060RTDECL(int) RTStrCatP(char **ppszDst, size_t *pcbDst, const char *pszSrc);
2061
2062/**
2063 * String concatenation with overflow handling and buffer advancing.
2064 *
2065 * @retval VINF_SUCCESS on success.
2066 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2067 * buffer will contain as much of the string as it can hold, fully
2068 * terminated.
2069 *
2070 * @param ppszDst Pointer to the destination buffer pointer.
2071 * This will be advanced to the end of the copied
2072 * bytes (points at the terminator). This is also
2073 * updated on overflow.
2074 * @param pcbDst Pointer to the destination buffer size
2075 * variable. This will be updated in accord with
2076 * the buffer pointer.
2077 * @param pszSrc The source string. NULL is not OK.
2078 * @param cchSrcMax The maximum number of chars (not code points) to
2079 * copy from the source string, not counting the
2080 * terminator as usual.
2081 */
2082RTDECL(int) RTStrCatPEx(char **ppszDst, size_t *pcbDst, const char *pszSrc, size_t cchSrcMax);
2083
2084/**
2085 * Performs a case sensitive string compare between two UTF-8 strings.
2086 *
2087 * Encoding errors are ignored by the current implementation. So, the only
2088 * difference between this and the CRT strcmp function is the handling of
2089 * NULL arguments.
2090 *
2091 * @returns < 0 if the first string less than the second string.
2092 * @returns 0 if the first string identical to the second string.
2093 * @returns > 0 if the first string greater than the second string.
2094 * @param psz1 First UTF-8 string. Null is allowed.
2095 * @param psz2 Second UTF-8 string. Null is allowed.
2096 */
2097RTDECL(int) RTStrCmp(const char *psz1, const char *psz2);
2098
2099/**
2100 * Performs a case sensitive string compare between two UTF-8 strings, given
2101 * a maximum string length.
2102 *
2103 * Encoding errors are ignored by the current implementation. So, the only
2104 * difference between this and the CRT strncmp function is the handling of
2105 * NULL arguments.
2106 *
2107 * @returns < 0 if the first string less than the second string.
2108 * @returns 0 if the first string identical to the second string.
2109 * @returns > 0 if the first string greater than the second string.
2110 * @param psz1 First UTF-8 string. Null is allowed.
2111 * @param psz2 Second UTF-8 string. Null is allowed.
2112 * @param cchMax The maximum string length
2113 */
2114RTDECL(int) RTStrNCmp(const char *psz1, const char *psz2, size_t cchMax);
2115
2116/**
2117 * Performs a case insensitive string compare between two UTF-8 strings.
2118 *
2119 * This is a simplified compare, as only the simplified lower/upper case folding
2120 * specified by the unicode specs are used. It does not consider character pairs
2121 * as they are used in some languages, just simple upper & lower case compares.
2122 *
2123 * The result is the difference between the mismatching codepoints after they
2124 * both have been lower cased.
2125 *
2126 * If the string encoding is invalid the function will assert (strict builds)
2127 * and use RTStrCmp for the remainder of the string.
2128 *
2129 * @returns < 0 if the first string less than the second string.
2130 * @returns 0 if the first string identical to the second string.
2131 * @returns > 0 if the first string greater than the second string.
2132 * @param psz1 First UTF-8 string. Null is allowed.
2133 * @param psz2 Second UTF-8 string. Null is allowed.
2134 */
2135RTDECL(int) RTStrICmp(const char *psz1, const char *psz2);
2136
2137/**
2138 * Performs a case insensitive string compare between two UTF-8 strings, given a
2139 * maximum string length.
2140 *
2141 * This is a simplified compare, as only the simplified lower/upper case folding
2142 * specified by the unicode specs are used. It does not consider character pairs
2143 * as they are used in some languages, just simple upper & lower case compares.
2144 *
2145 * The result is the difference between the mismatching codepoints after they
2146 * both have been lower cased.
2147 *
2148 * If the string encoding is invalid the function will assert (strict builds)
2149 * and use RTStrCmp for the remainder of the string.
2150 *
2151 * @returns < 0 if the first string less than the second string.
2152 * @returns 0 if the first string identical to the second string.
2153 * @returns > 0 if the first string greater than the second string.
2154 * @param psz1 First UTF-8 string. Null is allowed.
2155 * @param psz2 Second UTF-8 string. Null is allowed.
2156 * @param cchMax Maximum string length
2157 */
2158RTDECL(int) RTStrNICmp(const char *psz1, const char *psz2, size_t cchMax);
2159
2160/**
2161 * Checks whether @a pszString starts with @a pszStart.
2162 *
2163 * @returns true / false.
2164 * @param pszString The string to check.
2165 * @param pszStart The start string to check for.
2166 */
2167RTDECL(int) RTStrStartsWith(const char *pszString, const char *pszStart);
2168
2169/**
2170 * Checks whether @a pszString starts with @a pszStart, case insensitive.
2171 *
2172 * @returns true / false.
2173 * @param pszString The string to check.
2174 * @param pszStart The start string to check for.
2175 */
2176RTDECL(int) RTStrIStartsWith(const char *pszString, const char *pszStart);
2177
2178/**
2179 * Locates a case sensitive substring.
2180 *
2181 * If any of the two strings are NULL, then NULL is returned. If the needle is
2182 * an empty string, then the haystack is returned (i.e. matches anything).
2183 *
2184 * @returns Pointer to the first occurrence of the substring if found, NULL if
2185 * not.
2186 *
2187 * @param pszHaystack The string to search.
2188 * @param pszNeedle The substring to search for.
2189 *
2190 * @remarks The difference between this and strstr is the handling of NULL
2191 * pointers.
2192 */
2193RTDECL(char *) RTStrStr(const char *pszHaystack, const char *pszNeedle);
2194
2195/**
2196 * Locates a case insensitive substring.
2197 *
2198 * If any of the two strings are NULL, then NULL is returned. If the needle is
2199 * an empty string, then the haystack is returned (i.e. matches anything).
2200 *
2201 * @returns Pointer to the first occurrence of the substring if found, NULL if
2202 * not.
2203 *
2204 * @param pszHaystack The string to search.
2205 * @param pszNeedle The substring to search for.
2206 *
2207 */
2208RTDECL(char *) RTStrIStr(const char *pszHaystack, const char *pszNeedle);
2209
2210/**
2211 * Converts the string to lower case.
2212 *
2213 * @returns Pointer to the converted string.
2214 * @param psz The string to convert.
2215 */
2216RTDECL(char *) RTStrToLower(char *psz);
2217
2218/**
2219 * Converts the string to upper case.
2220 *
2221 * @returns Pointer to the converted string.
2222 * @param psz The string to convert.
2223 */
2224RTDECL(char *) RTStrToUpper(char *psz);
2225
2226/**
2227 * Checks if the string is case foldable, i.e. whether it would change if
2228 * subject to RTStrToLower or RTStrToUpper.
2229 *
2230 * @returns true / false
2231 * @param psz The string in question.
2232 */
2233RTDECL(bool) RTStrIsCaseFoldable(const char *psz);
2234
2235/**
2236 * Checks if the string is upper cased (no lower case chars in it).
2237 *
2238 * @returns true / false
2239 * @param psz The string in question.
2240 */
2241RTDECL(bool) RTStrIsUpperCased(const char *psz);
2242
2243/**
2244 * Checks if the string is lower cased (no upper case chars in it).
2245 *
2246 * @returns true / false
2247 * @param psz The string in question.
2248 */
2249RTDECL(bool) RTStrIsLowerCased(const char *psz);
2250
2251/**
2252 * Find the length of a zero-terminated byte string, given
2253 * a max string length.
2254 *
2255 * See also RTStrNLenEx.
2256 *
2257 * @returns The string length or cbMax. The returned length does not include
2258 * the zero terminator if it was found.
2259 *
2260 * @param pszString The string.
2261 * @param cchMax The max string length.
2262 */
2263RTDECL(size_t) RTStrNLen(const char *pszString, size_t cchMax);
2264
2265/**
2266 * Find the length of a zero-terminated byte string, given
2267 * a max string length.
2268 *
2269 * See also RTStrNLen.
2270 *
2271 * @returns IPRT status code.
2272 * @retval VINF_SUCCESS if the string has a length less than cchMax.
2273 * @retval VERR_BUFFER_OVERFLOW if the end of the string wasn't found
2274 * before cchMax was reached.
2275 *
2276 * @param pszString The string.
2277 * @param cchMax The max string length.
2278 * @param pcch Where to store the string length excluding the
2279 * terminator. This is set to cchMax if the terminator
2280 * isn't found.
2281 */
2282RTDECL(int) RTStrNLenEx(const char *pszString, size_t cchMax, size_t *pcch);
2283
2284RT_C_DECLS_END
2285
2286/** The maximum size argument of a memchr call. */
2287#define RTSTR_MEMCHR_MAX ((~(size_t)0 >> 1) - 15)
2288
2289/**
2290 * Find the zero terminator in a string with a limited length.
2291 *
2292 * @returns Pointer to the zero terminator.
2293 * @returns NULL if the zero terminator was not found.
2294 *
2295 * @param pszString The string.
2296 * @param cchMax The max string length. RTSTR_MAX is fine.
2297 */
2298#if defined(__cplusplus) && !defined(DOXYGEN_RUNNING)
2299DECLINLINE(char const *) RTStrEnd(char const *pszString, size_t cchMax)
2300{
2301 /* Avoid potential issues with memchr seen in glibc.
2302 * See sysdeps/x86_64/memchr.S in glibc versions older than 2.11 */
2303 while (cchMax > RTSTR_MEMCHR_MAX)
2304 {
2305 char const *pszRet = (char const *)memchr(pszString, '\0', RTSTR_MEMCHR_MAX);
2306 if (RT_LIKELY(pszRet))
2307 return pszRet;
2308 pszString += RTSTR_MEMCHR_MAX;
2309 cchMax -= RTSTR_MEMCHR_MAX;
2310 }
2311 return (char const *)memchr(pszString, '\0', cchMax);
2312}
2313
2314DECLINLINE(char *) RTStrEnd(char *pszString, size_t cchMax)
2315#else
2316DECLINLINE(char *) RTStrEnd(const char *pszString, size_t cchMax)
2317#endif
2318{
2319 /* Avoid potential issues with memchr seen in glibc.
2320 * See sysdeps/x86_64/memchr.S in glibc versions older than 2.11 */
2321 while (cchMax > RTSTR_MEMCHR_MAX)
2322 {
2323 char *pszRet = (char *)memchr(pszString, '\0', RTSTR_MEMCHR_MAX);
2324 if (RT_LIKELY(pszRet))
2325 return pszRet;
2326 pszString += RTSTR_MEMCHR_MAX;
2327 cchMax -= RTSTR_MEMCHR_MAX;
2328 }
2329 return (char *)memchr(pszString, '\0', cchMax);
2330}
2331
2332RT_C_DECLS_BEGIN
2333
2334/**
2335 * Finds the offset at which a simple character first occurs in a string.
2336 *
2337 * @returns The offset of the first occurence or the terminator offset.
2338 * @param pszHaystack The string to search.
2339 * @param chNeedle The character to search for.
2340 */
2341DECLINLINE(size_t) RTStrOffCharOrTerm(const char *pszHaystack, char chNeedle)
2342{
2343 const char *psz = pszHaystack;
2344 char ch;
2345 while ( (ch = *psz) != chNeedle
2346 && ch != '\0')
2347 psz++;
2348 return psz - pszHaystack;
2349}
2350
2351
2352/**
2353 * Matches a simple string pattern.
2354 *
2355 * @returns true if the string matches the pattern, otherwise false.
2356 *
2357 * @param pszPattern The pattern. Special chars are '*' and '?', where the
2358 * asterisk matches zero or more characters and question
2359 * mark matches exactly one character.
2360 * @param pszString The string to match against the pattern.
2361 */
2362RTDECL(bool) RTStrSimplePatternMatch(const char *pszPattern, const char *pszString);
2363
2364/**
2365 * Matches a simple string pattern, neither which needs to be zero terminated.
2366 *
2367 * This is identical to RTStrSimplePatternMatch except that you can optionally
2368 * specify the length of both the pattern and the string. The function will
2369 * stop when it hits a string terminator or either of the lengths.
2370 *
2371 * @returns true if the string matches the pattern, otherwise false.
2372 *
2373 * @param pszPattern The pattern. Special chars are '*' and '?', where the
2374 * asterisk matches zero or more characters and question
2375 * mark matches exactly one character.
2376 * @param cchPattern The pattern length. Pass RTSTR_MAX if you don't know the
2377 * length and wish to stop at the string terminator.
2378 * @param pszString The string to match against the pattern.
2379 * @param cchString The string length. Pass RTSTR_MAX if you don't know the
2380 * length and wish to match up to the string terminator.
2381 */
2382RTDECL(bool) RTStrSimplePatternNMatch(const char *pszPattern, size_t cchPattern,
2383 const char *pszString, size_t cchString);
2384
2385/**
2386 * Matches multiple patterns against a string.
2387 *
2388 * The patterns are separated by the pipe character (|).
2389 *
2390 * @returns true if the string matches the pattern, otherwise false.
2391 *
2392 * @param pszPatterns The patterns.
2393 * @param cchPatterns The lengths of the patterns to use. Pass RTSTR_MAX to
2394 * stop at the terminator.
2395 * @param pszString The string to match against the pattern.
2396 * @param cchString The string length. Pass RTSTR_MAX stop stop at the
2397 * terminator.
2398 * @param poffPattern Offset into the patterns string of the patttern that
2399 * matched. If no match, this will be set to RTSTR_MAX.
2400 * This is optional, NULL is fine.
2401 */
2402RTDECL(bool) RTStrSimplePatternMultiMatch(const char *pszPatterns, size_t cchPatterns,
2403 const char *pszString, size_t cchString,
2404 size_t *poffPattern);
2405
2406/**
2407 * Compares two version strings RTStrICmp fashion.
2408 *
2409 * The version string is split up into sections at punctuation, spaces,
2410 * underscores, dashes and plus signs. The sections are then split up into
2411 * numeric and string sub-sections. Finally, the sub-sections are compared
2412 * in a numeric or case insesntivie fashion depending on what they are.
2413 *
2414 * The following strings are considered to be equal: "1.0.0", "1.00.0", "1.0",
2415 * "1". These aren't: "1.0.0r993", "1.0", "1.0r993", "1.0_Beta3", "1.1"
2416 *
2417 * @returns < 0 if the first string less than the second string.
2418 * @returns 0 if the first string identical to the second string.
2419 * @returns > 0 if the first string greater than the second string.
2420 *
2421 * @param pszVer1 First version string to compare.
2422 * @param pszVer2 Second version string to compare first version with.
2423 */
2424RTDECL(int) RTStrVersionCompare(const char *pszVer1, const char *pszVer2);
2425
2426
2427/** @defgroup rt_str_conv String To/From Number Conversions
2428 * @{ */
2429
2430/**
2431 * Converts a string representation of a number to a 64-bit unsigned number.
2432 *
2433 * @returns iprt status code.
2434 * Warnings are used to indicate conversion problems.
2435 * @retval VWRN_NUMBER_TOO_BIG
2436 * @retval VWRN_NEGATIVE_UNSIGNED
2437 * @retval VWRN_TRAILING_CHARS
2438 * @retval VWRN_TRAILING_SPACES
2439 * @retval VINF_SUCCESS
2440 * @retval VERR_NO_DIGITS
2441 *
2442 * @param pszValue Pointer to the string value.
2443 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2444 * @param uBase The base of the representation used.
2445 * If 0 the function will look for known prefixes before defaulting to 10.
2446 * @param pu64 Where to store the converted number. (optional)
2447 */
2448RTDECL(int) RTStrToUInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint64_t *pu64);
2449
2450/**
2451 * Converts a string representation of a number to a 64-bit unsigned number,
2452 * making sure the full string is converted.
2453 *
2454 * @returns iprt status code.
2455 * Warnings are used to indicate conversion problems.
2456 * @retval VWRN_NUMBER_TOO_BIG
2457 * @retval VWRN_NEGATIVE_UNSIGNED
2458 * @retval VINF_SUCCESS
2459 * @retval VERR_NO_DIGITS
2460 * @retval VERR_TRAILING_SPACES
2461 * @retval VERR_TRAILING_CHARS
2462 *
2463 * @param pszValue Pointer to the string value.
2464 * @param uBase The base of the representation used.
2465 * If 0 the function will look for known prefixes before defaulting to 10.
2466 * @param pu64 Where to store the converted number. (optional)
2467 */
2468RTDECL(int) RTStrToUInt64Full(const char *pszValue, unsigned uBase, uint64_t *pu64);
2469
2470/**
2471 * Converts a string representation of a number to a 64-bit unsigned number.
2472 * The base is guessed.
2473 *
2474 * @returns 64-bit unsigned number on success.
2475 * @returns 0 on failure.
2476 * @param pszValue Pointer to the string value.
2477 */
2478RTDECL(uint64_t) RTStrToUInt64(const char *pszValue);
2479
2480/**
2481 * Converts a string representation of a number to a 32-bit unsigned number.
2482 *
2483 * @returns iprt status code.
2484 * Warnings are used to indicate conversion problems.
2485 * @retval VWRN_NUMBER_TOO_BIG
2486 * @retval VWRN_NEGATIVE_UNSIGNED
2487 * @retval VWRN_TRAILING_CHARS
2488 * @retval VWRN_TRAILING_SPACES
2489 * @retval VINF_SUCCESS
2490 * @retval VERR_NO_DIGITS
2491 *
2492 * @param pszValue Pointer to the string value.
2493 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2494 * @param uBase The base of the representation used.
2495 * If 0 the function will look for known prefixes before defaulting to 10.
2496 * @param pu32 Where to store the converted number. (optional)
2497 */
2498RTDECL(int) RTStrToUInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint32_t *pu32);
2499
2500/**
2501 * Converts a string representation of a number to a 32-bit unsigned number,
2502 * making sure the full string is converted.
2503 *
2504 * @returns iprt status code.
2505 * Warnings are used to indicate conversion problems.
2506 * @retval VWRN_NUMBER_TOO_BIG
2507 * @retval VWRN_NEGATIVE_UNSIGNED
2508 * @retval VINF_SUCCESS
2509 * @retval VERR_NO_DIGITS
2510 * @retval VERR_TRAILING_SPACES
2511 * @retval VERR_TRAILING_CHARS
2512 *
2513 * @param pszValue Pointer to the string value.
2514 * @param uBase The base of the representation used.
2515 * If 0 the function will look for known prefixes before defaulting to 10.
2516 * @param pu32 Where to store the converted number. (optional)
2517 */
2518RTDECL(int) RTStrToUInt32Full(const char *pszValue, unsigned uBase, uint32_t *pu32);
2519
2520/**
2521 * Converts a string representation of a number to a 64-bit unsigned number.
2522 * The base is guessed.
2523 *
2524 * @returns 32-bit unsigned number on success.
2525 * @returns 0 on failure.
2526 * @param pszValue Pointer to the string value.
2527 */
2528RTDECL(uint32_t) RTStrToUInt32(const char *pszValue);
2529
2530/**
2531 * Converts a string representation of a number to a 16-bit unsigned number.
2532 *
2533 * @returns iprt status code.
2534 * Warnings are used to indicate conversion problems.
2535 * @retval VWRN_NUMBER_TOO_BIG
2536 * @retval VWRN_NEGATIVE_UNSIGNED
2537 * @retval VWRN_TRAILING_CHARS
2538 * @retval VWRN_TRAILING_SPACES
2539 * @retval VINF_SUCCESS
2540 * @retval VERR_NO_DIGITS
2541 *
2542 * @param pszValue Pointer to the string value.
2543 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2544 * @param uBase The base of the representation used.
2545 * If 0 the function will look for known prefixes before defaulting to 10.
2546 * @param pu16 Where to store the converted number. (optional)
2547 */
2548RTDECL(int) RTStrToUInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint16_t *pu16);
2549
2550/**
2551 * Converts a string representation of a number to a 16-bit unsigned number,
2552 * making sure the full string is converted.
2553 *
2554 * @returns iprt status code.
2555 * Warnings are used to indicate conversion problems.
2556 * @retval VWRN_NUMBER_TOO_BIG
2557 * @retval VWRN_NEGATIVE_UNSIGNED
2558 * @retval VINF_SUCCESS
2559 * @retval VERR_NO_DIGITS
2560 * @retval VERR_TRAILING_SPACES
2561 * @retval VERR_TRAILING_CHARS
2562 *
2563 * @param pszValue Pointer to the string value.
2564 * @param uBase The base of the representation used.
2565 * If 0 the function will look for known prefixes before defaulting to 10.
2566 * @param pu16 Where to store the converted number. (optional)
2567 */
2568RTDECL(int) RTStrToUInt16Full(const char *pszValue, unsigned uBase, uint16_t *pu16);
2569
2570/**
2571 * Converts a string representation of a number to a 16-bit unsigned number.
2572 * The base is guessed.
2573 *
2574 * @returns 16-bit unsigned number on success.
2575 * @returns 0 on failure.
2576 * @param pszValue Pointer to the string value.
2577 */
2578RTDECL(uint16_t) RTStrToUInt16(const char *pszValue);
2579
2580/**
2581 * Converts a string representation of a number to a 8-bit unsigned number.
2582 *
2583 * @returns iprt status code.
2584 * Warnings are used to indicate conversion problems.
2585 * @retval VWRN_NUMBER_TOO_BIG
2586 * @retval VWRN_NEGATIVE_UNSIGNED
2587 * @retval VWRN_TRAILING_CHARS
2588 * @retval VWRN_TRAILING_SPACES
2589 * @retval VINF_SUCCESS
2590 * @retval VERR_NO_DIGITS
2591 *
2592 * @param pszValue Pointer to the string value.
2593 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2594 * @param uBase The base of the representation used.
2595 * If 0 the function will look for known prefixes before defaulting to 10.
2596 * @param pu8 Where to store the converted number. (optional)
2597 */
2598RTDECL(int) RTStrToUInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint8_t *pu8);
2599
2600/**
2601 * Converts a string representation of a number to a 8-bit unsigned number,
2602 * making sure the full string is converted.
2603 *
2604 * @returns iprt status code.
2605 * Warnings are used to indicate conversion problems.
2606 * @retval VWRN_NUMBER_TOO_BIG
2607 * @retval VWRN_NEGATIVE_UNSIGNED
2608 * @retval VINF_SUCCESS
2609 * @retval VERR_NO_DIGITS
2610 * @retval VERR_TRAILING_SPACES
2611 * @retval VERR_TRAILING_CHARS
2612 *
2613 * @param pszValue Pointer to the string value.
2614 * @param uBase The base of the representation used.
2615 * If 0 the function will look for known prefixes before defaulting to 10.
2616 * @param pu8 Where to store the converted number. (optional)
2617 */
2618RTDECL(int) RTStrToUInt8Full(const char *pszValue, unsigned uBase, uint8_t *pu8);
2619
2620/**
2621 * Converts a string representation of a number to a 8-bit unsigned number.
2622 * The base is guessed.
2623 *
2624 * @returns 8-bit unsigned number on success.
2625 * @returns 0 on failure.
2626 * @param pszValue Pointer to the string value.
2627 */
2628RTDECL(uint8_t) RTStrToUInt8(const char *pszValue);
2629
2630/**
2631 * Converts a string representation of a number to a 64-bit signed number.
2632 *
2633 * @returns iprt status code.
2634 * Warnings are used to indicate conversion problems.
2635 * @retval VWRN_NUMBER_TOO_BIG
2636 * @retval VWRN_TRAILING_CHARS
2637 * @retval VWRN_TRAILING_SPACES
2638 * @retval VINF_SUCCESS
2639 * @retval VERR_NO_DIGITS
2640 *
2641 * @param pszValue Pointer to the string value.
2642 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2643 * @param uBase The base of the representation used.
2644 * If 0 the function will look for known prefixes before defaulting to 10.
2645 * @param pi64 Where to store the converted number. (optional)
2646 */
2647RTDECL(int) RTStrToInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, int64_t *pi64);
2648
2649/**
2650 * Converts a string representation of a number to a 64-bit signed number,
2651 * making sure the full string is converted.
2652 *
2653 * @returns iprt status code.
2654 * Warnings are used to indicate conversion problems.
2655 * @retval VWRN_NUMBER_TOO_BIG
2656 * @retval VINF_SUCCESS
2657 * @retval VERR_TRAILING_CHARS
2658 * @retval VERR_TRAILING_SPACES
2659 * @retval VERR_NO_DIGITS
2660 *
2661 * @param pszValue Pointer to the string value.
2662 * @param uBase The base of the representation used.
2663 * If 0 the function will look for known prefixes before defaulting to 10.
2664 * @param pi64 Where to store the converted number. (optional)
2665 */
2666RTDECL(int) RTStrToInt64Full(const char *pszValue, unsigned uBase, int64_t *pi64);
2667
2668/**
2669 * Converts a string representation of a number to a 64-bit signed number.
2670 * The base is guessed.
2671 *
2672 * @returns 64-bit signed number on success.
2673 * @returns 0 on failure.
2674 * @param pszValue Pointer to the string value.
2675 */
2676RTDECL(int64_t) RTStrToInt64(const char *pszValue);
2677
2678/**
2679 * Converts a string representation of a number to a 32-bit signed number.
2680 *
2681 * @returns iprt status code.
2682 * Warnings are used to indicate conversion problems.
2683 * @retval VWRN_NUMBER_TOO_BIG
2684 * @retval VWRN_TRAILING_CHARS
2685 * @retval VWRN_TRAILING_SPACES
2686 * @retval VINF_SUCCESS
2687 * @retval VERR_NO_DIGITS
2688 *
2689 * @param pszValue Pointer to the string value.
2690 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2691 * @param uBase The base of the representation used.
2692 * If 0 the function will look for known prefixes before defaulting to 10.
2693 * @param pi32 Where to store the converted number. (optional)
2694 */
2695RTDECL(int) RTStrToInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, int32_t *pi32);
2696
2697/**
2698 * Converts a string representation of a number to a 32-bit signed number,
2699 * making sure the full string is converted.
2700 *
2701 * @returns iprt status code.
2702 * Warnings are used to indicate conversion problems.
2703 * @retval VWRN_NUMBER_TOO_BIG
2704 * @retval VINF_SUCCESS
2705 * @retval VERR_TRAILING_CHARS
2706 * @retval VERR_TRAILING_SPACES
2707 * @retval VERR_NO_DIGITS
2708 *
2709 * @param pszValue Pointer to the string value.
2710 * @param uBase The base of the representation used.
2711 * If 0 the function will look for known prefixes before defaulting to 10.
2712 * @param pi32 Where to store the converted number. (optional)
2713 */
2714RTDECL(int) RTStrToInt32Full(const char *pszValue, unsigned uBase, int32_t *pi32);
2715
2716/**
2717 * Converts a string representation of a number to a 32-bit signed number.
2718 * The base is guessed.
2719 *
2720 * @returns 32-bit signed number on success.
2721 * @returns 0 on failure.
2722 * @param pszValue Pointer to the string value.
2723 */
2724RTDECL(int32_t) RTStrToInt32(const char *pszValue);
2725
2726/**
2727 * Converts a string representation of a number to a 16-bit signed number.
2728 *
2729 * @returns iprt status code.
2730 * Warnings are used to indicate conversion problems.
2731 * @retval VWRN_NUMBER_TOO_BIG
2732 * @retval VWRN_TRAILING_CHARS
2733 * @retval VWRN_TRAILING_SPACES
2734 * @retval VINF_SUCCESS
2735 * @retval VERR_NO_DIGITS
2736 *
2737 * @param pszValue Pointer to the string value.
2738 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2739 * @param uBase The base of the representation used.
2740 * If 0 the function will look for known prefixes before defaulting to 10.
2741 * @param pi16 Where to store the converted number. (optional)
2742 */
2743RTDECL(int) RTStrToInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, int16_t *pi16);
2744
2745/**
2746 * Converts a string representation of a number to a 16-bit signed number,
2747 * making sure the full string is converted.
2748 *
2749 * @returns iprt status code.
2750 * Warnings are used to indicate conversion problems.
2751 * @retval VWRN_NUMBER_TOO_BIG
2752 * @retval VINF_SUCCESS
2753 * @retval VERR_TRAILING_CHARS
2754 * @retval VERR_TRAILING_SPACES
2755 * @retval VERR_NO_DIGITS
2756 *
2757 * @param pszValue Pointer to the string value.
2758 * @param uBase The base of the representation used.
2759 * If 0 the function will look for known prefixes before defaulting to 10.
2760 * @param pi16 Where to store the converted number. (optional)
2761 */
2762RTDECL(int) RTStrToInt16Full(const char *pszValue, unsigned uBase, int16_t *pi16);
2763
2764/**
2765 * Converts a string representation of a number to a 16-bit signed number.
2766 * The base is guessed.
2767 *
2768 * @returns 16-bit signed number on success.
2769 * @returns 0 on failure.
2770 * @param pszValue Pointer to the string value.
2771 */
2772RTDECL(int16_t) RTStrToInt16(const char *pszValue);
2773
2774/**
2775 * Converts a string representation of a number to a 8-bit signed number.
2776 *
2777 * @returns iprt status code.
2778 * Warnings are used to indicate conversion problems.
2779 * @retval VWRN_NUMBER_TOO_BIG
2780 * @retval VWRN_TRAILING_CHARS
2781 * @retval VWRN_TRAILING_SPACES
2782 * @retval VINF_SUCCESS
2783 * @retval VERR_NO_DIGITS
2784 *
2785 * @param pszValue Pointer to the string value.
2786 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2787 * @param uBase The base of the representation used.
2788 * If 0 the function will look for known prefixes before defaulting to 10.
2789 * @param pi8 Where to store the converted number. (optional)
2790 */
2791RTDECL(int) RTStrToInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, int8_t *pi8);
2792
2793/**
2794 * Converts a string representation of a number to a 8-bit signed number,
2795 * making sure the full string is converted.
2796 *
2797 * @returns iprt status code.
2798 * Warnings are used to indicate conversion problems.
2799 * @retval VWRN_NUMBER_TOO_BIG
2800 * @retval VINF_SUCCESS
2801 * @retval VERR_TRAILING_CHARS
2802 * @retval VERR_TRAILING_SPACES
2803 * @retval VERR_NO_DIGITS
2804 *
2805 * @param pszValue Pointer to the string value.
2806 * @param uBase The base of the representation used.
2807 * If 0 the function will look for known prefixes before defaulting to 10.
2808 * @param pi8 Where to store the converted number. (optional)
2809 */
2810RTDECL(int) RTStrToInt8Full(const char *pszValue, unsigned uBase, int8_t *pi8);
2811
2812/**
2813 * Converts a string representation of a number to a 8-bit signed number.
2814 * The base is guessed.
2815 *
2816 * @returns 8-bit signed number on success.
2817 * @returns 0 on failure.
2818 * @param pszValue Pointer to the string value.
2819 */
2820RTDECL(int8_t) RTStrToInt8(const char *pszValue);
2821
2822/**
2823 * Formats a buffer stream as hex bytes.
2824 *
2825 * The default is no separating spaces or line breaks or anything.
2826 *
2827 * @returns IPRT status code.
2828 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
2829 * @retval VERR_BUFFER_OVERFLOW if the buffer is insufficent to hold the bytes.
2830 *
2831 * @param pszBuf Output string buffer.
2832 * @param cbBuf The size of the output buffer.
2833 * @param pv Pointer to the bytes to stringify.
2834 * @param cb The number of bytes to stringify.
2835 * @param fFlags Combination of RTSTRPRINTHEXBYTES_F_XXX values.
2836 * @sa RTUtf16PrintHexBytes.
2837 */
2838RTDECL(int) RTStrPrintHexBytes(char *pszBuf, size_t cbBuf, void const *pv, size_t cb, uint32_t fFlags);
2839/** @name RTSTRPRINTHEXBYTES_F_XXX - flags for RTStrPrintHexBytes and RTUtf16PritnHexBytes.
2840 * @{ */
2841/** Upper case hex digits, the default is lower case. */
2842#define RTSTRPRINTHEXBYTES_F_UPPER RT_BIT(0)
2843/** Add a space between each group. */
2844#define RTSTRPRINTHEXBYTES_F_SEP_SPACE RT_BIT(1)
2845/** Add a colon between each group. */
2846#define RTSTRPRINTHEXBYTES_F_SEP_COLON RT_BIT(2)
2847/** @} */
2848
2849/**
2850 * Converts a string of hex bytes back into binary data.
2851 *
2852 * @returns IPRT status code.
2853 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
2854 * @retval VERR_BUFFER_OVERFLOW if the string contains too many hex bytes.
2855 * @retval VERR_BUFFER_UNDERFLOW if there aren't enough hex bytes to fill up
2856 * the output buffer.
2857 * @retval VERR_UNEVEN_INPUT if the input contains a half byte.
2858 * @retval VERR_NO_DIGITS
2859 * @retval VWRN_TRAILING_CHARS
2860 * @retval VWRN_TRAILING_SPACES
2861 *
2862 * @param pszHex The string containing the hex bytes.
2863 * @param pv Output buffer.
2864 * @param cb The size of the output buffer.
2865 * @param fFlags Must be zero, reserved for future use.
2866 */
2867RTDECL(int) RTStrConvertHexBytes(char const *pszHex, void *pv, size_t cb, uint32_t fFlags);
2868
2869/** @} */
2870
2871
2872/** @defgroup rt_str_space Unique String Space
2873 * @{
2874 */
2875
2876/** Pointer to a string name space container node core. */
2877typedef struct RTSTRSPACECORE *PRTSTRSPACECORE;
2878/** Pointer to a pointer to a string name space container node core. */
2879typedef PRTSTRSPACECORE *PPRTSTRSPACECORE;
2880
2881/**
2882 * String name space container node core.
2883 */
2884typedef struct RTSTRSPACECORE
2885{
2886 /** Hash key. Don't touch. */
2887 uint32_t Key;
2888 /** Pointer to the left leaf node. Don't touch. */
2889 PRTSTRSPACECORE pLeft;
2890 /** Pointer to the left right node. Don't touch. */
2891 PRTSTRSPACECORE pRight;
2892 /** Pointer to the list of string with the same key. Don't touch. */
2893 PRTSTRSPACECORE pList;
2894 /** Height of this tree: max(heigth(left), heigth(right)) + 1. Don't touch */
2895 unsigned char uchHeight;
2896 /** The string length. Read only! */
2897 size_t cchString;
2898 /** Pointer to the string. Read only! */
2899 const char *pszString;
2900} RTSTRSPACECORE;
2901
2902/** String space. (Initialize with NULL.) */
2903typedef PRTSTRSPACECORE RTSTRSPACE;
2904/** Pointer to a string space. */
2905typedef PPRTSTRSPACECORE PRTSTRSPACE;
2906
2907
2908/**
2909 * Inserts a string into a unique string space.
2910 *
2911 * @returns true on success.
2912 * @returns false if the string collided with an existing string.
2913 * @param pStrSpace The space to insert it into.
2914 * @param pStr The string node.
2915 */
2916RTDECL(bool) RTStrSpaceInsert(PRTSTRSPACE pStrSpace, PRTSTRSPACECORE pStr);
2917
2918/**
2919 * Removes a string from a unique string space.
2920 *
2921 * @returns Pointer to the removed string node.
2922 * @returns NULL if the string was not found in the string space.
2923 * @param pStrSpace The space to remove it from.
2924 * @param pszString The string to remove.
2925 */
2926RTDECL(PRTSTRSPACECORE) RTStrSpaceRemove(PRTSTRSPACE pStrSpace, const char *pszString);
2927
2928/**
2929 * Gets a string from a unique string space.
2930 *
2931 * @returns Pointer to the string node.
2932 * @returns NULL if the string was not found in the string space.
2933 * @param pStrSpace The space to get it from.
2934 * @param pszString The string to get.
2935 */
2936RTDECL(PRTSTRSPACECORE) RTStrSpaceGet(PRTSTRSPACE pStrSpace, const char *pszString);
2937
2938/**
2939 * Gets a string from a unique string space.
2940 *
2941 * @returns Pointer to the string node.
2942 * @returns NULL if the string was not found in the string space.
2943 * @param pStrSpace The space to get it from.
2944 * @param pszString The string to get.
2945 * @param cchMax The max string length to evaluate. Passing
2946 * RTSTR_MAX is ok and makes it behave just like
2947 * RTStrSpaceGet.
2948 */
2949RTDECL(PRTSTRSPACECORE) RTStrSpaceGetN(PRTSTRSPACE pStrSpace, const char *pszString, size_t cchMax);
2950
2951/**
2952 * Callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy().
2953 *
2954 * @returns 0 on continue.
2955 * @returns Non-zero to aborts the operation.
2956 * @param pStr The string node
2957 * @param pvUser The user specified argument.
2958 */
2959typedef DECLCALLBACK(int) FNRTSTRSPACECALLBACK(PRTSTRSPACECORE pStr, void *pvUser);
2960/** Pointer to callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy(). */
2961typedef FNRTSTRSPACECALLBACK *PFNRTSTRSPACECALLBACK;
2962
2963/**
2964 * Destroys the string space.
2965 *
2966 * The caller supplies a callback which will be called for each of the string
2967 * nodes in for freeing their memory and other resources.
2968 *
2969 * @returns 0 or what ever non-zero return value pfnCallback returned
2970 * when aborting the destruction.
2971 * @param pStrSpace The space to destroy.
2972 * @param pfnCallback The callback.
2973 * @param pvUser The user argument.
2974 */
2975RTDECL(int) RTStrSpaceDestroy(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
2976
2977/**
2978 * Enumerates the string space.
2979 * The caller supplies a callback which will be called for each of
2980 * the string nodes.
2981 *
2982 * @returns 0 or what ever non-zero return value pfnCallback returned
2983 * when aborting the destruction.
2984 * @param pStrSpace The space to enumerate.
2985 * @param pfnCallback The callback.
2986 * @param pvUser The user argument.
2987 */
2988RTDECL(int) RTStrSpaceEnumerate(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
2989
2990/** @} */
2991
2992
2993/** @defgroup rt_str_hash Sting hashing
2994 * @{ */
2995
2996/**
2997 * Hashes the given string using algorithm \#1.
2998 *
2999 * @returns String hash.
3000 * @param pszString The string to hash.
3001 */
3002RTDECL(uint32_t) RTStrHash1(const char *pszString);
3003
3004/**
3005 * Hashes the given string using algorithm \#1.
3006 *
3007 * @returns String hash.
3008 * @param pszString The string to hash.
3009 * @param cchString The max length to hash. Hashing will stop if the
3010 * terminator character is encountered first. Passing
3011 * RTSTR_MAX is fine.
3012 */
3013RTDECL(uint32_t) RTStrHash1N(const char *pszString, size_t cchString);
3014
3015/**
3016 * Hashes the given strings as if they were concatenated using algorithm \#1.
3017 *
3018 * @returns String hash.
3019 * @param cPairs The number of string / length pairs in the
3020 * ellipsis.
3021 * @param ... List of string (const char *) and length
3022 * (size_t) pairs. Passing RTSTR_MAX as the size is
3023 * fine.
3024 */
3025RTDECL(uint32_t) RTStrHash1ExN(size_t cPairs, ...);
3026
3027/**
3028 * Hashes the given strings as if they were concatenated using algorithm \#1.
3029 *
3030 * @returns String hash.
3031 * @param cPairs The number of string / length pairs in the @a va.
3032 * @param va List of string (const char *) and length
3033 * (size_t) pairs. Passing RTSTR_MAX as the size is
3034 * fine.
3035 */
3036RTDECL(uint32_t) RTStrHash1ExNV(size_t cPairs, va_list va);
3037
3038/** @} */
3039
3040/** @} */
3041
3042RT_C_DECLS_END
3043
3044#endif
3045
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