VirtualBox

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

Last change on this file since 40091 was 40091, checked in by vboxsync, 13 years ago

Runtime/strings: add Utf-8 and Utf-16 sanitising to a white list of characters. Do not validate the string encoding in advance.

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

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