VirtualBox

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

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

iprt/string.h: Provide memrchr on platforms which doesn't have it.

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

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