VirtualBox

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

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

iprt/string: cleanup

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

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