VirtualBox

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

Last change on this file since 34435 was 34379, checked in by vboxsync, 14 years ago

doc nit

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 144.6 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 current
1445 * codeset).
1446 * - \%LS - Same as \%S except that the input is UCS-32 (output current
1447 * codeset).
1448 * - \%c - Takes a char and prints it.
1449 * - \%d - Takes a signed integer and prints it as decimal. Thousand
1450 * separator (\'), zero padding (0), adjustment (-+), width,
1451 * precision
1452 * - \%i - Same as \%d.
1453 * - \%u - Takes an unsigned integer and prints it as decimal. Thousand
1454 * separator (\'), zero padding (0), adjustment (-+), width,
1455 * precision
1456 * - \%x - Takes an unsigned integer and prints it as lowercased
1457 * hexadecimal. The special hash (\#) flag causes a '0x'
1458 * prefixed to be printed. Zero padding (0), adjustment (-+),
1459 * width, precision.
1460 * - \%X - Same as \%x except that it is uppercased.
1461 * - \%o - Takes an unsigned (?) integer and prints it as octal. Zero
1462 * padding (0), adjustment (-+), width, precision.
1463 * - \%p - Takes a pointer (void technically) and prints it. Zero
1464 * padding (0), adjustment (-+), width, precision.
1465 *
1466 * The \%d, \%i, \%u, \%x, \%X and \%o format types support the following
1467 * argument type specifiers:
1468 * - \%ll - long long (uint64_t).
1469 * - \%L - long long (uint64_t).
1470 * - \%l - long (uint32_t, uint64_t)
1471 * - \%h - short (int16_t).
1472 * - \%hh - char (int8_t).
1473 * - \%H - char (int8_t).
1474 * - \%z - size_t.
1475 * - \%j - intmax_t (int64_t).
1476 * - \%t - ptrdiff_t.
1477 * The type in parentheses is typical sizes, however when printing those types
1478 * you are better off using the special group 2 format types below (\%RX32 and
1479 * such).
1480 *
1481 *
1482 * Group 0b, IPRT format tricks:
1483 * - %M - Replaces the format string, takes a string pointer.
1484 * - %N - Nested formatting, takes a pointer to a format string
1485 * followed by the pointer to a va_list variable. The va_list
1486 * variable will not be modified and the caller must do va_end()
1487 * on it. Make sure the va_list variable is NOT in a parameter
1488 * list or some gcc versions/targets may get it all wrong.
1489 *
1490 *
1491 * Group 1, the basic runtime typedefs (excluding those which obviously are
1492 * pointer):
1493 * - \%RTbool - Takes a bool value and prints 'true', 'false', or '!%d!'.
1494 * - \%RTfile - Takes a #RTFILE value.
1495 * - \%RTfmode - Takes a #RTFMODE value.
1496 * - \%RTfoff - Takes a #RTFOFF value.
1497 * - \%RTfp16 - Takes a #RTFAR16 value.
1498 * - \%RTfp32 - Takes a #RTFAR32 value.
1499 * - \%RTfp64 - Takes a #RTFAR64 value.
1500 * - \%RTgid - Takes a #RTGID value.
1501 * - \%RTino - Takes a #RTINODE value.
1502 * - \%RTint - Takes a #RTINT value.
1503 * - \%RTiop - Takes a #RTIOPORT value.
1504 * - \%RTldrm - Takes a #RTLDRMOD value.
1505 * - \%RTmac - Takes a #PCRTMAC pointer.
1506 * - \%RTnaddr - Takes a #PCRTNETADDR value.
1507 * - \%RTnaipv4 - Takes a #RTNETADDRIPV4 value.
1508 * - \%RTnaipv6 - Takes a #PCRTNETADDRIPV6 value.
1509 * - \%RTnthrd - Takes a #RTNATIVETHREAD value.
1510 * - \%RTnthrd - Takes a #RTNATIVETHREAD value.
1511 * - \%RTproc - Takes a #RTPROCESS value.
1512 * - \%RTptr - Takes a #RTINTPTR or #RTUINTPTR value (but not void *).
1513 * - \%RTreg - Takes a #RTCCUINTREG value.
1514 * - \%RTsel - Takes a #RTSEL value.
1515 * - \%RTsem - Takes a #RTSEMEVENT, #RTSEMEVENTMULTI, #RTSEMMUTEX, #RTSEMFASTMUTEX, or #RTSEMRW value.
1516 * - \%RTsock - Takes a #RTSOCKET value.
1517 * - \%RTthrd - Takes a #RTTHREAD value.
1518 * - \%RTuid - Takes a #RTUID value.
1519 * - \%RTuint - Takes a #RTUINT value.
1520 * - \%RTunicp - Takes a #RTUNICP value.
1521 * - \%RTutf16 - Takes a #RTUTF16 value.
1522 * - \%RTuuid - Takes a #PCRTUUID and will print the UUID as a string.
1523 * - \%RTxuint - Takes a #RTUINT or #RTINT value, formatting it as hex.
1524 * - \%RGi - Takes a #RTGCINT value.
1525 * - \%RGp - Takes a #RTGCPHYS value.
1526 * - \%RGr - Takes a #RTGCUINTREG value.
1527 * - \%RGu - Takes a #RTGCUINT value.
1528 * - \%RGv - Takes a #RTGCPTR, #RTGCINTPTR or #RTGCUINTPTR value.
1529 * - \%RGx - Takes a #RTGCUINT or #RTGCINT value, formatting it as hex.
1530 * - \%RHi - Takes a #RTHCINT value.
1531 * - \%RHp - Takes a #RTHCPHYS value.
1532 * - \%RHr - Takes a #RTHCUINTREG value.
1533 * - \%RHu - Takes a #RTHCUINT value.
1534 * - \%RHv - Takes a #RTHCPTR, #RTHCINTPTR or #RTHCUINTPTR value.
1535 * - \%RHx - Takes a #RTHCUINT or #RTHCINT value, formatting it as hex.
1536 * - \%RRv - Takes a #RTRCPTR, #RTRCINTPTR or #RTRCUINTPTR value.
1537 * - \%RCi - Takes a #RTINT value.
1538 * - \%RCp - Takes a #RTCCPHYS value.
1539 * - \%RCr - Takes a #RTCCUINTREG value.
1540 * - \%RCu - Takes a #RTUINT value.
1541 * - \%RCv - Takes a #uintptr_t, #intptr_t, void * value.
1542 * - \%RCx - Takes a #RTUINT or #RTINT value, formatting it as hex.
1543 *
1544 *
1545 * Group 2, the generic integer types which are prefered over relying on what
1546 * bit-count a 'long', 'short', or 'long long' has on a platform. This are
1547 * highly prefered for the [u]intXX_t kind of types:
1548 * - \%RI[8|16|32|64] - Signed integer value of the specifed bit count.
1549 * - \%RU[8|16|32|64] - Unsigned integer value of the specifed bit count.
1550 * - \%RX[8|16|32|64] - Hexadecimal integer value of the specifed bit count.
1551 *
1552 *
1553 * Group 3, hex dumpers and other complex stuff which requires more than simple
1554 * formatting:
1555 * - \%Rhxd - Takes a pointer to the memory which is to be dumped in typical
1556 * hex format. Use the precision to specify the length, and the width to
1557 * set the number of bytes per line. Default width and precision is 16.
1558 * - \%Rhxs - Takes a pointer to the memory to be displayed as a hex string,
1559 * i.e. a series of space separated bytes formatted as two digit hex value.
1560 * Use the precision to specify the length. Default length is 16 bytes.
1561 * The width, if specified, is ignored.
1562 * - \%Rrc - Takes an integer iprt status code as argument. Will insert the
1563 * status code define corresponding to the iprt status code.
1564 * - \%Rrs - Takes an integer iprt status code as argument. Will insert the
1565 * short description of the specified status code.
1566 * - \%Rrf - Takes an integer iprt status code as argument. Will insert the
1567 * full description of the specified status code.
1568 * - \%Rra - Takes an integer iprt status code as argument. Will insert the
1569 * status code define + full description.
1570 * - \%Rwc - Takes a long Windows error code as argument. Will insert the status
1571 * code define corresponding to the Windows error code.
1572 * - \%Rwf - Takes a long Windows error code as argument. Will insert the
1573 * full description of the specified status code.
1574 * - \%Rwa - Takes a long Windows error code as argument. Will insert the
1575 * error code define + full description.
1576 *
1577 * - \%Rhrc - Takes a COM/XPCOM status code as argument. Will insert the status
1578 * code define corresponding to the Windows error code.
1579 * - \%Rhrf - Takes a COM/XPCOM status code as argument. Will insert the
1580 * full description of the specified status code.
1581 * - \%Rhra - Takes a COM/XPCOM error code as argument. Will insert the
1582 * error code define + full description.
1583 *
1584 * - \%Rfn - Pretty printing of a function or method. It drops the
1585 * return code and parameter list.
1586 * - \%Rbn - Prints the base name. For dropping the path in
1587 * order to save space when printing a path name.
1588 *
1589 * On other platforms, \%Rw? simply prints the argument in a form of 0xXXXXXXXX.
1590 *
1591 *
1592 * Group 4, structure dumpers:
1593 * - \%RDtimespec - Takes a PCRTTIMESPEC.
1594 *
1595 *
1596 * Group 5, XML / HTML escapers:
1597 * - \%RMas - Takes a string pointer (const char *) and outputs
1598 * it as an attribute value with the proper escaping.
1599 * This typically ends up in double quotes.
1600 *
1601 * - \%RMes - Takes a string pointer (const char *) and outputs
1602 * it as an element with the necessary escaping.
1603 *
1604 *
1605 */
1606
1607#ifndef DECLARED_FNRTSTROUTPUT /* duplicated in iprt/log.h */
1608# define DECLARED_FNRTSTROUTPUT
1609/**
1610 * Output callback.
1611 *
1612 * @returns number of bytes written.
1613 * @param pvArg User argument.
1614 * @param pachChars Pointer to an array of utf-8 characters.
1615 * @param cbChars Number of bytes in the character array pointed to by pachChars.
1616 */
1617typedef DECLCALLBACK(size_t) FNRTSTROUTPUT(void *pvArg, const char *pachChars, size_t cbChars);
1618/** Pointer to callback function. */
1619typedef FNRTSTROUTPUT *PFNRTSTROUTPUT;
1620#endif
1621
1622/** Format flag.
1623 * These are used by RTStrFormat extensions and RTStrFormatNumber, mind
1624 * that not all flags makes sense to both of the functions.
1625 * @{ */
1626#define RTSTR_F_CAPITAL 0x0001
1627#define RTSTR_F_LEFT 0x0002
1628#define RTSTR_F_ZEROPAD 0x0004
1629#define RTSTR_F_SPECIAL 0x0008
1630#define RTSTR_F_VALSIGNED 0x0010
1631#define RTSTR_F_PLUS 0x0020
1632#define RTSTR_F_BLANK 0x0040
1633#define RTSTR_F_WIDTH 0x0080
1634#define RTSTR_F_PRECISION 0x0100
1635#define RTSTR_F_THOUSAND_SEP 0x0200
1636
1637#define RTSTR_F_BIT_MASK 0xf800
1638#define RTSTR_F_8BIT 0x0800
1639#define RTSTR_F_16BIT 0x1000
1640#define RTSTR_F_32BIT 0x2000
1641#define RTSTR_F_64BIT 0x4000
1642#define RTSTR_F_128BIT 0x8000
1643/** @} */
1644
1645/** @def RTSTR_GET_BIT_FLAG
1646 * Gets the bit flag for the specified type.
1647 */
1648#define RTSTR_GET_BIT_FLAG(type) \
1649 ( sizeof(type) * 8 == 32 ? RTSTR_F_32BIT \
1650 : sizeof(type) * 8 == 64 ? RTSTR_F_64BIT \
1651 : sizeof(type) * 8 == 16 ? RTSTR_F_16BIT \
1652 : sizeof(type) * 8 == 8 ? RTSTR_F_8BIT \
1653 : sizeof(type) * 8 == 128 ? RTSTR_F_128BIT \
1654 : 0)
1655
1656
1657/**
1658 * Callback to format non-standard format specifiers.
1659 *
1660 * @returns The number of bytes formatted.
1661 * @param pvArg Formatter argument.
1662 * @param pfnOutput Pointer to output function.
1663 * @param pvArgOutput Argument for the output function.
1664 * @param ppszFormat Pointer to the format string pointer. Advance this till the char
1665 * after the format specifier.
1666 * @param pArgs Pointer to the argument list. Use this to fetch the arguments.
1667 * @param cchWidth Format Width. -1 if not specified.
1668 * @param cchPrecision Format Precision. -1 if not specified.
1669 * @param fFlags Flags (RTSTR_NTFS_*).
1670 * @param chArgSize The argument size specifier, 'l' or 'L'.
1671 */
1672typedef DECLCALLBACK(size_t) FNSTRFORMAT(void *pvArg, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
1673 const char **ppszFormat, va_list *pArgs, int cchWidth,
1674 int cchPrecision, unsigned fFlags, char chArgSize);
1675/** Pointer to a FNSTRFORMAT() function. */
1676typedef FNSTRFORMAT *PFNSTRFORMAT;
1677
1678
1679/**
1680 * Partial implementation of a printf like formatter.
1681 * It doesn't do everything correct, and there is no floating point support.
1682 * However, it supports custom formats by the means of a format callback.
1683 *
1684 * @returns number of bytes formatted.
1685 * @param pfnOutput Output worker.
1686 * Called in two ways. Normally with a string and its length.
1687 * For termination, it's called with NULL for string, 0 for length.
1688 * @param pvArgOutput Argument to the output worker.
1689 * @param pfnFormat Custom format worker.
1690 * @param pvArgFormat Argument to the format worker.
1691 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1692 * @param InArgs Argument list.
1693 */
1694RTDECL(size_t) RTStrFormatV(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat, const char *pszFormat, va_list InArgs);
1695
1696/**
1697 * Partial implementation of a printf like formatter.
1698 * It doesn't do everything correct, and there is no floating point support.
1699 * However, it supports custom formats by the means of a format callback.
1700 *
1701 * @returns number of bytes formatted.
1702 * @param pfnOutput Output worker.
1703 * Called in two ways. Normally with a string and its length.
1704 * For termination, it's called with NULL for string, 0 for length.
1705 * @param pvArgOutput Argument to the output worker.
1706 * @param pfnFormat Custom format worker.
1707 * @param pvArgFormat Argument to the format worker.
1708 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1709 * @param ... Argument list.
1710 */
1711RTDECL(size_t) RTStrFormat(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat, const char *pszFormat, ...);
1712
1713/**
1714 * Formats an integer number according to the parameters.
1715 *
1716 * @returns Length of the formatted number.
1717 * @param psz Pointer to output string buffer of sufficient size.
1718 * @param u64Value Value to format.
1719 * @param uiBase Number representation base.
1720 * @param cchWidth Width.
1721 * @param cchPrecision Precision.
1722 * @param fFlags Flags (NTFS_*).
1723 */
1724RTDECL(int) RTStrFormatNumber(char *psz, uint64_t u64Value, unsigned int uiBase, signed int cchWidth, signed int cchPrecision, unsigned int fFlags);
1725
1726
1727/**
1728 * Callback for formatting a type.
1729 *
1730 * This is registered using the RTStrFormatTypeRegister function and will
1731 * be called during string formatting to handle the specified %R[type].
1732 * The argument for this format type is assumed to be a pointer and it's
1733 * passed in the @a pvValue argument.
1734 *
1735 * @returns Length of the formatted output.
1736 * @param pfnOutput Output worker.
1737 * @param pvArgOutput Argument to the output worker.
1738 * @param pszType The type name.
1739 * @param pvValue The argument value.
1740 * @param cchWidth Width.
1741 * @param cchPrecision Precision.
1742 * @param fFlags Flags (NTFS_*).
1743 * @param pvUser The user argument.
1744 */
1745typedef DECLCALLBACK(size_t) FNRTSTRFORMATTYPE(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
1746 const char *pszType, void const *pvValue,
1747 int cchWidth, int cchPrecision, unsigned fFlags,
1748 void *pvUser);
1749/** Pointer to a FNRTSTRFORMATTYPE. */
1750typedef FNRTSTRFORMATTYPE *PFNRTSTRFORMATTYPE;
1751
1752
1753/**
1754 * Register a format handler for a type.
1755 *
1756 * The format handler is used to handle '%R[type]' format types, where the argument
1757 * in the vector is a pointer value (a bit restrictive, but keeps it simple).
1758 *
1759 * The caller must ensure that no other thread will be making use of any of
1760 * the dynamic formatting type facilities simultaneously with this call.
1761 *
1762 * @returns IPRT status code.
1763 * @retval VINF_SUCCESS on success.
1764 * @retval VERR_ALREADY_EXISTS if the type has already been registered.
1765 * @retval VERR_TOO_MANY_OPEN_FILES if all the type slots has been allocated already.
1766 *
1767 * @param pszType The type name.
1768 * @param pfnHandler The handler address. See FNRTSTRFORMATTYPE for details.
1769 * @param pvUser The user argument to pass to the handler. See RTStrFormatTypeSetUser
1770 * for how to update this later.
1771 */
1772RTDECL(int) RTStrFormatTypeRegister(const char *pszType, PFNRTSTRFORMATTYPE pfnHandler, void *pvUser);
1773
1774/**
1775 * Deregisters a format type.
1776 *
1777 * The caller must ensure that no other thread will be making use of any of
1778 * the dynamic formatting type facilities simultaneously with this call.
1779 *
1780 * @returns IPRT status code.
1781 * @retval VINF_SUCCESS on success.
1782 * @retval VERR_FILE_NOT_FOUND if not found.
1783 *
1784 * @param pszType The type to deregister.
1785 */
1786RTDECL(int) RTStrFormatTypeDeregister(const char *pszType);
1787
1788/**
1789 * Sets the user argument for a type.
1790 *
1791 * This can be used if a user argument needs relocating in GC.
1792 *
1793 * @returns IPRT status code.
1794 * @retval VINF_SUCCESS on success.
1795 * @retval VERR_FILE_NOT_FOUND if not found.
1796 *
1797 * @param pszType The type to update.
1798 * @param pvUser The new user argument value.
1799 */
1800RTDECL(int) RTStrFormatTypeSetUser(const char *pszType, void *pvUser);
1801
1802
1803/**
1804 * String printf.
1805 *
1806 * @returns The length of the returned string (in pszBuffer) excluding the
1807 * terminator.
1808 * @param pszBuffer Output buffer.
1809 * @param cchBuffer Size of the output buffer.
1810 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1811 * @param args The format argument.
1812 */
1813RTDECL(size_t) RTStrPrintfV(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args);
1814
1815/**
1816 * String printf.
1817 *
1818 * @returns The length of the returned string (in pszBuffer) excluding the
1819 * terminator.
1820 * @param pszBuffer Output buffer.
1821 * @param cchBuffer Size of the output buffer.
1822 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1823 * @param ... The format argument.
1824 */
1825RTDECL(size_t) RTStrPrintf(char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...);
1826
1827
1828/**
1829 * String printf with custom formatting.
1830 *
1831 * @returns The length of the returned string (in pszBuffer) excluding the
1832 * terminator.
1833 * @param pfnFormat Pointer to handler function for the custom formats.
1834 * @param pvArg Argument to the pfnFormat function.
1835 * @param pszBuffer Output buffer.
1836 * @param cchBuffer Size of the output buffer.
1837 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1838 * @param args The format argument.
1839 */
1840RTDECL(size_t) RTStrPrintfExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args);
1841
1842/**
1843 * String printf with custom formatting.
1844 *
1845 * @returns The length of the returned string (in pszBuffer) excluding the
1846 * terminator.
1847 * @param pfnFormat Pointer to handler function for the custom formats.
1848 * @param pvArg Argument to the pfnFormat function.
1849 * @param pszBuffer Output buffer.
1850 * @param cchBuffer Size of the output buffer.
1851 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1852 * @param ... The format argument.
1853 */
1854RTDECL(size_t) RTStrPrintfEx(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...);
1855
1856
1857/**
1858 * Allocating string printf (default tag).
1859 *
1860 * @returns The length of the string in the returned *ppszBuffer excluding the
1861 * terminator.
1862 * @returns -1 on failure.
1863 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
1864 * The buffer should be freed using RTStrFree().
1865 * On failure *ppszBuffer will be set to NULL.
1866 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1867 * @param args The format argument.
1868 */
1869#define RTStrAPrintfV(ppszBuffer, pszFormat, args) RTStrAPrintfVTag((ppszBuffer), (pszFormat), (args), RTSTR_TAG)
1870
1871/**
1872 * Allocating string printf (custom tag).
1873 *
1874 * @returns The length of the string in the returned *ppszBuffer excluding the
1875 * terminator.
1876 * @returns -1 on failure.
1877 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
1878 * The buffer should be freed using RTStrFree().
1879 * On failure *ppszBuffer will be set to NULL.
1880 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1881 * @param args The format argument.
1882 * @param pszTag Allocation tag used for statistics and such.
1883 */
1884RTDECL(int) RTStrAPrintfVTag(char **ppszBuffer, const char *pszFormat, va_list args, const char *pszTag);
1885
1886/**
1887 * Allocating string printf.
1888 *
1889 * @returns The length of the string in the returned *ppszBuffer excluding the
1890 * terminator.
1891 * @returns -1 on failure.
1892 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
1893 * The buffer should be freed using RTStrFree().
1894 * On failure *ppszBuffer will be set to NULL.
1895 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1896 * @param ... The format argument.
1897 */
1898DECLINLINE(int) RTStrAPrintf(char **ppszBuffer, const char *pszFormat, ...)
1899{
1900 int cbRet;
1901 va_list va;
1902 va_start(va, pszFormat);
1903 cbRet = RTStrAPrintfVTag(ppszBuffer, pszFormat, va, RTSTR_TAG);
1904 va_end(va);
1905 return cbRet;
1906}
1907
1908/**
1909 * Allocating string printf (custom tag).
1910 *
1911 * @returns The length of the string in the returned *ppszBuffer excluding the
1912 * terminator.
1913 * @returns -1 on failure.
1914 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
1915 * The buffer should be freed using RTStrFree().
1916 * On failure *ppszBuffer will be set to NULL.
1917 * @param pszTag Allocation tag used for statistics and such.
1918 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1919 * @param ... The format argument.
1920 */
1921DECLINLINE(int) RTStrAPrintfTag(char **ppszBuffer, const char *pszTag, const char *pszFormat, ...)
1922{
1923 int cbRet;
1924 va_list va;
1925 va_start(va, pszFormat);
1926 cbRet = RTStrAPrintfVTag(ppszBuffer, pszFormat, va, pszTag);
1927 va_end(va);
1928 return cbRet;
1929}
1930
1931/**
1932 * Allocating string printf, version 2.
1933 *
1934 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
1935 * memory.
1936 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1937 * @param args The format argument.
1938 */
1939#define RTStrAPrintf2V(pszFormat, args) RTStrAPrintf2VTag((pszFormat), (args), RTSTR_TAG)
1940
1941/**
1942 * Allocating string printf, version 2.
1943 *
1944 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
1945 * memory.
1946 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1947 * @param args The format argument.
1948 * @param pszTag Allocation tag used for statistics and such.
1949 */
1950RTDECL(char *) RTStrAPrintf2VTag(const char *pszFormat, va_list args, const char *pszTag);
1951
1952/**
1953 * Allocating string printf, version 2 (default tag).
1954 *
1955 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
1956 * memory.
1957 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1958 * @param ... The format argument.
1959 */
1960DECLINLINE(char *) RTStrAPrintf2(const char *pszFormat, ...)
1961{
1962 char *pszRet;
1963 va_list va;
1964 va_start(va, pszFormat);
1965 pszRet = RTStrAPrintf2VTag(pszFormat, va, RTSTR_TAG);
1966 va_end(va);
1967 return pszRet;
1968}
1969
1970/**
1971 * Allocating string printf, version 2 (custom tag).
1972 *
1973 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
1974 * memory.
1975 * @param pszTag Allocation tag used for statistics and such.
1976 * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
1977 * @param ... The format argument.
1978 */
1979DECLINLINE(char *) RTStrAPrintf2Tag(const char *pszTag, const char *pszFormat, ...)
1980{
1981 char *pszRet;
1982 va_list va;
1983 va_start(va, pszFormat);
1984 pszRet = RTStrAPrintf2VTag(pszFormat, va, pszTag);
1985 va_end(va);
1986 return pszRet;
1987}
1988
1989/**
1990 * Strips blankspaces from both ends of the string.
1991 *
1992 * @returns Pointer to first non-blank char in the string.
1993 * @param psz The string to strip.
1994 */
1995RTDECL(char *) RTStrStrip(char *psz);
1996
1997/**
1998 * Strips blankspaces from the start of the string.
1999 *
2000 * @returns Pointer to first non-blank char in the string.
2001 * @param psz The string to strip.
2002 */
2003RTDECL(char *) RTStrStripL(const char *psz);
2004
2005/**
2006 * Strips blankspaces from the end of the string.
2007 *
2008 * @returns psz.
2009 * @param psz The string to strip.
2010 */
2011RTDECL(char *) RTStrStripR(char *psz);
2012
2013/**
2014 * String copy with overflow handling.
2015 *
2016 * @retval VINF_SUCCESS on success.
2017 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2018 * buffer will contain as much of the string as it can hold, fully
2019 * terminated.
2020 *
2021 * @param pszDst The destination buffer.
2022 * @param cbDst The size of the destination buffer (in bytes).
2023 * @param pszSrc The source string. NULL is not OK.
2024 */
2025RTDECL(int) RTStrCopy(char *pszDst, size_t cbDst, const char *pszSrc);
2026
2027/**
2028 * String copy with overflow handling.
2029 *
2030 * @retval VINF_SUCCESS on success.
2031 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2032 * buffer will contain as much of the string as it can hold, fully
2033 * terminated.
2034 *
2035 * @param pszDst The destination buffer.
2036 * @param cbDst The size of the destination buffer (in bytes).
2037 * @param pszSrc The source string. NULL is not OK.
2038 * @param cchSrcMax The maximum number of chars (not code points) to
2039 * copy from the source string, not counting the
2040 * terminator as usual.
2041 */
2042RTDECL(int) RTStrCopyEx(char *pszDst, size_t cbDst, const char *pszSrc, size_t cchSrcMax);
2043
2044/**
2045 * String concatenation with overflow handling.
2046 *
2047 * @retval VINF_SUCCESS on success.
2048 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2049 * buffer will contain as much of the string as it can hold, fully
2050 * terminated.
2051 *
2052 * @param pszDst The destination buffer.
2053 * @param cbDst The size of the destination buffer (in bytes).
2054 * @param pszSrc The source string. NULL is not OK.
2055 */
2056RTDECL(int) RTStrCat(char *pszDst, size_t cbDst, const char *pszSrc);
2057
2058/**
2059 * String concatenation with overflow handling.
2060 *
2061 * @retval VINF_SUCCESS on success.
2062 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
2063 * buffer will contain as much of the string as it can hold, fully
2064 * terminated.
2065 *
2066 * @param pszDst The destination buffer.
2067 * @param cbDst The size of the destination buffer (in bytes).
2068 * @param pszSrc The source string. NULL is not OK.
2069 * @param cchSrcMax The maximum number of chars (not code points) to
2070 * copy from the source string, not counting the
2071 * terminator as usual.
2072 */
2073RTDECL(int) RTStrCatEx(char *pszDst, size_t cbDst, const char *pszSrc, size_t cchSrcMax);
2074
2075/**
2076 * Performs a case sensitive string compare between two UTF-8 strings.
2077 *
2078 * Encoding errors are ignored by the current implementation. So, the only
2079 * difference between this and the CRT strcmp function is the handling of
2080 * NULL arguments.
2081 *
2082 * @returns < 0 if the first string less than the second string.
2083 * @returns 0 if the first string identical to the second string.
2084 * @returns > 0 if the first string greater than the second string.
2085 * @param psz1 First UTF-8 string. Null is allowed.
2086 * @param psz2 Second UTF-8 string. Null is allowed.
2087 */
2088RTDECL(int) RTStrCmp(const char *psz1, const char *psz2);
2089
2090/**
2091 * Performs a case sensitive string compare between two UTF-8 strings, given
2092 * a maximum string length.
2093 *
2094 * Encoding errors are ignored by the current implementation. So, the only
2095 * difference between this and the CRT strncmp function is the handling of
2096 * NULL arguments.
2097 *
2098 * @returns < 0 if the first string less than the second string.
2099 * @returns 0 if the first string identical to the second string.
2100 * @returns > 0 if the first string greater than the second string.
2101 * @param psz1 First UTF-8 string. Null is allowed.
2102 * @param psz2 Second UTF-8 string. Null is allowed.
2103 * @param cchMax The maximum string length
2104 */
2105RTDECL(int) RTStrNCmp(const char *psz1, const char *psz2, size_t cchMax);
2106
2107/**
2108 * Performs a case insensitive string compare between two UTF-8 strings.
2109 *
2110 * This is a simplified compare, as only the simplified lower/upper case folding
2111 * specified by the unicode specs are used. It does not consider character pairs
2112 * as they are used in some languages, just simple upper & lower case compares.
2113 *
2114 * The result is the difference between the mismatching codepoints after they
2115 * both have been lower cased.
2116 *
2117 * If the string encoding is invalid the function will assert (strict builds)
2118 * and use RTStrCmp for the remainder of the string.
2119 *
2120 * @returns < 0 if the first string less than the second string.
2121 * @returns 0 if the first string identical to the second string.
2122 * @returns > 0 if the first string greater than the second string.
2123 * @param psz1 First UTF-8 string. Null is allowed.
2124 * @param psz2 Second UTF-8 string. Null is allowed.
2125 */
2126RTDECL(int) RTStrICmp(const char *psz1, const char *psz2);
2127
2128/**
2129 * Performs a case insensitive string compare between two UTF-8 strings, given a
2130 * maximum string length.
2131 *
2132 * This is a simplified compare, as only the simplified lower/upper case folding
2133 * specified by the unicode specs are used. It does not consider character pairs
2134 * as they are used in some languages, just simple upper & lower case compares.
2135 *
2136 * The result is the difference between the mismatching codepoints after they
2137 * both have been lower cased.
2138 *
2139 * If the string encoding is invalid the function will assert (strict builds)
2140 * and use RTStrCmp for the remainder of the string.
2141 *
2142 * @returns < 0 if the first string less than the second string.
2143 * @returns 0 if the first string identical to the second string.
2144 * @returns > 0 if the first string greater than the second string.
2145 * @param psz1 First UTF-8 string. Null is allowed.
2146 * @param psz2 Second UTF-8 string. Null is allowed.
2147 * @param cchMax Maximum string length
2148 */
2149RTDECL(int) RTStrNICmp(const char *psz1, const char *psz2, size_t cchMax);
2150
2151/**
2152 * Locates a case sensitive substring.
2153 *
2154 * If any of the two strings are NULL, then NULL is returned. If the needle is
2155 * an empty string, then the haystack is returned (i.e. matches anything).
2156 *
2157 * @returns Pointer to the first occurrence of the substring if found, NULL if
2158 * not.
2159 *
2160 * @param pszHaystack The string to search.
2161 * @param pszNeedle The substring to search for.
2162 *
2163 * @remarks The difference between this and strstr is the handling of NULL
2164 * pointers.
2165 */
2166RTDECL(char *) RTStrStr(const char *pszHaystack, const char *pszNeedle);
2167
2168/**
2169 * Locates a case insensitive substring.
2170 *
2171 * If any of the two strings are NULL, then NULL is returned. If the needle is
2172 * an empty string, then the haystack is returned (i.e. matches anything).
2173 *
2174 * @returns Pointer to the first occurrence of the substring if found, NULL if
2175 * not.
2176 *
2177 * @param pszHaystack The string to search.
2178 * @param pszNeedle The substring to search for.
2179 *
2180 */
2181RTDECL(char *) RTStrIStr(const char *pszHaystack, const char *pszNeedle);
2182
2183/**
2184 * Converts the string to lower case.
2185 *
2186 * @returns Pointer to the converted string.
2187 * @param psz The string to convert.
2188 */
2189RTDECL(char *) RTStrToLower(char *psz);
2190
2191/**
2192 * Converts the string to upper case.
2193 *
2194 * @returns Pointer to the converted string.
2195 * @param psz The string to convert.
2196 */
2197RTDECL(char *) RTStrToUpper(char *psz);
2198
2199/**
2200 * Find the length of a zero-terminated byte string, given
2201 * a max string length.
2202 *
2203 * See also RTStrNLenEx.
2204 *
2205 * @returns The string length or cbMax. The returned length does not include
2206 * the zero terminator if it was found.
2207 *
2208 * @param pszString The string.
2209 * @param cchMax The max string length.
2210 */
2211RTDECL(size_t) RTStrNLen(const char *pszString, size_t cchMax);
2212
2213/**
2214 * Find the length of a zero-terminated byte string, given
2215 * a max string length.
2216 *
2217 * See also RTStrNLen.
2218 *
2219 * @returns IPRT status code.
2220 * @retval VINF_SUCCESS if the string has a length less than cchMax.
2221 * @retval VERR_BUFFER_OVERFLOW if the end of the string wasn't found
2222 * before cchMax was reached.
2223 *
2224 * @param pszString The string.
2225 * @param cchMax The max string length.
2226 * @param pcch Where to store the string length excluding the
2227 * terminator. This is set to cchMax if the terminator
2228 * isn't found.
2229 */
2230RTDECL(int) RTStrNLenEx(const char *pszString, size_t cchMax, size_t *pcch);
2231
2232RT_C_DECLS_END
2233
2234/** The maximum size argument of a memchr call. */
2235#define RTSTR_MEMCHR_MAX (~(size_t)0x10000)
2236
2237/**
2238 * Find the zero terminator in a string with a limited length.
2239 *
2240 * @returns Pointer to the zero terminator.
2241 * @returns NULL if the zero terminator was not found.
2242 *
2243 * @param pszString The string.
2244 * @param cchMax The max string length. RTSTR_MAX is fine.
2245 */
2246#if defined(__cplusplus) && !defined(DOXYGEN_RUNNING)
2247DECLINLINE(char const *) RTStrEnd(char const *pszString, size_t cchMax)
2248{
2249 /* Avoid potential issues with memchr seen in glibc. */
2250 if (cchMax > RTSTR_MEMCHR_MAX)
2251 {
2252 char const *pszRet = (char const *)memchr(pszString, '\0', RTSTR_MEMCHR_MAX);
2253 if (RT_LIKELY(pszRet))
2254 return pszRet;
2255 pszString += RTSTR_MEMCHR_MAX;
2256 cchMax -= RTSTR_MEMCHR_MAX;
2257 }
2258 return (char const *)memchr(pszString, '\0', cchMax);
2259}
2260
2261DECLINLINE(char *) RTStrEnd(char *pszString, size_t cchMax)
2262#else
2263DECLINLINE(char *) RTStrEnd(const char *pszString, size_t cchMax)
2264#endif
2265{
2266 /* Avoid potential issues with memchr seen in glibc. */
2267 if (cchMax > RTSTR_MEMCHR_MAX)
2268 {
2269 char *pszRet = (char *)memchr(pszString, '\0', RTSTR_MEMCHR_MAX);
2270 if (RT_LIKELY(pszRet))
2271 return pszRet;
2272 pszString += RTSTR_MEMCHR_MAX;
2273 cchMax -= RTSTR_MEMCHR_MAX;
2274 }
2275 return (char *)memchr(pszString, '\0', cchMax);
2276}
2277
2278RT_C_DECLS_BEGIN
2279
2280/**
2281 * Matches a simple string pattern.
2282 *
2283 * @returns true if the string matches the pattern, otherwise false.
2284 *
2285 * @param pszPattern The pattern. Special chars are '*' and '?', where the
2286 * asterisk matches zero or more characters and question
2287 * mark matches exactly one character.
2288 * @param pszString The string to match against the pattern.
2289 */
2290RTDECL(bool) RTStrSimplePatternMatch(const char *pszPattern, const char *pszString);
2291
2292/**
2293 * Matches a simple string pattern, neither which needs to be zero terminated.
2294 *
2295 * This is identical to RTStrSimplePatternMatch except that you can optionally
2296 * specify the length of both the pattern and the string. The function will
2297 * stop when it hits a string terminator or either of the lengths.
2298 *
2299 * @returns true if the string matches the pattern, otherwise false.
2300 *
2301 * @param pszPattern The pattern. Special chars are '*' and '?', where the
2302 * asterisk matches zero or more characters and question
2303 * mark matches exactly one character.
2304 * @param cchPattern The pattern length. Pass RTSTR_MAX if you don't know the
2305 * length and wish to stop at the string terminator.
2306 * @param pszString The string to match against the pattern.
2307 * @param cchString The string length. Pass RTSTR_MAX if you don't know the
2308 * length and wish to match up to the string terminator.
2309 */
2310RTDECL(bool) RTStrSimplePatternNMatch(const char *pszPattern, size_t cchPattern,
2311 const char *pszString, size_t cchString);
2312
2313/**
2314 * Matches multiple patterns against a string.
2315 *
2316 * The patterns are separated by the pipe character (|).
2317 *
2318 * @returns true if the string matches the pattern, otherwise false.
2319 *
2320 * @param pszPatterns The patterns.
2321 * @param cchPatterns The lengths of the patterns to use. Pass RTSTR_MAX to
2322 * stop at the terminator.
2323 * @param pszString The string to match against the pattern.
2324 * @param cchString The string length. Pass RTSTR_MAX stop stop at the
2325 * terminator.
2326 * @param poffPattern Offset into the patterns string of the patttern that
2327 * matched. If no match, this will be set to RTSTR_MAX.
2328 * This is optional, NULL is fine.
2329 */
2330RTDECL(bool) RTStrSimplePatternMultiMatch(const char *pszPatterns, size_t cchPatterns,
2331 const char *pszString, size_t cchString,
2332 size_t *poffPattern);
2333
2334/**
2335 * Compares two version strings RTStrICmp fashion.
2336 *
2337 * The version string is split up into sections at punctuation, spaces,
2338 * underscores, dashes and plus signs. The sections are then split up into
2339 * numeric and string sub-sections. Finally, the sub-sections are compared
2340 * in a numeric or case insesntivie fashion depending on what they are.
2341 *
2342 * The following strings are considered to be equal: "1.0.0", "1.00.0", "1.0",
2343 * "1". These aren't: "1.0.0r993", "1.0", "1.0r993", "1.0_Beta3", "1.1"
2344 *
2345 * @returns < 0 if the first string less than the second string.
2346 * @returns 0 if the first string identical to the second string.
2347 * @returns > 0 if the first string greater than the second string.
2348 *
2349 * @param pszVer1 First version string to compare.
2350 * @param pszVer2 Second version string to compare first version with.
2351 */
2352RTDECL(int) RTStrVersionCompare(const char *pszVer1, const char *pszVer2);
2353
2354
2355/** @defgroup rt_str_conv String To/From Number Conversions
2356 * @ingroup grp_rt_str
2357 * @{ */
2358
2359/**
2360 * Converts a string representation of a number to a 64-bit unsigned number.
2361 *
2362 * @returns iprt status code.
2363 * Warnings are used to indicate conversion problems.
2364 * @retval VWRN_NUMBER_TOO_BIG
2365 * @retval VWRN_NEGATIVE_UNSIGNED
2366 * @retval VWRN_TRAILING_CHARS
2367 * @retval VWRN_TRAILING_SPACES
2368 * @retval VINF_SUCCESS
2369 * @retval VERR_NO_DIGITS
2370 *
2371 * @param pszValue Pointer to the string value.
2372 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2373 * @param uBase The base of the representation used.
2374 * If 0 the function will look for known prefixes before defaulting to 10.
2375 * @param pu64 Where to store the converted number. (optional)
2376 */
2377RTDECL(int) RTStrToUInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint64_t *pu64);
2378
2379/**
2380 * Converts a string representation of a number to a 64-bit unsigned number,
2381 * making sure the full string is converted.
2382 *
2383 * @returns iprt status code.
2384 * Warnings are used to indicate conversion problems.
2385 * @retval VWRN_NUMBER_TOO_BIG
2386 * @retval VWRN_NEGATIVE_UNSIGNED
2387 * @retval VINF_SUCCESS
2388 * @retval VERR_NO_DIGITS
2389 * @retval VERR_TRAILING_SPACES
2390 * @retval VERR_TRAILING_CHARS
2391 *
2392 * @param pszValue Pointer to the string value.
2393 * @param uBase The base of the representation used.
2394 * If 0 the function will look for known prefixes before defaulting to 10.
2395 * @param pu64 Where to store the converted number. (optional)
2396 */
2397RTDECL(int) RTStrToUInt64Full(const char *pszValue, unsigned uBase, uint64_t *pu64);
2398
2399/**
2400 * Converts a string representation of a number to a 64-bit unsigned number.
2401 * The base is guessed.
2402 *
2403 * @returns 64-bit unsigned number on success.
2404 * @returns 0 on failure.
2405 * @param pszValue Pointer to the string value.
2406 */
2407RTDECL(uint64_t) RTStrToUInt64(const char *pszValue);
2408
2409/**
2410 * Converts a string representation of a number to a 32-bit unsigned number.
2411 *
2412 * @returns iprt status code.
2413 * Warnings are used to indicate conversion problems.
2414 * @retval VWRN_NUMBER_TOO_BIG
2415 * @retval VWRN_NEGATIVE_UNSIGNED
2416 * @retval VWRN_TRAILING_CHARS
2417 * @retval VWRN_TRAILING_SPACES
2418 * @retval VINF_SUCCESS
2419 * @retval VERR_NO_DIGITS
2420 *
2421 * @param pszValue Pointer to the string value.
2422 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2423 * @param uBase The base of the representation used.
2424 * If 0 the function will look for known prefixes before defaulting to 10.
2425 * @param pu32 Where to store the converted number. (optional)
2426 */
2427RTDECL(int) RTStrToUInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint32_t *pu32);
2428
2429/**
2430 * Converts a string representation of a number to a 32-bit unsigned number,
2431 * making sure the full string is converted.
2432 *
2433 * @returns iprt status code.
2434 * Warnings are used to indicate conversion problems.
2435 * @retval VWRN_NUMBER_TOO_BIG
2436 * @retval VWRN_NEGATIVE_UNSIGNED
2437 * @retval VINF_SUCCESS
2438 * @retval VERR_NO_DIGITS
2439 * @retval VERR_TRAILING_SPACES
2440 * @retval VERR_TRAILING_CHARS
2441 *
2442 * @param pszValue Pointer to the string value.
2443 * @param uBase The base of the representation used.
2444 * If 0 the function will look for known prefixes before defaulting to 10.
2445 * @param pu32 Where to store the converted number. (optional)
2446 */
2447RTDECL(int) RTStrToUInt32Full(const char *pszValue, unsigned uBase, uint32_t *pu32);
2448
2449/**
2450 * Converts a string representation of a number to a 64-bit unsigned number.
2451 * The base is guessed.
2452 *
2453 * @returns 32-bit unsigned number on success.
2454 * @returns 0 on failure.
2455 * @param pszValue Pointer to the string value.
2456 */
2457RTDECL(uint32_t) RTStrToUInt32(const char *pszValue);
2458
2459/**
2460 * Converts a string representation of a number to a 16-bit unsigned number.
2461 *
2462 * @returns iprt status code.
2463 * Warnings are used to indicate conversion problems.
2464 * @retval VWRN_NUMBER_TOO_BIG
2465 * @retval VWRN_NEGATIVE_UNSIGNED
2466 * @retval VWRN_TRAILING_CHARS
2467 * @retval VWRN_TRAILING_SPACES
2468 * @retval VINF_SUCCESS
2469 * @retval VERR_NO_DIGITS
2470 *
2471 * @param pszValue Pointer to the string value.
2472 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2473 * @param uBase The base of the representation used.
2474 * If 0 the function will look for known prefixes before defaulting to 10.
2475 * @param pu16 Where to store the converted number. (optional)
2476 */
2477RTDECL(int) RTStrToUInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint16_t *pu16);
2478
2479/**
2480 * Converts a string representation of a number to a 16-bit unsigned number,
2481 * making sure the full string is converted.
2482 *
2483 * @returns iprt status code.
2484 * Warnings are used to indicate conversion problems.
2485 * @retval VWRN_NUMBER_TOO_BIG
2486 * @retval VWRN_NEGATIVE_UNSIGNED
2487 * @retval VINF_SUCCESS
2488 * @retval VERR_NO_DIGITS
2489 * @retval VERR_TRAILING_SPACES
2490 * @retval VERR_TRAILING_CHARS
2491 *
2492 * @param pszValue Pointer to the string value.
2493 * @param uBase The base of the representation used.
2494 * If 0 the function will look for known prefixes before defaulting to 10.
2495 * @param pu16 Where to store the converted number. (optional)
2496 */
2497RTDECL(int) RTStrToUInt16Full(const char *pszValue, unsigned uBase, uint16_t *pu16);
2498
2499/**
2500 * Converts a string representation of a number to a 16-bit unsigned number.
2501 * The base is guessed.
2502 *
2503 * @returns 16-bit unsigned number on success.
2504 * @returns 0 on failure.
2505 * @param pszValue Pointer to the string value.
2506 */
2507RTDECL(uint16_t) RTStrToUInt16(const char *pszValue);
2508
2509/**
2510 * Converts a string representation of a number to a 8-bit unsigned number.
2511 *
2512 * @returns iprt status code.
2513 * Warnings are used to indicate conversion problems.
2514 * @retval VWRN_NUMBER_TOO_BIG
2515 * @retval VWRN_NEGATIVE_UNSIGNED
2516 * @retval VWRN_TRAILING_CHARS
2517 * @retval VWRN_TRAILING_SPACES
2518 * @retval VINF_SUCCESS
2519 * @retval VERR_NO_DIGITS
2520 *
2521 * @param pszValue Pointer to the string value.
2522 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2523 * @param uBase The base of the representation used.
2524 * If 0 the function will look for known prefixes before defaulting to 10.
2525 * @param pu8 Where to store the converted number. (optional)
2526 */
2527RTDECL(int) RTStrToUInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint8_t *pu8);
2528
2529/**
2530 * Converts a string representation of a number to a 8-bit unsigned number,
2531 * making sure the full string is converted.
2532 *
2533 * @returns iprt status code.
2534 * Warnings are used to indicate conversion problems.
2535 * @retval VWRN_NUMBER_TOO_BIG
2536 * @retval VWRN_NEGATIVE_UNSIGNED
2537 * @retval VINF_SUCCESS
2538 * @retval VERR_NO_DIGITS
2539 * @retval VERR_TRAILING_SPACES
2540 * @retval VERR_TRAILING_CHARS
2541 *
2542 * @param pszValue Pointer to the string value.
2543 * @param uBase The base of the representation used.
2544 * If 0 the function will look for known prefixes before defaulting to 10.
2545 * @param pu8 Where to store the converted number. (optional)
2546 */
2547RTDECL(int) RTStrToUInt8Full(const char *pszValue, unsigned uBase, uint8_t *pu8);
2548
2549/**
2550 * Converts a string representation of a number to a 8-bit unsigned number.
2551 * The base is guessed.
2552 *
2553 * @returns 8-bit unsigned number on success.
2554 * @returns 0 on failure.
2555 * @param pszValue Pointer to the string value.
2556 */
2557RTDECL(uint8_t) RTStrToUInt8(const char *pszValue);
2558
2559/**
2560 * Converts a string representation of a number to a 64-bit signed number.
2561 *
2562 * @returns iprt status code.
2563 * Warnings are used to indicate conversion problems.
2564 * @retval VWRN_NUMBER_TOO_BIG
2565 * @retval VWRN_TRAILING_CHARS
2566 * @retval VWRN_TRAILING_SPACES
2567 * @retval VINF_SUCCESS
2568 * @retval VERR_NO_DIGITS
2569 *
2570 * @param pszValue Pointer to the string value.
2571 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2572 * @param uBase The base of the representation used.
2573 * If 0 the function will look for known prefixes before defaulting to 10.
2574 * @param pi64 Where to store the converted number. (optional)
2575 */
2576RTDECL(int) RTStrToInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, int64_t *pi64);
2577
2578/**
2579 * Converts a string representation of a number to a 64-bit signed number,
2580 * making sure the full string is converted.
2581 *
2582 * @returns iprt status code.
2583 * Warnings are used to indicate conversion problems.
2584 * @retval VWRN_NUMBER_TOO_BIG
2585 * @retval VINF_SUCCESS
2586 * @retval VERR_TRAILING_CHARS
2587 * @retval VERR_TRAILING_SPACES
2588 * @retval VERR_NO_DIGITS
2589 *
2590 * @param pszValue Pointer to the string value.
2591 * @param uBase The base of the representation used.
2592 * If 0 the function will look for known prefixes before defaulting to 10.
2593 * @param pi64 Where to store the converted number. (optional)
2594 */
2595RTDECL(int) RTStrToInt64Full(const char *pszValue, unsigned uBase, int64_t *pi64);
2596
2597/**
2598 * Converts a string representation of a number to a 64-bit signed number.
2599 * The base is guessed.
2600 *
2601 * @returns 64-bit signed number on success.
2602 * @returns 0 on failure.
2603 * @param pszValue Pointer to the string value.
2604 */
2605RTDECL(int64_t) RTStrToInt64(const char *pszValue);
2606
2607/**
2608 * Converts a string representation of a number to a 32-bit signed number.
2609 *
2610 * @returns iprt status code.
2611 * Warnings are used to indicate conversion problems.
2612 * @retval VWRN_NUMBER_TOO_BIG
2613 * @retval VWRN_TRAILING_CHARS
2614 * @retval VWRN_TRAILING_SPACES
2615 * @retval VINF_SUCCESS
2616 * @retval VERR_NO_DIGITS
2617 *
2618 * @param pszValue Pointer to the string value.
2619 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2620 * @param uBase The base of the representation used.
2621 * If 0 the function will look for known prefixes before defaulting to 10.
2622 * @param pi32 Where to store the converted number. (optional)
2623 */
2624RTDECL(int) RTStrToInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, int32_t *pi32);
2625
2626/**
2627 * Converts a string representation of a number to a 32-bit signed number,
2628 * making sure the full string is converted.
2629 *
2630 * @returns iprt status code.
2631 * Warnings are used to indicate conversion problems.
2632 * @retval VWRN_NUMBER_TOO_BIG
2633 * @retval VINF_SUCCESS
2634 * @retval VERR_TRAILING_CHARS
2635 * @retval VERR_TRAILING_SPACES
2636 * @retval VERR_NO_DIGITS
2637 *
2638 * @param pszValue Pointer to the string value.
2639 * @param uBase The base of the representation used.
2640 * If 0 the function will look for known prefixes before defaulting to 10.
2641 * @param pi32 Where to store the converted number. (optional)
2642 */
2643RTDECL(int) RTStrToInt32Full(const char *pszValue, unsigned uBase, int32_t *pi32);
2644
2645/**
2646 * Converts a string representation of a number to a 32-bit signed number.
2647 * The base is guessed.
2648 *
2649 * @returns 32-bit signed number on success.
2650 * @returns 0 on failure.
2651 * @param pszValue Pointer to the string value.
2652 */
2653RTDECL(int32_t) RTStrToInt32(const char *pszValue);
2654
2655/**
2656 * Converts a string representation of a number to a 16-bit signed number.
2657 *
2658 * @returns iprt status code.
2659 * Warnings are used to indicate conversion problems.
2660 * @retval VWRN_NUMBER_TOO_BIG
2661 * @retval VWRN_TRAILING_CHARS
2662 * @retval VWRN_TRAILING_SPACES
2663 * @retval VINF_SUCCESS
2664 * @retval VERR_NO_DIGITS
2665 *
2666 * @param pszValue Pointer to the string value.
2667 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2668 * @param uBase The base of the representation used.
2669 * If 0 the function will look for known prefixes before defaulting to 10.
2670 * @param pi16 Where to store the converted number. (optional)
2671 */
2672RTDECL(int) RTStrToInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, int16_t *pi16);
2673
2674/**
2675 * Converts a string representation of a number to a 16-bit signed number,
2676 * making sure the full string is converted.
2677 *
2678 * @returns iprt status code.
2679 * Warnings are used to indicate conversion problems.
2680 * @retval VWRN_NUMBER_TOO_BIG
2681 * @retval VINF_SUCCESS
2682 * @retval VERR_TRAILING_CHARS
2683 * @retval VERR_TRAILING_SPACES
2684 * @retval VERR_NO_DIGITS
2685 *
2686 * @param pszValue Pointer to the string value.
2687 * @param uBase The base of the representation used.
2688 * If 0 the function will look for known prefixes before defaulting to 10.
2689 * @param pi16 Where to store the converted number. (optional)
2690 */
2691RTDECL(int) RTStrToInt16Full(const char *pszValue, unsigned uBase, int16_t *pi16);
2692
2693/**
2694 * Converts a string representation of a number to a 16-bit signed number.
2695 * The base is guessed.
2696 *
2697 * @returns 16-bit signed number on success.
2698 * @returns 0 on failure.
2699 * @param pszValue Pointer to the string value.
2700 */
2701RTDECL(int16_t) RTStrToInt16(const char *pszValue);
2702
2703/**
2704 * Converts a string representation of a number to a 8-bit signed number.
2705 *
2706 * @returns iprt status code.
2707 * Warnings are used to indicate conversion problems.
2708 * @retval VWRN_NUMBER_TOO_BIG
2709 * @retval VWRN_TRAILING_CHARS
2710 * @retval VWRN_TRAILING_SPACES
2711 * @retval VINF_SUCCESS
2712 * @retval VERR_NO_DIGITS
2713 *
2714 * @param pszValue Pointer to the string value.
2715 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2716 * @param uBase The base of the representation used.
2717 * If 0 the function will look for known prefixes before defaulting to 10.
2718 * @param pi8 Where to store the converted number. (optional)
2719 */
2720RTDECL(int) RTStrToInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, int8_t *pi8);
2721
2722/**
2723 * Converts a string representation of a number to a 8-bit signed number,
2724 * making sure the full string is converted.
2725 *
2726 * @returns iprt status code.
2727 * Warnings are used to indicate conversion problems.
2728 * @retval VWRN_NUMBER_TOO_BIG
2729 * @retval VINF_SUCCESS
2730 * @retval VERR_TRAILING_CHARS
2731 * @retval VERR_TRAILING_SPACES
2732 * @retval VERR_NO_DIGITS
2733 *
2734 * @param pszValue Pointer to the string value.
2735 * @param uBase The base of the representation used.
2736 * If 0 the function will look for known prefixes before defaulting to 10.
2737 * @param pi8 Where to store the converted number. (optional)
2738 */
2739RTDECL(int) RTStrToInt8Full(const char *pszValue, unsigned uBase, int8_t *pi8);
2740
2741/**
2742 * Converts a string representation of a number to a 8-bit signed number.
2743 * The base is guessed.
2744 *
2745 * @returns 8-bit signed number on success.
2746 * @returns 0 on failure.
2747 * @param pszValue Pointer to the string value.
2748 */
2749RTDECL(int8_t) RTStrToInt8(const char *pszValue);
2750
2751/**
2752 * Formats a buffer stream as hex bytes.
2753 *
2754 * The default is no separating spaces or line breaks or anything.
2755 *
2756 * @returns IPRT status code.
2757 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
2758 * @retval VERR_BUFFER_OVERFLOW if the buffer is insufficent to hold the bytes.
2759 *
2760 * @param pszBuf Output string buffer.
2761 * @param cchBuf The size of the output buffer.
2762 * @param pv Pointer to the bytes to stringify.
2763 * @param cb The number of bytes to stringify.
2764 * @param fFlags Must be zero, reserved for future use.
2765 */
2766RTDECL(int) RTStrPrintHexBytes(char *pszBuf, size_t cchBuf, void const *pv, size_t cb, uint32_t fFlags);
2767
2768/**
2769 * Converts a string of hex bytes back into binary data.
2770 *
2771 * @returns IPRT status code.
2772 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
2773 * @retval VERR_BUFFER_OVERFLOW if the string contains too many hex bytes.
2774 * @retval VERR_BUFFER_UNDERFLOW if there aren't enough hex bytes to fill up
2775 * the output buffer.
2776 * @retval VERR_UNEVEN_INPUT if the input contains a half byte.
2777 * @retval VERR_NO_DIGITS
2778 * @retval VWRN_TRAILING_CHARS
2779 * @retval VWRN_TRAILING_SPACES
2780 *
2781 * @param pszHex The string containing the hex bytes.
2782 * @param pv Output buffer.
2783 * @param cb The size of the output buffer.
2784 * @param fFlags Must be zero, reserved for future use.
2785 */
2786RTDECL(int) RTStrConvertHexBytes(char const *pszHex, void *pv, size_t cb, uint32_t fFlags);
2787
2788/** @} */
2789
2790
2791/** @defgroup rt_str_space Unique String Space
2792 * @ingroup grp_rt_str
2793 * @{
2794 */
2795
2796/** Pointer to a string name space container node core. */
2797typedef struct RTSTRSPACECORE *PRTSTRSPACECORE;
2798/** Pointer to a pointer to a string name space container node core. */
2799typedef PRTSTRSPACECORE *PPRTSTRSPACECORE;
2800
2801/**
2802 * String name space container node core.
2803 */
2804typedef struct RTSTRSPACECORE
2805{
2806 /** Hash key. Don't touch. */
2807 uint32_t Key;
2808 /** Pointer to the left leaf node. Don't touch. */
2809 PRTSTRSPACECORE pLeft;
2810 /** Pointer to the left right node. Don't touch. */
2811 PRTSTRSPACECORE pRight;
2812 /** Pointer to the list of string with the same key. Don't touch. */
2813 PRTSTRSPACECORE pList;
2814 /** Height of this tree: max(heigth(left), heigth(right)) + 1. Don't touch */
2815 unsigned char uchHeight;
2816 /** The string length. Read only! */
2817 size_t cchString;
2818 /** Pointer to the string. Read only! */
2819 const char *pszString;
2820} RTSTRSPACECORE;
2821
2822/** String space. (Initialize with NULL.) */
2823typedef PRTSTRSPACECORE RTSTRSPACE;
2824/** Pointer to a string space. */
2825typedef PPRTSTRSPACECORE PRTSTRSPACE;
2826
2827
2828/**
2829 * Inserts a string into a unique string space.
2830 *
2831 * @returns true on success.
2832 * @returns false if the string collided with an existing string.
2833 * @param pStrSpace The space to insert it into.
2834 * @param pStr The string node.
2835 */
2836RTDECL(bool) RTStrSpaceInsert(PRTSTRSPACE pStrSpace, PRTSTRSPACECORE pStr);
2837
2838/**
2839 * Removes a string from a unique string space.
2840 *
2841 * @returns Pointer to the removed string node.
2842 * @returns NULL if the string was not found in the string space.
2843 * @param pStrSpace The space to insert it into.
2844 * @param pszString The string to remove.
2845 */
2846RTDECL(PRTSTRSPACECORE) RTStrSpaceRemove(PRTSTRSPACE pStrSpace, const char *pszString);
2847
2848/**
2849 * Gets a string from a unique string space.
2850 *
2851 * @returns Pointer to the string node.
2852 * @returns NULL if the string was not found in the string space.
2853 * @param pStrSpace The space to insert it into.
2854 * @param pszString The string to get.
2855 */
2856RTDECL(PRTSTRSPACECORE) RTStrSpaceGet(PRTSTRSPACE pStrSpace, const char *pszString);
2857
2858/**
2859 * Callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy().
2860 *
2861 * @returns 0 on continue.
2862 * @returns Non-zero to aborts the operation.
2863 * @param pStr The string node
2864 * @param pvUser The user specified argument.
2865 */
2866typedef DECLCALLBACK(int) FNRTSTRSPACECALLBACK(PRTSTRSPACECORE pStr, void *pvUser);
2867/** Pointer to callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy(). */
2868typedef FNRTSTRSPACECALLBACK *PFNRTSTRSPACECALLBACK;
2869
2870/**
2871 * Destroys the string space.
2872 *
2873 * The caller supplies a callback which will be called for each of the string
2874 * nodes in for freeing their memory and other resources.
2875 *
2876 * @returns 0 or what ever non-zero return value pfnCallback returned
2877 * when aborting the destruction.
2878 * @param pStrSpace The space to insert it into.
2879 * @param pfnCallback The callback.
2880 * @param pvUser The user argument.
2881 */
2882RTDECL(int) RTStrSpaceDestroy(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
2883
2884/**
2885 * Enumerates the string space.
2886 * The caller supplies a callback which will be called for each of
2887 * the string nodes.
2888 *
2889 * @returns 0 or what ever non-zero return value pfnCallback returned
2890 * when aborting the destruction.
2891 * @param pStrSpace The space to insert it into.
2892 * @param pfnCallback The callback.
2893 * @param pvUser The user argument.
2894 */
2895RTDECL(int) RTStrSpaceEnumerate(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
2896
2897/** @} */
2898
2899
2900/** @defgroup rt_str_utf16 UTF-16 String Manipulation
2901 * @ingroup grp_rt_str
2902 * @{
2903 */
2904
2905/**
2906 * Free a UTF-16 string allocated by RTStrToUtf16(), RTStrToUtf16Ex(),
2907 * RTLatin1ToUtf16(), RTLatin1ToUtf16Ex(), RTUtf16Dup() or RTUtf16DupEx().
2908 *
2909 * @returns iprt status code.
2910 * @param pwszString The UTF-16 string to free. NULL is accepted.
2911 */
2912RTDECL(void) RTUtf16Free(PRTUTF16 pwszString);
2913
2914/**
2915 * Allocates a new copy of the specified UTF-16 string (default tag).
2916 *
2917 * @returns Pointer to the allocated string copy. Use RTUtf16Free() to free it.
2918 * @returns NULL when out of memory.
2919 * @param pwszString UTF-16 string to duplicate.
2920 * @remark This function will not make any attempt to validate the encoding.
2921 */
2922#define RTUtf16Dup(pwszString) RTUtf16DupTag((pwszString), RTSTR_TAG)
2923
2924/**
2925 * Allocates a new copy of the specified UTF-16 string (custom tag).
2926 *
2927 * @returns Pointer to the allocated string copy. Use RTUtf16Free() to free it.
2928 * @returns NULL when out of memory.
2929 * @param pwszString UTF-16 string to duplicate.
2930 * @param pszTag Allocation tag used for statistics and such.
2931 * @remark This function will not make any attempt to validate the encoding.
2932 */
2933RTDECL(PRTUTF16) RTUtf16DupTag(PCRTUTF16 pwszString, const char *pszTag);
2934
2935/**
2936 * Allocates a new copy of the specified UTF-16 string (default tag).
2937 *
2938 * @returns iprt status code.
2939 * @param ppwszString Receives pointer of the allocated UTF-16 string.
2940 * The returned pointer must be freed using RTUtf16Free().
2941 * @param pwszString UTF-16 string to duplicate.
2942 * @param cwcExtra Number of extra RTUTF16 items to allocate.
2943 * @remark This function will not make any attempt to validate the encoding.
2944 */
2945#define RTUtf16DupEx(ppwszString, pwszString, cwcExtra) \
2946 RTUtf16DupExTag((ppwszString), (pwszString), (cwcExtra), RTSTR_TAG)
2947
2948/**
2949 * Allocates a new copy of the specified UTF-16 string (custom tag).
2950 *
2951 * @returns iprt status code.
2952 * @param ppwszString Receives pointer of the allocated UTF-16 string.
2953 * The returned pointer must be freed using RTUtf16Free().
2954 * @param pwszString UTF-16 string to duplicate.
2955 * @param cwcExtra Number of extra RTUTF16 items to allocate.
2956 * @param pszTag Allocation tag used for statistics and such.
2957 * @remark This function will not make any attempt to validate the encoding.
2958 */
2959RTDECL(int) RTUtf16DupExTag(PRTUTF16 *ppwszString, PCRTUTF16 pwszString, size_t cwcExtra, const char *pszTag);
2960
2961/**
2962 * Returns the length of a UTF-16 string in UTF-16 characters
2963 * without trailing '\\0'.
2964 *
2965 * Surrogate pairs counts as two UTF-16 characters here. Use RTUtf16CpCnt()
2966 * to get the exact number of code points in the string.
2967 *
2968 * @returns The number of RTUTF16 items in the string.
2969 * @param pwszString Pointer the UTF-16 string.
2970 * @remark This function will not make any attempt to validate the encoding.
2971 */
2972RTDECL(size_t) RTUtf16Len(PCRTUTF16 pwszString);
2973
2974/**
2975 * Performs a case sensitive string compare between two UTF-16 strings.
2976 *
2977 * @returns < 0 if the first string less than the second string.s
2978 * @returns 0 if the first string identical to the second string.
2979 * @returns > 0 if the first string greater than the second string.
2980 * @param pwsz1 First UTF-16 string. Null is allowed.
2981 * @param pwsz2 Second UTF-16 string. Null is allowed.
2982 * @remark This function will not make any attempt to validate the encoding.
2983 */
2984RTDECL(int) RTUtf16Cmp(register PCRTUTF16 pwsz1, register PCRTUTF16 pwsz2);
2985
2986/**
2987 * Performs a case insensitive string compare between two UTF-16 strings.
2988 *
2989 * This is a simplified compare, as only the simplified lower/upper case folding
2990 * specified by the unicode specs are used. It does not consider character pairs
2991 * as they are used in some languages, just simple upper & lower case compares.
2992 *
2993 * @returns < 0 if the first string less than the second string.
2994 * @returns 0 if the first string identical to the second string.
2995 * @returns > 0 if the first string greater than the second string.
2996 * @param pwsz1 First UTF-16 string. Null is allowed.
2997 * @param pwsz2 Second UTF-16 string. Null is allowed.
2998 */
2999RTDECL(int) RTUtf16ICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
3000
3001/**
3002 * Performs a case insensitive string compare between two UTF-16 strings
3003 * using the current locale of the process (if applicable).
3004 *
3005 * This differs from RTUtf16ICmp() in that it will try, if a locale with the
3006 * required data is available, to do a correct case-insensitive compare. It
3007 * follows that it is more complex and thereby likely to be more expensive.
3008 *
3009 * @returns < 0 if the first string less than the second string.
3010 * @returns 0 if the first string identical to the second string.
3011 * @returns > 0 if the first string greater than the second string.
3012 * @param pwsz1 First UTF-16 string. Null is allowed.
3013 * @param pwsz2 Second UTF-16 string. Null is allowed.
3014 */
3015RTDECL(int) RTUtf16LocaleICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
3016
3017/**
3018 * Folds a UTF-16 string to lowercase.
3019 *
3020 * This is a very simple folding; is uses the simple lowercase
3021 * code point, it is not related to any locale just the most common
3022 * lowercase codepoint setup by the unicode specs, and it will not
3023 * create new surrogate pairs or remove existing ones.
3024 *
3025 * @returns Pointer to the passed in string.
3026 * @param pwsz The string to fold.
3027 */
3028RTDECL(PRTUTF16) RTUtf16ToLower(PRTUTF16 pwsz);
3029
3030/**
3031 * Folds a UTF-16 string to uppercase.
3032 *
3033 * This is a very simple folding; is uses the simple uppercase
3034 * code point, it is not related to any locale just the most common
3035 * uppercase codepoint setup by the unicode specs, and it will not
3036 * create new surrogate pairs or remove existing ones.
3037 *
3038 * @returns Pointer to the passed in string.
3039 * @param pwsz The string to fold.
3040 */
3041RTDECL(PRTUTF16) RTUtf16ToUpper(PRTUTF16 pwsz);
3042
3043/**
3044 * Translate a UTF-16 string into a UTF-8 allocating the result buffer (default
3045 * tag).
3046 *
3047 * @returns iprt status code.
3048 * @param pwszString UTF-16 string to convert.
3049 * @param ppszString Receives pointer of allocated UTF-8 string on
3050 * success, and is always set to NULL on failure.
3051 * The returned pointer must be freed using RTStrFree().
3052 */
3053#define RTUtf16ToUtf8(pwszString, ppszString) RTUtf16ToUtf8Tag((pwszString), (ppszString), RTSTR_TAG)
3054
3055/**
3056 * Translate a UTF-16 string into a UTF-8 allocating the result buffer.
3057 *
3058 * @returns iprt status code.
3059 * @param pwszString UTF-16 string to convert.
3060 * @param ppszString Receives pointer of allocated UTF-8 string on
3061 * success, and is always set to NULL on failure.
3062 * The returned pointer must be freed using RTStrFree().
3063 * @param pszTag Allocation tag used for statistics and such.
3064 */
3065RTDECL(int) RTUtf16ToUtf8Tag(PCRTUTF16 pwszString, char **ppszString, const char *pszTag);
3066
3067/**
3068 * Translates UTF-16 to UTF-8 using buffer provided by the caller or a fittingly
3069 * sized buffer allocated by the function (default tag).
3070 *
3071 * @returns iprt status code.
3072 * @param pwszString The UTF-16 string to convert.
3073 * @param cwcString The number of RTUTF16 items to translate from pwszString.
3074 * The translation will stop when reaching cwcString or the terminator ('\\0').
3075 * Use RTSTR_MAX to translate the entire string.
3076 * @param ppsz If cch is non-zero, this must either be pointing to a pointer to
3077 * a buffer of the specified size, or pointer to a NULL pointer.
3078 * If *ppsz is NULL or cch is zero a buffer of at least cch chars
3079 * will be allocated to hold the translated string.
3080 * If a buffer was requested it must be freed using RTStrFree().
3081 * @param cch The buffer size in chars (the type). This includes the terminator.
3082 * @param pcch Where to store the length of the translated string,
3083 * excluding the terminator. (Optional)
3084 *
3085 * This may be set under some error conditions,
3086 * however, only for VERR_BUFFER_OVERFLOW and
3087 * VERR_NO_STR_MEMORY will it contain a valid string
3088 * length that can be used to resize the buffer.
3089 */
3090#define RTUtf16ToUtf8Ex(pwszString, cwcString, ppsz, cch, pcch) \
3091 RTUtf16ToUtf8ExTag((pwszString), (cwcString), (ppsz), (cch), (pcch), RTSTR_TAG)
3092
3093/**
3094 * Translates UTF-16 to UTF-8 using buffer provided by the caller or a fittingly
3095 * sized buffer allocated by the function (custom tag).
3096 *
3097 * @returns iprt status code.
3098 * @param pwszString The UTF-16 string to convert.
3099 * @param cwcString The number of RTUTF16 items to translate from pwszString.
3100 * The translation will stop when reaching cwcString or the terminator ('\\0').
3101 * Use RTSTR_MAX to translate the entire string.
3102 * @param ppsz If cch is non-zero, this must either be pointing to a pointer to
3103 * a buffer of the specified size, or pointer to a NULL pointer.
3104 * If *ppsz is NULL or cch is zero a buffer of at least cch chars
3105 * will be allocated to hold the translated string.
3106 * If a buffer was requested it must be freed using RTStrFree().
3107 * @param cch The buffer size in chars (the type). This includes the terminator.
3108 * @param pcch Where to store the length of the translated string,
3109 * excluding the terminator. (Optional)
3110 *
3111 * This may be set under some error conditions,
3112 * however, only for VERR_BUFFER_OVERFLOW and
3113 * VERR_NO_STR_MEMORY will it contain a valid string
3114 * length that can be used to resize the buffer.
3115 * @param pszTag Allocation tag used for statistics and such.
3116 */
3117RTDECL(int) RTUtf16ToUtf8ExTag(PCRTUTF16 pwszString, size_t cwcString, char **ppsz, size_t cch, size_t *pcch, const char *pszTag);
3118
3119/**
3120 * Calculates the length of the UTF-16 string in UTF-8 chars (bytes).
3121 *
3122 * This function will validate the string, and incorrectly encoded UTF-16
3123 * strings will be rejected. The primary purpose of this function is to
3124 * help allocate buffers for RTUtf16ToUtf8() of the correct size. For most
3125 * other purposes RTUtf16ToUtf8Ex() should be used.
3126 *
3127 * @returns Number of char (bytes).
3128 * @returns 0 if the string was incorrectly encoded.
3129 * @param pwsz The UTF-16 string.
3130 */
3131RTDECL(size_t) RTUtf16CalcUtf8Len(PCRTUTF16 pwsz);
3132
3133/**
3134 * Calculates the length of the UTF-16 string in UTF-8 chars (bytes).
3135 *
3136 * This function will validate the string, and incorrectly encoded UTF-16
3137 * strings will be rejected.
3138 *
3139 * @returns iprt status code.
3140 * @param pwsz The string.
3141 * @param cwc The max string length. Use RTSTR_MAX to process the entire string.
3142 * @param pcch Where to store the string length (in bytes). Optional.
3143 * This is undefined on failure.
3144 */
3145RTDECL(int) RTUtf16CalcUtf8LenEx(PCRTUTF16 pwsz, size_t cwc, size_t *pcch);
3146
3147/**
3148 * Translate a UTF-16 string into a Latin-1 (ISO-8859-1) allocating the result
3149 * buffer (default tag).
3150 *
3151 * @returns iprt status code.
3152 * @param pwszString UTF-16 string to convert.
3153 * @param ppszString Receives pointer of allocated Latin1 string on
3154 * success, and is always set to NULL on failure.
3155 * The returned pointer must be freed using RTStrFree().
3156 */
3157#define RTUtf16ToLatin1(pwszString, ppszString) RTUtf16ToLatin1Tag((pwszString), (ppszString), RTSTR_TAG)
3158
3159/**
3160 * Translate a UTF-16 string into a Latin-1 (ISO-8859-1) allocating the result
3161 * buffer (custom tag).
3162 *
3163 * @returns iprt status code.
3164 * @param pwszString UTF-16 string to convert.
3165 * @param ppszString Receives pointer of allocated Latin1 string on
3166 * success, and is always set to NULL on failure.
3167 * The returned pointer must be freed using RTStrFree().
3168 * @param pszTag Allocation tag used for statistics and such.
3169 */
3170RTDECL(int) RTUtf16ToLatin1Tag(PCRTUTF16 pwszString, char **ppszString, const char *pszTag);
3171
3172/**
3173 * Translates UTF-16 to Latin-1 (ISO-8859-1) using buffer provided by the caller
3174 * or a fittingly sized buffer allocated by the function (default tag).
3175 *
3176 * @returns iprt status code.
3177 * @param pwszString The UTF-16 string to convert.
3178 * @param cwcString The number of RTUTF16 items to translate from
3179 * pwszString. The translation will stop when reaching
3180 * cwcString or the terminator ('\\0'). Use RTSTR_MAX
3181 * to translate the entire string.
3182 * @param ppsz Pointer to the pointer to the Latin-1 string. The
3183 * buffer can optionally be preallocated by the caller.
3184 *
3185 * If cch is zero, *ppsz is undefined.
3186 *
3187 * If cch is non-zero and *ppsz is not NULL, then this
3188 * will be used as the output buffer.
3189 * VERR_BUFFER_OVERFLOW will be returned if this is
3190 * insufficient.
3191 *
3192 * If cch is zero or *ppsz is NULL, then a buffer of
3193 * sufficient size is allocated. cch can be used to
3194 * specify a minimum size of this buffer. Use
3195 * RTUtf16Free() to free the result.
3196 *
3197 * @param cch The buffer size in chars (the type). This includes
3198 * the terminator.
3199 * @param pcch Where to store the length of the translated string,
3200 * excluding the terminator. (Optional)
3201 *
3202 * This may be set under some error conditions,
3203 * however, only for VERR_BUFFER_OVERFLOW and
3204 * VERR_NO_STR_MEMORY will it contain a valid string
3205 * length that can be used to resize the buffer.
3206 */
3207#define RTUtf16ToLatin1Ex(pwszString, cwcString, ppsz, cch, pcch) \
3208 RTUtf16ToLatin1ExTag((pwszString), (cwcString), (ppsz), (cch), (pcch), RTSTR_TAG)
3209
3210/**
3211 * Translates UTF-16 to Latin-1 (ISO-8859-1) using buffer provided by the caller
3212 * or a fittingly sized buffer allocated by the function (custom tag).
3213 *
3214 * @returns iprt status code.
3215 * @param pwszString The UTF-16 string to convert.
3216 * @param cwcString The number of RTUTF16 items to translate from
3217 * pwszString. The translation will stop when reaching
3218 * cwcString or the terminator ('\\0'). Use RTSTR_MAX
3219 * to translate the entire string.
3220 * @param ppsz Pointer to the pointer to the Latin-1 string. The
3221 * buffer can optionally be preallocated by the caller.
3222 *
3223 * If cch is zero, *ppsz is undefined.
3224 *
3225 * If cch is non-zero and *ppsz is not NULL, then this
3226 * will be used as the output buffer.
3227 * VERR_BUFFER_OVERFLOW will be returned if this is
3228 * insufficient.
3229 *
3230 * If cch is zero or *ppsz is NULL, then a buffer of
3231 * sufficient size is allocated. cch can be used to
3232 * specify a minimum size of this buffer. Use
3233 * RTUtf16Free() to free the result.
3234 *
3235 * @param cch The buffer size in chars (the type). This includes
3236 * the terminator.
3237 * @param pcch Where to store the length of the translated string,
3238 * excluding the terminator. (Optional)
3239 *
3240 * This may be set under some error conditions,
3241 * however, only for VERR_BUFFER_OVERFLOW and
3242 * VERR_NO_STR_MEMORY will it contain a valid string
3243 * length that can be used to resize the buffer.
3244 * @param pszTag Allocation tag used for statistics and such.
3245 */
3246RTDECL(int) RTUtf16ToLatin1ExTag(PCRTUTF16 pwszString, size_t cwcString, char **ppsz, size_t cch, size_t *pcch, const char *pszTag);
3247
3248/**
3249 * Calculates the length of the UTF-16 string in Latin-1 (ISO-8859-1) chars.
3250 *
3251 * This function will validate the string, and incorrectly encoded UTF-16
3252 * strings will be rejected. The primary purpose of this function is to
3253 * help allocate buffers for RTUtf16ToLatin1() of the correct size. For most
3254 * other purposes RTUtf16ToLatin1Ex() should be used.
3255 *
3256 * @returns Number of char (bytes).
3257 * @returns 0 if the string was incorrectly encoded.
3258 * @param pwsz The UTF-16 string.
3259 */
3260RTDECL(size_t) RTUtf16CalcLatin1Len(PCRTUTF16 pwsz);
3261
3262/**
3263 * Calculates the length of the UTF-16 string in Latin-1 (ISO-8859-1) chars.
3264 *
3265 * This function will validate the string, and incorrectly encoded UTF-16
3266 * strings will be rejected.
3267 *
3268 * @returns iprt status code.
3269 * @param pwsz The string.
3270 * @param cwc The max string length. Use RTSTR_MAX to process the
3271 * entire string.
3272 * @param pcch Where to store the string length (in bytes). Optional.
3273 * This is undefined on failure.
3274 */
3275RTDECL(int) RTUtf16CalcLatin1LenEx(PCRTUTF16 pwsz, size_t cwc, size_t *pcch);
3276
3277/**
3278 * Get the unicode code point at the given string position.
3279 *
3280 * @returns unicode code point.
3281 * @returns RTUNICP_INVALID if the encoding is invalid.
3282 * @param pwsz The string.
3283 *
3284 * @remark This is an internal worker for RTUtf16GetCp().
3285 */
3286RTDECL(RTUNICP) RTUtf16GetCpInternal(PCRTUTF16 pwsz);
3287
3288/**
3289 * Get the unicode code point at the given string position.
3290 *
3291 * @returns iprt status code.
3292 * @param ppwsz Pointer to the string pointer. This will be updated to
3293 * point to the char following the current code point.
3294 * @param pCp Where to store the code point.
3295 * RTUNICP_INVALID is stored here on failure.
3296 *
3297 * @remark This is an internal worker for RTUtf16GetCpEx().
3298 */
3299RTDECL(int) RTUtf16GetCpExInternal(PCRTUTF16 *ppwsz, PRTUNICP pCp);
3300
3301/**
3302 * Put the unicode code point at the given string position
3303 * and return the pointer to the char following it.
3304 *
3305 * This function will not consider anything at or following the
3306 * buffer area pointed to by pwsz. It is therefore not suitable for
3307 * inserting code points into a string, only appending/overwriting.
3308 *
3309 * @returns pointer to the char following the written code point.
3310 * @param pwsz The string.
3311 * @param CodePoint The code point to write.
3312 * This should not be RTUNICP_INVALID or any other
3313 * character out of the UTF-16 range.
3314 *
3315 * @remark This is an internal worker for RTUtf16GetCpEx().
3316 */
3317RTDECL(PRTUTF16) RTUtf16PutCpInternal(PRTUTF16 pwsz, RTUNICP CodePoint);
3318
3319/**
3320 * Get the unicode code point at the given string position.
3321 *
3322 * @returns unicode code point.
3323 * @returns RTUNICP_INVALID if the encoding is invalid.
3324 * @param pwsz The string.
3325 *
3326 * @remark We optimize this operation by using an inline function for
3327 * everything which isn't a surrogate pair or an endian indicator.
3328 */
3329DECLINLINE(RTUNICP) RTUtf16GetCp(PCRTUTF16 pwsz)
3330{
3331 const RTUTF16 wc = *pwsz;
3332 if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
3333 return wc;
3334 return RTUtf16GetCpInternal(pwsz);
3335}
3336
3337/**
3338 * Get the unicode code point at the given string position.
3339 *
3340 * @returns iprt status code.
3341 * @param ppwsz Pointer to the string pointer. This will be updated to
3342 * point to the char following the current code point.
3343 * @param pCp Where to store the code point.
3344 * RTUNICP_INVALID is stored here on failure.
3345 *
3346 * @remark We optimize this operation by using an inline function for
3347 * everything which isn't a surrogate pair or and endian indicator.
3348 */
3349DECLINLINE(int) RTUtf16GetCpEx(PCRTUTF16 *ppwsz, PRTUNICP pCp)
3350{
3351 const RTUTF16 wc = **ppwsz;
3352 if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
3353 {
3354 (*ppwsz)++;
3355 *pCp = wc;
3356 return VINF_SUCCESS;
3357 }
3358 return RTUtf16GetCpExInternal(ppwsz, pCp);
3359}
3360
3361/**
3362 * Put the unicode code point at the given string position
3363 * and return the pointer to the char following it.
3364 *
3365 * This function will not consider anything at or following the
3366 * buffer area pointed to by pwsz. It is therefore not suitable for
3367 * inserting code points into a string, only appending/overwriting.
3368 *
3369 * @returns pointer to the char following the written code point.
3370 * @param pwsz The string.
3371 * @param CodePoint The code point to write.
3372 * This should not be RTUNICP_INVALID or any other
3373 * character out of the UTF-16 range.
3374 *
3375 * @remark We optimize this operation by using an inline function for
3376 * everything which isn't a surrogate pair or and endian indicator.
3377 */
3378DECLINLINE(PRTUTF16) RTUtf16PutCp(PRTUTF16 pwsz, RTUNICP CodePoint)
3379{
3380 if (CodePoint < 0xd800 || (CodePoint > 0xd800 && CodePoint < 0xfffe))
3381 {
3382 *pwsz++ = (RTUTF16)CodePoint;
3383 return pwsz;
3384 }
3385 return RTUtf16PutCpInternal(pwsz, CodePoint);
3386}
3387
3388/**
3389 * Skips ahead, past the current code point.
3390 *
3391 * @returns Pointer to the char after the current code point.
3392 * @param pwsz Pointer to the current code point.
3393 * @remark This will not move the next valid code point, only past the current one.
3394 */
3395DECLINLINE(PRTUTF16) RTUtf16NextCp(PCRTUTF16 pwsz)
3396{
3397 RTUNICP Cp;
3398 RTUtf16GetCpEx(&pwsz, &Cp);
3399 return (PRTUTF16)pwsz;
3400}
3401
3402/**
3403 * Skips backwards, to the previous code point.
3404 *
3405 * @returns Pointer to the char after the current code point.
3406 * @param pwszStart Pointer to the start of the string.
3407 * @param pwsz Pointer to the current code point.
3408 */
3409RTDECL(PRTUTF16) RTUtf16PrevCp(PCRTUTF16 pwszStart, PCRTUTF16 pwsz);
3410
3411
3412/**
3413 * Checks if the UTF-16 char is the high surrogate char (i.e.
3414 * the 1st char in the pair).
3415 *
3416 * @returns true if it is.
3417 * @returns false if it isn't.
3418 * @param wc The character to investigate.
3419 */
3420DECLINLINE(bool) RTUtf16IsHighSurrogate(RTUTF16 wc)
3421{
3422 return wc >= 0xd800 && wc <= 0xdbff;
3423}
3424
3425/**
3426 * Checks if the UTF-16 char is the low surrogate char (i.e.
3427 * the 2nd char in the pair).
3428 *
3429 * @returns true if it is.
3430 * @returns false if it isn't.
3431 * @param wc The character to investigate.
3432 */
3433DECLINLINE(bool) RTUtf16IsLowSurrogate(RTUTF16 wc)
3434{
3435 return wc >= 0xdc00 && wc <= 0xdfff;
3436}
3437
3438
3439/**
3440 * Checks if the two UTF-16 chars form a valid surrogate pair.
3441 *
3442 * @returns true if they do.
3443 * @returns false if they doesn't.
3444 * @param wcHigh The high (1st) character.
3445 * @param wcLow The low (2nd) character.
3446 */
3447DECLINLINE(bool) RTUtf16IsSurrogatePair(RTUTF16 wcHigh, RTUTF16 wcLow)
3448{
3449 return RTUtf16IsHighSurrogate(wcHigh)
3450 && RTUtf16IsLowSurrogate(wcLow);
3451}
3452
3453/** @} */
3454
3455
3456/** @defgroup rt_str_latin1 Latin-1 (ISO-8859-1) String Manipulation
3457 * @ingroup grp_rt_str
3458 * @{
3459 */
3460
3461/**
3462 * Calculates the length of the Latin-1 (ISO-8859-1) string in RTUTF16 items.
3463 *
3464 * @returns Number of RTUTF16 items.
3465 * @param psz The Latin-1 string.
3466 */
3467RTDECL(size_t) RTLatin1CalcUtf16Len(const char *psz);
3468
3469/**
3470 * Calculates the length of the Latin-1 (ISO-8859-1) string in RTUTF16 items.
3471 *
3472 * @returns iprt status code.
3473 * @param psz The Latin-1 string.
3474 * @param cch The max string length. Use RTSTR_MAX to process the
3475 * entire string.
3476 * @param pcwc Where to store the string length. Optional.
3477 * This is undefined on failure.
3478 */
3479RTDECL(int) RTLatin1CalcUtf16LenEx(const char *psz, size_t cch, size_t *pcwc);
3480
3481/**
3482 * Translate a Latin-1 (ISO-8859-1) string into a UTF-16 allocating the result
3483 * buffer (default tag).
3484 *
3485 * @returns iprt status code.
3486 * @param pszString The Latin-1 string to convert.
3487 * @param ppwszString Receives pointer to the allocated UTF-16 string. The
3488 * returned string must be freed using RTUtf16Free().
3489 */
3490#define RTLatin1ToUtf16(pszString, ppwszString) RTLatin1ToUtf16Tag((pszString), (ppwszString), RTSTR_TAG)
3491
3492/**
3493 * Translate a Latin-1 (ISO-8859-1) string into a UTF-16 allocating the result
3494 * buffer (custom tag).
3495 *
3496 * @returns iprt status code.
3497 * @param pszString The Latin-1 string to convert.
3498 * @param ppwszString Receives pointer to the allocated UTF-16 string. The
3499 * returned string must be freed using RTUtf16Free().
3500 * @param pszTag Allocation tag used for statistics and such.
3501 */
3502RTDECL(int) RTLatin1ToUtf16Tag(const char *pszString, PRTUTF16 *ppwszString, const char *pszTag);
3503
3504/**
3505 * Translates pszString from Latin-1 (ISO-8859-1) to UTF-16, allocating the
3506 * result buffer if requested (default tag).
3507 *
3508 * @returns iprt status code.
3509 * @param pszString The Latin-1 string to convert.
3510 * @param cchString The maximum size in chars (the type) to convert.
3511 * The conversion stops when it reaches cchString or
3512 * the string terminator ('\\0').
3513 * Use RTSTR_MAX to translate the entire string.
3514 * @param ppwsz If cwc is non-zero, this must either be pointing
3515 * to pointer to a buffer of the specified size, or
3516 * pointer to a NULL pointer.
3517 * If *ppwsz is NULL or cwc is zero a buffer of at
3518 * least cwc items will be allocated to hold the
3519 * translated string. If a buffer was requested it
3520 * must be freed using RTUtf16Free().
3521 * @param cwc The buffer size in RTUTF16s. This includes the
3522 * terminator.
3523 * @param pcwc Where to store the length of the translated string,
3524 * excluding the terminator. (Optional)
3525 *
3526 * This may be set under some error conditions,
3527 * however, only for VERR_BUFFER_OVERFLOW and
3528 * VERR_NO_STR_MEMORY will it contain a valid string
3529 * length that can be used to resize the buffer.
3530 */
3531#define RTLatin1ToUtf16Ex(pszString, cchString, ppwsz, cwc, pcwc) \
3532 RTLatin1ToUtf16ExTag((pszString), (cchString), (ppwsz), (cwc), (pcwc), RTSTR_TAG)
3533
3534/**
3535 * Translates pszString from Latin-1 (ISO-8859-1) to UTF-16, allocating the
3536 * result buffer if requested.
3537 *
3538 * @returns iprt status code.
3539 * @param pszString The Latin-1 string to convert.
3540 * @param cchString The maximum size in chars (the type) to convert.
3541 * The conversion stops when it reaches cchString or
3542 * the string terminator ('\\0').
3543 * Use RTSTR_MAX to translate the entire string.
3544 * @param ppwsz If cwc is non-zero, this must either be pointing
3545 * to pointer to a buffer of the specified size, or
3546 * pointer to a NULL pointer.
3547 * If *ppwsz is NULL or cwc is zero a buffer of at
3548 * least cwc items will be allocated to hold the
3549 * translated string. If a buffer was requested it
3550 * must be freed using RTUtf16Free().
3551 * @param cwc The buffer size in RTUTF16s. This includes the
3552 * terminator.
3553 * @param pcwc Where to store the length of the translated string,
3554 * excluding the terminator. (Optional)
3555 *
3556 * This may be set under some error conditions,
3557 * however, only for VERR_BUFFER_OVERFLOW and
3558 * VERR_NO_STR_MEMORY will it contain a valid string
3559 * length that can be used to resize the buffer.
3560 * @param pszTag Allocation tag used for statistics and such.
3561 */
3562RTDECL(int) RTLatin1ToUtf16ExTag(const char *pszString, size_t cchString,
3563 PRTUTF16 *ppwsz, size_t cwc, size_t *pcwc, const char *pszTag);
3564
3565/** @} */
3566
3567
3568RT_C_DECLS_END
3569
3570/** @} */
3571
3572#endif
3573
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