VirtualBox

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

Last change on this file since 33111 was 33111, checked in by vboxsync, 14 years ago

FreeBSD: build fixes (thanks to Bernhard Froehlich)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 132.6 KB
Line 
1/** @file
2 * IPRT - String Manipluation.
3 */
4
5/*
6 * Copyright (C) 2006-2010 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___iprt_string_h
27#define ___iprt_string_h
28
29#include <iprt/cdefs.h>
30#include <iprt/types.h>
31#include <iprt/assert.h>
32#include <iprt/stdarg.h>
33#include <iprt/err.h> /* for VINF_SUCCESS */
34#if defined(RT_OS_LINUX) && defined(__KERNEL__)
35# include <linux/string.h>
36#elif defined(RT_OS_FREEBSD) && defined(_KERNEL)
37/** @todo
38 * XXX: Very ugly hack to get things build on recent FreeBSD builds. They have
39 * memchr now and we need to include param.h to get __FreeBSD_version and make
40 * memchr available based on the version below or we can't compile the kernel
41 * module on older versions anymore.
42 *
43 * But including param.h here opens Pandora's box because we clash with a few
44 * defines namely PVM and PAGE_SIZE. We can safely undefine PVM here but not
45 * PAGE_SIZE because this results in build errors sooner or later. Luckily this
46 * define is in a header included by param.h (machine/param.h). We define the
47 * guards here to prevent inclusion of it if PAGE_SIZE was defined already.
48 *
49 * @todo aeichner: Search for an elegant solution and cleanup this mess ASAP!
50 */
51# ifdef PAGE_SIZE
52# define _AMD64_INCLUDE_PARAM_H_
53# define _I386_INCLUDE_PARAM_H_
54# define _MACHINE_PARAM_H_
55# endif
56# include <sys/param.h> /* __FreeBSD_version */
57# undef PVM
58# include <sys/libkern.h>
59 /*
60 * No memmove on versions < 7.2
61 * Defining a macro using bcopy here
62 */
63# define memmove(dst, src, size) bcopy(src, dst, size)
64#elif defined(RT_OS_SOLARIS) && defined(_KERNEL)
65 /*
66 * Same case as with FreeBSD kernel:
67 * The string.h stuff clashes with sys/system.h
68 * ffs = find first set bit.
69 */
70# define ffs ffs_string_h
71# include <string.h>
72# undef ffs
73# undef strpbrk
74#else
75# include <string.h>
76#endif
77
78/*
79 * Supply prototypes for standard string functions provided by
80 * IPRT instead of the operating environment.
81 */
82#if defined(RT_OS_DARWIN) && defined(KERNEL)
83RT_C_DECLS_BEGIN
84void *memchr(const void *pv, int ch, size_t cb);
85char *strpbrk(const char *pszStr, const char *pszChars);
86RT_C_DECLS_END
87#endif
88
89#if defined(RT_OS_FREEBSD) && defined(_KERNEL)
90RT_C_DECLS_BEGIN
91#if __FreeBSD_version < 900000
92void *memchr(const void *pv, int ch, size_t cb);
93#endif
94char *strpbrk(const char *pszStr, const char *pszChars);
95RT_C_DECLS_END
96#endif
97
98/** @def RT_USE_RTC_3629
99 * When defined the UTF-8 range will stop at 0x10ffff. If not defined, the
100 * range stops at 0x7fffffff.
101 * @remarks Must be defined both when building and using the IPRT. */
102#ifdef DOXYGEN_RUNNING
103# define RT_USE_RTC_3629
104#endif
105
106
107/**
108 * Byte zero the specified object.
109 *
110 * This will use sizeof(Obj) to figure the size and will call memset, bzero
111 * or some compiler intrinsic to perform the actual zeroing.
112 *
113 * @param Obj The object to zero. Make sure to dereference pointers.
114 *
115 * @remarks Because the macro may use memset it has been placed in string.h
116 * instead of cdefs.h to avoid build issues because someone forgot
117 * to include this header.
118 *
119 * @ingroup grp_rt_cdefs
120 */
121#define RT_ZERO(Obj) RT_BZERO(&(Obj), sizeof(Obj))
122
123/**
124 * Byte zero the specified memory area.
125 *
126 * This will call memset, bzero or some compiler intrinsic to clear the
127 * specified bytes of memory.
128 *
129 * @param pv Pointer to the memory.
130 * @param cb The number of bytes to clear. Please, don't pass 0.
131 *
132 * @remarks Because the macro may use memset it has been placed in string.h
133 * instead of cdefs.h to avoid build issues because someone forgot
134 * to include this header.
135 *
136 * @ingroup grp_rt_cdefs
137 */
138#define RT_BZERO(pv, cb) do { memset((pv), 0, cb); } while (0)
139
140
141
142/** @defgroup grp_rt_str RTStr - String Manipulation
143 * Mostly UTF-8 related helpers where the standard string functions won't do.
144 * @ingroup grp_rt
145 * @{
146 */
147
148RT_C_DECLS_BEGIN
149
150
151/**
152 * The maximum string length.
153 */
154#define RTSTR_MAX (~(size_t)0)
155
156
157/** @def RTMEM_TAG
158 * The default allocation tag used by the RTStr allocation APIs.
159 *
160 * When not defined before the inclusion of iprt/string.h, this will default to
161 * the pointer to the current file name. The string API will make of use of
162 * this as pointer to a volatile but read-only string.
163 */
164#ifndef RTSTR_TAG
165# define RTSTR_TAG (__FILE__)
166#endif
167
168
169#ifdef IN_RING3
170
171/**
172 * Allocates tmp buffer with default tag, translates pszString from UTF8 to
173 * current codepage.
174 *
175 * @returns iprt status code.
176 * @param ppszString Receives pointer of allocated native CP string.
177 * The returned pointer must be freed using RTStrFree().
178 * @param pszString UTF-8 string to convert.
179 */
180#define RTStrUtf8ToCurrentCP(ppszString, pszString) RTStrUtf8ToCurrentCPTag((ppszString), (pszString), RTSTR_TAG)
181
182/**
183 * Allocates tmp buffer with custom tag, translates pszString from UTF8 to
184 * current codepage.
185 *
186 * @returns iprt status code.
187 * @param ppszString Receives pointer of allocated native CP string.
188 * The returned pointer must be freed using
189 * RTStrFree()., const char *pszTag
190 * @param pszString UTF-8 string to convert.
191 * @param pszTag Allocation tag used for statistics and such.
192 */
193RTR3DECL(int) RTStrUtf8ToCurrentCPTag(char **ppszString, const char *pszString, const char *pszTag);
194
195/**
196 * Allocates tmp buffer, translates pszString from current codepage to UTF-8.
197 *
198 * @returns iprt status code.
199 * @param ppszString Receives pointer of allocated UTF-8 string.
200 * The returned pointer must be freed using RTStrFree().
201 * @param pszString Native string to convert.
202 */
203#define RTStrCurrentCPToUtf8(ppszString, pszString) RTStrCurrentCPToUtf8Tag((ppszString), (pszString), RTSTR_TAG)
204
205/**
206 * Allocates tmp buffer, translates pszString from current codepage to UTF-8.
207 *
208 * @returns iprt status code.
209 * @param ppszString Receives pointer of allocated UTF-8 string.
210 * The returned pointer must be freed using RTStrFree().
211 * @param pszString Native string to convert.
212 * @param pszTag Allocation tag used for statistics and such.
213 */
214RTR3DECL(int) RTStrCurrentCPToUtf8Tag(char **ppszString, const char *pszString, const char *pszTag);
215
216#endif /* IN_RING3 */
217
218/**
219 * Free string allocated by any of the non-UCS-2 string functions.
220 *
221 * @returns iprt status code.
222 * @param pszString Pointer to buffer with string to free.
223 * NULL is accepted.
224 */
225RTDECL(void) RTStrFree(char *pszString);
226
227/**
228 * Allocates a new copy of the given UTF-8 string (default tag).
229 *
230 * @returns Pointer to the allocated UTF-8 string.
231 * @param pszString UTF-8 string to duplicate.
232 */
233#define RTStrDup(pszString) RTStrDupTag((pszString), RTSTR_TAG)
234
235/**
236 * Allocates a new copy of the given UTF-8 string (custom tag).
237 *
238 * @returns Pointer to the allocated UTF-8 string.
239 * @param pszString UTF-8 string to duplicate.
240 * @param pszTag Allocation tag used for statistics and such.
241 */
242RTDECL(char *) RTStrDupTag(const char *pszString, const char *pszTag);
243
244/**
245 * Allocates a new copy of the given UTF-8 string (default tag).
246 *
247 * @returns iprt status code.
248 * @param ppszString Receives pointer of the allocated UTF-8 string.
249 * The returned pointer must be freed using RTStrFree().
250 * @param pszString UTF-8 string to duplicate.
251 */
252#define RTStrDupEx(ppszString, pszString) RTStrDupExTag((ppszString), (pszString), RTSTR_TAG)
253
254/**
255 * Allocates a new copy of the given UTF-8 string (custom tag).
256 *
257 * @returns iprt status code.
258 * @param ppszString Receives pointer of the allocated UTF-8 string.
259 * The returned pointer must be freed using RTStrFree().
260 * @param pszString UTF-8 string to duplicate.
261 * @param pszTag Allocation tag used for statistics and such.
262 */
263RTDECL(int) RTStrDupExTag(char **ppszString, const char *pszString, const char *pszTag);
264
265/**
266 * Allocates a new copy of the given UTF-8 substring (default tag).
267 *
268 * @returns Pointer to the allocated UTF-8 substring.
269 * @param pszString UTF-8 string to duplicate.
270 * @param cchMax The max number of chars to duplicate, not counting
271 * the terminator.
272 */
273#define RTStrDupN(pszString, cchMax) RTStrDupNTag((pszString), (cchMax), RTSTR_TAG)
274
275/**
276 * Allocates a new copy of the given UTF-8 substring (custom tag).
277 *
278 * @returns Pointer to the allocated UTF-8 substring.
279 * @param pszString UTF-8 string to duplicate.
280 * @param cchMax The max number of chars to duplicate, not counting
281 * the terminator.
282 * @param pszTag Allocation tag used for statistics and such.
283 */
284RTDECL(char *) RTStrDupNTag(const char *pszString, size_t cchMax, const char *pszTag);
285
286/**
287 * Appends a string onto an existing IPRT allocated string (defaul tag).
288 *
289 * @retval VINF_SUCCESS
290 * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
291 * remains unchanged.
292 *
293 * @param ppsz Pointer to the string pointer. The string
294 * pointer must either be NULL or point to a string
295 * returned by an IPRT string API. (In/Out)
296 * @param pszAppend The string to append. NULL and empty strings
297 * are quietly ignored.
298 */
299#define RTStrAAppend(ppsz, pszAppend) RTStrAAppendTag((ppsz), (pszAppend), RTSTR_TAG)
300
301/**
302 * Appends a string onto an existing IPRT allocated string (custom tag).
303 *
304 * @retval VINF_SUCCESS
305 * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
306 * remains unchanged.
307 *
308 * @param ppsz Pointer to the string pointer. The string
309 * pointer must either be NULL or point to a string
310 * returned by an IPRT string API. (In/Out)
311 * @param pszAppend The string to append. NULL and empty strings
312 * are quietly ignored.
313 * @param pszTag Allocation tag used for statistics and such.
314 */
315RTDECL(int) RTStrAAppendTag(char **ppsz, const char *pszAppend, const char *pszTag);
316
317/**
318 * Appends N bytes from a strings onto an existing IPRT allocated string
319 * (default tag).
320 *
321 * @retval VINF_SUCCESS
322 * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
323 * remains unchanged.
324 *
325 * @param ppsz Pointer to the string pointer. The string
326 * pointer must either be NULL or point to a string
327 * returned by an IPRT string API. (In/Out)
328 * @param pszAppend The string to append. Can be NULL if cchAppend
329 * is NULL.
330 * @param cchAppend The number of chars (not code points) to append
331 * from pszAppend. Must not be more than
332 * @a pszAppend contains, except for the special
333 * value RTSTR_MAX that can be used to indicate all
334 * of @a pszAppend without having to strlen it.
335 */
336#define RTStrAAppendN(ppsz, pszAppend, cchAppend) RTStrAAppendNTag((ppsz), (pszAppend), (cchAppend), RTSTR_TAG)
337
338/**
339 * Appends N bytes from a strings onto an existing IPRT allocated string (custom
340 * tag).
341 *
342 * @retval VINF_SUCCESS
343 * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
344 * remains unchanged.
345 *
346 * @param ppsz Pointer to the string pointer. The string
347 * pointer must either be NULL or point to a string
348 * returned by an IPRT string API. (In/Out)
349 * @param pszAppend The string to append. Can be NULL if cchAppend
350 * is NULL.
351 * @param cchAppend The number of chars (not code points) to append
352 * from pszAppend. Must not be more than
353 * @a pszAppend contains, except for the special
354 * value RTSTR_MAX that can be used to indicate all
355 * of @a pszAppend without having to strlen it.
356 * @param pszTag Allocation tag used for statistics and such.
357 */
358RTDECL(int) RTStrAAppendNTag(char **ppsz, const char *pszAppend, size_t cchAppend, const char *pszTag);
359
360/**
361 * Appends one or more strings onto an existing IPRT allocated string.
362 *
363 * This is a very flexible and efficient alternative to using RTStrAPrintf to
364 * combine several strings together.
365 *
366 * @retval VINF_SUCCESS
367 * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
368 * remains unchanged.
369 *
370 * @param ppsz Pointer to the string pointer. The string
371 * pointer must either be NULL or point to a string
372 * returned by an IPRT string API. (In/Out)
373 * @param cPairs The number of string / length pairs in the
374 * @a va.
375 * @param va List of string (const char *) and length
376 * (size_t) pairs. The strings will be appended to
377 * the string in the first argument.
378 */
379#define RTStrAAppendExNV(ppsz, cPairs, va) RTStrAAppendExNVTag((ppsz), (cPairs), (va), RTSTR_TAG)
380
381/**
382 * Appends one or more strings onto an existing IPRT allocated string.
383 *
384 * This is a very flexible and efficient alternative to using RTStrAPrintf to
385 * combine several strings together.
386 *
387 * @retval VINF_SUCCESS
388 * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
389 * remains unchanged.
390 *
391 * @param ppsz Pointer to the string pointer. The string
392 * pointer must either be NULL or point to a string
393 * returned by an IPRT string API. (In/Out)
394 * @param cPairs The number of string / length pairs in the
395 * @a va.
396 * @param va List of string (const char *) and length
397 * (size_t) pairs. The strings will be appended to
398 * the string in the first argument.
399 * @param pszTag Allocation tag used for statistics and such.
400 */
401RTDECL(int) RTStrAAppendExNVTag(char **ppsz, size_t cPairs, va_list va, const char *pszTag);
402
403/**
404 * Appends one or more strings onto an existing IPRT allocated string
405 * (untagged).
406 *
407 * This is a very flexible and efficient alternative to using RTStrAPrintf to
408 * combine several strings together.
409 *
410 * @retval VINF_SUCCESS
411 * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
412 * remains unchanged.
413 *
414 * @param ppsz Pointer to the string pointer. The string
415 * pointer must either be NULL or point to a string
416 * returned by an IPRT string API. (In/Out)
417 * @param cPairs The number of string / length pairs in the
418 * ellipsis.
419 * @param ... List of string (const char *) and length
420 * (size_t) pairs. The strings will be appended to
421 * the string in the first argument.
422 */
423DECLINLINE(int) RTStrAAppendExN(char **ppsz, size_t cPairs, ...)
424{
425 int rc;
426 va_list va;
427 va_start(va, cPairs);
428 rc = RTStrAAppendExNVTag(ppsz, cPairs, va, RTSTR_TAG);
429 va_end(va);
430 return rc;
431}
432
433/**
434 * Appends one or more strings onto an existing IPRT allocated string (custom
435 * tag).
436 *
437 * This is a very flexible and efficient alternative to using RTStrAPrintf to
438 * combine several strings together.
439 *
440 * @retval VINF_SUCCESS
441 * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
442 * remains unchanged.
443 *
444 * @param ppsz Pointer to the string pointer. The string
445 * pointer must either be NULL or point to a string
446 * returned by an IPRT string API. (In/Out)
447 * @param pszTag Allocation tag used for statistics and such.
448 * @param cPairs The number of string / length pairs in the
449 * ellipsis.
450 * @param ... List of string (const char *) and length
451 * (size_t) pairs. The strings will be appended to
452 * the string in the first argument.
453 */
454DECLINLINE(int) RTStrAAppendExNTag(char **ppsz, const char *pszTag, size_t cPairs, ...)
455{
456 int rc;
457 va_list va;
458 va_start(va, cPairs);
459 rc = RTStrAAppendExNVTag(ppsz, cPairs, va, pszTag);
460 va_end(va);
461 return rc;
462}
463
464/**
465 * Truncates an IPRT allocated string (default tag).
466 *
467 * @retval VINF_SUCCESS.
468 * @retval VERR_OUT_OF_RANGE if cchNew is too long. Nothing is done.
469 *
470 * @param ppsz Pointer to the string pointer. The string
471 * pointer can be NULL if @a cchNew is 0, no change
472 * is made then. If we actually reallocate the
473 * string, the string pointer might be changed by
474 * this call. (In/Out)
475 * @param cchNew The new string length (excluding the
476 * terminator). The string must be at least this
477 * long or we'll return VERR_OUT_OF_RANGE and
478 * assert on you.
479 */
480#define RTStrATruncate(ppsz, cchNew) RTStrATruncateTag((ppsz), (cchNew), RTSTR_TAG)
481
482/**
483 * Truncates an IPRT allocated string.
484 *
485 * @retval VINF_SUCCESS.
486 * @retval VERR_OUT_OF_RANGE if cchNew is too long. Nothing is done.
487 *
488 * @param ppsz Pointer to the string pointer. The string
489 * pointer can be NULL if @a cchNew is 0, no change
490 * is made then. If we actually reallocate the
491 * string, the string pointer might be changed by
492 * this call. (In/Out)
493 * @param cchNew The new string length (excluding the
494 * terminator). The string must be at least this
495 * long or we'll return VERR_OUT_OF_RANGE and
496 * assert on you.
497 * @param pszTag Allocation tag used for statistics and such.
498 */
499RTDECL(int) RTStrATruncateTag(char **ppsz, size_t cchNew, const char *pszTag);
500
501/**
502 * Allocates memory for string storage (default tag).
503 *
504 * You should normally not use this function, except if there is some very
505 * custom string handling you need doing that isn't covered by any of the other
506 * APIs.
507 *
508 * @returns Pointer to the allocated string. The first byte is always set
509 * to the string terminator char, the contents of the remainder of the
510 * memory is undefined. The string must be freed by calling RTStrFree.
511 *
512 * NULL is returned if the allocation failed. Please translate this to
513 * VERR_NO_STR_MEMORY and not VERR_NO_MEMORY. Also consider
514 * RTStrAllocEx if an IPRT status code is required.
515 *
516 * @param cb How many bytes to allocate. If this is zero, we
517 * will allocate a terminator byte anyway.
518 */
519#define RTStrAlloc(cb) RTStrAllocTag((cb), RTSTR_TAG)
520
521/**
522 * Allocates memory for string storage (custom tag).
523 *
524 * You should normally not use this function, except if there is some very
525 * custom string handling you need doing that isn't covered by any of the other
526 * APIs.
527 *
528 * @returns Pointer to the allocated string. The first byte is always set
529 * to the string terminator char, the contents of the remainder of the
530 * memory is undefined. The string must be freed by calling RTStrFree.
531 *
532 * NULL is returned if the allocation failed. Please translate this to
533 * VERR_NO_STR_MEMORY and not VERR_NO_MEMORY. Also consider
534 * RTStrAllocEx if an IPRT status code is required.
535 *
536 * @param cb How many bytes to allocate. If this is zero, we
537 * will allocate a terminator byte anyway.
538 * @param pszTag Allocation tag used for statistics and such.
539 */
540RTDECL(char *) RTStrAllocTag(size_t cb, const char *pszTag);
541
542/**
543 * Allocates memory for string storage, with status code (default tag).
544 *
545 * You should normally not use this function, except if there is some very
546 * custom string handling you need doing that isn't covered by any of the other
547 * APIs.
548 *
549 * @retval VINF_SUCCESS
550 * @retval VERR_NO_STR_MEMORY
551 *
552 * @param ppsz Where to return the allocated string. This will
553 * be set to NULL on failure. On success, the
554 * returned memory will always start with a
555 * terminator char so that it is considered a valid
556 * C string, the contents of rest of the memory is
557 * undefined.
558 * @param cb How many bytes to allocate. If this is zero, we
559 * will allocate a terminator byte anyway.
560 */
561#define RTStrAllocEx(ppsz, cb) RTStrAllocExTag((ppsz), (cb), RTSTR_TAG)
562
563/**
564 * Allocates memory for string storage, with status code (custom tag).
565 *
566 * You should normally not use this function, except if there is some very
567 * custom string handling you need doing that isn't covered by any of the other
568 * APIs.
569 *
570 * @retval VINF_SUCCESS
571 * @retval VERR_NO_STR_MEMORY
572 *
573 * @param ppsz Where to return the allocated string. This will
574 * be set to NULL on failure. On success, the
575 * returned memory will always start with a
576 * terminator char so that it is considered a valid
577 * C string, the contents of rest of the memory is
578 * undefined.
579 * @param cb How many bytes to allocate. If this is zero, we
580 * will allocate a terminator byte anyway.
581 * @param pszTag Allocation tag used for statistics and such.
582 */
583RTDECL(int) RTStrAllocExTag(char **ppsz, size_t cb, const char *pszTag);
584
585/**
586 * Reallocates the specified string (default tag).
587 *
588 * You should normally not have use this function, except perhaps to truncate a
589 * really long string you've got from some IPRT string API, but then you should
590 * use RTStrATruncate.
591 *
592 * @returns VINF_SUCCESS.
593 * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
594 * remains unchanged.
595 *
596 * @param ppsz Pointer to the string variable containing the
597 * input and output string.
598 *
599 * When not freeing the string, the result will
600 * always have the last byte set to the terminator
601 * character so that when used for string
602 * truncation the result will be a valid C string
603 * (your job to keep it a valid UTF-8 string).
604 *
605 * When the input string is NULL and we're supposed
606 * to reallocate, the returned string will also
607 * have the first byte set to the terminator char
608 * so it will be a valid C string.
609 *
610 * @param cbNew When @a cbNew is zero, we'll behave like
611 * RTStrFree and @a *ppsz will be set to NULL.
612 *
613 * When not zero, this will be the new size of the
614 * memory backing the string, i.e. it includes the
615 * terminator char.
616 */
617#define RTStrRealloc(ppsz, cbNew) RTStrReallocTag((ppsz), (cbNew), RTSTR_TAG)
618
619/**
620 * Reallocates the specified string (custom tag).
621 *
622 * You should normally not have use this function, except perhaps to truncate a
623 * really long string you've got from some IPRT string API, but then you should
624 * use RTStrATruncate.
625 *
626 * @returns VINF_SUCCESS.
627 * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
628 * remains unchanged.
629 *
630 * @param ppsz Pointer to the string variable containing the
631 * input and output string.
632 *
633 * When not freeing the string, the result will
634 * always have the last byte set to the terminator
635 * character so that when used for string
636 * truncation the result will be a valid C string
637 * (your job to keep it a valid UTF-8 string).
638 *
639 * When the input string is NULL and we're supposed
640 * to reallocate, the returned string will also
641 * have the first byte set to the terminator char
642 * so it will be a valid C string.
643 *
644 * @param cbNew When @a cbNew is zero, we'll behave like
645 * RTStrFree and @a *ppsz will be set to NULL.
646 *
647 * When not zero, this will be the new size of the
648 * memory backing the string, i.e. it includes the
649 * terminator char.
650 * @param pszTag Allocation tag used for statistics and such.
651 */
652RTDECL(int) RTStrReallocTag(char **ppsz, size_t cbNew, const char *pszTag);
653
654/**
655 * Validates the UTF-8 encoding of the string.
656 *
657 * @returns iprt status code.
658 * @param psz The string.
659 */
660RTDECL(int) RTStrValidateEncoding(const char *psz);
661
662/** @name Flags for RTStrValidateEncodingEx
663 */
664/** Check that the string is zero terminated within the given size.
665 * VERR_BUFFER_OVERFLOW will be returned if the check fails. */
666#define RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED RT_BIT_32(0)
667/** @} */
668
669/**
670 * Validates the UTF-8 encoding of the string.
671 *
672 * @returns iprt status code.
673 * @param psz The string.
674 * @param cch The max string length. Use RTSTR_MAX to process the entire string.
675 * @param fFlags Reserved for future. Pass 0.
676 */
677RTDECL(int) RTStrValidateEncodingEx(const char *psz, size_t cch, uint32_t fFlags);
678
679/**
680 * Checks if the UTF-8 encoding is valid.
681 *
682 * @returns true / false.
683 * @param psz The string.
684 */
685RTDECL(bool) RTStrIsValidEncoding(const char *psz);
686
687/**
688 * Purge all bad UTF-8 encoding in the string, replacing it with '?'.
689 *
690 * @returns The number of bad characters (0 if nothing was done).
691 * @param psz The string to purge.
692 */
693RTDECL(size_t) RTStrPurgeEncoding(char *psz);
694
695/**
696 * Gets the number of code points the string is made up of, excluding
697 * the terminator.
698 *
699 *
700 * @returns Number of code points (RTUNICP).
701 * @returns 0 if the string was incorrectly encoded.
702 * @param psz The string.
703 */
704RTDECL(size_t) RTStrUniLen(const char *psz);
705
706/**
707 * Gets the number of code points the string is made up of, excluding
708 * the terminator.
709 *
710 * This function will validate the string, and incorrectly encoded UTF-8
711 * strings will be rejected.
712 *
713 * @returns iprt status code.
714 * @param psz The string.
715 * @param cch The max string length. Use RTSTR_MAX to process the entire string.
716 * @param pcuc Where to store the code point count.
717 * This is undefined on failure.
718 */
719RTDECL(int) RTStrUniLenEx(const char *psz, size_t cch, size_t *pcuc);
720
721/**
722 * Translate a UTF-8 string into an unicode string (i.e. RTUNICPs), allocating the string buffer.
723 *
724 * @returns iprt status code.
725 * @param pszString UTF-8 string to convert.
726 * @param ppUniString Receives pointer to the allocated unicode string.
727 * The returned string must be freed using RTUniFree().
728 */
729RTDECL(int) RTStrToUni(const char *pszString, PRTUNICP *ppUniString);
730
731/**
732 * Translates pszString from UTF-8 to an array of code points, allocating the result
733 * array if requested.
734 *
735 * @returns iprt status code.
736 * @param pszString UTF-8 string to convert.
737 * @param cchString The maximum size in chars (the type) to convert. The conversion stop
738 * when it reaches cchString or the string terminator ('\\0').
739 * Use RTSTR_MAX to translate the entire string.
740 * @param ppaCps If cCps is non-zero, this must either be pointing to pointer to
741 * a buffer of the specified size, or pointer to a NULL pointer.
742 * If *ppusz is NULL or cCps is zero a buffer of at least cCps items
743 * will be allocated to hold the translated string.
744 * If a buffer was requested it must be freed using RTUtf16Free().
745 * @param cCps The number of code points in the unicode string. This includes the terminator.
746 * @param pcCps Where to store the length of the translated string,
747 * excluding the terminator. (Optional)
748 *
749 * This may be set under some error conditions,
750 * however, only for VERR_BUFFER_OVERFLOW and
751 * VERR_NO_STR_MEMORY will it contain a valid string
752 * length that can be used to resize the buffer.
753 */
754RTDECL(int) RTStrToUniEx(const char *pszString, size_t cchString, PRTUNICP *ppaCps, size_t cCps, size_t *pcCps);
755
756/**
757 * Calculates the length of the string in RTUTF16 items.
758 *
759 * This function will validate the string, and incorrectly encoded UTF-8
760 * strings will be rejected. The primary purpose of this function is to
761 * help allocate buffers for RTStrToUtf16Ex of the correct size. For most
762 * other purposes RTStrCalcUtf16LenEx() should be used.
763 *
764 * @returns Number of RTUTF16 items.
765 * @returns 0 if the string was incorrectly encoded.
766 * @param psz The string.
767 */
768RTDECL(size_t) RTStrCalcUtf16Len(const char *psz);
769
770/**
771 * Calculates the length of the string in RTUTF16 items.
772 *
773 * This function will validate the string, and incorrectly encoded UTF-8
774 * strings will be rejected.
775 *
776 * @returns iprt status code.
777 * @param psz The string.
778 * @param cch The max string length. Use RTSTR_MAX to process the entire string.
779 * @param pcwc Where to store the string length. Optional.
780 * This is undefined on failure.
781 */
782RTDECL(int) RTStrCalcUtf16LenEx(const char *psz, size_t cch, size_t *pcwc);
783
784/**
785 * Translate a UTF-8 string into a UTF-16 allocating the result buffer (default
786 * tag).
787 *
788 * @returns iprt status code.
789 * @param pszString UTF-8 string to convert.
790 * @param ppwszString Receives pointer to the allocated UTF-16 string.
791 * The returned string must be freed using RTUtf16Free().
792 */
793#define RTStrToUtf16(pszString, ppwszString) RTStrToUtf16Tag((pszString), (ppwszString), RTSTR_TAG)
794
795/**
796 * Translate a UTF-8 string into a UTF-16 allocating the result buffer (custom
797 * tag).
798 *
799 * @returns iprt status code.
800 * @param pszString UTF-8 string to convert.
801 * @param ppwszString Receives pointer to the allocated UTF-16 string.
802 * The returned string must be freed using RTUtf16Free().
803 * @param pszTag Allocation tag used for statistics and such.
804 */
805RTDECL(int) RTStrToUtf16Tag(const char *pszString, PRTUTF16 *ppwszString, const char *pszTag);
806
807/**
808 * Translates pszString from UTF-8 to UTF-16, allocating the result buffer if requested.
809 *
810 * @returns iprt status code.
811 * @param pszString UTF-8 string to convert.
812 * @param cchString The maximum size in chars (the type) to convert. The conversion stop
813 * when it reaches cchString or the string terminator ('\\0').
814 * Use RTSTR_MAX to translate the entire string.
815 * @param ppwsz If cwc is non-zero, this must either be pointing to pointer to
816 * a buffer of the specified size, or pointer to a NULL pointer.
817 * If *ppwsz is NULL or cwc is zero a buffer of at least cwc items
818 * will be allocated to hold the translated string.
819 * If a buffer was requested it must be freed using RTUtf16Free().
820 * @param cwc The buffer size in RTUTF16s. This includes the terminator.
821 * @param pcwc Where to store the length of the translated string,
822 * excluding the terminator. (Optional)
823 *
824 * This may be set under some error conditions,
825 * however, only for VERR_BUFFER_OVERFLOW and
826 * VERR_NO_STR_MEMORY will it contain a valid string
827 * length that can be used to resize the buffer.
828 */
829#define RTStrToUtf16Ex(pszString, cchString, ppwsz, cwc, pcwc) \
830 RTStrToUtf16ExTag((pszString), (cchString), (ppwsz), (cwc), (pcwc), RTSTR_TAG)
831
832/**
833 * Translates pszString from UTF-8 to UTF-16, allocating the result buffer if
834 * requested (custom tag).
835 *
836 * @returns iprt status code.
837 * @param pszString UTF-8 string to convert.
838 * @param cchString The maximum size in chars (the type) to convert. The conversion stop
839 * when it reaches cchString or the string terminator ('\\0').
840 * Use RTSTR_MAX to translate the entire string.
841 * @param ppwsz If cwc is non-zero, this must either be pointing to pointer to
842 * a buffer of the specified size, or pointer to a NULL pointer.
843 * If *ppwsz is NULL or cwc is zero a buffer of at least cwc items
844 * will be allocated to hold the translated string.
845 * If a buffer was requested it must be freed using RTUtf16Free().
846 * @param cwc The buffer size in RTUTF16s. This includes the terminator.
847 * @param pcwc Where to store the length of the translated string,
848 * excluding the terminator. (Optional)
849 *
850 * This may be set under some error conditions,
851 * however, only for VERR_BUFFER_OVERFLOW and
852 * VERR_NO_STR_MEMORY will it contain a valid string
853 * length that can be used to resize the buffer.
854 * @param pszTag Allocation tag used for statistics and such.
855 */
856RTDECL(int) RTStrToUtf16ExTag(const char *pszString, size_t cchString, PRTUTF16 *ppwsz, size_t cwc, size_t *pcwc, const char *pszTag);
857
858
859/**
860 * Calculates the length of the string in Latin-1 characters.
861 *
862 * This function will validate the string, and incorrectly encoded UTF-8
863 * strings as well as string with codepoints outside the latin-1 range will be
864 * rejected. The primary purpose of this function is to help allocate buffers
865 * for RTStrToLatin1Ex of the correct size. For most other purposes
866 * RTStrCalcLatin1LenEx() should be used.
867 *
868 * @returns Number of Latin-1 characters.
869 * @returns 0 if the string was incorrectly encoded.
870 * @param psz The string.
871 */
872RTDECL(size_t) RTStrCalcLatin1Len(const char *psz);
873
874/**
875 * Calculates the length of the string in Latin-1 characters.
876 *
877 * This function will validate the string, and incorrectly encoded UTF-8
878 * strings as well as string with codepoints outside the latin-1 range will be
879 * rejected.
880 *
881 * @returns iprt status code.
882 * @param psz The string.
883 * @param cch The max string length. Use RTSTR_MAX to process the
884 * entire string.
885 * @param pcch Where to store the string length. Optional.
886 * This is undefined on failure.
887 */
888RTDECL(int) RTStrCalcLatin1LenEx(const char *psz, size_t cch, size_t *pcwc);
889
890/**
891 * Translate a UTF-8 string into a Latin-1 allocating the result buffer (default
892 * tag).
893 *
894 * @returns iprt status code.
895 * @param pszString UTF-8 string to convert.
896 * @param ppszString Receives pointer to the allocated Latin-1 string.
897 * The returned string must be freed using RTStrFree().
898 */
899#define RTStrToLatin1(pszString, ppszString) RTStrToLatin1Tag((pszString), (ppszString), RTSTR_TAG)
900
901/**
902 * Translate a UTF-8 string into a Latin-1 allocating the result buffer (custom
903 * tag).
904 *
905 * @returns iprt status code.
906 * @param pszString UTF-8 string to convert.
907 * @param ppszString Receives pointer to the allocated Latin-1 string.
908 * The returned string must be freed using RTStrFree().
909 * @param pszTag Allocation tag used for statistics and such.
910 */
911RTDECL(int) RTStrToLatin1Tag(const char *pszString, char **ppszString, const char *pszTag);
912
913/**
914 * Translates pszString from UTF-8 to Latin-1, allocating the result buffer if requested.
915 *
916 * @returns iprt status code.
917 * @param pszString UTF-8 string to convert.
918 * @param cchString The maximum size in chars (the type) to convert.
919 * The conversion stop when it reaches cchString or
920 * the string terminator ('\\0'). Use RTSTR_MAX to
921 * translate the entire string.
922 * @param ppsz If cch is non-zero, this must either be pointing to
923 * pointer to a buffer of the specified size, or
924 * pointer to a NULL pointer. If *ppsz is NULL or cch
925 * is zero a buffer of at least cch items will be
926 * allocated to hold the translated string. If a
927 * buffer was requested it must be freed using
928 * RTStrFree().
929 * @param cch The buffer size in bytes. This includes the
930 * terminator.
931 * @param pcch Where to store the length of the translated string,
932 * excluding the terminator. (Optional)
933 *
934 * This may be set under some error conditions,
935 * however, only for VERR_BUFFER_OVERFLOW and
936 * VERR_NO_STR_MEMORY will it contain a valid string
937 * length that can be used to resize the buffer.
938 */
939#define RTStrToLatin1Ex(pszString, cchString, ppsz, cch, pcch) \
940 RTStrToLatin1ExTag((pszString), (cchString), (ppsz), (cch), (pcch), RTSTR_TAG)
941
942/**
943 * Translates pszString from UTF-8 to Latin1, allocating the result buffer if
944 * requested (custom tag).
945 *
946 * @returns iprt status code.
947 * @param pszString UTF-8 string to convert.
948 * @param cchString The maximum size in chars (the type) to convert.
949 * The conversion stop when it reaches cchString or
950 * the string terminator ('\\0'). Use RTSTR_MAX to
951 * translate the entire string.
952 * @param ppsz If cch is non-zero, this must either be pointing to
953 * pointer to a buffer of the specified size, or
954 * pointer to a NULL pointer. If *ppsz is NULL or cch
955 * is zero a buffer of at least cch items will be
956 * allocated to hold the translated string. If a
957 * buffer was requested it must be freed using
958 * RTStrFree().
959 * @param cch The buffer size in bytes. This includes the
960 * terminator.
961 * @param pcch Where to store the length of the translated string,
962 * excluding the terminator. (Optional)
963 *
964 * This may be set under some error conditions,
965 * however, only for VERR_BUFFER_OVERFLOW and
966 * VERR_NO_STR_MEMORY will it contain a valid string
967 * length that can be used to resize the buffer.
968 * @param pszTag Allocation tag used for statistics and such.
969 */
970RTDECL(int) RTStrToLatin1ExTag(const char *pszString, size_t cchString, char **ppsz, size_t cch, size_t *pcch, const char *pszTag);
971
972
973/**
974 * Translate a Latin1 string into a UTF-8 allocating the result buffer (default
975 * tag).
976 *
977 * @returns iprt status code.
978 * @param pszString Latin1 string to convert.
979 * @param ppszString Receives pointer of allocated UTF-8 string on
980 * success, and is always set to NULL on failure.
981 * The returned pointer must be freed using RTStrFree().
982 */
983#define RTLatin1ToUtf8(pszString, ppszString) RTLatin1ToUtf8Tag((pszString), (ppszString), RTSTR_TAG)
984
985/**
986 * Translate a Latin-1 string into a UTF-8 allocating the result buffer.
987 *
988 * @returns iprt status code.
989 * @param pszString Latin-1 string to convert.
990 * @param ppszString Receives pointer of allocated UTF-8 string on
991 * success, and is always set to NULL on failure.
992 * The returned pointer must be freed using RTStrFree().
993 * @param pszTag Allocation tag used for statistics and such.
994 */
995RTDECL(int) RTLatin1ToUtf8Tag(const char *pszString, char **ppszString, const char *pszTag);
996
997/**
998 * Translates Latin-1 to UTF-8 using buffer provided by the caller or a fittingly
999 * sized buffer allocated by the function (default tag).
1000 *
1001 * @returns iprt status code.
1002 * @param pszString The Latin-1 string to convert.
1003 * @param cchString The number of Latin-1 characters to translate from
1004 * pszString. The translation will stop when reaching
1005 * cchString or the terminator ('\\0'). Use RTSTR_MAX
1006 * to translate the entire string.
1007 * @param ppsz If cch is non-zero, this must either be pointing to
1008 * a pointer to a buffer of the specified size, or
1009 * pointer to a NULL pointer. If *ppsz is NULL or cch
1010 * is zero a buffer of at least cch chars will be
1011 * allocated to hold the translated string. If a
1012 * buffer was requested it must be freed using
1013 * RTStrFree().
1014 * @param cch The buffer size in chars (the type). This includes the terminator.
1015 * @param pcch Where to store the length of the translated string,
1016 * excluding the terminator. (Optional)
1017 *
1018 * This may be set under some error conditions,
1019 * however, only for VERR_BUFFER_OVERFLOW and
1020 * VERR_NO_STR_MEMORY will it contain a valid string
1021 * length that can be used to resize the buffer.
1022 */
1023#define RTLatin1ToUtf8Ex(pszString, cchString, ppsz, cch, pcch) \
1024 RTLatin1ToUtf8ExTag((pszString), (cchString), (ppsz), (cch), (pcch), RTSTR_TAG)
1025
1026/**
1027 * Translates Latin1 to UTF-8 using buffer provided by the caller or a fittingly
1028 * sized buffer allocated by the function (custom tag).
1029 *
1030 * @returns iprt status code.
1031 * @param pszString The Latin1 string to convert.
1032 * @param cchString The number of Latin1 characters to translate from
1033 * pwszString. The translation will stop when
1034 * reaching cchString or the terminator ('\\0'). Use
1035 * RTSTR_MAX to translate the entire string.
1036 * @param ppsz If cch is non-zero, this must either be pointing to
1037 * a pointer to a buffer of the specified size, or
1038 * pointer to a NULL pointer. If *ppsz is NULL or cch
1039 * is zero a buffer of at least cch chars will be
1040 * allocated to hold the translated string. If a
1041 * buffer was requested it must be freed using
1042 * RTStrFree().
1043 * @param cch The buffer size in chars (the type). This includes
1044 * the terminator.
1045 * @param pcch Where to store the length of the translated string,
1046 * excluding the terminator. (Optional)
1047 *
1048 * This may be set under some error conditions,
1049 * however, only for VERR_BUFFER_OVERFLOW and
1050 * VERR_NO_STR_MEMORY will it contain a valid string
1051 * length that can be used to resize the buffer.
1052 * @param pszTag Allocation tag used for statistics and such.
1053 */
1054RTDECL(int) RTLatin1ToUtf8ExTag(const char *pszString, size_t cchString, char **ppsz, size_t cch, size_t *pcch, const char *pszTag);
1055
1056/**
1057 * Calculates the length of the Latin-1 string in UTF-8 chars (bytes).
1058 *
1059 * The primary purpose of this function is to help allocate buffers for
1060 * RTLatin1ToUtf8() of the correct size. For most other purposes
1061 * RTLatin1ToUtf8Ex() should be used.
1062 *
1063 * @returns Number of chars (bytes).
1064 * @returns 0 if the string was incorrectly encoded.
1065 * @param psz The Latin-1 string.
1066 */
1067RTDECL(size_t) RTLatin1CalcUtf8Len(const char *psz);
1068
1069/**
1070 * Calculates the length of the Latin-1 string in UTF-8 chars (bytes).
1071 *
1072 * @returns iprt status code.
1073 * @param psz The string.
1074 * @param cch The max string length. Use RTSTR_MAX to process the entire string.
1075 * @param pcch Where to store the string length (in bytes). Optional.
1076 * This is undefined on failure.
1077 */
1078RTDECL(int) RTLatin1CalcUtf8LenEx(const char *psz, size_t cch, size_t *pcch);
1079
1080/**
1081 * Get the unicode code point at the given string position.
1082 *
1083 * @returns unicode code point.
1084 * @returns RTUNICP_INVALID if the encoding is invalid.
1085 * @param psz The string.
1086 */
1087RTDECL(RTUNICP) RTStrGetCpInternal(const char *psz);
1088
1089/**
1090 * Get the unicode code point at the given string position.
1091 *
1092 * @returns iprt status code
1093 * @returns VERR_INVALID_UTF8_ENCODING if the encoding is invalid.
1094 * @param ppsz The string cursor.
1095 * This is advanced one character forward on failure.
1096 * @param pCp Where to store the unicode code point.
1097 * Stores RTUNICP_INVALID if the encoding is invalid.
1098 */
1099RTDECL(int) RTStrGetCpExInternal(const char **ppsz, PRTUNICP pCp);
1100
1101/**
1102 * Get the unicode code point at the given string position for a string of a
1103 * given length.
1104 *
1105 * @returns iprt status code
1106 * @retval VERR_INVALID_UTF8_ENCODING if the encoding is invalid.
1107 * @retval VERR_END_OF_STRING if *pcch is 0. *pCp is set to RTUNICP_INVALID.
1108 *
1109 * @param ppsz The string.
1110 * @param pcch Pointer to the length of the string. This will be
1111 * decremented by the size of the code point.
1112 * @param pCp Where to store the unicode code point.
1113 * Stores RTUNICP_INVALID if the encoding is invalid.
1114 */
1115RTDECL(int) RTStrGetCpNExInternal(const char **ppsz, size_t *pcch, PRTUNICP pCp);
1116
1117/**
1118 * Put the unicode code point at the given string position
1119 * and return the pointer to the char following it.
1120 *
1121 * This function will not consider anything at or following the
1122 * buffer area pointed to by psz. It is therefore not suitable for
1123 * inserting code points into a string, only appending/overwriting.
1124 *
1125 * @returns pointer to the char following the written code point.
1126 * @param psz The string.
1127 * @param CodePoint The code point to write.
1128 * This should not be RTUNICP_INVALID or any other
1129 * character out of the UTF-8 range.
1130 *
1131 * @remark This is a worker function for RTStrPutCp().
1132 *
1133 */
1134RTDECL(char *) RTStrPutCpInternal(char *psz, RTUNICP CodePoint);
1135
1136/**
1137 * Get the unicode code point at the given string position.
1138 *
1139 * @returns unicode code point.
1140 * @returns RTUNICP_INVALID if the encoding is invalid.
1141 * @param psz The string.
1142 *
1143 * @remark We optimize this operation by using an inline function for
1144 * the most frequent and simplest sequence, the rest is
1145 * handled by RTStrGetCpInternal().
1146 */
1147DECLINLINE(RTUNICP) RTStrGetCp(const char *psz)
1148{
1149 const unsigned char uch = *(const unsigned char *)psz;
1150 if (!(uch & RT_BIT(7)))
1151 return uch;
1152 return RTStrGetCpInternal(psz);
1153}
1154
1155/**
1156 * Get the unicode code point at the given string position.
1157 *
1158 * @returns iprt status code.
1159 * @param ppsz Pointer to the string pointer. This will be updated to
1160 * point to the char following the current code point.
1161 * This is advanced one character forward on failure.
1162 * @param pCp Where to store the code point.
1163 * RTUNICP_INVALID is stored here on failure.
1164 *
1165 * @remark We optimize this operation by using an inline function for
1166 * the most frequent and simplest sequence, the rest is
1167 * handled by RTStrGetCpExInternal().
1168 */
1169DECLINLINE(int) RTStrGetCpEx(const char **ppsz, PRTUNICP pCp)
1170{
1171 const unsigned char uch = **(const unsigned char **)ppsz;
1172 if (!(uch & RT_BIT(7)))
1173 {
1174 (*ppsz)++;
1175 *pCp = uch;
1176 return VINF_SUCCESS;
1177 }
1178 return RTStrGetCpExInternal(ppsz, pCp);
1179}
1180
1181/**
1182 * Get the unicode code point at the given string position for a string of a
1183 * given maximum length.
1184 *
1185 * @returns iprt status code.
1186 * @retval VERR_INVALID_UTF8_ENCODING if the encoding is invalid.
1187 * @retval VERR_END_OF_STRING if *pcch is 0. *pCp is set to RTUNICP_INVALID.
1188 *
1189 * @param ppsz Pointer to the string pointer. This will be updated to
1190 * point to the char following the current code point.
1191 * @param pcch Pointer to the maximum string length. This will be
1192 * decremented by the size of the code point found.
1193 * @param pCp Where to store the code point.
1194 * RTUNICP_INVALID is stored here on failure.
1195 *
1196 * @remark We optimize this operation by using an inline function for
1197 * the most frequent and simplest sequence, the rest is
1198 * handled by RTStrGetCpNExInternal().
1199 */
1200DECLINLINE(int) RTStrGetCpNEx(const char **ppsz, size_t *pcch, PRTUNICP pCp)
1201{
1202 if (RT_LIKELY(*pcch != 0))
1203 {
1204 const unsigned char uch = **(const unsigned char **)ppsz;
1205 if (!(uch & RT_BIT(7)))
1206 {
1207 (*ppsz)++;
1208 (*pcch)--;
1209 *pCp = uch;
1210 return VINF_SUCCESS;
1211 }
1212 }
1213 return RTStrGetCpNExInternal(ppsz, pcch, pCp);
1214}
1215
1216/**
1217 * Get the UTF-8 size in characters of a given Unicode code point.
1218 *
1219 * The code point is expected to be a valid Unicode one, but not necessarily in
1220 * the range supported by UTF-8.
1221 *
1222 * @returns The number of chars (bytes) required to encode the code point, or
1223 * zero if there is no UTF-8 encoding.
1224 * @param CodePoint The unicode code point.
1225 */
1226DECLINLINE(size_t) RTStrCpSize(RTUNICP CodePoint)
1227{
1228 if (CodePoint < 0x00000080)
1229 return 1;
1230 if (CodePoint < 0x00000800)
1231 return 2;
1232 if (CodePoint < 0x00010000)
1233 return 3;
1234#ifdef RT_USE_RTC_3629
1235 if (CodePoint < 0x00011000)
1236 return 4;
1237#else
1238 if (CodePoint < 0x00200000)
1239 return 4;
1240 if (CodePoint < 0x04000000)
1241 return 5;
1242 if (CodePoint < 0x7fffffff)
1243 return 6;
1244#endif
1245 return 0;
1246}
1247
1248/**
1249 * Put the unicode code point at the given string position
1250 * and return the pointer to the char following it.
1251 *
1252 * This function will not consider anything at or following the
1253 * buffer area pointed to by psz. It is therefore not suitable for
1254 * inserting code points into a string, only appending/overwriting.
1255 *
1256 * @returns pointer to the char following the written code point.
1257 * @param psz The string.
1258 * @param CodePoint The code point to write.
1259 * This should not be RTUNICP_INVALID or any other
1260 * character out of the UTF-8 range.
1261 *
1262 * @remark We optimize this operation by using an inline function for
1263 * the most frequent and simplest sequence, the rest is
1264 * handled by RTStrPutCpInternal().
1265 */
1266DECLINLINE(char *) RTStrPutCp(char *psz, RTUNICP CodePoint)
1267{
1268 if (CodePoint < 0x80)
1269 {
1270 *psz++ = (unsigned char)CodePoint;
1271 return psz;
1272 }
1273 return RTStrPutCpInternal(psz, CodePoint);
1274}
1275
1276/**
1277 * Skips ahead, past the current code point.
1278 *
1279 * @returns Pointer to the char after the current code point.
1280 * @param psz Pointer to the current code point.
1281 * @remark This will not move the next valid code point, only past the current one.
1282 */
1283DECLINLINE(char *) RTStrNextCp(const char *psz)
1284{
1285 RTUNICP Cp;
1286 RTStrGetCpEx(&psz, &Cp);
1287 return (char *)psz;
1288}
1289
1290/**
1291 * Skips back to the previous code point.
1292 *
1293 * @returns Pointer to the char before the current code point.
1294 * @returns pszStart on failure.
1295 * @param pszStart Pointer to the start of the string.
1296 * @param psz Pointer to the current code point.
1297 */
1298RTDECL(char *) RTStrPrevCp(const char *pszStart, const char *psz);
1299
1300/**
1301 * Get the unicode code point at the given string position.
1302 *
1303 * @returns unicode code point.
1304 * @returns RTUNICP_INVALID if the encoding is invalid.
1305 * @param psz The string.
1306 */
1307DECLINLINE(RTUNICP) RTLatin1GetCp(const char *psz)
1308{
1309 return *(const unsigned char *)psz;
1310}
1311
1312/**
1313 * Get the unicode code point at the given string position.
1314 *
1315 * @returns iprt status code.
1316 * @param ppsz Pointer to the string pointer. This will be updated to
1317 * point to the char following the current code point.
1318 * This is advanced one character forward on failure.
1319 * @param pCp Where to store the code point.
1320 * RTUNICP_INVALID is stored here on failure.
1321 *
1322 * @remark We optimize this operation by using an inline function for
1323 * the most frequent and simplest sequence, the rest is
1324 * handled by RTStrGetCpExInternal().
1325 */
1326DECLINLINE(int) RTLatin1GetCpEx(const char **ppsz, PRTUNICP pCp)
1327{
1328 const unsigned char uch = **(const unsigned char **)ppsz;
1329 (*ppsz)++;
1330 *pCp = uch;
1331 return VINF_SUCCESS;
1332}
1333
1334/**
1335 * Get the unicode code point at the given string position for a string of a
1336 * given maximum length.
1337 *
1338 * @returns iprt status code.
1339 * @retval VERR_END_OF_STRING if *pcch is 0. *pCp is set to RTUNICP_INVALID.
1340 *
1341 * @param ppsz Pointer to the string pointer. This will be updated to
1342 * point to the char following the current code point.
1343 * @param pcch Pointer to the maximum string length. This will be
1344 * decremented by the size of the code point found.
1345 * @param pCp Where to store the code point.
1346 * RTUNICP_INVALID is stored here on failure.
1347 */
1348DECLINLINE(int) RTLatin1GetCpNEx(const char **ppsz, size_t *pcch, PRTUNICP pCp)
1349{
1350 if (RT_LIKELY(*pcch != 0))
1351 {
1352 const unsigned char uch = **(const unsigned char **)ppsz;
1353 (*ppsz)++;
1354 (*pcch)--;
1355 *pCp = uch;
1356 return VINF_SUCCESS;
1357 }
1358 *pCp = RTUNICP_INVALID;
1359 return VERR_END_OF_STRING;
1360}
1361
1362/**
1363 * Get the Latin-1 size in characters of a given Unicode code point.
1364 *
1365 * The code point is expected to be a valid Unicode one, but not necessarily in
1366 * the range supported by Latin-1.
1367 *
1368 * @returns the size in characters, or zero if there is no Latin-1 encoding
1369 */
1370DECLINLINE(size_t) RTLatin1CpSize(RTUNICP CodePoint)
1371{
1372 if (CodePoint < 0x100)
1373 return 1;
1374 return 0;
1375}
1376
1377/**
1378 * Put the unicode code point at the given string position
1379 * and return the pointer to the char following it.
1380 *
1381 * This function will not consider anything at or following the
1382 * buffer area pointed to by psz. It is therefore not suitable for
1383 * inserting code points into a string, only appending/overwriting.
1384 *
1385 * @returns pointer to the char following the written code point.
1386 * @param psz The string.
1387 * @param CodePoint The code point to write.
1388 * This should not be RTUNICP_INVALID or any other
1389 * character out of the Latin-1 range.
1390 */
1391DECLINLINE(char *) RTLatin1PutCp(char *psz, RTUNICP CodePoint)
1392{
1393 AssertReturn(CodePoint < 0x100, NULL);
1394 *psz++ = (unsigned char)CodePoint;
1395 return psz;
1396}
1397
1398/**
1399 * Skips ahead, past the current code point.
1400 *
1401 * @returns Pointer to the char after the current code point.
1402 * @param psz Pointer to the current code point.
1403 * @remark This will not move the next valid code point, only past the current one.
1404 */
1405DECLINLINE(char *) RTLatin1NextCp(const char *psz)
1406{
1407 psz++;
1408 return (char *)psz;
1409}
1410
1411/**
1412 * Skips back to the previous code point.
1413 *
1414 * @returns Pointer to the char before the current code point.
1415 * @returns pszStart on failure.
1416 * @param pszStart Pointer to the start of the string.
1417 * @param psz Pointer to the current code point.
1418 */
1419DECLINLINE(char *) RTLatin1PrevCp(const char *psz)
1420{
1421 psz--;
1422 return (char *)psz;
1423}
1424
1425
1426
1427#ifndef DECLARED_FNRTSTROUTPUT /* duplicated in iprt/log.h */
1428#define DECLARED_FNRTSTROUTPUT
1429/**
1430 * Output callback.
1431 *
1432 * @returns number of bytes written.
1433 * @param pvArg User argument.
1434 * @param pachChars Pointer to an array of utf-8 characters.
1435 * @param cbChars Number of bytes in the character array pointed to by pachChars.
1436 */
1437typedef DECLCALLBACK(size_t) FNRTSTROUTPUT(void *pvArg, const char *pachChars, size_t cbChars);
1438/** Pointer to callback function. */
1439typedef FNRTSTROUTPUT *PFNRTSTROUTPUT;
1440#endif
1441
1442/** Format flag.
1443 * These are used by RTStrFormat extensions and RTStrFormatNumber, mind
1444 * that not all flags makes sense to both of the functions.
1445 * @{ */
1446#define RTSTR_F_CAPITAL 0x0001
1447#define RTSTR_F_LEFT 0x0002
1448#define RTSTR_F_ZEROPAD 0x0004
1449#define RTSTR_F_SPECIAL 0x0008
1450#define RTSTR_F_VALSIGNED 0x0010
1451#define RTSTR_F_PLUS 0x0020
1452#define RTSTR_F_BLANK 0x0040
1453#define RTSTR_F_WIDTH 0x0080
1454#define RTSTR_F_PRECISION 0x0100
1455#define RTSTR_F_THOUSAND_SEP 0x0200
1456
1457#define RTSTR_F_BIT_MASK 0xf800
1458#define RTSTR_F_8BIT 0x0800
1459#define RTSTR_F_16BIT 0x1000
1460#define RTSTR_F_32BIT 0x2000
1461#define RTSTR_F_64BIT 0x4000
1462#define RTSTR_F_128BIT 0x8000
1463/** @} */
1464
1465/** @def RTSTR_GET_BIT_FLAG
1466 * Gets the bit flag for the specified type.
1467 */
1468#define RTSTR_GET_BIT_FLAG(type) \
1469 ( sizeof(type) * 8 == 32 ? RTSTR_F_32BIT \
1470 : sizeof(type) * 8 == 64 ? RTSTR_F_64BIT \
1471 : sizeof(type) * 8 == 16 ? RTSTR_F_16BIT \
1472 : sizeof(type) * 8 == 8 ? RTSTR_F_8BIT \
1473 : sizeof(type) * 8 == 128 ? RTSTR_F_128BIT \
1474 : 0)
1475
1476
1477/**
1478 * Callback to format non-standard format specifiers.
1479 *
1480 * @returns The number of bytes formatted.
1481 * @param pvArg Formatter argument.
1482 * @param pfnOutput Pointer to output function.
1483 * @param pvArgOutput Argument for the output function.
1484 * @param ppszFormat Pointer to the format string pointer. Advance this till the char
1485 * after the format specifier.
1486 * @param pArgs Pointer to the argument list. Use this to fetch the arguments.
1487 * @param cchWidth Format Width. -1 if not specified.
1488 * @param cchPrecision Format Precision. -1 if not specified.
1489 * @param fFlags Flags (RTSTR_NTFS_*).
1490 * @param chArgSize The argument size specifier, 'l' or 'L'.
1491 */
1492typedef DECLCALLBACK(size_t) FNSTRFORMAT(void *pvArg, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
1493 const char **ppszFormat, va_list *pArgs, int cchWidth,
1494 int cchPrecision, unsigned fFlags, char chArgSize);
1495/** Pointer to a FNSTRFORMAT() function. */
1496typedef FNSTRFORMAT *PFNSTRFORMAT;
1497
1498
1499/**
1500 * Partial implementation of a printf like formatter.
1501 * It doesn't do everything correct, and there is no floating point support.
1502 * However, it supports custom formats by the means of a format callback.
1503 *
1504 * @returns number of bytes formatted.
1505 * @param pfnOutput Output worker.
1506 * Called in two ways. Normally with a string and its length.
1507 * For termination, it's called with NULL for string, 0 for length.
1508 * @param pvArgOutput Argument to the output worker.
1509 * @param pfnFormat Custom format worker.
1510 * @param pvArgFormat Argument to the format worker.
1511 * @param pszFormat Format string pointer.
1512 * @param InArgs Argument list.
1513 */
1514RTDECL(size_t) RTStrFormatV(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat, const char *pszFormat, va_list InArgs);
1515
1516/**
1517 * Partial implementation of a printf like formatter.
1518 * It doesn't do everything correct, and there is no floating point support.
1519 * However, it supports custom formats by the means of a format callback.
1520 *
1521 * @returns number of bytes formatted.
1522 * @param pfnOutput Output worker.
1523 * Called in two ways. Normally with a string and its length.
1524 * For termination, it's called with NULL for string, 0 for length.
1525 * @param pvArgOutput Argument to the output worker.
1526 * @param pfnFormat Custom format worker.
1527 * @param pvArgFormat Argument to the format worker.
1528 * @param pszFormat Format string.
1529 * @param ... Argument list.
1530 */
1531RTDECL(size_t) RTStrFormat(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat, const char *pszFormat, ...);
1532
1533/**
1534 * Formats an integer number according to the parameters.
1535 *
1536 * @returns Length of the formatted number.
1537 * @param psz Pointer to output string buffer of sufficient size.
1538 * @param u64Value Value to format.
1539 * @param uiBase Number representation base.
1540 * @param cchWidth Width.
1541 * @param cchPrecision Precision.
1542 * @param fFlags Flags (NTFS_*).
1543 */
1544RTDECL(int) RTStrFormatNumber(char *psz, uint64_t u64Value, unsigned int uiBase, signed int cchWidth, signed int cchPrecision, unsigned int fFlags);
1545
1546
1547/**
1548 * Callback for formatting a type.
1549 *
1550 * This is registered using the RTStrFormatTypeRegister function and will
1551 * be called during string formatting to handle the specified %R[type].
1552 * The argument for this format type is assumed to be a pointer and it's
1553 * passed in the @a pvValue argument.
1554 *
1555 * @returns Length of the formatted output.
1556 * @param pfnOutput Output worker.
1557 * @param pvArgOutput Argument to the output worker.
1558 * @param pszType The type name.
1559 * @param pvValue The argument value.
1560 * @param cchWidth Width.
1561 * @param cchPrecision Precision.
1562 * @param fFlags Flags (NTFS_*).
1563 * @param pvUser The user argument.
1564 */
1565typedef DECLCALLBACK(size_t) FNRTSTRFORMATTYPE(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
1566 const char *pszType, void const *pvValue,
1567 int cchWidth, int cchPrecision, unsigned fFlags,
1568 void *pvUser);
1569/** Pointer to a FNRTSTRFORMATTYPE. */
1570typedef FNRTSTRFORMATTYPE *PFNRTSTRFORMATTYPE;
1571
1572
1573/**
1574 * Register a format handler for a type.
1575 *
1576 * The format handler is used to handle '%R[type]' format types, where the argument
1577 * in the vector is a pointer value (a bit restrictive, but keeps it simple).
1578 *
1579 * The caller must ensure that no other thread will be making use of any of
1580 * the dynamic formatting type facilities simultaneously with this call.
1581 *
1582 * @returns IPRT status code.
1583 * @retval VINF_SUCCESS on success.
1584 * @retval VERR_ALREADY_EXISTS if the type has already been registered.
1585 * @retval VERR_TOO_MANY_OPEN_FILES if all the type slots has been allocated already.
1586 *
1587 * @param pszType The type name.
1588 * @param pfnHandler The handler address. See FNRTSTRFORMATTYPE for details.
1589 * @param pvUser The user argument to pass to the handler. See RTStrFormatTypeSetUser
1590 * for how to update this later.
1591 */
1592RTDECL(int) RTStrFormatTypeRegister(const char *pszType, PFNRTSTRFORMATTYPE pfnHandler, void *pvUser);
1593
1594/**
1595 * Deregisters a format type.
1596 *
1597 * The caller must ensure that no other thread will be making use of any of
1598 * the dynamic formatting type facilities simultaneously with this call.
1599 *
1600 * @returns IPRT status code.
1601 * @retval VINF_SUCCESS on success.
1602 * @retval VERR_FILE_NOT_FOUND if not found.
1603 *
1604 * @param pszType The type to deregister.
1605 */
1606RTDECL(int) RTStrFormatTypeDeregister(const char *pszType);
1607
1608/**
1609 * Sets the user argument for a type.
1610 *
1611 * This can be used if a user argument needs relocating in GC.
1612 *
1613 * @returns IPRT status code.
1614 * @retval VINF_SUCCESS on success.
1615 * @retval VERR_FILE_NOT_FOUND if not found.
1616 *
1617 * @param pszType The type to update.
1618 * @param pvUser The new user argument value.
1619 */
1620RTDECL(int) RTStrFormatTypeSetUser(const char *pszType, void *pvUser);
1621
1622
1623/**
1624 * String printf.
1625 *
1626 * @returns The length of the returned string (in pszBuffer) excluding the
1627 * terminator.
1628 * @param pszBuffer Output buffer.
1629 * @param cchBuffer Size of the output buffer.
1630 * @param pszFormat The format string.
1631 * @param args The format argument.
1632 */
1633RTDECL(size_t) RTStrPrintfV(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args);
1634
1635/**
1636 * String printf.
1637 *
1638 * @returns The length of the returned string (in pszBuffer) excluding the
1639 * terminator.
1640 * @param pszBuffer Output buffer.
1641 * @param cchBuffer Size of the output buffer.
1642 * @param pszFormat The format string.
1643 * @param ... The format argument.
1644 */
1645RTDECL(size_t) RTStrPrintf(char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...);
1646
1647
1648/**
1649 * String printf with custom formatting.
1650 *
1651 * @returns The length of the returned string (in pszBuffer) excluding the
1652 * terminator.
1653 * @param pfnFormat Pointer to handler function for the custom formats.
1654 * @param pvArg Argument to the pfnFormat function.
1655 * @param pszBuffer Output buffer.
1656 * @param cchBuffer Size of the output buffer.
1657 * @param pszFormat The format string.
1658 * @param args The format argument.
1659 */
1660RTDECL(size_t) RTStrPrintfExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args);
1661
1662/**
1663 * String printf with custom formatting.
1664 *
1665 * @returns The length of the returned string (in pszBuffer) excluding the
1666 * terminator.
1667 * @param pfnFormat Pointer to handler function for the custom formats.
1668 * @param pvArg Argument to the pfnFormat function.
1669 * @param pszBuffer Output buffer.
1670 * @param cchBuffer Size of the output buffer.
1671 * @param pszFormat The format string.
1672 * @param ... The format argument.
1673 */
1674RTDECL(size_t) RTStrPrintfEx(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...);
1675
1676
1677/**
1678 * Allocating string printf (default tag).
1679 *
1680 * @returns The length of the string in the returned *ppszBuffer excluding the
1681 * terminator.
1682 * @returns -1 on failure.
1683 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
1684 * The buffer should be freed using RTStrFree().
1685 * On failure *ppszBuffer will be set to NULL.
1686 * @param pszFormat The format string.
1687 * @param args The format argument.
1688 */
1689#define RTStrAPrintfV(ppszBuffer, pszFormat, args) RTStrAPrintfVTag((ppszBuffer), (pszFormat), (args), RTSTR_TAG)
1690
1691/**
1692 * Allocating string printf (custom tag).
1693 *
1694 * @returns The length of the string in the returned *ppszBuffer excluding the
1695 * terminator.
1696 * @returns -1 on failure.
1697 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
1698 * The buffer should be freed using RTStrFree().
1699 * On failure *ppszBuffer will be set to NULL.
1700 * @param pszFormat The format string.
1701 * @param args The format argument.
1702 * @param pszTag Allocation tag used for statistics and such.
1703 */
1704RTDECL(int) RTStrAPrintfVTag(char **ppszBuffer, const char *pszFormat, va_list args, const char *pszTag);
1705
1706/**
1707 * Allocating string printf.
1708 *
1709 * @returns The length of the string in the returned *ppszBuffer excluding the
1710 * terminator.
1711 * @returns -1 on failure.
1712 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
1713 * The buffer should be freed using RTStrFree().
1714 * On failure *ppszBuffer will be set to NULL.
1715 * @param pszFormat The format string.
1716 * @param ... The format argument.
1717 */
1718DECLINLINE(int) RTStrAPrintf(char **ppszBuffer, const char *pszFormat, ...)
1719{
1720 int cbRet;
1721 va_list va;
1722 va_start(va, pszFormat);
1723 cbRet = RTStrAPrintfVTag(ppszBuffer, pszFormat, va, RTSTR_TAG);
1724 va_end(va);
1725 return cbRet;
1726}
1727
1728/**
1729 * Allocating string printf (custom tag).
1730 *
1731 * @returns The length of the string in the returned *ppszBuffer excluding the
1732 * terminator.
1733 * @returns -1 on failure.
1734 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
1735 * The buffer should be freed using RTStrFree().
1736 * On failure *ppszBuffer will be set to NULL.
1737 * @param pszTag Allocation tag used for statistics and such.
1738 * @param pszFormat The format string.
1739 * @param ... The format argument.
1740 */
1741DECLINLINE(int) RTStrAPrintfTag(char **ppszBuffer, const char *pszTag, const char *pszFormat, ...)
1742{
1743 int cbRet;
1744 va_list va;
1745 va_start(va, pszFormat);
1746 cbRet = RTStrAPrintfVTag(ppszBuffer, pszFormat, va, pszTag);
1747 va_end(va);
1748 return cbRet;
1749}
1750
1751/**
1752 * Allocating string printf, version 2.
1753 *
1754 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
1755 * memory.
1756 * @param pszFormat The format string.
1757 * @param args The format argument.
1758 */
1759#define RTStrAPrintf2V(pszFormat, args) RTStrAPrintf2VTag((pszFormat), (args), RTSTR_TAG)
1760
1761/**
1762 * Allocating string printf, version 2.
1763 *
1764 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
1765 * memory.
1766 * @param pszFormat The format string.
1767 * @param args The format argument.
1768 * @param pszTag Allocation tag used for statistics and such.
1769 */
1770RTDECL(char *) RTStrAPrintf2VTag(const char *pszFormat, va_list args, const char *pszTag);
1771
1772/**
1773 * Allocating string printf, version 2 (default tag).
1774 *
1775 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
1776 * memory.
1777 * @param pszFormat The format string.
1778 * @param ... The format argument.
1779 */
1780DECLINLINE(char *) RTStrAPrintf2(const char *pszFormat, ...)
1781{
1782 char *pszRet;
1783 va_list va;
1784 va_start(va, pszFormat);
1785 pszRet = RTStrAPrintf2VTag(pszFormat, va, RTSTR_TAG);
1786 va_end(va);
1787 return pszRet;
1788}
1789
1790/**
1791 * Allocating string printf, version 2 (custom tag).
1792 *
1793 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
1794 * memory.
1795 * @param pszTag Allocation tag used for statistics and such.
1796 * @param pszFormat The format string.
1797 * @param ... The format argument.
1798 */
1799DECLINLINE(char *) RTStrAPrintf2Tag(const char *pszTag, const char *pszFormat, ...)
1800{
1801 char *pszRet;
1802 va_list va;
1803 va_start(va, pszFormat);
1804 pszRet = RTStrAPrintf2VTag(pszFormat, va, pszTag);
1805 va_end(va);
1806 return pszRet;
1807}
1808
1809/**
1810 * Strips blankspaces from both ends of the string.
1811 *
1812 * @returns Pointer to first non-blank char in the string.
1813 * @param psz The string to strip.
1814 */
1815RTDECL(char *) RTStrStrip(char *psz);
1816
1817/**
1818 * Strips blankspaces from the start of the string.
1819 *
1820 * @returns Pointer to first non-blank char in the string.
1821 * @param psz The string to strip.
1822 */
1823RTDECL(char *) RTStrStripL(const char *psz);
1824
1825/**
1826 * Strips blankspaces from the end of the string.
1827 *
1828 * @returns psz.
1829 * @param psz The string to strip.
1830 */
1831RTDECL(char *) RTStrStripR(char *psz);
1832
1833/**
1834 * String copy with overflow handling.
1835 *
1836 * @retval VINF_SUCCESS on success.
1837 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
1838 * buffer will contain as much of the string as it can hold, fully
1839 * terminated.
1840 *
1841 * @param pszDst The destination buffer.
1842 * @param cbDst The size of the destination buffer (in bytes).
1843 * @param pszSrc The source string. NULL is not OK.
1844 */
1845RTDECL(int) RTStrCopy(char *pszDst, size_t cbDst, const char *pszSrc);
1846
1847/**
1848 * String copy with overflow handling.
1849 *
1850 * @retval VINF_SUCCESS on success.
1851 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
1852 * buffer will contain as much of the string as it can hold, fully
1853 * terminated.
1854 *
1855 * @param pszDst The destination buffer.
1856 * @param cbDst The size of the destination buffer (in bytes).
1857 * @param pszSrc The source string. NULL is not OK.
1858 * @param cchSrcMax The maximum number of chars (not code points) to
1859 * copy from the source string, not counting the
1860 * terminator as usual.
1861 */
1862RTDECL(int) RTStrCopyEx(char *pszDst, size_t cbDst, const char *pszSrc, size_t cchSrcMax);
1863
1864/**
1865 * Performs a case sensitive string compare between two UTF-8 strings.
1866 *
1867 * Encoding errors are ignored by the current implementation. So, the only
1868 * difference between this and the CRT strcmp function is the handling of
1869 * NULL arguments.
1870 *
1871 * @returns < 0 if the first string less than the second string.
1872 * @returns 0 if the first string identical to the second string.
1873 * @returns > 0 if the first string greater than the second string.
1874 * @param psz1 First UTF-8 string. Null is allowed.
1875 * @param psz2 Second UTF-8 string. Null is allowed.
1876 */
1877RTDECL(int) RTStrCmp(const char *psz1, const char *psz2);
1878
1879/**
1880 * Performs a case sensitive string compare between two UTF-8 strings, given
1881 * a maximum string length.
1882 *
1883 * Encoding errors are ignored by the current implementation. So, the only
1884 * difference between this and the CRT strncmp function is the handling of
1885 * NULL arguments.
1886 *
1887 * @returns < 0 if the first string less than the second string.
1888 * @returns 0 if the first string identical to the second string.
1889 * @returns > 0 if the first string greater than the second string.
1890 * @param psz1 First UTF-8 string. Null is allowed.
1891 * @param psz2 Second UTF-8 string. Null is allowed.
1892 * @param cchMax The maximum string length
1893 */
1894RTDECL(int) RTStrNCmp(const char *psz1, const char *psz2, size_t cchMax);
1895
1896/**
1897 * Performs a case insensitive string compare between two UTF-8 strings.
1898 *
1899 * This is a simplified compare, as only the simplified lower/upper case folding
1900 * specified by the unicode specs are used. It does not consider character pairs
1901 * as they are used in some languages, just simple upper & lower case compares.
1902 *
1903 * The result is the difference between the mismatching codepoints after they
1904 * both have been lower cased.
1905 *
1906 * If the string encoding is invalid the function will assert (strict builds)
1907 * and use RTStrCmp for the remainder of the string.
1908 *
1909 * @returns < 0 if the first string less than the second string.
1910 * @returns 0 if the first string identical to the second string.
1911 * @returns > 0 if the first string greater than the second string.
1912 * @param psz1 First UTF-8 string. Null is allowed.
1913 * @param psz2 Second UTF-8 string. Null is allowed.
1914 */
1915RTDECL(int) RTStrICmp(const char *psz1, const char *psz2);
1916
1917/**
1918 * Performs a case insensitive string compare between two UTF-8 strings, given a
1919 * maximum string length.
1920 *
1921 * This is a simplified compare, as only the simplified lower/upper case folding
1922 * specified by the unicode specs are used. It does not consider character pairs
1923 * as they are used in some languages, just simple upper & lower case compares.
1924 *
1925 * The result is the difference between the mismatching codepoints after they
1926 * both have been lower cased.
1927 *
1928 * If the string encoding is invalid the function will assert (strict builds)
1929 * and use RTStrCmp for the remainder of the string.
1930 *
1931 * @returns < 0 if the first string less than the second string.
1932 * @returns 0 if the first string identical to the second string.
1933 * @returns > 0 if the first string greater than the second string.
1934 * @param psz1 First UTF-8 string. Null is allowed.
1935 * @param psz2 Second UTF-8 string. Null is allowed.
1936 * @param cchMax Maximum string length
1937 */
1938RTDECL(int) RTStrNICmp(const char *psz1, const char *psz2, size_t cchMax);
1939
1940/**
1941 * Locates a case sensitive substring.
1942 *
1943 * If any of the two strings are NULL, then NULL is returned. If the needle is
1944 * an empty string, then the haystack is returned (i.e. matches anything).
1945 *
1946 * @returns Pointer to the first occurrence of the substring if found, NULL if
1947 * not.
1948 *
1949 * @param pszHaystack The string to search.
1950 * @param pszNeedle The substring to search for.
1951 *
1952 * @remarks The difference between this and strstr is the handling of NULL
1953 * pointers.
1954 */
1955RTDECL(char *) RTStrStr(const char *pszHaystack, const char *pszNeedle);
1956
1957/**
1958 * Locates a case insensitive substring.
1959 *
1960 * If any of the two strings are NULL, then NULL is returned. If the needle is
1961 * an empty string, then the haystack is returned (i.e. matches anything).
1962 *
1963 * @returns Pointer to the first occurrence of the substring if found, NULL if
1964 * not.
1965 *
1966 * @param pszHaystack The string to search.
1967 * @param pszNeedle The substring to search for.
1968 *
1969 */
1970RTDECL(char *) RTStrIStr(const char *pszHaystack, const char *pszNeedle);
1971
1972/**
1973 * Converts the string to lower case.
1974 *
1975 * @returns Pointer to the converted string.
1976 * @param psz The string to convert.
1977 */
1978RTDECL(char *) RTStrToLower(char *psz);
1979
1980/**
1981 * Converts the string to upper case.
1982 *
1983 * @returns Pointer to the converted string.
1984 * @param psz The string to convert.
1985 */
1986RTDECL(char *) RTStrToUpper(char *psz);
1987
1988/**
1989 * Find the length of a zero-terminated byte string, given
1990 * a max string length.
1991 *
1992 * See also RTStrNLenEx.
1993 *
1994 * @returns The string length or cbMax. The returned length does not include
1995 * the zero terminator if it was found.
1996 *
1997 * @param pszString The string.
1998 * @param cchMax The max string length.
1999 */
2000RTDECL(size_t) RTStrNLen(const char *pszString, size_t cchMax);
2001
2002/**
2003 * Find the length of a zero-terminated byte string, given
2004 * a max string length.
2005 *
2006 * See also RTStrNLen.
2007 *
2008 * @returns IPRT status code.
2009 * @retval VINF_SUCCESS if the string has a length less than cchMax.
2010 * @retval VERR_BUFFER_OVERFLOW if the end of the string wasn't found
2011 * before cchMax was reached.
2012 *
2013 * @param pszString The string.
2014 * @param cchMax The max string length.
2015 * @param pcch Where to store the string length excluding the
2016 * terminator. This is set to cchMax if the terminator
2017 * isn't found.
2018 */
2019RTDECL(int) RTStrNLenEx(const char *pszString, size_t cchMax, size_t *pcch);
2020
2021RT_C_DECLS_END
2022
2023/** The maximum size argument of a memchr call. */
2024#define RTSTR_MEMCHR_MAX (~(size_t)0x10000)
2025
2026/**
2027 * Find the zero terminator in a string with a limited length.
2028 *
2029 * @returns Pointer to the zero terminator.
2030 * @returns NULL if the zero terminator was not found.
2031 *
2032 * @param pszString The string.
2033 * @param cchMax The max string length. RTSTR_MAX is fine.
2034 */
2035#if defined(__cplusplus) && !defined(DOXYGEN_RUNNING)
2036DECLINLINE(char const *) RTStrEnd(char const *pszString, size_t cchMax)
2037{
2038 /* Avoid potential issues with memchr seen in glibc. */
2039 if (cchMax > RTSTR_MEMCHR_MAX)
2040 {
2041 char const *pszRet = (char const *)memchr(pszString, '\0', RTSTR_MEMCHR_MAX);
2042 if (RT_LIKELY(pszRet))
2043 return pszRet;
2044 pszString += RTSTR_MEMCHR_MAX;
2045 cchMax -= RTSTR_MEMCHR_MAX;
2046 }
2047 return (char const *)memchr(pszString, '\0', cchMax);
2048}
2049
2050DECLINLINE(char *) RTStrEnd(char *pszString, size_t cchMax)
2051#else
2052DECLINLINE(char *) RTStrEnd(const char *pszString, size_t cchMax)
2053#endif
2054{
2055 /* Avoid potential issues with memchr seen in glibc. */
2056 if (cchMax > RTSTR_MEMCHR_MAX)
2057 {
2058 char *pszRet = (char *)memchr(pszString, '\0', RTSTR_MEMCHR_MAX);
2059 if (RT_LIKELY(pszRet))
2060 return pszRet;
2061 pszString += RTSTR_MEMCHR_MAX;
2062 cchMax -= RTSTR_MEMCHR_MAX;
2063 }
2064 return (char *)memchr(pszString, '\0', cchMax);
2065}
2066
2067RT_C_DECLS_BEGIN
2068
2069/**
2070 * Matches a simple string pattern.
2071 *
2072 * @returns true if the string matches the pattern, otherwise false.
2073 *
2074 * @param pszPattern The pattern. Special chars are '*' and '?', where the
2075 * asterisk matches zero or more characters and question
2076 * mark matches exactly one character.
2077 * @param pszString The string to match against the pattern.
2078 */
2079RTDECL(bool) RTStrSimplePatternMatch(const char *pszPattern, const char *pszString);
2080
2081/**
2082 * Matches a simple string pattern, neither which needs to be zero terminated.
2083 *
2084 * This is identical to RTStrSimplePatternMatch except that you can optionally
2085 * specify the length of both the pattern and the string. The function will
2086 * stop when it hits a string terminator or either of the lengths.
2087 *
2088 * @returns true if the string matches the pattern, otherwise false.
2089 *
2090 * @param pszPattern The pattern. Special chars are '*' and '?', where the
2091 * asterisk matches zero or more characters and question
2092 * mark matches exactly one character.
2093 * @param cchPattern The pattern length. Pass RTSTR_MAX if you don't know the
2094 * length and wish to stop at the string terminator.
2095 * @param pszString The string to match against the pattern.
2096 * @param cchString The string length. Pass RTSTR_MAX if you don't know the
2097 * length and wish to match up to the string terminator.
2098 */
2099RTDECL(bool) RTStrSimplePatternNMatch(const char *pszPattern, size_t cchPattern,
2100 const char *pszString, size_t cchString);
2101
2102/**
2103 * Matches multiple patterns against a string.
2104 *
2105 * The patterns are separated by the pipe character (|).
2106 *
2107 * @returns true if the string matches the pattern, otherwise false.
2108 *
2109 * @param pszPatterns The patterns.
2110 * @param cchPatterns The lengths of the patterns to use. Pass RTSTR_MAX to
2111 * stop at the terminator.
2112 * @param pszString The string to match against the pattern.
2113 * @param cchString The string length. Pass RTSTR_MAX stop stop at the
2114 * terminator.
2115 * @param poffPattern Offset into the patterns string of the patttern that
2116 * matched. If no match, this will be set to RTSTR_MAX.
2117 * This is optional, NULL is fine.
2118 */
2119RTDECL(bool) RTStrSimplePatternMultiMatch(const char *pszPatterns, size_t cchPatterns,
2120 const char *pszString, size_t cchString,
2121 size_t *poffPattern);
2122
2123/**
2124 * Compares two version strings RTStrICmp fashion.
2125 *
2126 * The version string is split up into sections at punctuation, spaces,
2127 * underscores, dashes and pluss signs. The sections are then split up into
2128 * numeric and string sub-sections. Finally, the sub-sections are compared
2129 * in a numeric or case insesntivie fashion depending on what they are.
2130 *
2131 * The following strings are considered to be equal: "1.0.0", "1.00.0", "1.0",
2132 * "1". These aren't: "1.0.0r993", "1.0", "1.0r993", "1.0_Beta3", "1.1"
2133 *
2134 * @returns < 0 if the first string less than the second string.
2135 * @returns 0 if the first string identical to the second string.
2136 * @returns > 0 if the first string greater than the second string.
2137 *
2138 * @param pszVer1 First version string to compare.
2139 * @param pszVer2 Second version string to compare first version with.
2140 */
2141RTDECL(int) RTStrVersionCompare(const char *pszVer1, const char *pszVer2);
2142
2143
2144/** @defgroup rt_str_conv String To/From Number Conversions
2145 * @ingroup grp_rt_str
2146 * @{ */
2147
2148/**
2149 * Converts a string representation of a number to a 64-bit unsigned number.
2150 *
2151 * @returns iprt status code.
2152 * Warnings are used to indicate conversion problems.
2153 * @retval VWRN_NUMBER_TOO_BIG
2154 * @retval VWRN_NEGATIVE_UNSIGNED
2155 * @retval VWRN_TRAILING_CHARS
2156 * @retval VWRN_TRAILING_SPACES
2157 * @retval VINF_SUCCESS
2158 * @retval VERR_NO_DIGITS
2159 *
2160 * @param pszValue Pointer to the string value.
2161 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2162 * @param uBase The base of the representation used.
2163 * If 0 the function will look for known prefixes before defaulting to 10.
2164 * @param pu64 Where to store the converted number. (optional)
2165 */
2166RTDECL(int) RTStrToUInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint64_t *pu64);
2167
2168/**
2169 * Converts a string representation of a number to a 64-bit unsigned number,
2170 * making sure the full string is converted.
2171 *
2172 * @returns iprt status code.
2173 * Warnings are used to indicate conversion problems.
2174 * @retval VWRN_NUMBER_TOO_BIG
2175 * @retval VWRN_NEGATIVE_UNSIGNED
2176 * @retval VINF_SUCCESS
2177 * @retval VERR_NO_DIGITS
2178 * @retval VERR_TRAILING_SPACES
2179 * @retval VERR_TRAILING_CHARS
2180 *
2181 * @param pszValue Pointer to the string value.
2182 * @param uBase The base of the representation used.
2183 * If 0 the function will look for known prefixes before defaulting to 10.
2184 * @param pu64 Where to store the converted number. (optional)
2185 */
2186RTDECL(int) RTStrToUInt64Full(const char *pszValue, unsigned uBase, uint64_t *pu64);
2187
2188/**
2189 * Converts a string representation of a number to a 64-bit unsigned number.
2190 * The base is guessed.
2191 *
2192 * @returns 64-bit unsigned number on success.
2193 * @returns 0 on failure.
2194 * @param pszValue Pointer to the string value.
2195 */
2196RTDECL(uint64_t) RTStrToUInt64(const char *pszValue);
2197
2198/**
2199 * Converts a string representation of a number to a 32-bit unsigned number.
2200 *
2201 * @returns iprt status code.
2202 * Warnings are used to indicate conversion problems.
2203 * @retval VWRN_NUMBER_TOO_BIG
2204 * @retval VWRN_NEGATIVE_UNSIGNED
2205 * @retval VWRN_TRAILING_CHARS
2206 * @retval VWRN_TRAILING_SPACES
2207 * @retval VINF_SUCCESS
2208 * @retval VERR_NO_DIGITS
2209 *
2210 * @param pszValue Pointer to the string value.
2211 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2212 * @param uBase The base of the representation used.
2213 * If 0 the function will look for known prefixes before defaulting to 10.
2214 * @param pu32 Where to store the converted number. (optional)
2215 */
2216RTDECL(int) RTStrToUInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint32_t *pu32);
2217
2218/**
2219 * Converts a string representation of a number to a 32-bit unsigned number,
2220 * making sure the full string is converted.
2221 *
2222 * @returns iprt status code.
2223 * Warnings are used to indicate conversion problems.
2224 * @retval VWRN_NUMBER_TOO_BIG
2225 * @retval VWRN_NEGATIVE_UNSIGNED
2226 * @retval VINF_SUCCESS
2227 * @retval VERR_NO_DIGITS
2228 * @retval VERR_TRAILING_SPACES
2229 * @retval VERR_TRAILING_CHARS
2230 *
2231 * @param pszValue Pointer to the string value.
2232 * @param uBase The base of the representation used.
2233 * If 0 the function will look for known prefixes before defaulting to 10.
2234 * @param pu32 Where to store the converted number. (optional)
2235 */
2236RTDECL(int) RTStrToUInt32Full(const char *pszValue, unsigned uBase, uint32_t *pu32);
2237
2238/**
2239 * Converts a string representation of a number to a 64-bit unsigned number.
2240 * The base is guessed.
2241 *
2242 * @returns 32-bit unsigned number on success.
2243 * @returns 0 on failure.
2244 * @param pszValue Pointer to the string value.
2245 */
2246RTDECL(uint32_t) RTStrToUInt32(const char *pszValue);
2247
2248/**
2249 * Converts a string representation of a number to a 16-bit unsigned number.
2250 *
2251 * @returns iprt status code.
2252 * Warnings are used to indicate conversion problems.
2253 * @retval VWRN_NUMBER_TOO_BIG
2254 * @retval VWRN_NEGATIVE_UNSIGNED
2255 * @retval VWRN_TRAILING_CHARS
2256 * @retval VWRN_TRAILING_SPACES
2257 * @retval VINF_SUCCESS
2258 * @retval VERR_NO_DIGITS
2259 *
2260 * @param pszValue Pointer to the string value.
2261 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2262 * @param uBase The base of the representation used.
2263 * If 0 the function will look for known prefixes before defaulting to 10.
2264 * @param pu16 Where to store the converted number. (optional)
2265 */
2266RTDECL(int) RTStrToUInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint16_t *pu16);
2267
2268/**
2269 * Converts a string representation of a number to a 16-bit unsigned number,
2270 * making sure the full string is converted.
2271 *
2272 * @returns iprt status code.
2273 * Warnings are used to indicate conversion problems.
2274 * @retval VWRN_NUMBER_TOO_BIG
2275 * @retval VWRN_NEGATIVE_UNSIGNED
2276 * @retval VINF_SUCCESS
2277 * @retval VERR_NO_DIGITS
2278 * @retval VERR_TRAILING_SPACES
2279 * @retval VERR_TRAILING_CHARS
2280 *
2281 * @param pszValue Pointer to the string value.
2282 * @param uBase The base of the representation used.
2283 * If 0 the function will look for known prefixes before defaulting to 10.
2284 * @param pu16 Where to store the converted number. (optional)
2285 */
2286RTDECL(int) RTStrToUInt16Full(const char *pszValue, unsigned uBase, uint16_t *pu16);
2287
2288/**
2289 * Converts a string representation of a number to a 16-bit unsigned number.
2290 * The base is guessed.
2291 *
2292 * @returns 16-bit unsigned number on success.
2293 * @returns 0 on failure.
2294 * @param pszValue Pointer to the string value.
2295 */
2296RTDECL(uint16_t) RTStrToUInt16(const char *pszValue);
2297
2298/**
2299 * Converts a string representation of a number to a 8-bit unsigned number.
2300 *
2301 * @returns iprt status code.
2302 * Warnings are used to indicate conversion problems.
2303 * @retval VWRN_NUMBER_TOO_BIG
2304 * @retval VWRN_NEGATIVE_UNSIGNED
2305 * @retval VWRN_TRAILING_CHARS
2306 * @retval VWRN_TRAILING_SPACES
2307 * @retval VINF_SUCCESS
2308 * @retval VERR_NO_DIGITS
2309 *
2310 * @param pszValue Pointer to the string value.
2311 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2312 * @param uBase The base of the representation used.
2313 * If 0 the function will look for known prefixes before defaulting to 10.
2314 * @param pu8 Where to store the converted number. (optional)
2315 */
2316RTDECL(int) RTStrToUInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint8_t *pu8);
2317
2318/**
2319 * Converts a string representation of a number to a 8-bit unsigned number,
2320 * making sure the full string is converted.
2321 *
2322 * @returns iprt status code.
2323 * Warnings are used to indicate conversion problems.
2324 * @retval VWRN_NUMBER_TOO_BIG
2325 * @retval VWRN_NEGATIVE_UNSIGNED
2326 * @retval VINF_SUCCESS
2327 * @retval VERR_NO_DIGITS
2328 * @retval VERR_TRAILING_SPACES
2329 * @retval VERR_TRAILING_CHARS
2330 *
2331 * @param pszValue Pointer to the string value.
2332 * @param uBase The base of the representation used.
2333 * If 0 the function will look for known prefixes before defaulting to 10.
2334 * @param pu8 Where to store the converted number. (optional)
2335 */
2336RTDECL(int) RTStrToUInt8Full(const char *pszValue, unsigned uBase, uint8_t *pu8);
2337
2338/**
2339 * Converts a string representation of a number to a 8-bit unsigned number.
2340 * The base is guessed.
2341 *
2342 * @returns 8-bit unsigned number on success.
2343 * @returns 0 on failure.
2344 * @param pszValue Pointer to the string value.
2345 */
2346RTDECL(uint8_t) RTStrToUInt8(const char *pszValue);
2347
2348/**
2349 * Converts a string representation of a number to a 64-bit signed number.
2350 *
2351 * @returns iprt status code.
2352 * Warnings are used to indicate conversion problems.
2353 * @retval VWRN_NUMBER_TOO_BIG
2354 * @retval VWRN_TRAILING_CHARS
2355 * @retval VWRN_TRAILING_SPACES
2356 * @retval VINF_SUCCESS
2357 * @retval VERR_NO_DIGITS
2358 *
2359 * @param pszValue Pointer to the string value.
2360 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2361 * @param uBase The base of the representation used.
2362 * If 0 the function will look for known prefixes before defaulting to 10.
2363 * @param pi64 Where to store the converted number. (optional)
2364 */
2365RTDECL(int) RTStrToInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, int64_t *pi64);
2366
2367/**
2368 * Converts a string representation of a number to a 64-bit signed number,
2369 * making sure the full string is converted.
2370 *
2371 * @returns iprt status code.
2372 * Warnings are used to indicate conversion problems.
2373 * @retval VWRN_NUMBER_TOO_BIG
2374 * @retval VINF_SUCCESS
2375 * @retval VERR_TRAILING_CHARS
2376 * @retval VERR_TRAILING_SPACES
2377 * @retval VERR_NO_DIGITS
2378 *
2379 * @param pszValue Pointer to the string value.
2380 * @param uBase The base of the representation used.
2381 * If 0 the function will look for known prefixes before defaulting to 10.
2382 * @param pi64 Where to store the converted number. (optional)
2383 */
2384RTDECL(int) RTStrToInt64Full(const char *pszValue, unsigned uBase, int64_t *pi64);
2385
2386/**
2387 * Converts a string representation of a number to a 64-bit signed number.
2388 * The base is guessed.
2389 *
2390 * @returns 64-bit signed number on success.
2391 * @returns 0 on failure.
2392 * @param pszValue Pointer to the string value.
2393 */
2394RTDECL(int64_t) RTStrToInt64(const char *pszValue);
2395
2396/**
2397 * Converts a string representation of a number to a 32-bit signed number.
2398 *
2399 * @returns iprt status code.
2400 * Warnings are used to indicate conversion problems.
2401 * @retval VWRN_NUMBER_TOO_BIG
2402 * @retval VWRN_TRAILING_CHARS
2403 * @retval VWRN_TRAILING_SPACES
2404 * @retval VINF_SUCCESS
2405 * @retval VERR_NO_DIGITS
2406 *
2407 * @param pszValue Pointer to the string value.
2408 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2409 * @param uBase The base of the representation used.
2410 * If 0 the function will look for known prefixes before defaulting to 10.
2411 * @param pi32 Where to store the converted number. (optional)
2412 */
2413RTDECL(int) RTStrToInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, int32_t *pi32);
2414
2415/**
2416 * Converts a string representation of a number to a 32-bit signed number,
2417 * making sure the full string is converted.
2418 *
2419 * @returns iprt status code.
2420 * Warnings are used to indicate conversion problems.
2421 * @retval VWRN_NUMBER_TOO_BIG
2422 * @retval VINF_SUCCESS
2423 * @retval VERR_TRAILING_CHARS
2424 * @retval VERR_TRAILING_SPACES
2425 * @retval VERR_NO_DIGITS
2426 *
2427 * @param pszValue Pointer to the string value.
2428 * @param uBase The base of the representation used.
2429 * If 0 the function will look for known prefixes before defaulting to 10.
2430 * @param pi32 Where to store the converted number. (optional)
2431 */
2432RTDECL(int) RTStrToInt32Full(const char *pszValue, unsigned uBase, int32_t *pi32);
2433
2434/**
2435 * Converts a string representation of a number to a 32-bit signed number.
2436 * The base is guessed.
2437 *
2438 * @returns 32-bit signed number on success.
2439 * @returns 0 on failure.
2440 * @param pszValue Pointer to the string value.
2441 */
2442RTDECL(int32_t) RTStrToInt32(const char *pszValue);
2443
2444/**
2445 * Converts a string representation of a number to a 16-bit signed number.
2446 *
2447 * @returns iprt status code.
2448 * Warnings are used to indicate conversion problems.
2449 * @retval VWRN_NUMBER_TOO_BIG
2450 * @retval VWRN_TRAILING_CHARS
2451 * @retval VWRN_TRAILING_SPACES
2452 * @retval VINF_SUCCESS
2453 * @retval VERR_NO_DIGITS
2454 *
2455 * @param pszValue Pointer to the string value.
2456 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2457 * @param uBase The base of the representation used.
2458 * If 0 the function will look for known prefixes before defaulting to 10.
2459 * @param pi16 Where to store the converted number. (optional)
2460 */
2461RTDECL(int) RTStrToInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, int16_t *pi16);
2462
2463/**
2464 * Converts a string representation of a number to a 16-bit signed number,
2465 * making sure the full string is converted.
2466 *
2467 * @returns iprt status code.
2468 * Warnings are used to indicate conversion problems.
2469 * @retval VWRN_NUMBER_TOO_BIG
2470 * @retval VINF_SUCCESS
2471 * @retval VERR_TRAILING_CHARS
2472 * @retval VERR_TRAILING_SPACES
2473 * @retval VERR_NO_DIGITS
2474 *
2475 * @param pszValue Pointer to the string value.
2476 * @param uBase The base of the representation used.
2477 * If 0 the function will look for known prefixes before defaulting to 10.
2478 * @param pi16 Where to store the converted number. (optional)
2479 */
2480RTDECL(int) RTStrToInt16Full(const char *pszValue, unsigned uBase, int16_t *pi16);
2481
2482/**
2483 * Converts a string representation of a number to a 16-bit signed number.
2484 * The base is guessed.
2485 *
2486 * @returns 16-bit signed number on success.
2487 * @returns 0 on failure.
2488 * @param pszValue Pointer to the string value.
2489 */
2490RTDECL(int16_t) RTStrToInt16(const char *pszValue);
2491
2492/**
2493 * Converts a string representation of a number to a 8-bit signed number.
2494 *
2495 * @returns iprt status code.
2496 * Warnings are used to indicate conversion problems.
2497 * @retval VWRN_NUMBER_TOO_BIG
2498 * @retval VWRN_TRAILING_CHARS
2499 * @retval VWRN_TRAILING_SPACES
2500 * @retval VINF_SUCCESS
2501 * @retval VERR_NO_DIGITS
2502 *
2503 * @param pszValue Pointer to the string value.
2504 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
2505 * @param uBase The base of the representation used.
2506 * If 0 the function will look for known prefixes before defaulting to 10.
2507 * @param pi8 Where to store the converted number. (optional)
2508 */
2509RTDECL(int) RTStrToInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, int8_t *pi8);
2510
2511/**
2512 * Converts a string representation of a number to a 8-bit signed number,
2513 * making sure the full string is converted.
2514 *
2515 * @returns iprt status code.
2516 * Warnings are used to indicate conversion problems.
2517 * @retval VWRN_NUMBER_TOO_BIG
2518 * @retval VINF_SUCCESS
2519 * @retval VERR_TRAILING_CHARS
2520 * @retval VERR_TRAILING_SPACES
2521 * @retval VERR_NO_DIGITS
2522 *
2523 * @param pszValue Pointer to the string value.
2524 * @param uBase The base of the representation used.
2525 * If 0 the function will look for known prefixes before defaulting to 10.
2526 * @param pi8 Where to store the converted number. (optional)
2527 */
2528RTDECL(int) RTStrToInt8Full(const char *pszValue, unsigned uBase, int8_t *pi8);
2529
2530/**
2531 * Converts a string representation of a number to a 8-bit signed number.
2532 * The base is guessed.
2533 *
2534 * @returns 8-bit signed number on success.
2535 * @returns 0 on failure.
2536 * @param pszValue Pointer to the string value.
2537 */
2538RTDECL(int8_t) RTStrToInt8(const char *pszValue);
2539
2540/**
2541 * Formats a buffer stream as hex bytes.
2542 *
2543 * The default is no separating spaces or line breaks or anything.
2544 *
2545 * @returns IPRT status code.
2546 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
2547 * @retval VERR_BUFFER_OVERFLOW if the buffer is insufficent to hold the bytes.
2548 *
2549 * @param pszBuf Output string buffer.
2550 * @param cchBuf The size of the output buffer.
2551 * @param pv Pointer to the bytes to stringify.
2552 * @param cb The number of bytes to stringify.
2553 * @param fFlags Must be zero, reserved for future use.
2554 */
2555RTDECL(int) RTStrPrintHexBytes(char *pszBuf, size_t cchBuf, void const *pv, size_t cb, uint32_t fFlags);
2556
2557/**
2558 * Converts a string of hex bytes back into binary data.
2559 *
2560 * @returns IPRT status code.
2561 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
2562 * @retval VERR_BUFFER_OVERFLOW if the string contains too many hex bytes.
2563 * @retval VERR_BUFFER_UNDERFLOW if there aren't enough hex bytes to fill up
2564 * the output buffer.
2565 * @retval VERR_UNEVEN_INPUT if the input contains a half byte.
2566 * @retval VERR_NO_DIGITS
2567 * @retval VWRN_TRAILING_CHARS
2568 * @retval VWRN_TRAILING_SPACES
2569 *
2570 * @param pszHex The string containing the hex bytes.
2571 * @param pv Output buffer.
2572 * @param cb The size of the output buffer.
2573 * @param fFlags Must be zero, reserved for future use.
2574 */
2575RTDECL(int) RTStrConvertHexBytes(char const *pszHex, void *pv, size_t cb, uint32_t fFlags);
2576
2577/** @} */
2578
2579
2580/** @defgroup rt_str_space Unique String Space
2581 * @ingroup grp_rt_str
2582 * @{
2583 */
2584
2585/** Pointer to a string name space container node core. */
2586typedef struct RTSTRSPACECORE *PRTSTRSPACECORE;
2587/** Pointer to a pointer to a string name space container node core. */
2588typedef PRTSTRSPACECORE *PPRTSTRSPACECORE;
2589
2590/**
2591 * String name space container node core.
2592 */
2593typedef struct RTSTRSPACECORE
2594{
2595 /** Hash key. Don't touch. */
2596 uint32_t Key;
2597 /** Pointer to the left leaf node. Don't touch. */
2598 PRTSTRSPACECORE pLeft;
2599 /** Pointer to the left rigth node. Don't touch. */
2600 PRTSTRSPACECORE pRight;
2601 /** Pointer to the list of string with the same key. Don't touch. */
2602 PRTSTRSPACECORE pList;
2603 /** Height of this tree: max(heigth(left), heigth(right)) + 1. Don't touch */
2604 unsigned char uchHeight;
2605 /** The string length. Read only! */
2606 size_t cchString;
2607 /** Pointer to the string. Read only! */
2608 const char *pszString;
2609} RTSTRSPACECORE;
2610
2611/** String space. (Initialize with NULL.) */
2612typedef PRTSTRSPACECORE RTSTRSPACE;
2613/** Pointer to a string space. */
2614typedef PPRTSTRSPACECORE PRTSTRSPACE;
2615
2616
2617/**
2618 * Inserts a string into a unique string space.
2619 *
2620 * @returns true on success.
2621 * @returns false if the string collided with an existing string.
2622 * @param pStrSpace The space to insert it into.
2623 * @param pStr The string node.
2624 */
2625RTDECL(bool) RTStrSpaceInsert(PRTSTRSPACE pStrSpace, PRTSTRSPACECORE pStr);
2626
2627/**
2628 * Removes a string from a unique string space.
2629 *
2630 * @returns Pointer to the removed string node.
2631 * @returns NULL if the string was not found in the string space.
2632 * @param pStrSpace The space to insert it into.
2633 * @param pszString The string to remove.
2634 */
2635RTDECL(PRTSTRSPACECORE) RTStrSpaceRemove(PRTSTRSPACE pStrSpace, const char *pszString);
2636
2637/**
2638 * Gets a string from a unique string space.
2639 *
2640 * @returns Pointer to the string node.
2641 * @returns NULL if the string was not found in the string space.
2642 * @param pStrSpace The space to insert it into.
2643 * @param pszString The string to get.
2644 */
2645RTDECL(PRTSTRSPACECORE) RTStrSpaceGet(PRTSTRSPACE pStrSpace, const char *pszString);
2646
2647/**
2648 * Callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy().
2649 *
2650 * @returns 0 on continue.
2651 * @returns Non-zero to aborts the operation.
2652 * @param pStr The string node
2653 * @param pvUser The user specified argument.
2654 */
2655typedef DECLCALLBACK(int) FNRTSTRSPACECALLBACK(PRTSTRSPACECORE pStr, void *pvUser);
2656/** Pointer to callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy(). */
2657typedef FNRTSTRSPACECALLBACK *PFNRTSTRSPACECALLBACK;
2658
2659/**
2660 * Destroys the string space.
2661 * The caller supplies a callback which will be called for each of
2662 * the string nodes in for freeing their memory and other resources.
2663 *
2664 * @returns 0 or what ever non-zero return value pfnCallback returned
2665 * when aborting the destruction.
2666 * @param pStrSpace The space to insert it into.
2667 * @param pfnCallback The callback.
2668 * @param pvUser The user argument.
2669 */
2670RTDECL(int) RTStrSpaceDestroy(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
2671
2672/**
2673 * Enumerates the string space.
2674 * The caller supplies a callback which will be called for each of
2675 * the string nodes.
2676 *
2677 * @returns 0 or what ever non-zero return value pfnCallback returned
2678 * when aborting the destruction.
2679 * @param pStrSpace The space to insert it into.
2680 * @param pfnCallback The callback.
2681 * @param pvUser The user argument.
2682 */
2683RTDECL(int) RTStrSpaceEnumerate(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
2684
2685/** @} */
2686
2687
2688/** @defgroup rt_str_utf16 UTF-16 String Manipulation
2689 * @ingroup grp_rt_str
2690 * @{
2691 */
2692
2693/**
2694 * Free a UTF-16 string allocated by RTStrToUtf16(), RTStrToUtf16Ex(),
2695 * RTLatin1ToUtf16(), RTLatin1ToUtf16Ex(), RTUtf16Dup() or RTUtf16DupEx().
2696 *
2697 * @returns iprt status code.
2698 * @param pwszString The UTF-16 string to free. NULL is accepted.
2699 */
2700RTDECL(void) RTUtf16Free(PRTUTF16 pwszString);
2701
2702/**
2703 * Allocates a new copy of the specified UTF-16 string (default tag).
2704 *
2705 * @returns Pointer to the allocated string copy. Use RTUtf16Free() to free it.
2706 * @returns NULL when out of memory.
2707 * @param pwszString UTF-16 string to duplicate.
2708 * @remark This function will not make any attempt to validate the encoding.
2709 */
2710#define RTUtf16Dup(pwszString) RTUtf16DupTag((pwszString), RTSTR_TAG)
2711
2712/**
2713 * Allocates a new copy of the specified UTF-16 string (custom tag).
2714 *
2715 * @returns Pointer to the allocated string copy. Use RTUtf16Free() to free it.
2716 * @returns NULL when out of memory.
2717 * @param pwszString UTF-16 string to duplicate.
2718 * @param pszTag Allocation tag used for statistics and such.
2719 * @remark This function will not make any attempt to validate the encoding.
2720 */
2721RTDECL(PRTUTF16) RTUtf16DupTag(PCRTUTF16 pwszString, const char *pszTag);
2722
2723/**
2724 * Allocates a new copy of the specified UTF-16 string (default tag).
2725 *
2726 * @returns iprt status code.
2727 * @param ppwszString Receives pointer of the allocated UTF-16 string.
2728 * The returned pointer must be freed using RTUtf16Free().
2729 * @param pwszString UTF-16 string to duplicate.
2730 * @param cwcExtra Number of extra RTUTF16 items to allocate.
2731 * @remark This function will not make any attempt to validate the encoding.
2732 */
2733#define RTUtf16DupEx(ppwszString, pwszString, cwcExtra) \
2734 RTUtf16DupExTag((ppwszString), (pwszString), (cwcExtra), RTSTR_TAG)
2735
2736/**
2737 * Allocates a new copy of the specified UTF-16 string (custom tag).
2738 *
2739 * @returns iprt status code.
2740 * @param ppwszString Receives pointer of the allocated UTF-16 string.
2741 * The returned pointer must be freed using RTUtf16Free().
2742 * @param pwszString UTF-16 string to duplicate.
2743 * @param cwcExtra Number of extra RTUTF16 items to allocate.
2744 * @param pszTag Allocation tag used for statistics and such.
2745 * @remark This function will not make any attempt to validate the encoding.
2746 */
2747RTDECL(int) RTUtf16DupExTag(PRTUTF16 *ppwszString, PCRTUTF16 pwszString, size_t cwcExtra, const char *pszTag);
2748
2749/**
2750 * Returns the length of a UTF-16 string in UTF-16 characters
2751 * without trailing '\\0'.
2752 *
2753 * Surrogate pairs counts as two UTF-16 characters here. Use RTUtf16CpCnt()
2754 * to get the exact number of code points in the string.
2755 *
2756 * @returns The number of RTUTF16 items in the string.
2757 * @param pwszString Pointer the UTF-16 string.
2758 * @remark This function will not make any attempt to validate the encoding.
2759 */
2760RTDECL(size_t) RTUtf16Len(PCRTUTF16 pwszString);
2761
2762/**
2763 * Performs a case sensitive string compare between two UTF-16 strings.
2764 *
2765 * @returns < 0 if the first string less than the second string.s
2766 * @returns 0 if the first string identical to the second string.
2767 * @returns > 0 if the first string greater than the second string.
2768 * @param pwsz1 First UTF-16 string. Null is allowed.
2769 * @param pwsz2 Second UTF-16 string. Null is allowed.
2770 * @remark This function will not make any attempt to validate the encoding.
2771 */
2772RTDECL(int) RTUtf16Cmp(register PCRTUTF16 pwsz1, register PCRTUTF16 pwsz2);
2773
2774/**
2775 * Performs a case insensitive string compare between two UTF-16 strings.
2776 *
2777 * This is a simplified compare, as only the simplified lower/upper case folding
2778 * specified by the unicode specs are used. It does not consider character pairs
2779 * as they are used in some languages, just simple upper & lower case compares.
2780 *
2781 * @returns < 0 if the first string less than the second string.
2782 * @returns 0 if the first string identical to the second string.
2783 * @returns > 0 if the first string greater than the second string.
2784 * @param pwsz1 First UTF-16 string. Null is allowed.
2785 * @param pwsz2 Second UTF-16 string. Null is allowed.
2786 */
2787RTDECL(int) RTUtf16ICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
2788
2789/**
2790 * Performs a case insensitive string compare between two UTF-16 strings
2791 * using the current locale of the process (if applicable).
2792 *
2793 * This differs from RTUtf16ICmp() in that it will try, if a locale with the
2794 * required data is available, to do a correct case-insensitive compare. It
2795 * follows that it is more complex and thereby likely to be more expensive.
2796 *
2797 * @returns < 0 if the first string less than the second string.
2798 * @returns 0 if the first string identical to the second string.
2799 * @returns > 0 if the first string greater than the second string.
2800 * @param pwsz1 First UTF-16 string. Null is allowed.
2801 * @param pwsz2 Second UTF-16 string. Null is allowed.
2802 */
2803RTDECL(int) RTUtf16LocaleICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
2804
2805/**
2806 * Folds a UTF-16 string to lowercase.
2807 *
2808 * This is a very simple folding; is uses the simple lowercase
2809 * code point, it is not related to any locale just the most common
2810 * lowercase codepoint setup by the unicode specs, and it will not
2811 * create new surrogate pairs or remove existing ones.
2812 *
2813 * @returns Pointer to the passed in string.
2814 * @param pwsz The string to fold.
2815 */
2816RTDECL(PRTUTF16) RTUtf16ToLower(PRTUTF16 pwsz);
2817
2818/**
2819 * Folds a UTF-16 string to uppercase.
2820 *
2821 * This is a very simple folding; is uses the simple uppercase
2822 * code point, it is not related to any locale just the most common
2823 * uppercase codepoint setup by the unicode specs, and it will not
2824 * create new surrogate pairs or remove existing ones.
2825 *
2826 * @returns Pointer to the passed in string.
2827 * @param pwsz The string to fold.
2828 */
2829RTDECL(PRTUTF16) RTUtf16ToUpper(PRTUTF16 pwsz);
2830
2831/**
2832 * Translate a UTF-16 string into a UTF-8 allocating the result buffer (default
2833 * tag).
2834 *
2835 * @returns iprt status code.
2836 * @param pwszString UTF-16 string to convert.
2837 * @param ppszString Receives pointer of allocated UTF-8 string on
2838 * success, and is always set to NULL on failure.
2839 * The returned pointer must be freed using RTStrFree().
2840 */
2841#define RTUtf16ToUtf8(pwszString, ppszString) RTUtf16ToUtf8Tag((pwszString), (ppszString), RTSTR_TAG)
2842
2843/**
2844 * Translate a UTF-16 string into a UTF-8 allocating the result buffer.
2845 *
2846 * @returns iprt status code.
2847 * @param pwszString UTF-16 string to convert.
2848 * @param ppszString Receives pointer of allocated UTF-8 string on
2849 * success, and is always set to NULL on failure.
2850 * The returned pointer must be freed using RTStrFree().
2851 * @param pszTag Allocation tag used for statistics and such.
2852 */
2853RTDECL(int) RTUtf16ToUtf8Tag(PCRTUTF16 pwszString, char **ppszString, const char *pszTag);
2854
2855/**
2856 * Translates UTF-16 to UTF-8 using buffer provided by the caller or a fittingly
2857 * sized buffer allocated by the function (default tag).
2858 *
2859 * @returns iprt status code.
2860 * @param pwszString The UTF-16 string to convert.
2861 * @param cwcString The number of RTUTF16 items to translate from pwszString.
2862 * The translation will stop when reaching cwcString or the terminator ('\\0').
2863 * Use RTSTR_MAX to translate the entire string.
2864 * @param ppsz If cch is non-zero, this must either be pointing to a pointer to
2865 * a buffer of the specified size, or pointer to a NULL pointer.
2866 * If *ppsz is NULL or cch is zero a buffer of at least cch chars
2867 * will be allocated to hold the translated string.
2868 * If a buffer was requested it must be freed using RTStrFree().
2869 * @param cch The buffer size in chars (the type). This includes the terminator.
2870 * @param pcch Where to store the length of the translated string,
2871 * excluding the terminator. (Optional)
2872 *
2873 * This may be set under some error conditions,
2874 * however, only for VERR_BUFFER_OVERFLOW and
2875 * VERR_NO_STR_MEMORY will it contain a valid string
2876 * length that can be used to resize the buffer.
2877 */
2878#define RTUtf16ToUtf8Ex(pwszString, cwcString, ppsz, cch, pcch) \
2879 RTUtf16ToUtf8ExTag((pwszString), (cwcString), (ppsz), (cch), (pcch), RTSTR_TAG)
2880
2881/**
2882 * Translates UTF-16 to UTF-8 using buffer provided by the caller or a fittingly
2883 * sized buffer allocated by the function (custom tag).
2884 *
2885 * @returns iprt status code.
2886 * @param pwszString The UTF-16 string to convert.
2887 * @param cwcString The number of RTUTF16 items to translate from pwszString.
2888 * The translation will stop when reaching cwcString or the terminator ('\\0').
2889 * Use RTSTR_MAX to translate the entire string.
2890 * @param ppsz If cch is non-zero, this must either be pointing to a pointer to
2891 * a buffer of the specified size, or pointer to a NULL pointer.
2892 * If *ppsz is NULL or cch is zero a buffer of at least cch chars
2893 * will be allocated to hold the translated string.
2894 * If a buffer was requested it must be freed using RTStrFree().
2895 * @param cch The buffer size in chars (the type). This includes the terminator.
2896 * @param pcch Where to store the length of the translated string,
2897 * excluding the terminator. (Optional)
2898 *
2899 * This may be set under some error conditions,
2900 * however, only for VERR_BUFFER_OVERFLOW and
2901 * VERR_NO_STR_MEMORY will it contain a valid string
2902 * length that can be used to resize the buffer.
2903 * @param pszTag Allocation tag used for statistics and such.
2904 */
2905RTDECL(int) RTUtf16ToUtf8ExTag(PCRTUTF16 pwszString, size_t cwcString, char **ppsz, size_t cch, size_t *pcch, const char *pszTag);
2906
2907/**
2908 * Calculates the length of the UTF-16 string in UTF-8 chars (bytes).
2909 *
2910 * This function will validate the string, and incorrectly encoded UTF-16
2911 * strings will be rejected. The primary purpose of this function is to
2912 * help allocate buffers for RTUtf16ToUtf8() of the correct size. For most
2913 * other purposes RTUtf16ToUtf8Ex() should be used.
2914 *
2915 * @returns Number of char (bytes).
2916 * @returns 0 if the string was incorrectly encoded.
2917 * @param pwsz The UTF-16 string.
2918 */
2919RTDECL(size_t) RTUtf16CalcUtf8Len(PCRTUTF16 pwsz);
2920
2921/**
2922 * Calculates the length of the UTF-16 string in UTF-8 chars (bytes).
2923 *
2924 * This function will validate the string, and incorrectly encoded UTF-16
2925 * strings will be rejected.
2926 *
2927 * @returns iprt status code.
2928 * @param pwsz The string.
2929 * @param cwc The max string length. Use RTSTR_MAX to process the entire string.
2930 * @param pcch Where to store the string length (in bytes). Optional.
2931 * This is undefined on failure.
2932 */
2933RTDECL(int) RTUtf16CalcUtf8LenEx(PCRTUTF16 pwsz, size_t cwc, size_t *pcch);
2934
2935/**
2936 * Translate a UTF-16 string into a Latin-1 (ISO-8859-1) allocating the result
2937 * buffer (default tag).
2938 *
2939 * @returns iprt status code.
2940 * @param pwszString UTF-16 string to convert.
2941 * @param ppszString Receives pointer of allocated Latin1 string on
2942 * success, and is always set to NULL on failure.
2943 * The returned pointer must be freed using RTStrFree().
2944 */
2945#define RTUtf16ToLatin1(pwszString, ppszString) RTUtf16ToLatin1Tag((pwszString), (ppszString), RTSTR_TAG)
2946
2947/**
2948 * Translate a UTF-16 string into a Latin-1 (ISO-8859-1) allocating the result
2949 * buffer (custom tag).
2950 *
2951 * @returns iprt status code.
2952 * @param pwszString UTF-16 string to convert.
2953 * @param ppszString Receives pointer of allocated Latin1 string on
2954 * success, and is always set to NULL on failure.
2955 * The returned pointer must be freed using RTStrFree().
2956 * @param pszTag Allocation tag used for statistics and such.
2957 */
2958RTDECL(int) RTUtf16ToLatin1Tag(PCRTUTF16 pwszString, char **ppszString, const char *pszTag);
2959
2960/**
2961 * Translates UTF-16 to Latin-1 (ISO-8859-1) using buffer provided by the caller
2962 * or a fittingly sized buffer allocated by the function (default tag).
2963 *
2964 * @returns iprt status code.
2965 * @param pwszString The UTF-16 string to convert.
2966 * @param cwcString The number of RTUTF16 items to translate from
2967 * pwszString. The translation will stop when reaching
2968 * cwcString or the terminator ('\\0'). Use RTSTR_MAX
2969 * to translate the entire string.
2970 * @param ppsz Pointer to the pointer to the Latin-1 string. The
2971 * buffer can optionally be preallocated by the caller.
2972 *
2973 * If cch is zero, *ppsz is undefined.
2974 *
2975 * If cch is non-zero and *ppsz is not NULL, then this
2976 * will be used as the output buffer.
2977 * VERR_BUFFER_OVERFLOW will be returned if this is
2978 * insufficient.
2979 *
2980 * If cch is zero or *ppsz is NULL, then a buffer of
2981 * sufficent size is allocated. cch can be used to
2982 * specify a minimum size of this buffer. Use
2983 * RTUtf16Free() to free the result.
2984 *
2985 * @param cch The buffer size in chars (the type). This includes
2986 * the terminator.
2987 * @param pcch Where to store the length of the translated string,
2988 * excluding the terminator. (Optional)
2989 *
2990 * This may be set under some error conditions,
2991 * however, only for VERR_BUFFER_OVERFLOW and
2992 * VERR_NO_STR_MEMORY will it contain a valid string
2993 * length that can be used to resize the buffer.
2994 */
2995#define RTUtf16ToLatin1Ex(pwszString, cwcString, ppsz, cch, pcch) \
2996 RTUtf16ToLatin1ExTag((pwszString), (cwcString), (ppsz), (cch), (pcch), RTSTR_TAG)
2997
2998/**
2999 * Translates UTF-16 to Latin-1 (ISO-8859-1) using buffer provided by the caller
3000 * or a fittingly sized buffer allocated by the function (custom tag).
3001 *
3002 * @returns iprt status code.
3003 * @param pwszString The UTF-16 string to convert.
3004 * @param cwcString The number of RTUTF16 items to translate from
3005 * pwszString. The translation will stop when reaching
3006 * cwcString or the terminator ('\\0'). Use RTSTR_MAX
3007 * to translate the entire string.
3008 * @param ppsz Pointer to the pointer to the Latin-1 string. The
3009 * buffer can optionally be preallocated by the caller.
3010 *
3011 * If cch is zero, *ppsz is undefined.
3012 *
3013 * If cch is non-zero and *ppsz is not NULL, then this
3014 * will be used as the output buffer.
3015 * VERR_BUFFER_OVERFLOW will be returned if this is
3016 * insufficient.
3017 *
3018 * If cch is zero or *ppsz is NULL, then a buffer of
3019 * sufficent size is allocated. cch can be used to
3020 * specify a minimum size of this buffer. Use
3021 * RTUtf16Free() to free the result.
3022 *
3023 * @param cch The buffer size in chars (the type). This includes
3024 * the terminator.
3025 * @param pcch Where to store the length of the translated string,
3026 * excluding the terminator. (Optional)
3027 *
3028 * This may be set under some error conditions,
3029 * however, only for VERR_BUFFER_OVERFLOW and
3030 * VERR_NO_STR_MEMORY will it contain a valid string
3031 * length that can be used to resize the buffer.
3032 * @param pszTag Allocation tag used for statistics and such.
3033 */
3034RTDECL(int) RTUtf16ToLatin1ExTag(PCRTUTF16 pwszString, size_t cwcString, char **ppsz, size_t cch, size_t *pcch, const char *pszTag);
3035
3036/**
3037 * Calculates the length of the UTF-16 string in Latin-1 (ISO-8859-1) chars.
3038 *
3039 * This function will validate the string, and incorrectly encoded UTF-16
3040 * strings will be rejected. The primary purpose of this function is to
3041 * help allocate buffers for RTUtf16ToLatin1() of the correct size. For most
3042 * other purposes RTUtf16ToLatin1Ex() should be used.
3043 *
3044 * @returns Number of char (bytes).
3045 * @returns 0 if the string was incorrectly encoded.
3046 * @param pwsz The UTF-16 string.
3047 */
3048RTDECL(size_t) RTUtf16CalcLatin1Len(PCRTUTF16 pwsz);
3049
3050/**
3051 * Calculates the length of the UTF-16 string in Latin-1 (ISO-8859-1) chars.
3052 *
3053 * This function will validate the string, and incorrectly encoded UTF-16
3054 * strings will be rejected.
3055 *
3056 * @returns iprt status code.
3057 * @param pwsz The string.
3058 * @param cwc The max string length. Use RTSTR_MAX to process the
3059 * entire string.
3060 * @param pcch Where to store the string length (in bytes). Optional.
3061 * This is undefined on failure.
3062 */
3063RTDECL(int) RTUtf16CalcLatin1LenEx(PCRTUTF16 pwsz, size_t cwc, size_t *pcch);
3064
3065/**
3066 * Get the unicode code point at the given string position.
3067 *
3068 * @returns unicode code point.
3069 * @returns RTUNICP_INVALID if the encoding is invalid.
3070 * @param pwsz The string.
3071 *
3072 * @remark This is an internal worker for RTUtf16GetCp().
3073 */
3074RTDECL(RTUNICP) RTUtf16GetCpInternal(PCRTUTF16 pwsz);
3075
3076/**
3077 * Get the unicode code point at the given string position.
3078 *
3079 * @returns iprt status code.
3080 * @param ppwsz Pointer to the string pointer. This will be updated to
3081 * point to the char following the current code point.
3082 * @param pCp Where to store the code point.
3083 * RTUNICP_INVALID is stored here on failure.
3084 *
3085 * @remark This is an internal worker for RTUtf16GetCpEx().
3086 */
3087RTDECL(int) RTUtf16GetCpExInternal(PCRTUTF16 *ppwsz, PRTUNICP pCp);
3088
3089/**
3090 * Put the unicode code point at the given string position
3091 * and return the pointer to the char following it.
3092 *
3093 * This function will not consider anything at or following the
3094 * buffer area pointed to by pwsz. It is therefore not suitable for
3095 * inserting code points into a string, only appending/overwriting.
3096 *
3097 * @returns pointer to the char following the written code point.
3098 * @param pwsz The string.
3099 * @param CodePoint The code point to write.
3100 * This should not be RTUNICP_INVALID or any other
3101 * character out of the UTF-16 range.
3102 *
3103 * @remark This is an internal worker for RTUtf16GetCpEx().
3104 */
3105RTDECL(PRTUTF16) RTUtf16PutCpInternal(PRTUTF16 pwsz, RTUNICP CodePoint);
3106
3107/**
3108 * Get the unicode code point at the given string position.
3109 *
3110 * @returns unicode code point.
3111 * @returns RTUNICP_INVALID if the encoding is invalid.
3112 * @param pwsz The string.
3113 *
3114 * @remark We optimize this operation by using an inline function for
3115 * everything which isn't a surrogate pair or an endian indicator.
3116 */
3117DECLINLINE(RTUNICP) RTUtf16GetCp(PCRTUTF16 pwsz)
3118{
3119 const RTUTF16 wc = *pwsz;
3120 if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
3121 return wc;
3122 return RTUtf16GetCpInternal(pwsz);
3123}
3124
3125/**
3126 * Get the unicode code point at the given string position.
3127 *
3128 * @returns iprt status code.
3129 * @param ppwsz Pointer to the string pointer. This will be updated to
3130 * point to the char following the current code point.
3131 * @param pCp Where to store the code point.
3132 * RTUNICP_INVALID is stored here on failure.
3133 *
3134 * @remark We optimize this operation by using an inline function for
3135 * everything which isn't a surrogate pair or and endian indicator.
3136 */
3137DECLINLINE(int) RTUtf16GetCpEx(PCRTUTF16 *ppwsz, PRTUNICP pCp)
3138{
3139 const RTUTF16 wc = **ppwsz;
3140 if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
3141 {
3142 (*ppwsz)++;
3143 *pCp = wc;
3144 return VINF_SUCCESS;
3145 }
3146 return RTUtf16GetCpExInternal(ppwsz, pCp);
3147}
3148
3149/**
3150 * Put the unicode code point at the given string position
3151 * and return the pointer to the char following it.
3152 *
3153 * This function will not consider anything at or following the
3154 * buffer area pointed to by pwsz. It is therefore not suitable for
3155 * inserting code points into a string, only appending/overwriting.
3156 *
3157 * @returns pointer to the char following the written code point.
3158 * @param pwsz The string.
3159 * @param CodePoint The code point to write.
3160 * This should not be RTUNICP_INVALID or any other
3161 * character out of the UTF-16 range.
3162 *
3163 * @remark We optimize this operation by using an inline function for
3164 * everything which isn't a surrogate pair or and endian indicator.
3165 */
3166DECLINLINE(PRTUTF16) RTUtf16PutCp(PRTUTF16 pwsz, RTUNICP CodePoint)
3167{
3168 if (CodePoint < 0xd800 || (CodePoint > 0xd800 && CodePoint < 0xfffe))
3169 {
3170 *pwsz++ = (RTUTF16)CodePoint;
3171 return pwsz;
3172 }
3173 return RTUtf16PutCpInternal(pwsz, CodePoint);
3174}
3175
3176/**
3177 * Skips ahead, past the current code point.
3178 *
3179 * @returns Pointer to the char after the current code point.
3180 * @param pwsz Pointer to the current code point.
3181 * @remark This will not move the next valid code point, only past the current one.
3182 */
3183DECLINLINE(PRTUTF16) RTUtf16NextCp(PCRTUTF16 pwsz)
3184{
3185 RTUNICP Cp;
3186 RTUtf16GetCpEx(&pwsz, &Cp);
3187 return (PRTUTF16)pwsz;
3188}
3189
3190/**
3191 * Skips backwards, to the previous code point.
3192 *
3193 * @returns Pointer to the char after the current code point.
3194 * @param pwszStart Pointer to the start of the string.
3195 * @param pwsz Pointer to the current code point.
3196 */
3197RTDECL(PRTUTF16) RTUtf16PrevCp(PCRTUTF16 pwszStart, PCRTUTF16 pwsz);
3198
3199
3200/**
3201 * Checks if the UTF-16 char is the high surrogate char (i.e.
3202 * the 1st char in the pair).
3203 *
3204 * @returns true if it is.
3205 * @returns false if it isn't.
3206 * @param wc The character to investigate.
3207 */
3208DECLINLINE(bool) RTUtf16IsHighSurrogate(RTUTF16 wc)
3209{
3210 return wc >= 0xd800 && wc <= 0xdbff;
3211}
3212
3213/**
3214 * Checks if the UTF-16 char is the low surrogate char (i.e.
3215 * the 2nd char in the pair).
3216 *
3217 * @returns true if it is.
3218 * @returns false if it isn't.
3219 * @param wc The character to investigate.
3220 */
3221DECLINLINE(bool) RTUtf16IsLowSurrogate(RTUTF16 wc)
3222{
3223 return wc >= 0xdc00 && wc <= 0xdfff;
3224}
3225
3226
3227/**
3228 * Checks if the two UTF-16 chars form a valid surrogate pair.
3229 *
3230 * @returns true if they do.
3231 * @returns false if they doesn't.
3232 * @param wcHigh The high (1st) character.
3233 * @param wcLow The low (2nd) character.
3234 */
3235DECLINLINE(bool) RTUtf16IsSurrogatePair(RTUTF16 wcHigh, RTUTF16 wcLow)
3236{
3237 return RTUtf16IsHighSurrogate(wcHigh)
3238 && RTUtf16IsLowSurrogate(wcLow);
3239}
3240
3241/** @} */
3242
3243
3244/** @defgroup rt_str_latin1 Latin-1 (ISO-8859-1) String Manipulation
3245 * @ingroup grp_rt_str
3246 * @{
3247 */
3248
3249/**
3250 * Calculates the length of the Latin-1 (ISO-8859-1) string in RTUTF16 items.
3251 *
3252 * @returns Number of RTUTF16 items.
3253 * @param psz The Latin-1 string.
3254 */
3255RTDECL(size_t) RTLatin1CalcUtf16Len(const char *psz);
3256
3257/**
3258 * Calculates the length of the Latin-1 (ISO-8859-1) string in RTUTF16 items.
3259 *
3260 * @returns iprt status code.
3261 * @param psz The Latin-1 string.
3262 * @param cch The max string length. Use RTSTR_MAX to process the
3263 * entire string.
3264 * @param pcwc Where to store the string length. Optional.
3265 * This is undefined on failure.
3266 */
3267RTDECL(int) RTLatin1CalcUtf16LenEx(const char *psz, size_t cch, size_t *pcwc);
3268
3269/**
3270 * Translate a Latin-1 (ISO-8859-1) string into a UTF-16 allocating the result
3271 * buffer (default tag).
3272 *
3273 * @returns iprt status code.
3274 * @param pszString The Latin-1 string to convert.
3275 * @param ppwszString Receives pointer to the allocated UTF-16 string. The
3276 * returned string must be freed using RTUtf16Free().
3277 */
3278#define RTLatin1ToUtf16(pszString, ppwszString) RTLatin1ToUtf16Tag((pszString), (ppwszString), RTSTR_TAG)
3279
3280/**
3281 * Translate a Latin-1 (ISO-8859-1) string into a UTF-16 allocating the result
3282 * buffer (custom tag).
3283 *
3284 * @returns iprt status code.
3285 * @param pszString The Latin-1 string to convert.
3286 * @param ppwszString Receives pointer to the allocated UTF-16 string. The
3287 * returned string must be freed using RTUtf16Free().
3288 * @param pszTag Allocation tag used for statistics and such.
3289 */
3290RTDECL(int) RTLatin1ToUtf16Tag(const char *pszString, PRTUTF16 *ppwszString, const char *pszTag);
3291
3292/**
3293 * Translates pszString from Latin-1 (ISO-8859-1) to UTF-16, allocating the
3294 * result buffer if requested (default tag).
3295 *
3296 * @returns iprt status code.
3297 * @param pszString The Latin-1 string to convert.
3298 * @param cchString The maximum size in chars (the type) to convert.
3299 * The conversion stops when it reaches cchString or
3300 * the string terminator ('\\0').
3301 * Use RTSTR_MAX to translate the entire string.
3302 * @param ppwsz If cwc is non-zero, this must either be pointing
3303 * to pointer to a buffer of the specified size, or
3304 * pointer to a NULL pointer.
3305 * If *ppwsz is NULL or cwc is zero a buffer of at
3306 * least cwc items will be allocated to hold the
3307 * translated string. If a buffer was requested it
3308 * must be freed using RTUtf16Free().
3309 * @param cwc The buffer size in RTUTF16s. This includes the
3310 * terminator.
3311 * @param pcwc Where to store the length of the translated string,
3312 * excluding the terminator. (Optional)
3313 *
3314 * This may be set under some error conditions,
3315 * however, only for VERR_BUFFER_OVERFLOW and
3316 * VERR_NO_STR_MEMORY will it contain a valid string
3317 * length that can be used to resize the buffer.
3318 */
3319#define RTLatin1ToUtf16Ex(pszString, cchString, ppwsz, cwc, pcwc) \
3320 RTLatin1ToUtf16ExTag((pszString), (cchString), (ppwsz), (cwc), (pcwc), RTSTR_TAG)
3321
3322/**
3323 * Translates pszString from Latin-1 (ISO-8859-1) to UTF-16, allocating the
3324 * result buffer if requested.
3325 *
3326 * @returns iprt status code.
3327 * @param pszString The Latin-1 string to convert.
3328 * @param cchString The maximum size in chars (the type) to convert.
3329 * The conversion stops when it reaches cchString or
3330 * the string terminator ('\\0').
3331 * Use RTSTR_MAX to translate the entire string.
3332 * @param ppwsz If cwc is non-zero, this must either be pointing
3333 * to pointer to a buffer of the specified size, or
3334 * pointer to a NULL pointer.
3335 * If *ppwsz is NULL or cwc is zero a buffer of at
3336 * least cwc items will be allocated to hold the
3337 * translated string. If a buffer was requested it
3338 * must be freed using RTUtf16Free().
3339 * @param cwc The buffer size in RTUTF16s. This includes the
3340 * terminator.
3341 * @param pcwc Where to store the length of the translated string,
3342 * excluding the terminator. (Optional)
3343 *
3344 * This may be set under some error conditions,
3345 * however, only for VERR_BUFFER_OVERFLOW and
3346 * VERR_NO_STR_MEMORY will it contain a valid string
3347 * length that can be used to resize the buffer.
3348 * @param pszTag Allocation tag used for statistics and such.
3349 */
3350RTDECL(int) RTLatin1ToUtf16ExTag(const char *pszString, size_t cchString,
3351 PRTUTF16 *ppwsz, size_t cwc, size_t *pcwc, const char *pszTag);
3352
3353/** @} */
3354
3355
3356RT_C_DECLS_END
3357
3358/** @} */
3359
3360#endif
3361
Note: See TracBrowser for help on using the repository browser.

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