VirtualBox

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

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

Introducing RT_IPRT_FORMAT_ATTR for the purpose of marking functions working on our special format strings.

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

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