VirtualBox

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

Last change on this file since 34066 was 33678, checked in by vboxsync, 14 years ago

IPRT: Added RTStrCat and RTStrCatEx.

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

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