VirtualBox

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

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

IPRT: Separated out RTPathAbs from the posix, fixing it up for windows + OS/2, and made it generic. Also added some NT path routines.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 169.1 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 * @ingroup grp_rt_str
2605 * @{ */
2606
2607/**
2608 * Converts a string representation of a number to a 64-bit unsigned number.
2609 *
2610 * @returns iprt status code.
2611 * Warnings are used to indicate conversion problems.
2612 * @retval VWRN_NUMBER_TOO_BIG
2613 * @retval VWRN_NEGATIVE_UNSIGNED
2614 * @retval VWRN_TRAILING_CHARS
2615 * @retval VWRN_TRAILING_SPACES
2616 * @retval VINF_SUCCESS
2617 * @retval VERR_NO_DIGITS
2618 *
2619 * @param pszValue Pointer to the string value.
2620 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2621 * @param uBase The base of the representation used.
2622 * If 0 the function will look for known prefixes before defaulting to 10.
2623 * @param pu64 Where to store the converted number. (optional)
2624 */
2625RTDECL(int) RTStrToUInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint64_t *pu64);
2626
2627/**
2628 * Converts a string representation of a number to a 64-bit unsigned number,
2629 * making sure the full string is converted.
2630 *
2631 * @returns iprt status code.
2632 * Warnings are used to indicate conversion problems.
2633 * @retval VWRN_NUMBER_TOO_BIG
2634 * @retval VWRN_NEGATIVE_UNSIGNED
2635 * @retval VINF_SUCCESS
2636 * @retval VERR_NO_DIGITS
2637 * @retval VERR_TRAILING_SPACES
2638 * @retval VERR_TRAILING_CHARS
2639 *
2640 * @param pszValue Pointer to the string value.
2641 * @param uBase The base of the representation used.
2642 * If 0 the function will look for known prefixes before defaulting to 10.
2643 * @param pu64 Where to store the converted number. (optional)
2644 */
2645RTDECL(int) RTStrToUInt64Full(const char *pszValue, unsigned uBase, uint64_t *pu64);
2646
2647/**
2648 * Converts a string representation of a number to a 64-bit unsigned number.
2649 * The base is guessed.
2650 *
2651 * @returns 64-bit unsigned number on success.
2652 * @returns 0 on failure.
2653 * @param pszValue Pointer to the string value.
2654 */
2655RTDECL(uint64_t) RTStrToUInt64(const char *pszValue);
2656
2657/**
2658 * Converts a string representation of a number to a 32-bit unsigned number.
2659 *
2660 * @returns iprt status code.
2661 * Warnings are used to indicate conversion problems.
2662 * @retval VWRN_NUMBER_TOO_BIG
2663 * @retval VWRN_NEGATIVE_UNSIGNED
2664 * @retval VWRN_TRAILING_CHARS
2665 * @retval VWRN_TRAILING_SPACES
2666 * @retval VINF_SUCCESS
2667 * @retval VERR_NO_DIGITS
2668 *
2669 * @param pszValue Pointer to the string value.
2670 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2671 * @param uBase The base of the representation used.
2672 * If 0 the function will look for known prefixes before defaulting to 10.
2673 * @param pu32 Where to store the converted number. (optional)
2674 */
2675RTDECL(int) RTStrToUInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint32_t *pu32);
2676
2677/**
2678 * Converts a string representation of a number to a 32-bit unsigned number,
2679 * making sure the full string is converted.
2680 *
2681 * @returns iprt status code.
2682 * Warnings are used to indicate conversion problems.
2683 * @retval VWRN_NUMBER_TOO_BIG
2684 * @retval VWRN_NEGATIVE_UNSIGNED
2685 * @retval VINF_SUCCESS
2686 * @retval VERR_NO_DIGITS
2687 * @retval VERR_TRAILING_SPACES
2688 * @retval VERR_TRAILING_CHARS
2689 *
2690 * @param pszValue Pointer to the string value.
2691 * @param uBase The base of the representation used.
2692 * If 0 the function will look for known prefixes before defaulting to 10.
2693 * @param pu32 Where to store the converted number. (optional)
2694 */
2695RTDECL(int) RTStrToUInt32Full(const char *pszValue, unsigned uBase, uint32_t *pu32);
2696
2697/**
2698 * Converts a string representation of a number to a 64-bit unsigned number.
2699 * The base is guessed.
2700 *
2701 * @returns 32-bit unsigned number on success.
2702 * @returns 0 on failure.
2703 * @param pszValue Pointer to the string value.
2704 */
2705RTDECL(uint32_t) RTStrToUInt32(const char *pszValue);
2706
2707/**
2708 * Converts a string representation of a number to a 16-bit unsigned number.
2709 *
2710 * @returns iprt status code.
2711 * Warnings are used to indicate conversion problems.
2712 * @retval VWRN_NUMBER_TOO_BIG
2713 * @retval VWRN_NEGATIVE_UNSIGNED
2714 * @retval VWRN_TRAILING_CHARS
2715 * @retval VWRN_TRAILING_SPACES
2716 * @retval VINF_SUCCESS
2717 * @retval VERR_NO_DIGITS
2718 *
2719 * @param pszValue Pointer to the string value.
2720 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2721 * @param uBase The base of the representation used.
2722 * If 0 the function will look for known prefixes before defaulting to 10.
2723 * @param pu16 Where to store the converted number. (optional)
2724 */
2725RTDECL(int) RTStrToUInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint16_t *pu16);
2726
2727/**
2728 * Converts a string representation of a number to a 16-bit unsigned number,
2729 * making sure the full string is converted.
2730 *
2731 * @returns iprt status code.
2732 * Warnings are used to indicate conversion problems.
2733 * @retval VWRN_NUMBER_TOO_BIG
2734 * @retval VWRN_NEGATIVE_UNSIGNED
2735 * @retval VINF_SUCCESS
2736 * @retval VERR_NO_DIGITS
2737 * @retval VERR_TRAILING_SPACES
2738 * @retval VERR_TRAILING_CHARS
2739 *
2740 * @param pszValue Pointer to the string value.
2741 * @param uBase The base of the representation used.
2742 * If 0 the function will look for known prefixes before defaulting to 10.
2743 * @param pu16 Where to store the converted number. (optional)
2744 */
2745RTDECL(int) RTStrToUInt16Full(const char *pszValue, unsigned uBase, uint16_t *pu16);
2746
2747/**
2748 * Converts a string representation of a number to a 16-bit unsigned number.
2749 * The base is guessed.
2750 *
2751 * @returns 16-bit unsigned number on success.
2752 * @returns 0 on failure.
2753 * @param pszValue Pointer to the string value.
2754 */
2755RTDECL(uint16_t) RTStrToUInt16(const char *pszValue);
2756
2757/**
2758 * Converts a string representation of a number to a 8-bit unsigned number.
2759 *
2760 * @returns iprt status code.
2761 * Warnings are used to indicate conversion problems.
2762 * @retval VWRN_NUMBER_TOO_BIG
2763 * @retval VWRN_NEGATIVE_UNSIGNED
2764 * @retval VWRN_TRAILING_CHARS
2765 * @retval VWRN_TRAILING_SPACES
2766 * @retval VINF_SUCCESS
2767 * @retval VERR_NO_DIGITS
2768 *
2769 * @param pszValue Pointer to the string value.
2770 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2771 * @param uBase The base of the representation used.
2772 * If 0 the function will look for known prefixes before defaulting to 10.
2773 * @param pu8 Where to store the converted number. (optional)
2774 */
2775RTDECL(int) RTStrToUInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint8_t *pu8);
2776
2777/**
2778 * Converts a string representation of a number to a 8-bit unsigned number,
2779 * making sure the full string is converted.
2780 *
2781 * @returns iprt status code.
2782 * Warnings are used to indicate conversion problems.
2783 * @retval VWRN_NUMBER_TOO_BIG
2784 * @retval VWRN_NEGATIVE_UNSIGNED
2785 * @retval VINF_SUCCESS
2786 * @retval VERR_NO_DIGITS
2787 * @retval VERR_TRAILING_SPACES
2788 * @retval VERR_TRAILING_CHARS
2789 *
2790 * @param pszValue Pointer to the string value.
2791 * @param uBase The base of the representation used.
2792 * If 0 the function will look for known prefixes before defaulting to 10.
2793 * @param pu8 Where to store the converted number. (optional)
2794 */
2795RTDECL(int) RTStrToUInt8Full(const char *pszValue, unsigned uBase, uint8_t *pu8);
2796
2797/**
2798 * Converts a string representation of a number to a 8-bit unsigned number.
2799 * The base is guessed.
2800 *
2801 * @returns 8-bit unsigned number on success.
2802 * @returns 0 on failure.
2803 * @param pszValue Pointer to the string value.
2804 */
2805RTDECL(uint8_t) RTStrToUInt8(const char *pszValue);
2806
2807/**
2808 * Converts a string representation of a number to a 64-bit signed number.
2809 *
2810 * @returns iprt status code.
2811 * Warnings are used to indicate conversion problems.
2812 * @retval VWRN_NUMBER_TOO_BIG
2813 * @retval VWRN_TRAILING_CHARS
2814 * @retval VWRN_TRAILING_SPACES
2815 * @retval VINF_SUCCESS
2816 * @retval VERR_NO_DIGITS
2817 *
2818 * @param pszValue Pointer to the string value.
2819 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2820 * @param uBase The base of the representation used.
2821 * If 0 the function will look for known prefixes before defaulting to 10.
2822 * @param pi64 Where to store the converted number. (optional)
2823 */
2824RTDECL(int) RTStrToInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, int64_t *pi64);
2825
2826/**
2827 * Converts a string representation of a number to a 64-bit signed number,
2828 * making sure the full string is converted.
2829 *
2830 * @returns iprt status code.
2831 * Warnings are used to indicate conversion problems.
2832 * @retval VWRN_NUMBER_TOO_BIG
2833 * @retval VINF_SUCCESS
2834 * @retval VERR_TRAILING_CHARS
2835 * @retval VERR_TRAILING_SPACES
2836 * @retval VERR_NO_DIGITS
2837 *
2838 * @param pszValue Pointer to the string value.
2839 * @param uBase The base of the representation used.
2840 * If 0 the function will look for known prefixes before defaulting to 10.
2841 * @param pi64 Where to store the converted number. (optional)
2842 */
2843RTDECL(int) RTStrToInt64Full(const char *pszValue, unsigned uBase, int64_t *pi64);
2844
2845/**
2846 * Converts a string representation of a number to a 64-bit signed number.
2847 * The base is guessed.
2848 *
2849 * @returns 64-bit signed number on success.
2850 * @returns 0 on failure.
2851 * @param pszValue Pointer to the string value.
2852 */
2853RTDECL(int64_t) RTStrToInt64(const char *pszValue);
2854
2855/**
2856 * Converts a string representation of a number to a 32-bit signed number.
2857 *
2858 * @returns iprt status code.
2859 * Warnings are used to indicate conversion problems.
2860 * @retval VWRN_NUMBER_TOO_BIG
2861 * @retval VWRN_TRAILING_CHARS
2862 * @retval VWRN_TRAILING_SPACES
2863 * @retval VINF_SUCCESS
2864 * @retval VERR_NO_DIGITS
2865 *
2866 * @param pszValue Pointer to the string value.
2867 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2868 * @param uBase The base of the representation used.
2869 * If 0 the function will look for known prefixes before defaulting to 10.
2870 * @param pi32 Where to store the converted number. (optional)
2871 */
2872RTDECL(int) RTStrToInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, int32_t *pi32);
2873
2874/**
2875 * Converts a string representation of a number to a 32-bit signed number,
2876 * making sure the full string is converted.
2877 *
2878 * @returns iprt status code.
2879 * Warnings are used to indicate conversion problems.
2880 * @retval VWRN_NUMBER_TOO_BIG
2881 * @retval VINF_SUCCESS
2882 * @retval VERR_TRAILING_CHARS
2883 * @retval VERR_TRAILING_SPACES
2884 * @retval VERR_NO_DIGITS
2885 *
2886 * @param pszValue Pointer to the string value.
2887 * @param uBase The base of the representation used.
2888 * If 0 the function will look for known prefixes before defaulting to 10.
2889 * @param pi32 Where to store the converted number. (optional)
2890 */
2891RTDECL(int) RTStrToInt32Full(const char *pszValue, unsigned uBase, int32_t *pi32);
2892
2893/**
2894 * Converts a string representation of a number to a 32-bit signed number.
2895 * The base is guessed.
2896 *
2897 * @returns 32-bit signed number on success.
2898 * @returns 0 on failure.
2899 * @param pszValue Pointer to the string value.
2900 */
2901RTDECL(int32_t) RTStrToInt32(const char *pszValue);
2902
2903/**
2904 * Converts a string representation of a number to a 16-bit signed number.
2905 *
2906 * @returns iprt status code.
2907 * Warnings are used to indicate conversion problems.
2908 * @retval VWRN_NUMBER_TOO_BIG
2909 * @retval VWRN_TRAILING_CHARS
2910 * @retval VWRN_TRAILING_SPACES
2911 * @retval VINF_SUCCESS
2912 * @retval VERR_NO_DIGITS
2913 *
2914 * @param pszValue Pointer to the string value.
2915 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2916 * @param uBase The base of the representation used.
2917 * If 0 the function will look for known prefixes before defaulting to 10.
2918 * @param pi16 Where to store the converted number. (optional)
2919 */
2920RTDECL(int) RTStrToInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, int16_t *pi16);
2921
2922/**
2923 * Converts a string representation of a number to a 16-bit signed number,
2924 * making sure the full string is converted.
2925 *
2926 * @returns iprt status code.
2927 * Warnings are used to indicate conversion problems.
2928 * @retval VWRN_NUMBER_TOO_BIG
2929 * @retval VINF_SUCCESS
2930 * @retval VERR_TRAILING_CHARS
2931 * @retval VERR_TRAILING_SPACES
2932 * @retval VERR_NO_DIGITS
2933 *
2934 * @param pszValue Pointer to the string value.
2935 * @param uBase The base of the representation used.
2936 * If 0 the function will look for known prefixes before defaulting to 10.
2937 * @param pi16 Where to store the converted number. (optional)
2938 */
2939RTDECL(int) RTStrToInt16Full(const char *pszValue, unsigned uBase, int16_t *pi16);
2940
2941/**
2942 * Converts a string representation of a number to a 16-bit signed number.
2943 * The base is guessed.
2944 *
2945 * @returns 16-bit signed number on success.
2946 * @returns 0 on failure.
2947 * @param pszValue Pointer to the string value.
2948 */
2949RTDECL(int16_t) RTStrToInt16(const char *pszValue);
2950
2951/**
2952 * Converts a string representation of a number to a 8-bit signed number.
2953 *
2954 * @returns iprt status code.
2955 * Warnings are used to indicate conversion problems.
2956 * @retval VWRN_NUMBER_TOO_BIG
2957 * @retval VWRN_TRAILING_CHARS
2958 * @retval VWRN_TRAILING_SPACES
2959 * @retval VINF_SUCCESS
2960 * @retval VERR_NO_DIGITS
2961 *
2962 * @param pszValue Pointer to the string value.
2963 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2964 * @param uBase The base of the representation used.
2965 * If 0 the function will look for known prefixes before defaulting to 10.
2966 * @param pi8 Where to store the converted number. (optional)
2967 */
2968RTDECL(int) RTStrToInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, int8_t *pi8);
2969
2970/**
2971 * Converts a string representation of a number to a 8-bit signed number,
2972 * making sure the full string is converted.
2973 *
2974 * @returns iprt status code.
2975 * Warnings are used to indicate conversion problems.
2976 * @retval VWRN_NUMBER_TOO_BIG
2977 * @retval VINF_SUCCESS
2978 * @retval VERR_TRAILING_CHARS
2979 * @retval VERR_TRAILING_SPACES
2980 * @retval VERR_NO_DIGITS
2981 *
2982 * @param pszValue Pointer to the string value.
2983 * @param uBase The base of the representation used.
2984 * If 0 the function will look for known prefixes before defaulting to 10.
2985 * @param pi8 Where to store the converted number. (optional)
2986 */
2987RTDECL(int) RTStrToInt8Full(const char *pszValue, unsigned uBase, int8_t *pi8);
2988
2989/**
2990 * Converts a string representation of a number to a 8-bit signed number.
2991 * The base is guessed.
2992 *
2993 * @returns 8-bit signed number on success.
2994 * @returns 0 on failure.
2995 * @param pszValue Pointer to the string value.
2996 */
2997RTDECL(int8_t) RTStrToInt8(const char *pszValue);
2998
2999/**
3000 * Formats a buffer stream as hex bytes.
3001 *
3002 * The default is no separating spaces or line breaks or anything.
3003 *
3004 * @returns IPRT status code.
3005 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
3006 * @retval VERR_BUFFER_OVERFLOW if the buffer is insufficent to hold the bytes.
3007 *
3008 * @param pszBuf Output string buffer.
3009 * @param cchBuf The size of the output buffer.
3010 * @param pv Pointer to the bytes to stringify.
3011 * @param cb The number of bytes to stringify.
3012 * @param fFlags Combination of RTSTRPRINTHEXBYTES_F_XXX values.
3013 * @sa RTUtf16PrintHexBytes.
3014 */
3015RTDECL(int) RTStrPrintHexBytes(char *pszBuf, size_t cchBuf, void const *pv, size_t cb, uint32_t fFlags);
3016/** @name RTSTRPRINTHEXBYTES_F_XXX - flags for RTStrPrintHexBytes and RTUtf16PritnHexBytes.
3017 * @{ */
3018/** Upper case hex digits, the default is lower case. */
3019#define RTSTRPRINTHEXBYTES_F_UPPER RT_BIT(0)
3020/** @} */
3021
3022/**
3023 * Converts a string of hex bytes back into binary data.
3024 *
3025 * @returns IPRT status code.
3026 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
3027 * @retval VERR_BUFFER_OVERFLOW if the string contains too many hex bytes.
3028 * @retval VERR_BUFFER_UNDERFLOW if there aren't enough hex bytes to fill up
3029 * the output buffer.
3030 * @retval VERR_UNEVEN_INPUT if the input contains a half byte.
3031 * @retval VERR_NO_DIGITS
3032 * @retval VWRN_TRAILING_CHARS
3033 * @retval VWRN_TRAILING_SPACES
3034 *
3035 * @param pszHex The string containing the hex bytes.
3036 * @param pv Output buffer.
3037 * @param cb The size of the output buffer.
3038 * @param fFlags Must be zero, reserved for future use.
3039 */
3040RTDECL(int) RTStrConvertHexBytes(char const *pszHex, void *pv, size_t cb, uint32_t fFlags);
3041
3042/** @} */
3043
3044
3045/** @defgroup rt_str_space Unique String Space
3046 * @ingroup grp_rt_str
3047 * @{
3048 */
3049
3050/** Pointer to a string name space container node core. */
3051typedef struct RTSTRSPACECORE *PRTSTRSPACECORE;
3052/** Pointer to a pointer to a string name space container node core. */
3053typedef PRTSTRSPACECORE *PPRTSTRSPACECORE;
3054
3055/**
3056 * String name space container node core.
3057 */
3058typedef struct RTSTRSPACECORE
3059{
3060 /** Hash key. Don't touch. */
3061 uint32_t Key;
3062 /** Pointer to the left leaf node. Don't touch. */
3063 PRTSTRSPACECORE pLeft;
3064 /** Pointer to the left right node. Don't touch. */
3065 PRTSTRSPACECORE pRight;
3066 /** Pointer to the list of string with the same key. Don't touch. */
3067 PRTSTRSPACECORE pList;
3068 /** Height of this tree: max(heigth(left), heigth(right)) + 1. Don't touch */
3069 unsigned char uchHeight;
3070 /** The string length. Read only! */
3071 size_t cchString;
3072 /** Pointer to the string. Read only! */
3073 const char *pszString;
3074} RTSTRSPACECORE;
3075
3076/** String space. (Initialize with NULL.) */
3077typedef PRTSTRSPACECORE RTSTRSPACE;
3078/** Pointer to a string space. */
3079typedef PPRTSTRSPACECORE PRTSTRSPACE;
3080
3081
3082/**
3083 * Inserts a string into a unique string space.
3084 *
3085 * @returns true on success.
3086 * @returns false if the string collided with an existing string.
3087 * @param pStrSpace The space to insert it into.
3088 * @param pStr The string node.
3089 */
3090RTDECL(bool) RTStrSpaceInsert(PRTSTRSPACE pStrSpace, PRTSTRSPACECORE pStr);
3091
3092/**
3093 * Removes a string from a unique string space.
3094 *
3095 * @returns Pointer to the removed string node.
3096 * @returns NULL if the string was not found in the string space.
3097 * @param pStrSpace The space to remove it from.
3098 * @param pszString The string to remove.
3099 */
3100RTDECL(PRTSTRSPACECORE) RTStrSpaceRemove(PRTSTRSPACE pStrSpace, const char *pszString);
3101
3102/**
3103 * Gets a string from a unique string space.
3104 *
3105 * @returns Pointer to the string node.
3106 * @returns NULL if the string was not found in the string space.
3107 * @param pStrSpace The space to get it from.
3108 * @param pszString The string to get.
3109 */
3110RTDECL(PRTSTRSPACECORE) RTStrSpaceGet(PRTSTRSPACE pStrSpace, const char *pszString);
3111
3112/**
3113 * Gets a string from a unique string space.
3114 *
3115 * @returns Pointer to the string node.
3116 * @returns NULL if the string was not found in the string space.
3117 * @param pStrSpace The space to get it from.
3118 * @param pszString The string to get.
3119 * @param cchMax The max string length to evaluate. Passing
3120 * RTSTR_MAX is ok and makes it behave just like
3121 * RTStrSpaceGet.
3122 */
3123RTDECL(PRTSTRSPACECORE) RTStrSpaceGetN(PRTSTRSPACE pStrSpace, const char *pszString, size_t cchMax);
3124
3125/**
3126 * Callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy().
3127 *
3128 * @returns 0 on continue.
3129 * @returns Non-zero to aborts the operation.
3130 * @param pStr The string node
3131 * @param pvUser The user specified argument.
3132 */
3133typedef DECLCALLBACK(int) FNRTSTRSPACECALLBACK(PRTSTRSPACECORE pStr, void *pvUser);
3134/** Pointer to callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy(). */
3135typedef FNRTSTRSPACECALLBACK *PFNRTSTRSPACECALLBACK;
3136
3137/**
3138 * Destroys the string space.
3139 *
3140 * The caller supplies a callback which will be called for each of the string
3141 * nodes in for freeing their memory and other resources.
3142 *
3143 * @returns 0 or what ever non-zero return value pfnCallback returned
3144 * when aborting the destruction.
3145 * @param pStrSpace The space to destroy.
3146 * @param pfnCallback The callback.
3147 * @param pvUser The user argument.
3148 */
3149RTDECL(int) RTStrSpaceDestroy(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
3150
3151/**
3152 * Enumerates the string space.
3153 * The caller supplies a callback which will be called for each of
3154 * the string nodes.
3155 *
3156 * @returns 0 or what ever non-zero return value pfnCallback returned
3157 * when aborting the destruction.
3158 * @param pStrSpace The space to enumerate.
3159 * @param pfnCallback The callback.
3160 * @param pvUser The user argument.
3161 */
3162RTDECL(int) RTStrSpaceEnumerate(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
3163
3164/** @} */
3165
3166
3167/** @defgroup rt_str_hash Sting hashing
3168 * @ingroup grp_rt_str
3169 * @{ */
3170
3171/**
3172 * Hashes the given string using algorithm \#1.
3173 *
3174 * @returns String hash.
3175 * @param pszString The string to hash.
3176 */
3177RTDECL(uint32_t) RTStrHash1(const char *pszString);
3178
3179/**
3180 * Hashes the given string using algorithm \#1.
3181 *
3182 * @returns String hash.
3183 * @param pszString The string to hash.
3184 * @param cchString The max length to hash. Hashing will stop if the
3185 * terminator character is encountered first. Passing
3186 * RTSTR_MAX is fine.
3187 */
3188RTDECL(uint32_t) RTStrHash1N(const char *pszString, size_t cchString);
3189
3190/**
3191 * Hashes the given strings as if they were concatenated using algorithm \#1.
3192 *
3193 * @returns String hash.
3194 * @param cPairs The number of string / length pairs in the
3195 * ellipsis.
3196 * @param ... List of string (const char *) and length
3197 * (size_t) pairs. Passing RTSTR_MAX as the size is
3198 * fine.
3199 */
3200RTDECL(uint32_t) RTStrHash1ExN(size_t cPairs, ...);
3201
3202/**
3203 * Hashes the given strings as if they were concatenated using algorithm \#1.
3204 *
3205 * @returns String hash.
3206 * @param cPairs The number of string / length pairs in the @a va.
3207 * @param va List of string (const char *) and length
3208 * (size_t) pairs. Passing RTSTR_MAX as the size is
3209 * fine.
3210 */
3211RTDECL(uint32_t) RTStrHash1ExNV(size_t cPairs, va_list va);
3212
3213/** @} */
3214
3215
3216/** @defgroup rt_str_utf16 UTF-16 String Manipulation
3217 * @ingroup grp_rt_str
3218 * @{
3219 */
3220
3221/**
3222 * Allocates memory for UTF-16 string storage (default tag).
3223 *
3224 * You should normally not use this function, except if there is some very
3225 * custom string handling you need doing that isn't covered by any of the other
3226 * APIs.
3227 *
3228 * @returns Pointer to the allocated UTF-16 string. The first wide char is
3229 * always set to the string terminator char, the contents of the
3230 * remainder of the memory is undefined. The string must be freed by
3231 * calling RTUtf16Free.
3232 *
3233 * NULL is returned if the allocation failed. Please translate this to
3234 * VERR_NO_UTF16_MEMORY and not VERR_NO_MEMORY. Also consider
3235 * RTUtf16AllocEx if an IPRT status code is required.
3236 *
3237 * @param cb How many bytes to allocate, will be rounded up
3238 * to a multiple of two. If this is zero, we will
3239 * allocate a terminator wide char anyway.
3240 */
3241#define RTUtf16Alloc(cb) RTUtf16AllocTag((cb), RTSTR_TAG)
3242
3243/**
3244 * Allocates memory for UTF-16 string storage (custom tag).
3245 *
3246 * You should normally not use this function, except if there is some very
3247 * custom string handling you need doing that isn't covered by any of the other
3248 * APIs.
3249 *
3250 * @returns Pointer to the allocated UTF-16 string. The first wide char is
3251 * always set to the string terminator char, the contents of the
3252 * remainder of the memory is undefined. The string must be freed by
3253 * calling RTUtf16Free.
3254 *
3255 * NULL is returned if the allocation failed. Please translate this to
3256 * VERR_NO_UTF16_MEMORY and not VERR_NO_MEMORY. Also consider
3257 * RTUtf16AllocExTag if an IPRT status code is required.
3258 *
3259 * @param cb How many bytes to allocate, will be rounded up
3260 * to a multiple of two. If this is zero, we will
3261 * allocate a terminator wide char anyway.
3262 * @param pszTag Allocation tag used for statistics and such.
3263 */
3264RTDECL(PRTUTF16) RTUtf16AllocTag(size_t cb, const char *pszTag);
3265
3266
3267/**
3268 * Free a UTF-16 string allocated by RTStrToUtf16(), RTStrToUtf16Ex(),
3269 * RTLatin1ToUtf16(), RTLatin1ToUtf16Ex(), RTUtf16Dup() or RTUtf16DupEx().
3270 *
3271 * @returns iprt status code.
3272 * @param pwszString The UTF-16 string to free. NULL is accepted.
3273 */
3274RTDECL(void) RTUtf16Free(PRTUTF16 pwszString);
3275
3276/**
3277 * Allocates a new copy of the specified UTF-16 string (default tag).
3278 *
3279 * @returns Pointer to the allocated string copy. Use RTUtf16Free() to free it.
3280 * @returns NULL when out of memory.
3281 * @param pwszString UTF-16 string to duplicate.
3282 * @remark This function will not make any attempt to validate the encoding.
3283 */
3284#define RTUtf16Dup(pwszString) RTUtf16DupTag((pwszString), RTSTR_TAG)
3285
3286/**
3287 * Allocates a new copy of the specified UTF-16 string (custom tag).
3288 *
3289 * @returns Pointer to the allocated string copy. Use RTUtf16Free() to free it.
3290 * @returns NULL when out of memory.
3291 * @param pwszString UTF-16 string to duplicate.
3292 * @param pszTag Allocation tag used for statistics and such.
3293 * @remark This function will not make any attempt to validate the encoding.
3294 */
3295RTDECL(PRTUTF16) RTUtf16DupTag(PCRTUTF16 pwszString, const char *pszTag);
3296
3297/**
3298 * Allocates a new copy of the specified UTF-16 string (default tag).
3299 *
3300 * @returns iprt status code.
3301 * @param ppwszString Receives pointer of the allocated UTF-16 string.
3302 * The returned pointer must be freed using RTUtf16Free().
3303 * @param pwszString UTF-16 string to duplicate.
3304 * @param cwcExtra Number of extra RTUTF16 items to allocate.
3305 * @remark This function will not make any attempt to validate the encoding.
3306 */
3307#define RTUtf16DupEx(ppwszString, pwszString, cwcExtra) \
3308 RTUtf16DupExTag((ppwszString), (pwszString), (cwcExtra), RTSTR_TAG)
3309
3310/**
3311 * Allocates a new copy of the specified UTF-16 string (custom tag).
3312 *
3313 * @returns iprt status code.
3314 * @param ppwszString Receives pointer of the allocated UTF-16 string.
3315 * The returned pointer must be freed using RTUtf16Free().
3316 * @param pwszString UTF-16 string to duplicate.
3317 * @param cwcExtra Number of extra RTUTF16 items to allocate.
3318 * @param pszTag Allocation tag used for statistics and such.
3319 * @remark This function will not make any attempt to validate the encoding.
3320 */
3321RTDECL(int) RTUtf16DupExTag(PRTUTF16 *ppwszString, PCRTUTF16 pwszString, size_t cwcExtra, const char *pszTag);
3322
3323/**
3324 * Returns the length of a UTF-16 string in UTF-16 characters
3325 * without trailing '\\0'.
3326 *
3327 * Surrogate pairs counts as two UTF-16 characters here. Use RTUtf16CpCnt()
3328 * to get the exact number of code points in the string.
3329 *
3330 * @returns The number of RTUTF16 items in the string.
3331 * @param pwszString Pointer the UTF-16 string.
3332 * @remark This function will not make any attempt to validate the encoding.
3333 */
3334RTDECL(size_t) RTUtf16Len(PCRTUTF16 pwszString);
3335
3336/**
3337 * Find the length of a zero-terminated byte string, given a max string length.
3338 *
3339 * @returns The string length or cbMax. The returned length does not include
3340 * the zero terminator if it was found.
3341 *
3342 * @param pwszString The string.
3343 * @param cwcMax The max string length in RTUTF16s.
3344 * @sa RTUtf16NLenEx, RTStrNLen.
3345 */
3346RTDECL(size_t) RTUtf16NLen(PCRTUTF16 pwszString, size_t cwcMax);
3347
3348/**
3349 * Find the length of a zero-terminated byte string, given
3350 * a max string length.
3351 *
3352 * @returns IPRT status code.
3353 * @retval VINF_SUCCESS if the string has a length less than cchMax.
3354 * @retval VERR_BUFFER_OVERFLOW if the end of the string wasn't found
3355 * before cwcMax was reached.
3356 *
3357 * @param pwszString The string.
3358 * @param cwcMax The max string length in RTUTF16s.
3359 * @param pcwc Where to store the string length excluding the
3360 * terminator. This is set to cwcMax if the terminator
3361 * isn't found.
3362 * @sa RTUtf16NLen, RTStrNLenEx.
3363 */
3364RTDECL(int) RTUtf16NLenEx(PCRTUTF16 pwszString, size_t cwcMax, size_t *pcwc);
3365
3366/**
3367 * Find the zero terminator in a string with a limited length.
3368 *
3369 * @returns Pointer to the zero terminator.
3370 * @returns NULL if the zero terminator was not found.
3371 *
3372 * @param pwszString The string.
3373 * @param cwcMax The max string length. RTSTR_MAX is fine.
3374 */
3375RTDECL(PCRTUTF16) RTUtf16End(PCRTUTF16 pwszString, size_t cwcMax);
3376
3377/**
3378 * Strips blankspaces from both ends of the string.
3379 *
3380 * @returns Pointer to first non-blank char in the string.
3381 * @param pwsz The string to strip.
3382 */
3383RTDECL(PRTUTF16) RTUtf16Strip(PRTUTF16 pwsz);
3384
3385/**
3386 * Strips blankspaces from the start of the string.
3387 *
3388 * @returns Pointer to first non-blank char in the string.
3389 * @param pwsz The string to strip.
3390 */
3391RTDECL(PRTUTF16) RTUtf16StripL(PCRTUTF16 pwsz);
3392
3393/**
3394 * Strips blankspaces from the end of the string.
3395 *
3396 * @returns pwsz.
3397 * @param pwsz The string to strip.
3398 */
3399RTDECL(PRTUTF16) RTUtf16StripR(PRTUTF16 pwsz);
3400
3401/**
3402 * String copy with overflow handling.
3403 *
3404 * @retval VINF_SUCCESS on success.
3405 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
3406 * buffer will contain as much of the string as it can hold, fully
3407 * terminated.
3408 *
3409 * @param pwszDst The destination buffer.
3410 * @param cwcDst The size of the destination buffer in RTUTF16s.
3411 * @param pwszSrc The source string. NULL is not OK.
3412 */
3413RTDECL(int) RTUtf16Copy(PRTUTF16 pwszDst, size_t cwcDst, PCRTUTF16 pwszSrc);
3414
3415/**
3416 * String copy with overflow handling, ASCII source.
3417 *
3418 * @retval VINF_SUCCESS on success.
3419 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
3420 * buffer will contain as much of the string as it can hold, fully
3421 * terminated.
3422 *
3423 * @param pwszDst The destination buffer.
3424 * @param cwcDst The size of the destination buffer in RTUTF16s.
3425 * @param pszSrc The source string, pure ASCII. NULL is not OK.
3426 */
3427RTDECL(int) RTUtf16CopyAscii(PRTUTF16 pwszDst, size_t cwcDst, const char *pszSrc);
3428
3429/**
3430 * String copy with overflow handling.
3431 *
3432 * @retval VINF_SUCCESS on success.
3433 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
3434 * buffer will contain as much of the string as it can hold, fully
3435 * terminated.
3436 *
3437 * @param pwszDst The destination buffer.
3438 * @param cwcDst The size of the destination buffer in RTUTF16s.
3439 * @param pwszSrc The source string. NULL is not OK.
3440 * @param cwcSrcMax The maximum number of chars (not code points) to
3441 * copy from the source string, not counting the
3442 * terminator as usual.
3443 */
3444RTDECL(int) RTUtf16CopyEx(PRTUTF16 pwszDst, size_t cwcDst, PCRTUTF16 pwszSrc, size_t cwcSrcMax);
3445
3446/**
3447 * String concatenation with overflow handling.
3448 *
3449 * @retval VINF_SUCCESS on success.
3450 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
3451 * buffer will contain as much of the string as it can hold, fully
3452 * terminated.
3453 *
3454 * @param pszDst The destination buffer.
3455 * @param cwcDst The size of the destination buffer in RTUTF16s.
3456 * @param pwszSrc The source string. NULL is not OK.
3457 */
3458RTDECL(int) RTUtf16Cat(PRTUTF16 pwszDst, size_t cwcDst, PCRTUTF16 pwszSrc);
3459
3460/**
3461 * String concatenation with overflow handling, ASCII source.
3462 *
3463 * @retval VINF_SUCCESS on success.
3464 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
3465 * buffer will contain as much of the string as it can hold, fully
3466 * terminated.
3467 *
3468 * @param pszDst The destination buffer.
3469 * @param cwcDst The size of the destination buffer in RTUTF16s.
3470 * @param pszSrc The source string, pure ASCII. NULL is not OK.
3471 */
3472RTDECL(int) RTUtf16CatAscii(PRTUTF16 pwszDst, size_t cwcDst, const char *pwszSrc);
3473
3474/**
3475 * String concatenation with overflow handling.
3476 *
3477 * @retval VINF_SUCCESS on success.
3478 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
3479 * buffer will contain as much of the string as it can hold, fully
3480 * terminated.
3481 *
3482 * @param pwszDst The destination buffer.
3483 * @param cwcDst The size of the destination buffer in RTUTF16s.
3484 * @param pwszSrc The source string. NULL is not OK.
3485 * @param cwcSrcMax The maximum number of UTF-16 chars (not code
3486 * points) to copy from the source string, not
3487 * counting the terminator as usual.
3488 */
3489RTDECL(int) RTUtf16CatEx(PRTUTF16 pwszDst, size_t cwcDst, PCRTUTF16 pwszSrc, size_t cwcSrcMax);
3490
3491/**
3492 * Performs a case sensitive string compare between two UTF-16 strings.
3493 *
3494 * @returns < 0 if the first string less than the second string.s
3495 * @returns 0 if the first string identical to the second string.
3496 * @returns > 0 if the first string greater than the second string.
3497 * @param pwsz1 First UTF-16 string. Null is allowed.
3498 * @param pwsz2 Second UTF-16 string. Null is allowed.
3499 * @remark This function will not make any attempt to validate the encoding.
3500 */
3501RTDECL(int) RTUtf16Cmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
3502
3503/**
3504 * Performs a case sensitive string compare between an UTF-16 string and a pure
3505 * ASCII string.
3506 *
3507 * @returns < 0 if the first string less than the second string.s
3508 * @returns 0 if the first string identical to the second string.
3509 * @returns > 0 if the first string greater than the second string.
3510 * @param pwsz1 First UTF-16 string. Null is allowed.
3511 * @param psz2 Second string, pure ASCII. Null is allowed.
3512 * @remark This function will not make any attempt to validate the encoding.
3513 */
3514RTDECL(int) RTUtf16CmpAscii(PCRTUTF16 pwsz1, const char *psz2);
3515
3516/**
3517 * Performs a case insensitive string compare between two UTF-16 strings.
3518 *
3519 * This is a simplified compare, as only the simplified lower/upper case folding
3520 * specified by the unicode specs are used. It does not consider character pairs
3521 * as they are used in some languages, just simple upper & lower case compares.
3522 *
3523 * @returns < 0 if the first string less than the second string.
3524 * @returns 0 if the first string identical to the second string.
3525 * @returns > 0 if the first string greater than the second string.
3526 * @param pwsz1 First UTF-16 string. Null is allowed.
3527 * @param pwsz2 Second UTF-16 string. Null is allowed.
3528 */
3529RTDECL(int) RTUtf16ICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
3530
3531/**
3532 * Performs a case insensitive string compare between an UTF-16 string and an
3533 * pure ASCII string.
3534 *
3535 * Since this compare only takes cares about the first 128 codepoints in
3536 * unicode, no tables are needed and there aren't any real complications.
3537 *
3538 * @returns < 0 if the first string less than the second string.
3539 * @returns 0 if the first string identical to the second string.
3540 * @returns > 0 if the first string greater than the second string.
3541 * @param pwsz1 First UTF-16 string. Null is allowed.
3542 * @param psz2 Second string, pure ASCII. Null is allowed.
3543 */
3544RTDECL(int) RTUtf16ICmpAscii(PCRTUTF16 pwsz1, const char *psz2);
3545
3546/**
3547 * Performs a case insensitive string compare between two UTF-16 strings
3548 * using the current locale of the process (if applicable).
3549 *
3550 * This differs from RTUtf16ICmp() in that it will try, if a locale with the
3551 * required data is available, to do a correct case-insensitive compare. It
3552 * follows that it is more complex and thereby likely to be more expensive.
3553 *
3554 * @returns < 0 if the first string less than the second string.
3555 * @returns 0 if the first string identical to the second string.
3556 * @returns > 0 if the first string greater than the second string.
3557 * @param pwsz1 First UTF-16 string. Null is allowed.
3558 * @param pwsz2 Second UTF-16 string. Null is allowed.
3559 */
3560RTDECL(int) RTUtf16LocaleICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
3561
3562/**
3563 * Folds a UTF-16 string to lowercase.
3564 *
3565 * This is a very simple folding; is uses the simple lowercase
3566 * code point, it is not related to any locale just the most common
3567 * lowercase codepoint setup by the unicode specs, and it will not
3568 * create new surrogate pairs or remove existing ones.
3569 *
3570 * @returns Pointer to the passed in string.
3571 * @param pwsz The string to fold.
3572 */
3573RTDECL(PRTUTF16) RTUtf16ToLower(PRTUTF16 pwsz);
3574
3575/**
3576 * Folds a UTF-16 string to uppercase.
3577 *
3578 * This is a very simple folding; is uses the simple uppercase
3579 * code point, it is not related to any locale just the most common
3580 * uppercase codepoint setup by the unicode specs, and it will not
3581 * create new surrogate pairs or remove existing ones.
3582 *
3583 * @returns Pointer to the passed in string.
3584 * @param pwsz The string to fold.
3585 */
3586RTDECL(PRTUTF16) RTUtf16ToUpper(PRTUTF16 pwsz);
3587
3588/**
3589 * Validates the UTF-16 encoding of the string.
3590 *
3591 * @returns iprt status code.
3592 * @param pwsz The string.
3593 */
3594RTDECL(int) RTUtf16ValidateEncoding(PCRTUTF16 pwsz);
3595
3596/**
3597 * Validates the UTF-16 encoding of the string.
3598 *
3599 * @returns iprt status code.
3600 * @param pwsz The string.
3601 * @param cwc The max string length (/ size) in UTF-16 units. Use
3602 * RTSTR_MAX to process the entire string.
3603 * @param fFlags Combination of RTSTR_VALIDATE_ENCODING_XXX flags.
3604 */
3605RTDECL(int) RTUtf16ValidateEncodingEx(PCRTUTF16 pwsz, size_t cwc, uint32_t fFlags);
3606
3607/**
3608 * Checks if the UTF-16 encoding is valid.
3609 *
3610 * @returns true / false.
3611 * @param pwsz The string.
3612 */
3613RTDECL(bool) RTUtf16IsValidEncoding(PCRTUTF16 pwsz);
3614
3615/**
3616 * Sanitise a (valid) UTF-16 string by replacing all characters outside a white
3617 * list in-place by an ASCII replacement character. Multi-byte characters will
3618 * be replaced byte by byte.
3619 *
3620 * @returns The number of code points replaced, or a negative value if the
3621 * string is not correctly encoded. In this last case the string
3622 * may be partially processed.
3623 * @param pwsz The string to sanitise.
3624 * @param puszValidSets A zero-terminated array of pairs of Unicode points.
3625 * Each pair is the start and end point of a range,
3626 * and the union of these ranges forms the white list.
3627 * @param chReplacement The ASCII replacement character.
3628 */
3629RTDECL(ssize_t) RTUtf16PurgeComplementSet(PRTUTF16 pwsz, PCRTUNICP puszValidSet, char chReplacement);
3630
3631/**
3632 * Translate a UTF-16 string into a UTF-8 allocating the result buffer (default
3633 * tag).
3634 *
3635 * @returns iprt status code.
3636 * @param pwszString UTF-16 string to convert.
3637 * @param ppszString Receives pointer of allocated UTF-8 string on
3638 * success, and is always set to NULL on failure.
3639 * The returned pointer must be freed using RTStrFree().
3640 */
3641#define RTUtf16ToUtf8(pwszString, ppszString) RTUtf16ToUtf8Tag((pwszString), (ppszString), RTSTR_TAG)
3642
3643/**
3644 * Translate a UTF-16 string into a UTF-8 allocating the result buffer.
3645 *
3646 * @returns iprt status code.
3647 * @param pwszString UTF-16 string to convert.
3648 * @param ppszString Receives pointer of allocated UTF-8 string on
3649 * success, and is always set to NULL on failure.
3650 * The returned pointer must be freed using RTStrFree().
3651 * @param pszTag Allocation tag used for statistics and such.
3652 */
3653RTDECL(int) RTUtf16ToUtf8Tag(PCRTUTF16 pwszString, char **ppszString, const char *pszTag);
3654
3655/**
3656 * Translates UTF-16 to UTF-8 using buffer provided by the caller or a fittingly
3657 * sized buffer allocated by the function (default tag).
3658 *
3659 * @returns iprt status code.
3660 * @param pwszString The UTF-16 string to convert.
3661 * @param cwcString The number of RTUTF16 items to translate from pwszString.
3662 * The translation will stop when reaching cwcString or the terminator ('\\0').
3663 * Use RTSTR_MAX to translate the entire string.
3664 * @param ppsz If cch is non-zero, this must either be pointing to a pointer to
3665 * a buffer of the specified size, or pointer to a NULL pointer.
3666 * If *ppsz is NULL or cch is zero a buffer of at least cch chars
3667 * will be allocated to hold the translated string.
3668 * If a buffer was requested it must be freed using RTStrFree().
3669 * @param cch The buffer size in chars (the type). This includes the terminator.
3670 * @param pcch Where to store the length of the translated string,
3671 * excluding the terminator. (Optional)
3672 *
3673 * This may be set under some error conditions,
3674 * however, only for VERR_BUFFER_OVERFLOW and
3675 * VERR_NO_STR_MEMORY will it contain a valid string
3676 * length that can be used to resize the buffer.
3677 */
3678#define RTUtf16ToUtf8Ex(pwszString, cwcString, ppsz, cch, pcch) \
3679 RTUtf16ToUtf8ExTag((pwszString), (cwcString), (ppsz), (cch), (pcch), RTSTR_TAG)
3680
3681/**
3682 * Translates UTF-16 to UTF-8 using buffer provided by the caller or a fittingly
3683 * sized buffer allocated by the function (custom tag).
3684 *
3685 * @returns iprt status code.
3686 * @param pwszString The UTF-16 string to convert.
3687 * @param cwcString The number of RTUTF16 items to translate from pwszString.
3688 * The translation will stop when reaching cwcString or the terminator ('\\0').
3689 * Use RTSTR_MAX to translate the entire string.
3690 * @param ppsz If cch is non-zero, this must either be pointing to a pointer to
3691 * a buffer of the specified size, or pointer to a NULL pointer.
3692 * If *ppsz is NULL or cch is zero a buffer of at least cch chars
3693 * will be allocated to hold the translated string.
3694 * If a buffer was requested it must be freed using RTStrFree().
3695 * @param cch The buffer size in chars (the type). This includes the terminator.
3696 * @param pcch Where to store the length of the translated string,
3697 * excluding the terminator. (Optional)
3698 *
3699 * This may be set under some error conditions,
3700 * however, only for VERR_BUFFER_OVERFLOW and
3701 * VERR_NO_STR_MEMORY will it contain a valid string
3702 * length that can be used to resize the buffer.
3703 * @param pszTag Allocation tag used for statistics and such.
3704 */
3705RTDECL(int) RTUtf16ToUtf8ExTag(PCRTUTF16 pwszString, size_t cwcString, char **ppsz, size_t cch, size_t *pcch, const char *pszTag);
3706
3707/**
3708 * Calculates the length of the UTF-16 string in UTF-8 chars (bytes).
3709 *
3710 * This function will validate the string, and incorrectly encoded UTF-16
3711 * strings will be rejected. The primary purpose of this function is to
3712 * help allocate buffers for RTUtf16ToUtf8() of the correct size. For most
3713 * other purposes RTUtf16ToUtf8Ex() should be used.
3714 *
3715 * @returns Number of char (bytes).
3716 * @returns 0 if the string was incorrectly encoded.
3717 * @param pwsz The UTF-16 string.
3718 */
3719RTDECL(size_t) RTUtf16CalcUtf8Len(PCRTUTF16 pwsz);
3720
3721/**
3722 * Calculates the length of the UTF-16 string in UTF-8 chars (bytes).
3723 *
3724 * This function will validate the string, and incorrectly encoded UTF-16
3725 * strings will be rejected.
3726 *
3727 * @returns iprt status code.
3728 * @param pwsz The string.
3729 * @param cwc The max string length. Use RTSTR_MAX to process the entire string.
3730 * @param pcch Where to store the string length (in bytes). Optional.
3731 * This is undefined on failure.
3732 */
3733RTDECL(int) RTUtf16CalcUtf8LenEx(PCRTUTF16 pwsz, size_t cwc, size_t *pcch);
3734
3735/**
3736 * Translate a UTF-16 string into a Latin-1 (ISO-8859-1) allocating the result
3737 * buffer (default tag).
3738 *
3739 * @returns iprt status code.
3740 * @param pwszString UTF-16 string to convert.
3741 * @param ppszString Receives pointer of allocated Latin1 string on
3742 * success, and is always set to NULL on failure.
3743 * The returned pointer must be freed using RTStrFree().
3744 */
3745#define RTUtf16ToLatin1(pwszString, ppszString) RTUtf16ToLatin1Tag((pwszString), (ppszString), RTSTR_TAG)
3746
3747/**
3748 * Translate a UTF-16 string into a Latin-1 (ISO-8859-1) allocating the result
3749 * buffer (custom tag).
3750 *
3751 * @returns iprt status code.
3752 * @param pwszString UTF-16 string to convert.
3753 * @param ppszString Receives pointer of allocated Latin1 string on
3754 * success, and is always set to NULL on failure.
3755 * The returned pointer must be freed using RTStrFree().
3756 * @param pszTag Allocation tag used for statistics and such.
3757 */
3758RTDECL(int) RTUtf16ToLatin1Tag(PCRTUTF16 pwszString, char **ppszString, const char *pszTag);
3759
3760/**
3761 * Translates UTF-16 to Latin-1 (ISO-8859-1) using buffer provided by the caller
3762 * or a fittingly sized buffer allocated by the function (default tag).
3763 *
3764 * @returns iprt status code.
3765 * @param pwszString The UTF-16 string to convert.
3766 * @param cwcString The number of RTUTF16 items to translate from
3767 * pwszString. The translation will stop when reaching
3768 * cwcString or the terminator ('\\0'). Use RTSTR_MAX
3769 * to translate the entire string.
3770 * @param ppsz Pointer to the pointer to the Latin-1 string. The
3771 * buffer can optionally be preallocated by the caller.
3772 *
3773 * If cch is zero, *ppsz is undefined.
3774 *
3775 * If cch is non-zero and *ppsz is not NULL, then this
3776 * will be used as the output buffer.
3777 * VERR_BUFFER_OVERFLOW will be returned if this is
3778 * insufficient.
3779 *
3780 * If cch is zero or *ppsz is NULL, then a buffer of
3781 * sufficient size is allocated. cch can be used to
3782 * specify a minimum size of this buffer. Use
3783 * RTUtf16Free() to free the result.
3784 *
3785 * @param cch The buffer size in chars (the type). This includes
3786 * the terminator.
3787 * @param pcch Where to store the length of the translated string,
3788 * excluding the terminator. (Optional)
3789 *
3790 * This may be set under some error conditions,
3791 * however, only for VERR_BUFFER_OVERFLOW and
3792 * VERR_NO_STR_MEMORY will it contain a valid string
3793 * length that can be used to resize the buffer.
3794 */
3795#define RTUtf16ToLatin1Ex(pwszString, cwcString, ppsz, cch, pcch) \
3796 RTUtf16ToLatin1ExTag((pwszString), (cwcString), (ppsz), (cch), (pcch), RTSTR_TAG)
3797
3798/**
3799 * Translates UTF-16 to Latin-1 (ISO-8859-1) using buffer provided by the caller
3800 * or a fittingly sized buffer allocated by the function (custom tag).
3801 *
3802 * @returns iprt status code.
3803 * @param pwszString The UTF-16 string to convert.
3804 * @param cwcString The number of RTUTF16 items to translate from
3805 * pwszString. The translation will stop when reaching
3806 * cwcString or the terminator ('\\0'). Use RTSTR_MAX
3807 * to translate the entire string.
3808 * @param ppsz Pointer to the pointer to the Latin-1 string. The
3809 * buffer can optionally be preallocated by the caller.
3810 *
3811 * If cch is zero, *ppsz is undefined.
3812 *
3813 * If cch is non-zero and *ppsz is not NULL, then this
3814 * will be used as the output buffer.
3815 * VERR_BUFFER_OVERFLOW will be returned if this is
3816 * insufficient.
3817 *
3818 * If cch is zero or *ppsz is NULL, then a buffer of
3819 * sufficient size is allocated. cch can be used to
3820 * specify a minimum size of this buffer. Use
3821 * RTUtf16Free() to free the result.
3822 *
3823 * @param cch The buffer size in chars (the type). This includes
3824 * the terminator.
3825 * @param pcch Where to store the length of the translated string,
3826 * excluding the terminator. (Optional)
3827 *
3828 * This may be set under some error conditions,
3829 * however, only for VERR_BUFFER_OVERFLOW and
3830 * VERR_NO_STR_MEMORY will it contain a valid string
3831 * length that can be used to resize the buffer.
3832 * @param pszTag Allocation tag used for statistics and such.
3833 */
3834RTDECL(int) RTUtf16ToLatin1ExTag(PCRTUTF16 pwszString, size_t cwcString, char **ppsz, size_t cch, size_t *pcch, const char *pszTag);
3835
3836/**
3837 * Calculates the length of the UTF-16 string in Latin-1 (ISO-8859-1) chars.
3838 *
3839 * This function will validate the string, and incorrectly encoded UTF-16
3840 * strings will be rejected. The primary purpose of this function is to
3841 * help allocate buffers for RTUtf16ToLatin1() of the correct size. For most
3842 * other purposes RTUtf16ToLatin1Ex() should be used.
3843 *
3844 * @returns Number of char (bytes).
3845 * @returns 0 if the string was incorrectly encoded.
3846 * @param pwsz The UTF-16 string.
3847 */
3848RTDECL(size_t) RTUtf16CalcLatin1Len(PCRTUTF16 pwsz);
3849
3850/**
3851 * Calculates the length of the UTF-16 string in Latin-1 (ISO-8859-1) chars.
3852 *
3853 * This function will validate the string, and incorrectly encoded UTF-16
3854 * strings will be rejected.
3855 *
3856 * @returns iprt status code.
3857 * @param pwsz The string.
3858 * @param cwc The max string length. Use RTSTR_MAX to process the
3859 * entire string.
3860 * @param pcch Where to store the string length (in bytes). Optional.
3861 * This is undefined on failure.
3862 */
3863RTDECL(int) RTUtf16CalcLatin1LenEx(PCRTUTF16 pwsz, size_t cwc, size_t *pcch);
3864
3865/**
3866 * Get the unicode code point at the given string position.
3867 *
3868 * @returns unicode code point.
3869 * @returns RTUNICP_INVALID if the encoding is invalid.
3870 * @param pwsz The string.
3871 *
3872 * @remark This is an internal worker for RTUtf16GetCp().
3873 */
3874RTDECL(RTUNICP) RTUtf16GetCpInternal(PCRTUTF16 pwsz);
3875
3876/**
3877 * Get the unicode code point at the given string position.
3878 *
3879 * @returns iprt status code.
3880 * @param ppwsz Pointer to the string pointer. This will be updated to
3881 * point to the char following the current code point.
3882 * @param pCp Where to store the code point.
3883 * RTUNICP_INVALID is stored here on failure.
3884 *
3885 * @remark This is an internal worker for RTUtf16GetCpEx().
3886 */
3887RTDECL(int) RTUtf16GetCpExInternal(PCRTUTF16 *ppwsz, PRTUNICP pCp);
3888
3889/**
3890 * Put the unicode code point at the given string position
3891 * and return the pointer to the char following it.
3892 *
3893 * This function will not consider anything at or following the
3894 * buffer area pointed to by pwsz. It is therefore not suitable for
3895 * inserting code points into a string, only appending/overwriting.
3896 *
3897 * @returns pointer to the char following the written code point.
3898 * @param pwsz The string.
3899 * @param CodePoint The code point to write.
3900 * This should not be RTUNICP_INVALID or any other
3901 * character out of the UTF-16 range.
3902 *
3903 * @remark This is an internal worker for RTUtf16GetCpEx().
3904 */
3905RTDECL(PRTUTF16) RTUtf16PutCpInternal(PRTUTF16 pwsz, RTUNICP CodePoint);
3906
3907/**
3908 * Get the unicode code point at the given string position.
3909 *
3910 * @returns unicode code point.
3911 * @returns RTUNICP_INVALID if the encoding is invalid.
3912 * @param pwsz The string.
3913 *
3914 * @remark We optimize this operation by using an inline function for
3915 * everything which isn't a surrogate pair or an endian indicator.
3916 */
3917DECLINLINE(RTUNICP) RTUtf16GetCp(PCRTUTF16 pwsz)
3918{
3919 const RTUTF16 wc = *pwsz;
3920 if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
3921 return wc;
3922 return RTUtf16GetCpInternal(pwsz);
3923}
3924
3925/**
3926 * Get the unicode code point at the given string position.
3927 *
3928 * @returns iprt status code.
3929 * @param ppwsz Pointer to the string pointer. This will be updated to
3930 * point to the char following the current code point.
3931 * @param pCp Where to store the code point.
3932 * RTUNICP_INVALID is stored here on failure.
3933 *
3934 * @remark We optimize this operation by using an inline function for
3935 * everything which isn't a surrogate pair or and endian indicator.
3936 */
3937DECLINLINE(int) RTUtf16GetCpEx(PCRTUTF16 *ppwsz, PRTUNICP pCp)
3938{
3939 const RTUTF16 wc = **ppwsz;
3940 if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
3941 {
3942 (*ppwsz)++;
3943 *pCp = wc;
3944 return VINF_SUCCESS;
3945 }
3946 return RTUtf16GetCpExInternal(ppwsz, pCp);
3947}
3948
3949/**
3950 * Put the unicode code point at the given string position
3951 * and return the pointer to the char following it.
3952 *
3953 * This function will not consider anything at or following the
3954 * buffer area pointed to by pwsz. It is therefore not suitable for
3955 * inserting code points into a string, only appending/overwriting.
3956 *
3957 * @returns pointer to the char following the written code point.
3958 * @param pwsz The string.
3959 * @param CodePoint The code point to write.
3960 * This should not be RTUNICP_INVALID or any other
3961 * character out of the UTF-16 range.
3962 *
3963 * @remark We optimize this operation by using an inline function for
3964 * everything which isn't a surrogate pair or and endian indicator.
3965 */
3966DECLINLINE(PRTUTF16) RTUtf16PutCp(PRTUTF16 pwsz, RTUNICP CodePoint)
3967{
3968 if (CodePoint < 0xd800 || (CodePoint > 0xd800 && CodePoint < 0xfffe))
3969 {
3970 *pwsz++ = (RTUTF16)CodePoint;
3971 return pwsz;
3972 }
3973 return RTUtf16PutCpInternal(pwsz, CodePoint);
3974}
3975
3976/**
3977 * Skips ahead, past the current code point.
3978 *
3979 * @returns Pointer to the char after the current code point.
3980 * @param pwsz Pointer to the current code point.
3981 * @remark This will not move the next valid code point, only past the current one.
3982 */
3983DECLINLINE(PRTUTF16) RTUtf16NextCp(PCRTUTF16 pwsz)
3984{
3985 RTUNICP Cp;
3986 RTUtf16GetCpEx(&pwsz, &Cp);
3987 return (PRTUTF16)pwsz;
3988}
3989
3990/**
3991 * Skips backwards, to the previous code point.
3992 *
3993 * @returns Pointer to the char after the current code point.
3994 * @param pwszStart Pointer to the start of the string.
3995 * @param pwsz Pointer to the current code point.
3996 */
3997RTDECL(PRTUTF16) RTUtf16PrevCp(PCRTUTF16 pwszStart, PCRTUTF16 pwsz);
3998
3999
4000/**
4001 * Checks if the UTF-16 char is the high surrogate char (i.e.
4002 * the 1st char in the pair).
4003 *
4004 * @returns true if it is.
4005 * @returns false if it isn't.
4006 * @param wc The character to investigate.
4007 */
4008DECLINLINE(bool) RTUtf16IsHighSurrogate(RTUTF16 wc)
4009{
4010 return wc >= 0xd800 && wc <= 0xdbff;
4011}
4012
4013/**
4014 * Checks if the UTF-16 char is the low surrogate char (i.e.
4015 * the 2nd char in the pair).
4016 *
4017 * @returns true if it is.
4018 * @returns false if it isn't.
4019 * @param wc The character to investigate.
4020 */
4021DECLINLINE(bool) RTUtf16IsLowSurrogate(RTUTF16 wc)
4022{
4023 return wc >= 0xdc00 && wc <= 0xdfff;
4024}
4025
4026
4027/**
4028 * Checks if the two UTF-16 chars form a valid surrogate pair.
4029 *
4030 * @returns true if they do.
4031 * @returns false if they doesn't.
4032 * @param wcHigh The high (1st) character.
4033 * @param wcLow The low (2nd) character.
4034 */
4035DECLINLINE(bool) RTUtf16IsSurrogatePair(RTUTF16 wcHigh, RTUTF16 wcLow)
4036{
4037 return RTUtf16IsHighSurrogate(wcHigh)
4038 && RTUtf16IsLowSurrogate(wcLow);
4039}
4040
4041/**
4042 * Formats a buffer stream as hex bytes.
4043 *
4044 * The default is no separating spaces or line breaks or anything.
4045 *
4046 * @returns IPRT status code.
4047 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
4048 * @retval VERR_BUFFER_OVERFLOW if the buffer is insufficent to hold the bytes.
4049 *
4050 * @param pwszBuf Output string buffer.
4051 * @param cwcBuf The size of the output buffer in RTUTF16 units.
4052 * @param pv Pointer to the bytes to stringify.
4053 * @param cb The number of bytes to stringify.
4054 * @param fFlags Combination of RTSTRPRINTHEXBYTES_F_XXX values.
4055 * @sa RTStrPrintHexBytes.
4056 */
4057RTDECL(int) RTUtf16PrintHexBytes(PRTUTF16 pwszBuf, size_t cwcBuf, void const *pv, size_t cb, uint32_t fFlags);
4058
4059/** @} */
4060
4061
4062/** @defgroup rt_str_latin1 Latin-1 (ISO-8859-1) String Manipulation
4063 * @ingroup grp_rt_str
4064 * @{
4065 */
4066
4067/**
4068 * Calculates the length of the Latin-1 (ISO-8859-1) string in RTUTF16 items.
4069 *
4070 * @returns Number of RTUTF16 items.
4071 * @param psz The Latin-1 string.
4072 */
4073RTDECL(size_t) RTLatin1CalcUtf16Len(const char *psz);
4074
4075/**
4076 * Calculates the length of the Latin-1 (ISO-8859-1) string in RTUTF16 items.
4077 *
4078 * @returns iprt status code.
4079 * @param psz The Latin-1 string.
4080 * @param cch The max string length. Use RTSTR_MAX to process the
4081 * entire string.
4082 * @param pcwc Where to store the string length. Optional.
4083 * This is undefined on failure.
4084 */
4085RTDECL(int) RTLatin1CalcUtf16LenEx(const char *psz, size_t cch, size_t *pcwc);
4086
4087/**
4088 * Translate a Latin-1 (ISO-8859-1) string into a UTF-16 allocating the result
4089 * buffer (default tag).
4090 *
4091 * @returns iprt status code.
4092 * @param pszString The Latin-1 string to convert.
4093 * @param ppwszString Receives pointer to the allocated UTF-16 string. The
4094 * returned string must be freed using RTUtf16Free().
4095 */
4096#define RTLatin1ToUtf16(pszString, ppwszString) RTLatin1ToUtf16Tag((pszString), (ppwszString), RTSTR_TAG)
4097
4098/**
4099 * Translate a Latin-1 (ISO-8859-1) string into a UTF-16 allocating the result
4100 * buffer (custom tag).
4101 *
4102 * @returns iprt status code.
4103 * @param pszString The Latin-1 string to convert.
4104 * @param ppwszString Receives pointer to the allocated UTF-16 string. The
4105 * returned string must be freed using RTUtf16Free().
4106 * @param pszTag Allocation tag used for statistics and such.
4107 */
4108RTDECL(int) RTLatin1ToUtf16Tag(const char *pszString, PRTUTF16 *ppwszString, const char *pszTag);
4109
4110/**
4111 * Translates pszString from Latin-1 (ISO-8859-1) to UTF-16, allocating the
4112 * result buffer if requested (default tag).
4113 *
4114 * @returns iprt status code.
4115 * @param pszString The Latin-1 string to convert.
4116 * @param cchString The maximum size in chars (the type) to convert.
4117 * The conversion stops when it reaches cchString or
4118 * the string terminator ('\\0').
4119 * Use RTSTR_MAX to translate the entire string.
4120 * @param ppwsz If cwc is non-zero, this must either be pointing
4121 * to pointer to a buffer of the specified size, or
4122 * pointer to a NULL pointer.
4123 * If *ppwsz is NULL or cwc is zero a buffer of at
4124 * least cwc items will be allocated to hold the
4125 * translated string. If a buffer was requested it
4126 * must be freed using RTUtf16Free().
4127 * @param cwc The buffer size in RTUTF16s. This includes the
4128 * terminator.
4129 * @param pcwc Where to store the length of the translated string,
4130 * excluding the terminator. (Optional)
4131 *
4132 * This may be set under some error conditions,
4133 * however, only for VERR_BUFFER_OVERFLOW and
4134 * VERR_NO_STR_MEMORY will it contain a valid string
4135 * length that can be used to resize the buffer.
4136 */
4137#define RTLatin1ToUtf16Ex(pszString, cchString, ppwsz, cwc, pcwc) \
4138 RTLatin1ToUtf16ExTag((pszString), (cchString), (ppwsz), (cwc), (pcwc), RTSTR_TAG)
4139
4140/**
4141 * Translates pszString from Latin-1 (ISO-8859-1) to UTF-16, allocating the
4142 * result buffer if requested.
4143 *
4144 * @returns iprt status code.
4145 * @param pszString The Latin-1 string to convert.
4146 * @param cchString The maximum size in chars (the type) to convert.
4147 * The conversion stops when it reaches cchString or
4148 * the string terminator ('\\0').
4149 * Use RTSTR_MAX to translate the entire string.
4150 * @param ppwsz If cwc is non-zero, this must either be pointing
4151 * to pointer to a buffer of the specified size, or
4152 * pointer to a NULL pointer.
4153 * If *ppwsz is NULL or cwc is zero a buffer of at
4154 * least cwc items will be allocated to hold the
4155 * translated string. If a buffer was requested it
4156 * must be freed using RTUtf16Free().
4157 * @param cwc The buffer size in RTUTF16s. This includes the
4158 * terminator.
4159 * @param pcwc Where to store the length of the translated string,
4160 * excluding the terminator. (Optional)
4161 *
4162 * This may be set under some error conditions,
4163 * however, only for VERR_BUFFER_OVERFLOW and
4164 * VERR_NO_STR_MEMORY will it contain a valid string
4165 * length that can be used to resize the buffer.
4166 * @param pszTag Allocation tag used for statistics and such.
4167 */
4168RTDECL(int) RTLatin1ToUtf16ExTag(const char *pszString, size_t cchString,
4169 PRTUTF16 *ppwsz, size_t cwc, size_t *pcwc, const char *pszTag);
4170
4171/** @} */
4172
4173#ifndef ___iprt_nocrt_string_h
4174# if defined(RT_OS_WINDOWS)
4175RTDECL(void *) mempcpy(void *pvDst, const void *pvSrc, size_t cb);
4176# endif
4177#endif
4178
4179
4180RT_C_DECLS_END
4181
4182/** @} */
4183
4184#endif
4185
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