VirtualBox

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

Last change on this file since 57250 was 57184, checked in by vboxsync, 9 years ago

include/iprt/string.h: hack for Linux 4.2

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

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