VirtualBox

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

最後變更 在這個檔案從37871是 37871,由 vboxsync 提交於 14 年 前

iprt/string.h: build fix for some FreeBSD versions which declare strlen incorrectly

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

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