VirtualBox

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

Last change on this file since 55747 was 53615, checked in by vboxsync, 10 years ago

doxygen fixes.

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

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