VirtualBox

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

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

iprt/string.h: Added RTStrOffCharOrTerm.

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

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