VirtualBox

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

Last change on this file since 26558 was 26482, checked in by vboxsync, 15 years ago

IPRT: Added RTStrATruncate.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 86.9 KB
Line 
1/** @file
2 * IPRT - String Manipluation.
3 */
4
5/*
6 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
26 * Clara, CA 95054 USA or visit http://www.sun.com if you need
27 * additional information or have any questions.
28 */
29
30#ifndef ___iprt_string_h
31#define ___iprt_string_h
32
33#include <iprt/cdefs.h>
34#include <iprt/types.h>
35#include <iprt/stdarg.h>
36#include <iprt/err.h> /* for VINF_SUCCESS */
37#if defined(RT_OS_LINUX) && defined(__KERNEL__)
38# include <linux/string.h>
39#elif defined(RT_OS_FREEBSD) && defined(_KERNEL)
40# include <sys/libkern.h>
41 /*
42 * No memmove on versions < 7.2
43 * Defining a macro using bcopy here
44 */
45# define memmove(dst, src, size) bcopy(src, dst, size)
46#elif defined(RT_OS_SOLARIS) && defined(_KERNEL)
47 /*
48 * Same case as with FreeBSD kernel:
49 * The string.h stuff clashes with sys/system.h
50 * ffs = find first set bit.
51 */
52# define ffs ffs_string_h
53# include <string.h>
54# undef ffs
55# undef strpbrk
56#else
57# include <string.h>
58#endif
59
60/*
61 * Supply prototypes for standard string functions provided by
62 * IPRT instead of the operating environment.
63 */
64#if (defined(RT_OS_DARWIN) && defined(KERNEL)) \
65 || (defined(RT_OS_FREEBSD) && defined(_KERNEL))
66RT_C_DECLS_BEGIN
67void *memchr(const void *pv, int ch, size_t cb);
68char *strpbrk(const char *pszStr, const char *pszChars);
69RT_C_DECLS_END
70#endif
71
72/**
73 * Byte zero the specified object.
74 *
75 * This will use sizeof(Obj) to figure the size and will call memset, bzero
76 * or some compiler intrinsic to perform the actual zeroing.
77 *
78 * @param Obj The object to zero. Make sure to dereference pointers.
79 *
80 * @remarks Because the macro may use memset it has been placed in string.h
81 * instead of cdefs.h to avoid build issues because someone forgot
82 * to include this header.
83 *
84 * @ingroup grp_rt_cdefs
85 */
86#define RT_ZERO(Obj) RT_BZERO(&(Obj), sizeof(Obj))
87
88/**
89 * Byte zero the specified memory area.
90 *
91 * This will call memset, bzero or some compiler intrinsic to clear the
92 * specified bytes of memory.
93 *
94 * @param pv Pointer to the memory.
95 * @param cb The number of bytes to clear. Please, don't pass 0.
96 *
97 * @remarks Because the macro may use memset it has been placed in string.h
98 * instead of cdefs.h to avoid build issues because someone forgot
99 * to include this header.
100 *
101 * @ingroup grp_rt_cdefs
102 */
103#define RT_BZERO(pv, cb) do { memset((pv), 0, cb); } while (0)
104
105
106/** @defgroup grp_rt_str RTStr - String Manipulation
107 * Mostly UTF-8 related helpers where the standard string functions won't do.
108 * @ingroup grp_rt
109 * @{
110 */
111
112RT_C_DECLS_BEGIN
113
114
115/**
116 * The maximum string length.
117 */
118#define RTSTR_MAX (~(size_t)0)
119
120
121#ifdef IN_RING3
122
123/**
124 * Allocates tmp buffer, translates pszString from UTF8 to current codepage.
125 *
126 * @returns iprt status code.
127 * @param ppszString Receives pointer of allocated native CP string.
128 * The returned pointer must be freed using RTStrFree().
129 * @param pszString UTF-8 string to convert.
130 */
131RTR3DECL(int) RTStrUtf8ToCurrentCP(char **ppszString, const char *pszString);
132
133/**
134 * Allocates tmp buffer, translates pszString from current codepage to UTF-8.
135 *
136 * @returns iprt status code.
137 * @param ppszString Receives pointer of allocated UTF-8 string.
138 * The returned pointer must be freed using RTStrFree().
139 * @param pszString Native string to convert.
140 */
141RTR3DECL(int) RTStrCurrentCPToUtf8(char **ppszString, const char *pszString);
142
143#endif
144
145/**
146 * Free string allocated by any of the non-UCS-2 string functions.
147 *
148 * @returns iprt status code.
149 * @param pszString Pointer to buffer with string to free.
150 * NULL is accepted.
151 */
152RTDECL(void) RTStrFree(char *pszString);
153
154/**
155 * Allocates a new copy of the given UTF-8 string.
156 *
157 * @returns Pointer to the allocated UTF-8 string.
158 * @param pszString UTF-8 string to duplicate.
159 */
160RTDECL(char *) RTStrDup(const char *pszString);
161
162/**
163 * Allocates a new copy of the given UTF-8 string.
164 *
165 * @returns iprt status code.
166 * @param ppszString Receives pointer of the allocated UTF-8 string.
167 * The returned pointer must be freed using RTStrFree().
168 * @param pszString UTF-8 string to duplicate.
169 */
170RTDECL(int) RTStrDupEx(char **ppszString, const char *pszString);
171
172/**
173 * Allocates a new copy of the given UTF-8 substring.
174 *
175 * @returns Pointer to the allocated UTF-8 substring.
176 * @param pszString UTF-8 string to duplicate.
177 * @param cchMax The max number of chars to duplicate, not counting
178 * the terminator.
179 */
180RTDECL(char *) RTStrDupN(const char *pszString, size_t cchMax);
181
182/**
183 * Appends a string onto an existing IPRT allocated string.
184 *
185 * @retval VINF_SUCCESS
186 * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
187 * remains unchanged.
188 *
189 * @param ppsz Pointer to the string pointer. The string
190 * pointer must either be NULL or point to a string
191 * returned by an IPRT string API. (In/Out)
192 * @param pszAppend The string to append. NULL and empty strings
193 * are quietly ignored.
194 */
195RTDECL(int) RTStrAAppend(char **ppsz, const char *pszAppend);
196
197/**
198 * Appends N bytes from a strings onto an existing IPRT allocated string.
199 *
200 * @retval VINF_SUCCESS
201 * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
202 * remains unchanged.
203 *
204 * @param ppsz Pointer to the string pointer. The string
205 * pointer must either be NULL or point to a string
206 * returned by an IPRT string API. (In/Out)
207 * @param pszAppend The string to append. Can be NULL if cchAppend
208 * is NULL.
209 * @param cchAppend The number of chars (not code points) to append
210 * from pszAppend. Must not be more than
211 * @a pszAppend contains, except for the special
212 * value RTSTR_MAX that can be used to indicate all
213 * of @a pszAppend without having to strlen it.
214 */
215RTDECL(int) RTStrAAppendN(char **ppsz, const char *pszAppend, size_t cchAppend);
216
217/**
218 * Appends one or more strings onto an existing IPRT allocated string.
219 *
220 * This is a very flexible and efficient alternative to using RTStrAPrintf to
221 * combine several strings together.
222 *
223 * @retval VINF_SUCCESS
224 * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
225 * remains unchanged.
226 *
227 * @param ppsz Pointer to the string pointer. The string
228 * pointer must either be NULL or point to a string
229 * returned by an IPRT string API. (In/Out)
230 * @param cPairs The number of string / length pairs in the
231 * @a va.
232 * @param va List of string (const char *) and length
233 * (size_t) pairs. The strings will be appended to
234 * the string in the first argument.
235 */
236RTDECL(int) RTStrAAppendExNV(char **ppsz, size_t cPairs, va_list va);
237
238/**
239 * Appends one or more strings onto an existing IPRT allocated string.
240 *
241 * This is a very flexible and efficient alternative to using RTStrAPrintf to
242 * combine several strings together.
243 *
244 * @retval VINF_SUCCESS
245 * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
246 * remains unchanged.
247 *
248 * @param ppsz Pointer to the string pointer. The string
249 * pointer must either be NULL or point to a string
250 * returned by an IPRT string API. (In/Out)
251 * @param cPairs The number of string / length pairs in the
252 * ellipsis.
253 * @param ... List of string (const char *) and length
254 * (size_t) pairs. The strings will be appended to
255 * the string in the first argument.
256 */
257RTDECL(int) RTStrAAppendExN(char **ppsz, size_t cPairs, ...);
258
259/**
260 * Truncates an IPRT allocated string.
261 *
262 * @retval VINF_SUCCESS.
263 * @retval VERR_OUT_OF_RANGE if cchNew is too long. Nothing is done.
264 *
265 * @param ppsz Pointer to the string pointer. The string
266 * pointer can be NULL if @a cchNew is 0, no change
267 * is made then. If we actually reallocate the
268 * string, the string pointer might be changed by
269 * this call. (In/Out)
270 * @param cchNew The new string length (excluding the
271 * terminator). The string must be at least this
272 * long or we'll return VERR_OUT_OF_RANGE and
273 * assert on you.
274 */
275RTDECL(int) RTStrATruncate(char **ppsz, size_t cchNew);
276
277/**
278 * Allocates memory for string storage.
279 *
280 * You should normally not use this function, except if there is some very
281 * custom string handling you need doing that isn't covered by any of the other
282 * APIs.
283 *
284 * @returns Pointer to the allocated string. The first byte is always set
285 * to the string terminator char, the contents of the remainder of the
286 * memory is undefined. The string must be freed by calling RTStrFree.
287 *
288 * NULL is returned if the allocation failed. Please translate this to
289 * VERR_NO_STR_MEMORY and not VERR_NO_MEMORY. Also consider
290 * RTStrAllocEx if an IPRT status code is required.
291 *
292 * @param cb How many bytes to allocate. If this is zero, we
293 * will allocate a terminator byte anyway.
294 */
295RTDECL(char *) RTStrAlloc(size_t cb);
296
297/**
298 * Allocates memory for string storage, with status code.
299 *
300 * You should normally not use this function, except if there is some very
301 * custom string handling you need doing that isn't covered by any of the other
302 * APIs.
303 *
304 * @retval VINF_SUCCESS
305 * @retval VERR_NO_STR_MEMORY
306 *
307 * @param ppsz Where to return the allocated string. This will
308 * be set to NULL on failure. On success, the
309 * returned memory will always start with a
310 * terminator char so that it is considered a valid
311 * C string, the contents of rest of the memory is
312 * undefined.
313 * @param cb How many bytes to allocate. If this is zero, we
314 * will allocate a terminator byte anyway.
315 */
316RTDECL(int) RTStrAllocEx(char **ppsz, size_t cb);
317
318/**
319 * Reallocates the specifed string.
320 *
321 * You should normally not have use this function, except perhaps to truncate a
322 * really long string you've got from some IPRT string API, but then you should
323 * use RTStrATruncate.
324 *
325 * @returns VINF_SUCCESS.
326 * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
327 * remains unchanged.
328 *
329 * @param ppsz Pointer to the string variable containing the
330 * input and output string.
331 *
332 * When not freeing the string, the result will
333 * always have the last byte set to the terminator
334 * character so that when used for string
335 * truncation the result will be a valid C string
336 * (your job to keep it a valid UTF-8 string).
337 *
338 * When the input string is NULL and we're supposed
339 * to reallocate, the returned string will also
340 * have the first byte set to the terminator char
341 * so it will be a valid C string.
342 *
343 * @param cbNew When @a cbNew is zero, we'll behave like
344 * RTStrFree and @a *ppsz will be set to NULL.
345 *
346 * When not zero, this will be the new size of the
347 * memory backing the string, i.e. it includes the
348 * terminator char.
349 */
350RTDECL(int) RTStrRealloc(char **ppsz, size_t cbNew);
351
352/**
353 * Validates the UTF-8 encoding of the string.
354 *
355 * @returns iprt status code.
356 * @param psz The string.
357 */
358RTDECL(int) RTStrValidateEncoding(const char *psz);
359
360/** @name Flags for RTStrValidateEncodingEx
361 */
362/** Check that the string is zero terminated within the given size.
363 * VERR_BUFFER_OVERFLOW will be returned if the check fails. */
364#define RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED RT_BIT_32(0)
365/** @} */
366
367/**
368 * Validates the UTF-8 encoding of the string.
369 *
370 * @returns iprt status code.
371 * @param psz The string.
372 * @param cch The max string length. Use RTSTR_MAX to process the entire string.
373 * @param fFlags Reserved for future. Pass 0.
374 */
375RTDECL(int) RTStrValidateEncodingEx(const char *psz, size_t cch, uint32_t fFlags);
376
377/**
378 * Checks if the UTF-8 encoding is valid.
379 *
380 * @returns true / false.
381 * @param psz The string.
382 */
383RTDECL(bool) RTStrIsValidEncoding(const char *psz);
384
385/**
386 * Gets the number of code points the string is made up of, excluding
387 * the terminator.
388 *
389 *
390 * @returns Number of code points (RTUNICP).
391 * @returns 0 if the string was incorrectly encoded.
392 * @param psz The string.
393 */
394RTDECL(size_t) RTStrUniLen(const char *psz);
395
396/**
397 * Gets the number of code points the string is made up of, excluding
398 * the terminator.
399 *
400 * This function will validate the string, and incorrectly encoded UTF-8
401 * strings will be rejected.
402 *
403 * @returns iprt status code.
404 * @param psz The string.
405 * @param cch The max string length. Use RTSTR_MAX to process the entire string.
406 * @param pcuc Where to store the code point count.
407 * This is undefined on failure.
408 */
409RTDECL(int) RTStrUniLenEx(const char *psz, size_t cch, size_t *pcuc);
410
411/**
412 * Translate a UTF-8 string into an unicode string (i.e. RTUNICPs), allocating the string buffer.
413 *
414 * @returns iprt status code.
415 * @param pszString UTF-8 string to convert.
416 * @param ppUniString Receives pointer to the allocated unicode string.
417 * The returned string must be freed using RTUniFree().
418 */
419RTDECL(int) RTStrToUni(const char *pszString, PRTUNICP *ppUniString);
420
421/**
422 * Translates pszString from UTF-8 to an array of code points, allocating the result
423 * array if requested.
424 *
425 * @returns iprt status code.
426 * @param pszString UTF-8 string to convert.
427 * @param cchString The maximum size in chars (the type) to convert. The conversion stop
428 * when it reaches cchString or the string terminator ('\\0').
429 * Use RTSTR_MAX to translate the entire string.
430 * @param ppaCps If cCps is non-zero, this must either be pointing to pointer to
431 * a buffer of the specified size, or pointer to a NULL pointer.
432 * If *ppusz is NULL or cCps is zero a buffer of at least cCps items
433 * will be allocated to hold the translated string.
434 * If a buffer was requested it must be freed using RTUtf16Free().
435 * @param cCps The number of code points in the unicode string. This includes the terminator.
436 * @param pcCps Where to store the length of the translated string,
437 * excluding the terminator. (Optional)
438 *
439 * This may be set under some error conditions,
440 * however, only for VERR_BUFFER_OVERFLOW and
441 * VERR_NO_STR_MEMORY will it contain a valid string
442 * length that can be used to resize the buffer.
443 */
444RTDECL(int) RTStrToUniEx(const char *pszString, size_t cchString, PRTUNICP *ppaCps, size_t cCps, size_t *pcCps);
445
446/**
447 * Calculates the length of the string in RTUTF16 items.
448 *
449 * This function will validate the string, and incorrectly encoded UTF-8
450 * strings will be rejected. The primary purpose of this function is to
451 * help allocate buffers for RTStrToUtf16Ex of the correct size. For most
452 * other purposes RTStrCalcUtf16LenEx() should be used.
453 *
454 * @returns Number of RTUTF16 items.
455 * @returns 0 if the string was incorrectly encoded.
456 * @param psz The string.
457 */
458RTDECL(size_t) RTStrCalcUtf16Len(const char *psz);
459
460/**
461 * Calculates the length of the string in RTUTF16 items.
462 *
463 * This function will validate the string, and incorrectly encoded UTF-8
464 * strings will be rejected.
465 *
466 * @returns iprt status code.
467 * @param psz The string.
468 * @param cch The max string length. Use RTSTR_MAX to process the entire string.
469 * @param pcwc Where to store the string length. Optional.
470 * This is undefined on failure.
471 */
472RTDECL(int) RTStrCalcUtf16LenEx(const char *psz, size_t cch, size_t *pcwc);
473
474/**
475 * Translate a UTF-8 string into a UTF-16 allocating the result buffer.
476 *
477 * @returns iprt status code.
478 * @param pszString UTF-8 string to convert.
479 * @param ppwszString Receives pointer to the allocated UTF-16 string.
480 * The returned string must be freed using RTUtf16Free().
481 */
482RTDECL(int) RTStrToUtf16(const char *pszString, PRTUTF16 *ppwszString);
483
484/**
485 * Translates pszString from UTF-8 to UTF-16, allocating the result buffer if requested.
486 *
487 * @returns iprt status code.
488 * @param pszString UTF-8 string to convert.
489 * @param cchString The maximum size in chars (the type) to convert. The conversion stop
490 * when it reaches cchString or the string terminator ('\\0').
491 * Use RTSTR_MAX to translate the entire string.
492 * @param ppwsz If cwc is non-zero, this must either be pointing to pointer to
493 * a buffer of the specified size, or pointer to a NULL pointer.
494 * If *ppwsz is NULL or cwc is zero a buffer of at least cwc items
495 * will be allocated to hold the translated string.
496 * If a buffer was requested it must be freed using RTUtf16Free().
497 * @param cwc The buffer size in RTUTF16s. This includes the terminator.
498 * @param pcwc Where to store the length of the translated string,
499 * excluding the terminator. (Optional)
500 *
501 * This may be set under some error conditions,
502 * however, only for VERR_BUFFER_OVERFLOW and
503 * VERR_NO_STR_MEMORY will it contain a valid string
504 * length that can be used to resize the buffer.
505 */
506RTDECL(int) RTStrToUtf16Ex(const char *pszString, size_t cchString, PRTUTF16 *ppwsz, size_t cwc, size_t *pcwc);
507
508
509/**
510 * Get the unicode code point at the given string position.
511 *
512 * @returns unicode code point.
513 * @returns RTUNICP_INVALID if the encoding is invalid.
514 * @param psz The string.
515 */
516RTDECL(RTUNICP) RTStrGetCpInternal(const char *psz);
517
518/**
519 * Get the unicode code point at the given string position.
520 *
521 * @returns iprt status code
522 * @returns VERR_INVALID_UTF8_ENCODING if the encoding is invalid.
523 * @param ppsz The string.
524 * @param pCp Where to store the unicode code point.
525 * Stores RTUNICP_INVALID if the encoding is invalid.
526 */
527RTDECL(int) RTStrGetCpExInternal(const char **ppsz, PRTUNICP pCp);
528
529/**
530 * Get the unicode code point at the given string position for a string of a
531 * given length.
532 *
533 * @returns iprt status code
534 * @retval VERR_INVALID_UTF8_ENCODING if the encoding is invalid.
535 * @retval VERR_END_OF_STRING if *pcch is 0. *pCp is set to RTUNICP_INVALID.
536 *
537 * @param ppsz The string.
538 * @param pcch Pointer to the length of the string. This will be
539 * decremented by the size of the code point.
540 * @param pCp Where to store the unicode code point.
541 * Stores RTUNICP_INVALID if the encoding is invalid.
542 */
543RTDECL(int) RTStrGetCpNExInternal(const char **ppsz, size_t *pcch, PRTUNICP pCp);
544
545/**
546 * Put the unicode code point at the given string position
547 * and return the pointer to the char following it.
548 *
549 * This function will not consider anything at or following the
550 * buffer area pointed to by psz. It is therefore not suitable for
551 * inserting code points into a string, only appending/overwriting.
552 *
553 * @returns pointer to the char following the written code point.
554 * @param psz The string.
555 * @param CodePoint The code point to write.
556 * This should not be RTUNICP_INVALID or any other
557 * character out of the UTF-8 range.
558 *
559 * @remark This is a worker function for RTStrPutCp().
560 *
561 */
562RTDECL(char *) RTStrPutCpInternal(char *psz, RTUNICP CodePoint);
563
564/**
565 * Get the unicode code point at the given string position.
566 *
567 * @returns unicode code point.
568 * @returns RTUNICP_INVALID if the encoding is invalid.
569 * @param psz The string.
570 *
571 * @remark We optimize this operation by using an inline function for
572 * the most frequent and simplest sequence, the rest is
573 * handled by RTStrGetCpInternal().
574 */
575DECLINLINE(RTUNICP) RTStrGetCp(const char *psz)
576{
577 const unsigned char uch = *(const unsigned char *)psz;
578 if (!(uch & RT_BIT(7)))
579 return uch;
580 return RTStrGetCpInternal(psz);
581}
582
583/**
584 * Get the unicode code point at the given string position.
585 *
586 * @returns iprt status code.
587 * @param ppsz Pointer to the string pointer. This will be updated to
588 * point to the char following the current code point.
589 * @param pCp Where to store the code point.
590 * RTUNICP_INVALID is stored here on failure.
591 *
592 * @remark We optimize this operation by using an inline function for
593 * the most frequent and simplest sequence, the rest is
594 * handled by RTStrGetCpExInternal().
595 */
596DECLINLINE(int) RTStrGetCpEx(const char **ppsz, PRTUNICP pCp)
597{
598 const unsigned char uch = **(const unsigned char **)ppsz;
599 if (!(uch & RT_BIT(7)))
600 {
601 (*ppsz)++;
602 *pCp = uch;
603 return VINF_SUCCESS;
604 }
605 return RTStrGetCpExInternal(ppsz, pCp);
606}
607
608/**
609 * Get the unicode code point at the given string position for a string of a
610 * given maximum length.
611 *
612 * @returns iprt status code.
613 * @retval VERR_INVALID_UTF8_ENCODING if the encoding is invalid.
614 * @retval VERR_END_OF_STRING if *pcch is 0. *pCp is set to RTUNICP_INVALID.
615 *
616 * @param ppsz Pointer to the string pointer. This will be updated to
617 * point to the char following the current code point.
618 * @param pcch Pointer to the maximum string length. This will be
619 * decremented by the size of the code point found.
620 * @param pCp Where to store the code point.
621 * RTUNICP_INVALID is stored here on failure.
622 *
623 * @remark We optimize this operation by using an inline function for
624 * the most frequent and simplest sequence, the rest is
625 * handled by RTStrGetCpNExInternal().
626 */
627DECLINLINE(int) RTStrGetCpNEx(const char **ppsz, size_t *pcch, PRTUNICP pCp)
628{
629 if (RT_LIKELY(*pcch != 0))
630 {
631 const unsigned char uch = **(const unsigned char **)ppsz;
632 if (!(uch & RT_BIT(7)))
633 {
634 (*ppsz)++;
635 (*pcch)--;
636 *pCp = uch;
637 return VINF_SUCCESS;
638 }
639 }
640 return RTStrGetCpNExInternal(ppsz, pcch, pCp);
641}
642
643/**
644 * Put the unicode code point at the given string position
645 * and return the pointer to the char following it.
646 *
647 * This function will not consider anything at or following the
648 * buffer area pointed to by psz. It is therefore not suitable for
649 * inserting code points into a string, only appending/overwriting.
650 *
651 * @returns pointer to the char following the written code point.
652 * @param psz The string.
653 * @param CodePoint The code point to write.
654 * This should not be RTUNICP_INVALID or any other
655 * character out of the UTF-8 range.
656 *
657 * @remark We optimize this operation by using an inline function for
658 * the most frequent and simplest sequence, the rest is
659 * handled by RTStrPutCpInternal().
660 */
661DECLINLINE(char *) RTStrPutCp(char *psz, RTUNICP CodePoint)
662{
663 if (CodePoint < 0x80)
664 {
665 *psz++ = (unsigned char)CodePoint;
666 return psz;
667 }
668 return RTStrPutCpInternal(psz, CodePoint);
669}
670
671/**
672 * Skips ahead, past the current code point.
673 *
674 * @returns Pointer to the char after the current code point.
675 * @param psz Pointer to the current code point.
676 * @remark This will not move the next valid code point, only past the current one.
677 */
678DECLINLINE(char *) RTStrNextCp(const char *psz)
679{
680 RTUNICP Cp;
681 RTStrGetCpEx(&psz, &Cp);
682 return (char *)psz;
683}
684
685/**
686 * Skips back to the previous code point.
687 *
688 * @returns Pointer to the char before the current code point.
689 * @returns pszStart on failure.
690 * @param pszStart Pointer to the start of the string.
691 * @param psz Pointer to the current code point.
692 */
693RTDECL(char *) RTStrPrevCp(const char *pszStart, const char *psz);
694
695
696
697#ifndef DECLARED_FNRTSTROUTPUT /* duplicated in iprt/log.h */
698#define DECLARED_FNRTSTROUTPUT
699/**
700 * Output callback.
701 *
702 * @returns number of bytes written.
703 * @param pvArg User argument.
704 * @param pachChars Pointer to an array of utf-8 characters.
705 * @param cbChars Number of bytes in the character array pointed to by pachChars.
706 */
707typedef DECLCALLBACK(size_t) FNRTSTROUTPUT(void *pvArg, const char *pachChars, size_t cbChars);
708/** Pointer to callback function. */
709typedef FNRTSTROUTPUT *PFNRTSTROUTPUT;
710#endif
711
712/** Format flag.
713 * These are used by RTStrFormat extensions and RTStrFormatNumber, mind
714 * that not all flags makes sense to both of the functions.
715 * @{ */
716#define RTSTR_F_CAPITAL 0x0001
717#define RTSTR_F_LEFT 0x0002
718#define RTSTR_F_ZEROPAD 0x0004
719#define RTSTR_F_SPECIAL 0x0008
720#define RTSTR_F_VALSIGNED 0x0010
721#define RTSTR_F_PLUS 0x0020
722#define RTSTR_F_BLANK 0x0040
723#define RTSTR_F_WIDTH 0x0080
724#define RTSTR_F_PRECISION 0x0100
725#define RTSTR_F_THOUSAND_SEP 0x0200
726
727#define RTSTR_F_BIT_MASK 0xf800
728#define RTSTR_F_8BIT 0x0800
729#define RTSTR_F_16BIT 0x1000
730#define RTSTR_F_32BIT 0x2000
731#define RTSTR_F_64BIT 0x4000
732#define RTSTR_F_128BIT 0x8000
733/** @} */
734
735/** @def RTSTR_GET_BIT_FLAG
736 * Gets the bit flag for the specified type.
737 */
738#define RTSTR_GET_BIT_FLAG(type) \
739 ( sizeof(type) == 32 ? RTSTR_F_32BIT \
740 : sizeof(type) == 64 ? RTSTR_F_64BIT \
741 : sizeof(type) == 16 ? RTSTR_F_16BIT \
742 : sizeof(type) == 8 ? RTSTR_F_8BIT \
743 : sizeof(type) == 128? RTSTR_F_128BIT \
744 : 0)
745
746
747/**
748 * Callback to format non-standard format specifiers.
749 *
750 * @returns The number of bytes formatted.
751 * @param pvArg Formatter argument.
752 * @param pfnOutput Pointer to output function.
753 * @param pvArgOutput Argument for the output function.
754 * @param ppszFormat Pointer to the format string pointer. Advance this till the char
755 * after the format specifier.
756 * @param pArgs Pointer to the argument list. Use this to fetch the arguments.
757 * @param cchWidth Format Width. -1 if not specified.
758 * @param cchPrecision Format Precision. -1 if not specified.
759 * @param fFlags Flags (RTSTR_NTFS_*).
760 * @param chArgSize The argument size specifier, 'l' or 'L'.
761 */
762typedef DECLCALLBACK(size_t) FNSTRFORMAT(void *pvArg, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
763 const char **ppszFormat, va_list *pArgs, int cchWidth,
764 int cchPrecision, unsigned fFlags, char chArgSize);
765/** Pointer to a FNSTRFORMAT() function. */
766typedef FNSTRFORMAT *PFNSTRFORMAT;
767
768
769/**
770 * Partial implementation of a printf like formatter.
771 * It doesn't do everything correct, and there is no floating point support.
772 * However, it supports custom formats by the means of a format callback.
773 *
774 * @returns number of bytes formatted.
775 * @param pfnOutput Output worker.
776 * Called in two ways. Normally with a string and its length.
777 * For termination, it's called with NULL for string, 0 for length.
778 * @param pvArgOutput Argument to the output worker.
779 * @param pfnFormat Custom format worker.
780 * @param pvArgFormat Argument to the format worker.
781 * @param pszFormat Format string pointer.
782 * @param InArgs Argument list.
783 */
784RTDECL(size_t) RTStrFormatV(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat, const char *pszFormat, va_list InArgs);
785
786/**
787 * Partial implementation of a printf like formatter.
788 * It doesn't do everything correct, and there is no floating point support.
789 * However, it supports custom formats by the means of a format callback.
790 *
791 * @returns number of bytes formatted.
792 * @param pfnOutput Output worker.
793 * Called in two ways. Normally with a string and its length.
794 * For termination, it's called with NULL for string, 0 for length.
795 * @param pvArgOutput Argument to the output worker.
796 * @param pfnFormat Custom format worker.
797 * @param pvArgFormat Argument to the format worker.
798 * @param pszFormat Format string.
799 * @param ... Argument list.
800 */
801RTDECL(size_t) RTStrFormat(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat, const char *pszFormat, ...);
802
803/**
804 * Formats an integer number according to the parameters.
805 *
806 * @returns Length of the formatted number.
807 * @param psz Pointer to output string buffer of sufficient size.
808 * @param u64Value Value to format.
809 * @param uiBase Number representation base.
810 * @param cchWidth Width.
811 * @param cchPrecision Precision.
812 * @param fFlags Flags (NTFS_*).
813 */
814RTDECL(int) RTStrFormatNumber(char *psz, uint64_t u64Value, unsigned int uiBase, signed int cchWidth, signed int cchPrecision, unsigned int fFlags);
815
816
817/**
818 * Callback for formatting a type.
819 *
820 * This is registered using the RTStrFormatTypeRegister function and will
821 * be called during string formatting to handle the specified %R[type].
822 * The argument for this format type is assumed to be a pointer and it's
823 * passed in the @a pvValue argument.
824 *
825 * @returns Length of the formatted output.
826 * @param pfnOutput Output worker.
827 * @param pvArgOutput Argument to the output worker.
828 * @param pszType The type name.
829 * @param pvValue The argument value.
830 * @param cchWidth Width.
831 * @param cchPrecision Precision.
832 * @param fFlags Flags (NTFS_*).
833 * @param pvUser The user argument.
834 */
835typedef DECLCALLBACK(size_t) FNRTSTRFORMATTYPE(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
836 const char *pszType, void const *pvValue,
837 int cchWidth, int cchPrecision, unsigned fFlags,
838 void *pvUser);
839/** Pointer to a FNRTSTRFORMATTYPE. */
840typedef FNRTSTRFORMATTYPE *PFNRTSTRFORMATTYPE;
841
842
843/**
844 * Register a format handler for a type.
845 *
846 * The format handler is used to handle '%R[type]' format types, where the argument
847 * in the vector is a pointer value (a bit restrictive, but keeps it simple).
848 *
849 * The caller must ensure that no other thread will be making use of any of
850 * the dynamic formatting type facilities simultaneously with this call.
851 *
852 * @returns IPRT status code.
853 * @retval VINF_SUCCESS on success.
854 * @retval VERR_ALREADY_EXISTS if the type has already been registered.
855 * @retval VERR_TOO_MANY_OPEN_FILES if all the type slots has been allocated already.
856 *
857 * @param pszType The type name.
858 * @param pfnHandler The handler address. See FNRTSTRFORMATTYPE for details.
859 * @param pvUser The user argument to pass to the handler. See RTStrFormatTypeSetUser
860 * for how to update this later.
861 */
862RTDECL(int) RTStrFormatTypeRegister(const char *pszType, PFNRTSTRFORMATTYPE pfnHandler, void *pvUser);
863
864/**
865 * Deregisters a format type.
866 *
867 * The caller must ensure that no other thread will be making use of any of
868 * the dynamic formatting type facilities simultaneously with this call.
869 *
870 * @returns IPRT status code.
871 * @retval VINF_SUCCESS on success.
872 * @retval VERR_FILE_NOT_FOUND if not found.
873 *
874 * @param pszType The type to deregister.
875 */
876RTDECL(int) RTStrFormatTypeDeregister(const char *pszType);
877
878/**
879 * Sets the user argument for a type.
880 *
881 * This can be used if a user argument needs relocating in GC.
882 *
883 * @returns IPRT status code.
884 * @retval VINF_SUCCESS on success.
885 * @retval VERR_FILE_NOT_FOUND if not found.
886 *
887 * @param pszType The type to update.
888 * @param pvUser The new user argument value.
889 */
890RTDECL(int) RTStrFormatTypeSetUser(const char *pszType, void *pvUser);
891
892
893/**
894 * String printf.
895 *
896 * @returns The length of the returned string (in pszBuffer).
897 * @param pszBuffer Output buffer.
898 * @param cchBuffer Size of the output buffer.
899 * @param pszFormat The format string.
900 * @param args The format argument.
901 */
902RTDECL(size_t) RTStrPrintfV(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args);
903
904/**
905 * String printf.
906 *
907 * @returns The length of the returned string (in pszBuffer).
908 * @param pszBuffer Output buffer.
909 * @param cchBuffer Size of the output buffer.
910 * @param pszFormat The format string.
911 * @param ... The format argument.
912 */
913RTDECL(size_t) RTStrPrintf(char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...);
914
915
916/**
917 * String printf with custom formatting.
918 *
919 * @returns The length of the returned string (in pszBuffer).
920 * @param pfnFormat Pointer to handler function for the custom formats.
921 * @param pvArg Argument to the pfnFormat function.
922 * @param pszBuffer Output buffer.
923 * @param cchBuffer Size of the output buffer.
924 * @param pszFormat The format string.
925 * @param args The format argument.
926 */
927RTDECL(size_t) RTStrPrintfExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args);
928
929/**
930 * String printf with custom formatting.
931 *
932 * @returns The length of the returned string (in pszBuffer).
933 * @param pfnFormat Pointer to handler function for the custom formats.
934 * @param pvArg Argument to the pfnFormat function.
935 * @param pszBuffer Output buffer.
936 * @param cchBuffer Size of the output buffer.
937 * @param pszFormat The format string.
938 * @param ... The format argument.
939 */
940RTDECL(size_t) RTStrPrintfEx(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...);
941
942
943/**
944 * Allocating string printf.
945 *
946 * @returns The length of the string in the returned *ppszBuffer.
947 * @returns -1 on failure.
948 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
949 * The buffer should be freed using RTStrFree().
950 * On failure *ppszBuffer will be set to NULL.
951 * @param pszFormat The format string.
952 * @param args The format argument.
953 */
954RTDECL(int) RTStrAPrintfV(char **ppszBuffer, const char *pszFormat, va_list args);
955
956/**
957 * Allocating string printf.
958 *
959 * @returns The length of the string in the returned *ppszBuffer.
960 * @returns -1 on failure.
961 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
962 * The buffer should be freed using RTStrFree().
963 * On failure *ppszBuffer will be set to NULL.
964 * @param pszFormat The format string.
965 * @param ... The format argument.
966 */
967RTDECL(int) RTStrAPrintf(char **ppszBuffer, const char *pszFormat, ...);
968
969/**
970 * Allocating string printf, version 2.
971 *
972 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
973 * memory.
974 * @param pszFormat The format string.
975 * @param args The format argument.
976 */
977RTDECL(char *) RTStrAPrintf2V(const char *pszFormat, va_list args);
978
979/**
980 * Allocating string printf, version2.
981 *
982 * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
983 * memory.
984 * @param pszFormat The format string.
985 * @param ... The format argument.
986 */
987RTDECL(char *) RTStrAPrintf2(const char *pszFormat, ...);
988
989/**
990 * Strips blankspaces from both ends of the string.
991 *
992 * @returns Pointer to first non-blank char in the string.
993 * @param psz The string to strip.
994 */
995RTDECL(char *) RTStrStrip(char *psz);
996
997/**
998 * Strips blankspaces from the start of the string.
999 *
1000 * @returns Pointer to first non-blank char in the string.
1001 * @param psz The string to strip.
1002 */
1003RTDECL(char *) RTStrStripL(const char *psz);
1004
1005/**
1006 * Strips blankspaces from the end of the string.
1007 *
1008 * @returns psz.
1009 * @param psz The string to strip.
1010 */
1011RTDECL(char *) RTStrStripR(char *psz);
1012
1013/**
1014 * Performs a case sensitive string compare between two UTF-8 strings.
1015 *
1016 * Encoding errors are ignored by the current implementation. So, the only
1017 * difference between this and the CRT strcmp function is the handling of
1018 * NULL arguments.
1019 *
1020 * @returns < 0 if the first string less than the second string.
1021 * @returns 0 if the first string identical to the second string.
1022 * @returns > 0 if the first string greater than the second string.
1023 * @param psz1 First UTF-8 string. Null is allowed.
1024 * @param psz2 Second UTF-8 string. Null is allowed.
1025 */
1026RTDECL(int) RTStrCmp(const char *psz1, const char *psz2);
1027
1028/**
1029 * Performs a case sensitive string compare between two UTF-8 strings, given
1030 * a maximum string length.
1031 *
1032 * Encoding errors are ignored by the current implementation. So, the only
1033 * difference between this and the CRT strncmp function is the handling of
1034 * NULL arguments.
1035 *
1036 * @returns < 0 if the first string less than the second string.
1037 * @returns 0 if the first string identical to the second string.
1038 * @returns > 0 if the first string greater than the second string.
1039 * @param psz1 First UTF-8 string. Null is allowed.
1040 * @param psz2 Second UTF-8 string. Null is allowed.
1041 * @param cchMax The maximum string length
1042 */
1043RTDECL(int) RTStrNCmp(const char *psz1, const char *psz2, size_t cchMax);
1044
1045/**
1046 * Performs a case insensitive string compare between two UTF-8 strings.
1047 *
1048 * This is a simplified compare, as only the simplified lower/upper case folding
1049 * specified by the unicode specs are used. It does not consider character pairs
1050 * as they are used in some languages, just simple upper & lower case compares.
1051 *
1052 * The result is the difference between the mismatching codepoints after they
1053 * both have been lower cased.
1054 *
1055 * If the string encoding is invalid the function will assert (strict builds)
1056 * and use RTStrCmp for the remainder of the string.
1057 *
1058 * @returns < 0 if the first string less than the second string.
1059 * @returns 0 if the first string identical to the second string.
1060 * @returns > 0 if the first string greater than the second string.
1061 * @param psz1 First UTF-8 string. Null is allowed.
1062 * @param psz2 Second UTF-8 string. Null is allowed.
1063 */
1064RTDECL(int) RTStrICmp(const char *psz1, const char *psz2);
1065
1066/**
1067 * Performs a case insensitive string compare between two UTF-8 strings, given a
1068 * maximum string length.
1069 *
1070 * This is a simplified compare, as only the simplified lower/upper case folding
1071 * specified by the unicode specs are used. It does not consider character pairs
1072 * as they are used in some languages, just simple upper & lower case compares.
1073 *
1074 * The result is the difference between the mismatching codepoints after they
1075 * both have been lower cased.
1076 *
1077 * If the string encoding is invalid the function will assert (strict builds)
1078 * and use RTStrCmp for the remainder of the string.
1079 *
1080 * @returns < 0 if the first string less than the second string.
1081 * @returns 0 if the first string identical to the second string.
1082 * @returns > 0 if the first string greater than the second string.
1083 * @param psz1 First UTF-8 string. Null is allowed.
1084 * @param psz2 Second UTF-8 string. Null is allowed.
1085 * @param cchMax Maximum string length
1086 */
1087RTDECL(int) RTStrNICmp(const char *psz1, const char *psz2, size_t cchMax);
1088
1089/**
1090 * Locates a case sensitive substring.
1091 *
1092 * If any of the two strings are NULL, then NULL is returned. If the needle is
1093 * an empty string, then the haystack is returned (i.e. matches anything).
1094 *
1095 * @returns Pointer to the first occurrence of the substring if found, NULL if
1096 * not.
1097 *
1098 * @param pszHaystack The string to search.
1099 * @param pszNeedle The substring to search for.
1100 *
1101 * @remarks The difference between this and strstr is the handling of NULL
1102 * pointers.
1103 */
1104RTDECL(char *) RTStrStr(const char *pszHaystack, const char *pszNeedle);
1105
1106/**
1107 * Locates a case insensitive substring.
1108 *
1109 * If any of the two strings are NULL, then NULL is returned. If the needle is
1110 * an empty string, then the haystack is returned (i.e. matches anything).
1111 *
1112 * @returns Pointer to the first occurrence of the substring if found, NULL if
1113 * not.
1114 *
1115 * @param pszHaystack The string to search.
1116 * @param pszNeedle The substring to search for.
1117 *
1118 */
1119RTDECL(char *) RTStrIStr(const char *pszHaystack, const char *pszNeedle);
1120
1121/**
1122 * Converts the string to lower case.
1123 *
1124 * @returns Pointer to the converted string.
1125 * @param psz The string to convert.
1126 */
1127RTDECL(char *) RTStrToLower(char *psz);
1128
1129/**
1130 * Converts the string to upper case.
1131 *
1132 * @returns Pointer to the converted string.
1133 * @param psz The string to convert.
1134 */
1135RTDECL(char *) RTStrToUpper(char *psz);
1136
1137/**
1138 * Find the length of a zero-terminated byte string, given
1139 * a max string length.
1140 *
1141 * See also RTStrNLenEx.
1142 *
1143 * @returns The string length or cbMax. The returned length does not include
1144 * the zero terminator if it was found.
1145 *
1146 * @param pszString The string.
1147 * @param cchMax The max string length.
1148 */
1149RTDECL(size_t) RTStrNLen(const char *pszString, size_t cchMax);
1150
1151/**
1152 * Find the length of a zero-terminated byte string, given
1153 * a max string length.
1154 *
1155 * See also RTStrNLen.
1156 *
1157 * @returns IPRT status code.
1158 * @retval VINF_SUCCESS if the string has a length less than cchMax.
1159 * @retval VERR_BUFFER_OVERFLOW if the end of the string wasn't found
1160 * before cchMax was reached.
1161 *
1162 * @param pszString The string.
1163 * @param cchMax The max string length.
1164 * @param pcch Where to store the string length excluding the
1165 * terminator. This is set to cchMax if the terminator
1166 * isn't found.
1167 */
1168RTDECL(int) RTStrNLenEx(const char *pszString, size_t cchMax, size_t *pcch);
1169
1170/**
1171 * Matches a simple string pattern.
1172 *
1173 * @returns true if the string matches the pattern, otherwise false.
1174 *
1175 * @param pszPattern The pattern. Special chars are '*' and '?', where the
1176 * asterisk matches zero or more characters and question
1177 * mark matches exactly one character.
1178 * @param pszString The string to match against the pattern.
1179 */
1180RTDECL(bool) RTStrSimplePatternMatch(const char *pszPattern, const char *pszString);
1181
1182/**
1183 * Matches a simple string pattern, neither which needs to be zero terminated.
1184 *
1185 * This is identical to RTStrSimplePatternMatch except that you can optionally
1186 * specify the length of both the pattern and the string. The function will
1187 * stop when it hits a string terminator or either of the lengths.
1188 *
1189 * @returns true if the string matches the pattern, otherwise false.
1190 *
1191 * @param pszPattern The pattern. Special chars are '*' and '?', where the
1192 * asterisk matches zero or more characters and question
1193 * mark matches exactly one character.
1194 * @param cchPattern The pattern length. Pass RTSTR_MAX if you don't know the
1195 * length and wish to stop at the string terminator.
1196 * @param pszString The string to match against the pattern.
1197 * @param cchString The string length. Pass RTSTR_MAX if you don't know the
1198 * length and wish to match up to the string terminator.
1199 */
1200RTDECL(bool) RTStrSimplePatternNMatch(const char *pszPattern, size_t cchPattern,
1201 const char *pszString, size_t cchString);
1202
1203/**
1204 * Matches multiple patterns against a string.
1205 *
1206 * The patterns are separated by the pipe character (|).
1207 *
1208 * @returns true if the string matches the pattern, otherwise false.
1209 *
1210 * @param pszPatterns The patterns.
1211 * @param cchPatterns The lengths of the patterns to use. Pass RTSTR_MAX to
1212 * stop at the terminator.
1213 * @param pszString The string to match against the pattern.
1214 * @param cchString The string length. Pass RTSTR_MAX stop stop at the
1215 * terminator.
1216 * @param poffPattern Offset into the patterns string of the patttern that
1217 * matched. If no match, this will be set to RTSTR_MAX.
1218 * This is optional, NULL is fine.
1219 */
1220RTDECL(bool) RTStrSimplePatternMultiMatch(const char *pszPatterns, size_t cchPatterns,
1221 const char *pszString, size_t cchString,
1222 size_t *poffPattern);
1223
1224/**
1225 * Compares two version strings RTStrICmp fashion.
1226 *
1227 * The version string is split up into sections at punctuation, spaces,
1228 * underscores, dashes and pluss signs. The sections are then split up into
1229 * numeric and string sub-sections. Finally, the sub-sections are compared
1230 * in a numeric or case insesntivie fashion depending on what they are.
1231 *
1232 * The following strings are considered to be equal: "1.0.0", "1.00.0", "1.0",
1233 * "1". These aren't: "1.0.0r993", "1.0", "1.0r993", "1.0_Beta3", "1.1"
1234 *
1235 * @returns < 0 if the first string less than the second string.
1236 * @returns 0 if the first string identical to the second string.
1237 * @returns > 0 if the first string greater than the second string.
1238 *
1239 * @param pszVer1 First version string to compare.
1240 * @param pszVer2 Second version string to compare first version with.
1241 */
1242RTDECL(int) RTStrVersionCompare(const char *pszVer1, const char *pszVer2);
1243
1244
1245/** @defgroup rt_str_conv String To/From Number Conversions
1246 * @ingroup grp_rt_str
1247 * @{ */
1248
1249/**
1250 * Converts a string representation of a number to a 64-bit unsigned number.
1251 *
1252 * @returns iprt status code.
1253 * Warnings are used to indicate conversion problems.
1254 * @retval VWRN_NUMBER_TOO_BIG
1255 * @retval VWRN_NEGATIVE_UNSIGNED
1256 * @retval VWRN_TRAILING_CHARS
1257 * @retval VWRN_TRAILING_SPACES
1258 * @retval VINF_SUCCESS
1259 * @retval VERR_NO_DIGITS
1260 *
1261 * @param pszValue Pointer to the string value.
1262 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1263 * @param uBase The base of the representation used.
1264 * If 0 the function will look for known prefixes before defaulting to 10.
1265 * @param pu64 Where to store the converted number. (optional)
1266 */
1267RTDECL(int) RTStrToUInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint64_t *pu64);
1268
1269/**
1270 * Converts a string representation of a number to a 64-bit unsigned number,
1271 * making sure the full string is converted.
1272 *
1273 * @returns iprt status code.
1274 * Warnings are used to indicate conversion problems.
1275 * @retval VWRN_NUMBER_TOO_BIG
1276 * @retval VWRN_NEGATIVE_UNSIGNED
1277 * @retval VINF_SUCCESS
1278 * @retval VERR_NO_DIGITS
1279 * @retval VERR_TRAILING_SPACES
1280 * @retval VERR_TRAILING_CHARS
1281 *
1282 * @param pszValue Pointer to the string value.
1283 * @param uBase The base of the representation used.
1284 * If 0 the function will look for known prefixes before defaulting to 10.
1285 * @param pu64 Where to store the converted number. (optional)
1286 */
1287RTDECL(int) RTStrToUInt64Full(const char *pszValue, unsigned uBase, uint64_t *pu64);
1288
1289/**
1290 * Converts a string representation of a number to a 64-bit unsigned number.
1291 * The base is guessed.
1292 *
1293 * @returns 64-bit unsigned number on success.
1294 * @returns 0 on failure.
1295 * @param pszValue Pointer to the string value.
1296 */
1297RTDECL(uint64_t) RTStrToUInt64(const char *pszValue);
1298
1299/**
1300 * Converts a string representation of a number to a 32-bit unsigned number.
1301 *
1302 * @returns iprt status code.
1303 * Warnings are used to indicate conversion problems.
1304 * @retval VWRN_NUMBER_TOO_BIG
1305 * @retval VWRN_NEGATIVE_UNSIGNED
1306 * @retval VWRN_TRAILING_CHARS
1307 * @retval VWRN_TRAILING_SPACES
1308 * @retval VINF_SUCCESS
1309 * @retval VERR_NO_DIGITS
1310 *
1311 * @param pszValue Pointer to the string value.
1312 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1313 * @param uBase The base of the representation used.
1314 * If 0 the function will look for known prefixes before defaulting to 10.
1315 * @param pu32 Where to store the converted number. (optional)
1316 */
1317RTDECL(int) RTStrToUInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint32_t *pu32);
1318
1319/**
1320 * Converts a string representation of a number to a 32-bit unsigned number,
1321 * making sure the full string is converted.
1322 *
1323 * @returns iprt status code.
1324 * Warnings are used to indicate conversion problems.
1325 * @retval VWRN_NUMBER_TOO_BIG
1326 * @retval VWRN_NEGATIVE_UNSIGNED
1327 * @retval VINF_SUCCESS
1328 * @retval VERR_NO_DIGITS
1329 * @retval VERR_TRAILING_SPACES
1330 * @retval VERR_TRAILING_CHARS
1331 *
1332 * @param pszValue Pointer to the string value.
1333 * @param uBase The base of the representation used.
1334 * If 0 the function will look for known prefixes before defaulting to 10.
1335 * @param pu32 Where to store the converted number. (optional)
1336 */
1337RTDECL(int) RTStrToUInt32Full(const char *pszValue, unsigned uBase, uint32_t *pu32);
1338
1339/**
1340 * Converts a string representation of a number to a 64-bit unsigned number.
1341 * The base is guessed.
1342 *
1343 * @returns 32-bit unsigned number on success.
1344 * @returns 0 on failure.
1345 * @param pszValue Pointer to the string value.
1346 */
1347RTDECL(uint32_t) RTStrToUInt32(const char *pszValue);
1348
1349/**
1350 * Converts a string representation of a number to a 16-bit unsigned number.
1351 *
1352 * @returns iprt status code.
1353 * Warnings are used to indicate conversion problems.
1354 * @retval VWRN_NUMBER_TOO_BIG
1355 * @retval VWRN_NEGATIVE_UNSIGNED
1356 * @retval VWRN_TRAILING_CHARS
1357 * @retval VWRN_TRAILING_SPACES
1358 * @retval VINF_SUCCESS
1359 * @retval VERR_NO_DIGITS
1360 *
1361 * @param pszValue Pointer to the string value.
1362 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1363 * @param uBase The base of the representation used.
1364 * If 0 the function will look for known prefixes before defaulting to 10.
1365 * @param pu16 Where to store the converted number. (optional)
1366 */
1367RTDECL(int) RTStrToUInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint16_t *pu16);
1368
1369/**
1370 * Converts a string representation of a number to a 16-bit unsigned number,
1371 * making sure the full string is converted.
1372 *
1373 * @returns iprt status code.
1374 * Warnings are used to indicate conversion problems.
1375 * @retval VWRN_NUMBER_TOO_BIG
1376 * @retval VWRN_NEGATIVE_UNSIGNED
1377 * @retval VINF_SUCCESS
1378 * @retval VERR_NO_DIGITS
1379 * @retval VERR_TRAILING_SPACES
1380 * @retval VERR_TRAILING_CHARS
1381 *
1382 * @param pszValue Pointer to the string value.
1383 * @param uBase The base of the representation used.
1384 * If 0 the function will look for known prefixes before defaulting to 10.
1385 * @param pu16 Where to store the converted number. (optional)
1386 */
1387RTDECL(int) RTStrToUInt16Full(const char *pszValue, unsigned uBase, uint16_t *pu16);
1388
1389/**
1390 * Converts a string representation of a number to a 16-bit unsigned number.
1391 * The base is guessed.
1392 *
1393 * @returns 16-bit unsigned number on success.
1394 * @returns 0 on failure.
1395 * @param pszValue Pointer to the string value.
1396 */
1397RTDECL(uint16_t) RTStrToUInt16(const char *pszValue);
1398
1399/**
1400 * Converts a string representation of a number to a 8-bit unsigned number.
1401 *
1402 * @returns iprt status code.
1403 * Warnings are used to indicate conversion problems.
1404 * @retval VWRN_NUMBER_TOO_BIG
1405 * @retval VWRN_NEGATIVE_UNSIGNED
1406 * @retval VWRN_TRAILING_CHARS
1407 * @retval VWRN_TRAILING_SPACES
1408 * @retval VINF_SUCCESS
1409 * @retval VERR_NO_DIGITS
1410 *
1411 * @param pszValue Pointer to the string value.
1412 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1413 * @param uBase The base of the representation used.
1414 * If 0 the function will look for known prefixes before defaulting to 10.
1415 * @param pu8 Where to store the converted number. (optional)
1416 */
1417RTDECL(int) RTStrToUInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint8_t *pu8);
1418
1419/**
1420 * Converts a string representation of a number to a 8-bit unsigned number,
1421 * making sure the full string is converted.
1422 *
1423 * @returns iprt status code.
1424 * Warnings are used to indicate conversion problems.
1425 * @retval VWRN_NUMBER_TOO_BIG
1426 * @retval VWRN_NEGATIVE_UNSIGNED
1427 * @retval VINF_SUCCESS
1428 * @retval VERR_NO_DIGITS
1429 * @retval VERR_TRAILING_SPACES
1430 * @retval VERR_TRAILING_CHARS
1431 *
1432 * @param pszValue Pointer to the string value.
1433 * @param uBase The base of the representation used.
1434 * If 0 the function will look for known prefixes before defaulting to 10.
1435 * @param pu8 Where to store the converted number. (optional)
1436 */
1437RTDECL(int) RTStrToUInt8Full(const char *pszValue, unsigned uBase, uint8_t *pu8);
1438
1439/**
1440 * Converts a string representation of a number to a 8-bit unsigned number.
1441 * The base is guessed.
1442 *
1443 * @returns 8-bit unsigned number on success.
1444 * @returns 0 on failure.
1445 * @param pszValue Pointer to the string value.
1446 */
1447RTDECL(uint8_t) RTStrToUInt8(const char *pszValue);
1448
1449/**
1450 * Converts a string representation of a number to a 64-bit signed number.
1451 *
1452 * @returns iprt status code.
1453 * Warnings are used to indicate conversion problems.
1454 * @retval VWRN_NUMBER_TOO_BIG
1455 * @retval VWRN_TRAILING_CHARS
1456 * @retval VWRN_TRAILING_SPACES
1457 * @retval VINF_SUCCESS
1458 * @retval VERR_NO_DIGITS
1459 *
1460 * @param pszValue Pointer to the string value.
1461 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1462 * @param uBase The base of the representation used.
1463 * If 0 the function will look for known prefixes before defaulting to 10.
1464 * @param pi64 Where to store the converted number. (optional)
1465 */
1466RTDECL(int) RTStrToInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, int64_t *pi64);
1467
1468/**
1469 * Converts a string representation of a number to a 64-bit signed number,
1470 * making sure the full string is converted.
1471 *
1472 * @returns iprt status code.
1473 * Warnings are used to indicate conversion problems.
1474 * @retval VWRN_NUMBER_TOO_BIG
1475 * @retval VINF_SUCCESS
1476 * @retval VERR_TRAILING_CHARS
1477 * @retval VERR_TRAILING_SPACES
1478 * @retval VERR_NO_DIGITS
1479 *
1480 * @param pszValue Pointer to the string value.
1481 * @param uBase The base of the representation used.
1482 * If 0 the function will look for known prefixes before defaulting to 10.
1483 * @param pi64 Where to store the converted number. (optional)
1484 */
1485RTDECL(int) RTStrToInt64Full(const char *pszValue, unsigned uBase, int64_t *pi64);
1486
1487/**
1488 * Converts a string representation of a number to a 64-bit signed number.
1489 * The base is guessed.
1490 *
1491 * @returns 64-bit signed number on success.
1492 * @returns 0 on failure.
1493 * @param pszValue Pointer to the string value.
1494 */
1495RTDECL(int64_t) RTStrToInt64(const char *pszValue);
1496
1497/**
1498 * Converts a string representation of a number to a 32-bit signed number.
1499 *
1500 * @returns iprt status code.
1501 * Warnings are used to indicate conversion problems.
1502 * @retval VWRN_NUMBER_TOO_BIG
1503 * @retval VWRN_TRAILING_CHARS
1504 * @retval VWRN_TRAILING_SPACES
1505 * @retval VINF_SUCCESS
1506 * @retval VERR_NO_DIGITS
1507 *
1508 * @param pszValue Pointer to the string value.
1509 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1510 * @param uBase The base of the representation used.
1511 * If 0 the function will look for known prefixes before defaulting to 10.
1512 * @param pi32 Where to store the converted number. (optional)
1513 */
1514RTDECL(int) RTStrToInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, int32_t *pi32);
1515
1516/**
1517 * Converts a string representation of a number to a 32-bit signed number,
1518 * making sure the full string is converted.
1519 *
1520 * @returns iprt status code.
1521 * Warnings are used to indicate conversion problems.
1522 * @retval VWRN_NUMBER_TOO_BIG
1523 * @retval VINF_SUCCESS
1524 * @retval VERR_TRAILING_CHARS
1525 * @retval VERR_TRAILING_SPACES
1526 * @retval VERR_NO_DIGITS
1527 *
1528 * @param pszValue Pointer to the string value.
1529 * @param uBase The base of the representation used.
1530 * If 0 the function will look for known prefixes before defaulting to 10.
1531 * @param pi32 Where to store the converted number. (optional)
1532 */
1533RTDECL(int) RTStrToInt32Full(const char *pszValue, unsigned uBase, int32_t *pi32);
1534
1535/**
1536 * Converts a string representation of a number to a 32-bit signed number.
1537 * The base is guessed.
1538 *
1539 * @returns 32-bit signed number on success.
1540 * @returns 0 on failure.
1541 * @param pszValue Pointer to the string value.
1542 */
1543RTDECL(int32_t) RTStrToInt32(const char *pszValue);
1544
1545/**
1546 * Converts a string representation of a number to a 16-bit signed number.
1547 *
1548 * @returns iprt status code.
1549 * Warnings are used to indicate conversion problems.
1550 * @retval VWRN_NUMBER_TOO_BIG
1551 * @retval VWRN_TRAILING_CHARS
1552 * @retval VWRN_TRAILING_SPACES
1553 * @retval VINF_SUCCESS
1554 * @retval VERR_NO_DIGITS
1555 *
1556 * @param pszValue Pointer to the string value.
1557 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1558 * @param uBase The base of the representation used.
1559 * If 0 the function will look for known prefixes before defaulting to 10.
1560 * @param pi16 Where to store the converted number. (optional)
1561 */
1562RTDECL(int) RTStrToInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, int16_t *pi16);
1563
1564/**
1565 * Converts a string representation of a number to a 16-bit signed number,
1566 * making sure the full string is converted.
1567 *
1568 * @returns iprt status code.
1569 * Warnings are used to indicate conversion problems.
1570 * @retval VWRN_NUMBER_TOO_BIG
1571 * @retval VINF_SUCCESS
1572 * @retval VERR_TRAILING_CHARS
1573 * @retval VERR_TRAILING_SPACES
1574 * @retval VERR_NO_DIGITS
1575 *
1576 * @param pszValue Pointer to the string value.
1577 * @param uBase The base of the representation used.
1578 * If 0 the function will look for known prefixes before defaulting to 10.
1579 * @param pi16 Where to store the converted number. (optional)
1580 */
1581RTDECL(int) RTStrToInt16Full(const char *pszValue, unsigned uBase, int16_t *pi16);
1582
1583/**
1584 * Converts a string representation of a number to a 16-bit signed number.
1585 * The base is guessed.
1586 *
1587 * @returns 16-bit signed number on success.
1588 * @returns 0 on failure.
1589 * @param pszValue Pointer to the string value.
1590 */
1591RTDECL(int16_t) RTStrToInt16(const char *pszValue);
1592
1593/**
1594 * Converts a string representation of a number to a 8-bit signed number.
1595 *
1596 * @returns iprt status code.
1597 * Warnings are used to indicate conversion problems.
1598 * @retval VWRN_NUMBER_TOO_BIG
1599 * @retval VWRN_TRAILING_CHARS
1600 * @retval VWRN_TRAILING_SPACES
1601 * @retval VINF_SUCCESS
1602 * @retval VERR_NO_DIGITS
1603 *
1604 * @param pszValue Pointer to the string value.
1605 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1606 * @param uBase The base of the representation used.
1607 * If 0 the function will look for known prefixes before defaulting to 10.
1608 * @param pi8 Where to store the converted number. (optional)
1609 */
1610RTDECL(int) RTStrToInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, int8_t *pi8);
1611
1612/**
1613 * Converts a string representation of a number to a 8-bit signed number,
1614 * making sure the full string is converted.
1615 *
1616 * @returns iprt status code.
1617 * Warnings are used to indicate conversion problems.
1618 * @retval VWRN_NUMBER_TOO_BIG
1619 * @retval VINF_SUCCESS
1620 * @retval VERR_TRAILING_CHARS
1621 * @retval VERR_TRAILING_SPACES
1622 * @retval VERR_NO_DIGITS
1623 *
1624 * @param pszValue Pointer to the string value.
1625 * @param uBase The base of the representation used.
1626 * If 0 the function will look for known prefixes before defaulting to 10.
1627 * @param pi8 Where to store the converted number. (optional)
1628 */
1629RTDECL(int) RTStrToInt8Full(const char *pszValue, unsigned uBase, int8_t *pi8);
1630
1631/**
1632 * Converts a string representation of a number to a 8-bit signed number.
1633 * The base is guessed.
1634 *
1635 * @returns 8-bit signed number on success.
1636 * @returns 0 on failure.
1637 * @param pszValue Pointer to the string value.
1638 */
1639RTDECL(int8_t) RTStrToInt8(const char *pszValue);
1640
1641/**
1642 * Formats a buffer stream as hex bytes.
1643 *
1644 * The default is no separating spaces or line breaks or anything.
1645 *
1646 * @returns IPRT status code.
1647 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
1648 * @retval VERR_BUFFER_OVERFLOW if the buffer is insufficent to hold the bytes.
1649 *
1650 * @param pszBuf Output string buffer.
1651 * @param cchBuf The size of the output buffer.
1652 * @param pv Pointer to the bytes to stringify.
1653 * @param cb The number of bytes to stringify.
1654 * @param fFlags Must be zero, reserved for future use.
1655 */
1656RTDECL(int) RTStrPrintHexBytes(char *pszBuf, size_t cchBuf, void const *pv, size_t cb, uint32_t fFlags);
1657
1658/**
1659 * Converts a string of hex bytes back into binary data.
1660 *
1661 * @returns IPRT status code.
1662 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
1663 * @retval VERR_BUFFER_OVERFLOW if the string contains too many hex bytes.
1664 * @retval VERR_BUFFER_UNDERFLOW if there aren't enough hex bytes to fill up
1665 * the output buffer.
1666 * @retval VERR_UNEVEN_INPUT if the input contains a half byte.
1667 * @retval VERR_NO_DIGITS
1668 * @retval VWRN_TRAILING_CHARS
1669 * @retval VWRN_TRAILING_SPACES
1670 *
1671 * @param pszHex The string containing the hex bytes.
1672 * @param pv Output buffer.
1673 * @param cb The size of the output buffer.
1674 * @param fFlags Must be zero, reserved for future use.
1675 */
1676RTDECL(int) RTStrConvertHexBytes(char const *pszHex, void *pv, size_t cb, uint32_t fFlags);
1677
1678/** @} */
1679
1680
1681/** @defgroup rt_str_space Unique String Space
1682 * @ingroup grp_rt_str
1683 * @{
1684 */
1685
1686/** Pointer to a string name space container node core. */
1687typedef struct RTSTRSPACECORE *PRTSTRSPACECORE;
1688/** Pointer to a pointer to a string name space container node core. */
1689typedef PRTSTRSPACECORE *PPRTSTRSPACECORE;
1690
1691/**
1692 * String name space container node core.
1693 */
1694typedef struct RTSTRSPACECORE
1695{
1696 /** Hash key. Don't touch. */
1697 uint32_t Key;
1698 /** Pointer to the left leaf node. Don't touch. */
1699 PRTSTRSPACECORE pLeft;
1700 /** Pointer to the left rigth node. Don't touch. */
1701 PRTSTRSPACECORE pRight;
1702 /** Pointer to the list of string with the same key. Don't touch. */
1703 PRTSTRSPACECORE pList;
1704 /** Height of this tree: max(heigth(left), heigth(right)) + 1. Don't touch */
1705 unsigned char uchHeight;
1706 /** The string length. Read only! */
1707 size_t cchString;
1708 /** Pointer to the string. Read only! */
1709 const char *pszString;
1710} RTSTRSPACECORE;
1711
1712/** String space. (Initialize with NULL.) */
1713typedef PRTSTRSPACECORE RTSTRSPACE;
1714/** Pointer to a string space. */
1715typedef PPRTSTRSPACECORE PRTSTRSPACE;
1716
1717
1718/**
1719 * Inserts a string into a unique string space.
1720 *
1721 * @returns true on success.
1722 * @returns false if the string collided with an existing string.
1723 * @param pStrSpace The space to insert it into.
1724 * @param pStr The string node.
1725 */
1726RTDECL(bool) RTStrSpaceInsert(PRTSTRSPACE pStrSpace, PRTSTRSPACECORE pStr);
1727
1728/**
1729 * Removes a string from a unique string space.
1730 *
1731 * @returns Pointer to the removed string node.
1732 * @returns NULL if the string was not found in the string space.
1733 * @param pStrSpace The space to insert it into.
1734 * @param pszString The string to remove.
1735 */
1736RTDECL(PRTSTRSPACECORE) RTStrSpaceRemove(PRTSTRSPACE pStrSpace, const char *pszString);
1737
1738/**
1739 * Gets a string from a unique string space.
1740 *
1741 * @returns Pointer to the string node.
1742 * @returns NULL if the string was not found in the string space.
1743 * @param pStrSpace The space to insert it into.
1744 * @param pszString The string to get.
1745 */
1746RTDECL(PRTSTRSPACECORE) RTStrSpaceGet(PRTSTRSPACE pStrSpace, const char *pszString);
1747
1748/**
1749 * Callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy().
1750 *
1751 * @returns 0 on continue.
1752 * @returns Non-zero to aborts the operation.
1753 * @param pStr The string node
1754 * @param pvUser The user specified argument.
1755 */
1756typedef DECLCALLBACK(int) FNRTSTRSPACECALLBACK(PRTSTRSPACECORE pStr, void *pvUser);
1757/** Pointer to callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy(). */
1758typedef FNRTSTRSPACECALLBACK *PFNRTSTRSPACECALLBACK;
1759
1760/**
1761 * Destroys the string space.
1762 * The caller supplies a callback which will be called for each of
1763 * the string nodes in for freeing their memory and other resources.
1764 *
1765 * @returns 0 or what ever non-zero return value pfnCallback returned
1766 * when aborting the destruction.
1767 * @param pStrSpace The space to insert it into.
1768 * @param pfnCallback The callback.
1769 * @param pvUser The user argument.
1770 */
1771RTDECL(int) RTStrSpaceDestroy(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
1772
1773/**
1774 * Enumerates the string space.
1775 * The caller supplies a callback which will be called for each of
1776 * the string nodes.
1777 *
1778 * @returns 0 or what ever non-zero return value pfnCallback returned
1779 * when aborting the destruction.
1780 * @param pStrSpace The space to insert it into.
1781 * @param pfnCallback The callback.
1782 * @param pvUser The user argument.
1783 */
1784RTDECL(int) RTStrSpaceEnumerate(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
1785
1786/** @} */
1787
1788
1789/** @defgroup rt_str_utf16 UTF-16 String Manipulation
1790 * @ingroup grp_rt_str
1791 * @{
1792 */
1793
1794/**
1795 * Free a UTF-16 string allocated by RTStrToUtf16(), RTStrToUtf16Ex(),
1796 * RTLatin1ToUtf16(), RTLatin1ToUtf16Ex(), RTUtf16Dup() or RTUtf16DupEx().
1797 *
1798 * @returns iprt status code.
1799 * @param pwszString The UTF-16 string to free. NULL is accepted.
1800 */
1801RTDECL(void) RTUtf16Free(PRTUTF16 pwszString);
1802
1803/**
1804 * Allocates a new copy of the specified UTF-16 string.
1805 *
1806 * @returns Pointer to the allocated string copy. Use RTUtf16Free() to free it.
1807 * @returns NULL when out of memory.
1808 * @param pwszString UTF-16 string to duplicate.
1809 * @remark This function will not make any attempt to validate the encoding.
1810 */
1811RTDECL(PRTUTF16) RTUtf16Dup(PCRTUTF16 pwszString);
1812
1813/**
1814 * Allocates a new copy of the specified UTF-16 string.
1815 *
1816 * @returns iprt status code.
1817 * @param ppwszString Receives pointer of the allocated UTF-16 string.
1818 * The returned pointer must be freed using RTUtf16Free().
1819 * @param pwszString UTF-16 string to duplicate.
1820 * @param cwcExtra Number of extra RTUTF16 items to allocate.
1821 * @remark This function will not make any attempt to validate the encoding.
1822 */
1823RTDECL(int) RTUtf16DupEx(PRTUTF16 *ppwszString, PCRTUTF16 pwszString, size_t cwcExtra);
1824
1825/**
1826 * Returns the length of a UTF-16 string in UTF-16 characters
1827 * without trailing '\\0'.
1828 *
1829 * Surrogate pairs counts as two UTF-16 characters here. Use RTUtf16CpCnt()
1830 * to get the exact number of code points in the string.
1831 *
1832 * @returns The number of RTUTF16 items in the string.
1833 * @param pwszString Pointer the UTF-16 string.
1834 * @remark This function will not make any attempt to validate the encoding.
1835 */
1836RTDECL(size_t) RTUtf16Len(PCRTUTF16 pwszString);
1837
1838/**
1839 * Performs a case sensitive string compare between two UTF-16 strings.
1840 *
1841 * @returns < 0 if the first string less than the second string.s
1842 * @returns 0 if the first string identical to the second string.
1843 * @returns > 0 if the first string greater than the second string.
1844 * @param pwsz1 First UTF-16 string. Null is allowed.
1845 * @param pwsz2 Second UTF-16 string. Null is allowed.
1846 * @remark This function will not make any attempt to validate the encoding.
1847 */
1848RTDECL(int) RTUtf16Cmp(register PCRTUTF16 pwsz1, register PCRTUTF16 pwsz2);
1849
1850/**
1851 * Performs a case insensitive string compare between two UTF-16 strings.
1852 *
1853 * This is a simplified compare, as only the simplified lower/upper case folding
1854 * specified by the unicode specs are used. It does not consider character pairs
1855 * as they are used in some languages, just simple upper & lower case compares.
1856 *
1857 * @returns < 0 if the first string less than the second string.
1858 * @returns 0 if the first string identical to the second string.
1859 * @returns > 0 if the first string greater than the second string.
1860 * @param pwsz1 First UTF-16 string. Null is allowed.
1861 * @param pwsz2 Second UTF-16 string. Null is allowed.
1862 */
1863RTDECL(int) RTUtf16ICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
1864
1865/**
1866 * Performs a case insensitive string compare between two UTF-16 strings
1867 * using the current locale of the process (if applicable).
1868 *
1869 * This differs from RTUtf16ICmp() in that it will try, if a locale with the
1870 * required data is available, to do a correct case-insensitive compare. It
1871 * follows that it is more complex and thereby likely to be more expensive.
1872 *
1873 * @returns < 0 if the first string less than the second string.
1874 * @returns 0 if the first string identical to the second string.
1875 * @returns > 0 if the first string greater than the second string.
1876 * @param pwsz1 First UTF-16 string. Null is allowed.
1877 * @param pwsz2 Second UTF-16 string. Null is allowed.
1878 */
1879RTDECL(int) RTUtf16LocaleICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
1880
1881/**
1882 * Folds a UTF-16 string to lowercase.
1883 *
1884 * This is a very simple folding; is uses the simple lowercase
1885 * code point, it is not related to any locale just the most common
1886 * lowercase codepoint setup by the unicode specs, and it will not
1887 * create new surrogate pairs or remove existing ones.
1888 *
1889 * @returns Pointer to the passed in string.
1890 * @param pwsz The string to fold.
1891 */
1892RTDECL(PRTUTF16) RTUtf16ToLower(PRTUTF16 pwsz);
1893
1894/**
1895 * Folds a UTF-16 string to uppercase.
1896 *
1897 * This is a very simple folding; is uses the simple uppercase
1898 * code point, it is not related to any locale just the most common
1899 * uppercase codepoint setup by the unicode specs, and it will not
1900 * create new surrogate pairs or remove existing ones.
1901 *
1902 * @returns Pointer to the passed in string.
1903 * @param pwsz The string to fold.
1904 */
1905RTDECL(PRTUTF16) RTUtf16ToUpper(PRTUTF16 pwsz);
1906
1907/**
1908 * Translate a UTF-16 string into a UTF-8 allocating the result buffer.
1909 *
1910 * @returns iprt status code.
1911 * @param pwszString UTF-16 string to convert.
1912 * @param ppszString Receives pointer of allocated UTF-8 string on
1913 * success, and is always set to NULL on failure.
1914 * The returned pointer must be freed using RTStrFree().
1915 */
1916RTDECL(int) RTUtf16ToUtf8(PCRTUTF16 pwszString, char **ppszString);
1917
1918/**
1919 * Translates UTF-16 to UTF-8 using buffer provided by the caller or
1920 * a fittingly sized buffer allocated by the function.
1921 *
1922 * @returns iprt status code.
1923 * @param pwszString The UTF-16 string to convert.
1924 * @param cwcString The number of RTUTF16 items to translate from pwszString.
1925 * The translation will stop when reaching cwcString or the terminator ('\\0').
1926 * Use RTSTR_MAX to translate the entire string.
1927 * @param ppsz If cch is non-zero, this must either be pointing to a pointer to
1928 * a buffer of the specified size, or pointer to a NULL pointer.
1929 * If *ppsz is NULL or cch is zero a buffer of at least cch chars
1930 * will be allocated to hold the translated string.
1931 * If a buffer was requested it must be freed using RTUtf16Free().
1932 * @param cch The buffer size in chars (the type). This includes the terminator.
1933 * @param pcch Where to store the length of the translated string,
1934 * excluding the terminator. (Optional)
1935 *
1936 * This may be set under some error conditions,
1937 * however, only for VERR_BUFFER_OVERFLOW and
1938 * VERR_NO_STR_MEMORY will it contain a valid string
1939 * length that can be used to resize the buffer.
1940 */
1941RTDECL(int) RTUtf16ToUtf8Ex(PCRTUTF16 pwszString, size_t cwcString, char **ppsz, size_t cch, size_t *pcch);
1942
1943/**
1944 * Calculates the length of the UTF-16 string in UTF-8 chars (bytes).
1945 *
1946 * This function will validate the string, and incorrectly encoded UTF-16
1947 * strings will be rejected. The primary purpose of this function is to
1948 * help allocate buffers for RTUtf16ToUtf8() of the correct size. For most
1949 * other purposes RTUtf16ToUtf8Ex() should be used.
1950 *
1951 * @returns Number of char (bytes).
1952 * @returns 0 if the string was incorrectly encoded.
1953 * @param pwsz The UTF-16 string.
1954 */
1955RTDECL(size_t) RTUtf16CalcUtf8Len(PCRTUTF16 pwsz);
1956
1957/**
1958 * Calculates the length of the UTF-16 string in UTF-8 chars (bytes).
1959 *
1960 * This function will validate the string, and incorrectly encoded UTF-16
1961 * strings will be rejected.
1962 *
1963 * @returns iprt status code.
1964 * @param pwsz The string.
1965 * @param cwc The max string length. Use RTSTR_MAX to process the entire string.
1966 * @param pcch Where to store the string length (in bytes). Optional.
1967 * This is undefined on failure.
1968 */
1969RTDECL(int) RTUtf16CalcUtf8LenEx(PCRTUTF16 pwsz, size_t cwc, size_t *pcch);
1970
1971/**
1972 * Translate a UTF-16 string into a Latin-1 (ISO-8859-1) allocating the result
1973 * buffer.
1974 *
1975 * @returns iprt status code.
1976 * @param pwszString UTF-16 string to convert.
1977 * @param ppszString Receives pointer of allocated Latin1 string on
1978 * success, and is always set to NULL on failure.
1979 * The returned pointer must be freed using RTStrFree().
1980 */
1981RTDECL(int) RTUtf16ToLatin1(PCRTUTF16 pwszString, char **ppszString);
1982
1983/**
1984 * Translates UTF-16 to Latin-1 (ISO-8859-1) using buffer provided by the caller
1985 * or a fittingly sized buffer allocated by the function.
1986 *
1987 * @returns iprt status code.
1988 * @param pwszString The UTF-16 string to convert.
1989 * @param cwcString The number of RTUTF16 items to translate from
1990 * pwszString. The translation will stop when reaching
1991 * cwcString or the terminator ('\\0'). Use RTSTR_MAX
1992 * to translate the entire string.
1993 * @param ppsz Pointer to the pointer to the Latin-1 string. The
1994 * buffer can optionally be preallocated by the caller.
1995 *
1996 * If cch is zero, *ppsz is undefined.
1997 *
1998 * If cch is non-zero and *ppsz is not NULL, then this
1999 * will be used as the output buffer.
2000 * VERR_BUFFER_OVERFLOW will be returned if this is
2001 * insufficient.
2002 *
2003 * If cch is zero or *ppsz is NULL, then a buffer of
2004 * sufficent size is allocated. cch can be used to
2005 * specify a minimum size of this buffer. Use
2006 * RTUtf16Free() to free the result.
2007 *
2008 * @param cch The buffer size in chars (the type). This includes
2009 * the terminator.
2010 * @param pcch Where to store the length of the translated string,
2011 * excluding the terminator. (Optional)
2012 *
2013 * This may be set under some error conditions,
2014 * however, only for VERR_BUFFER_OVERFLOW and
2015 * VERR_NO_STR_MEMORY will it contain a valid string
2016 * length that can be used to resize the buffer.
2017 */
2018RTDECL(int) RTUtf16ToLatin1Ex(PCRTUTF16 pwszString, size_t cwcString, char **ppsz, size_t cch, size_t *pcch);
2019
2020/**
2021 * Calculates the length of the UTF-16 string in Latin-1 (ISO-8859-1) chars.
2022 *
2023 * This function will validate the string, and incorrectly encoded UTF-16
2024 * strings will be rejected. The primary purpose of this function is to
2025 * help allocate buffers for RTUtf16ToLatin1() of the correct size. For most
2026 * other purposes RTUtf16ToLatin1Ex() should be used.
2027 *
2028 * @returns Number of char (bytes).
2029 * @returns 0 if the string was incorrectly encoded.
2030 * @param pwsz The UTF-16 string.
2031 */
2032RTDECL(size_t) RTUtf16CalcLatin1Len(PCRTUTF16 pwsz);
2033
2034/**
2035 * Calculates the length of the UTF-16 string in Latin-1 (ISO-8859-1) chars.
2036 *
2037 * This function will validate the string, and incorrectly encoded UTF-16
2038 * strings will be rejected.
2039 *
2040 * @returns iprt status code.
2041 * @param pwsz The string.
2042 * @param cwc The max string length. Use RTSTR_MAX to process the
2043 * entire string.
2044 * @param pcch Where to store the string length (in bytes). Optional.
2045 * This is undefined on failure.
2046 */
2047RTDECL(int) RTUtf16CalcLatin1LenEx(PCRTUTF16 pwsz, size_t cwc, size_t *pcch);
2048
2049/**
2050 * Get the unicode code point at the given string position.
2051 *
2052 * @returns unicode code point.
2053 * @returns RTUNICP_INVALID if the encoding is invalid.
2054 * @param pwsz The string.
2055 *
2056 * @remark This is an internal worker for RTUtf16GetCp().
2057 */
2058RTDECL(RTUNICP) RTUtf16GetCpInternal(PCRTUTF16 pwsz);
2059
2060/**
2061 * Get the unicode code point at the given string position.
2062 *
2063 * @returns iprt status code.
2064 * @param ppwsz Pointer to the string pointer. This will be updated to
2065 * point to the char following the current code point.
2066 * @param pCp Where to store the code point.
2067 * RTUNICP_INVALID is stored here on failure.
2068 *
2069 * @remark This is an internal worker for RTUtf16GetCpEx().
2070 */
2071RTDECL(int) RTUtf16GetCpExInternal(PCRTUTF16 *ppwsz, PRTUNICP pCp);
2072
2073/**
2074 * Put the unicode code point at the given string position
2075 * and return the pointer to the char following it.
2076 *
2077 * This function will not consider anything at or following the
2078 * buffer area pointed to by pwsz. It is therefore not suitable for
2079 * inserting code points into a string, only appending/overwriting.
2080 *
2081 * @returns pointer to the char following the written code point.
2082 * @param pwsz The string.
2083 * @param CodePoint The code point to write.
2084 * This should not be RTUNICP_INVALID or any other
2085 * character out of the UTF-16 range.
2086 *
2087 * @remark This is an internal worker for RTUtf16GetCpEx().
2088 */
2089RTDECL(PRTUTF16) RTUtf16PutCpInternal(PRTUTF16 pwsz, RTUNICP CodePoint);
2090
2091/**
2092 * Get the unicode code point at the given string position.
2093 *
2094 * @returns unicode code point.
2095 * @returns RTUNICP_INVALID if the encoding is invalid.
2096 * @param pwsz The string.
2097 *
2098 * @remark We optimize this operation by using an inline function for
2099 * everything which isn't a surrogate pair or an endian indicator.
2100 */
2101DECLINLINE(RTUNICP) RTUtf16GetCp(PCRTUTF16 pwsz)
2102{
2103 const RTUTF16 wc = *pwsz;
2104 if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
2105 return wc;
2106 return RTUtf16GetCpInternal(pwsz);
2107}
2108
2109/**
2110 * Get the unicode code point at the given string position.
2111 *
2112 * @returns iprt status code.
2113 * @param ppwsz Pointer to the string pointer. This will be updated to
2114 * point to the char following the current code point.
2115 * @param pCp Where to store the code point.
2116 * RTUNICP_INVALID is stored here on failure.
2117 *
2118 * @remark We optimize this operation by using an inline function for
2119 * everything which isn't a surrogate pair or and endian indicator.
2120 */
2121DECLINLINE(int) RTUtf16GetCpEx(PCRTUTF16 *ppwsz, PRTUNICP pCp)
2122{
2123 const RTUTF16 wc = **ppwsz;
2124 if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
2125 {
2126 (*ppwsz)++;
2127 *pCp = wc;
2128 return VINF_SUCCESS;
2129 }
2130 return RTUtf16GetCpExInternal(ppwsz, pCp);
2131}
2132
2133/**
2134 * Put the unicode code point at the given string position
2135 * and return the pointer to the char following it.
2136 *
2137 * This function will not consider anything at or following the
2138 * buffer area pointed to by pwsz. It is therefore not suitable for
2139 * inserting code points into a string, only appending/overwriting.
2140 *
2141 * @returns pointer to the char following the written code point.
2142 * @param pwsz The string.
2143 * @param CodePoint The code point to write.
2144 * This should not be RTUNICP_INVALID or any other
2145 * character out of the UTF-16 range.
2146 *
2147 * @remark We optimize this operation by using an inline function for
2148 * everything which isn't a surrogate pair or and endian indicator.
2149 */
2150DECLINLINE(PRTUTF16) RTUtf16PutCp(PRTUTF16 pwsz, RTUNICP CodePoint)
2151{
2152 if (CodePoint < 0xd800 || (CodePoint > 0xd800 && CodePoint < 0xfffe))
2153 {
2154 *pwsz++ = (RTUTF16)CodePoint;
2155 return pwsz;
2156 }
2157 return RTUtf16PutCpInternal(pwsz, CodePoint);
2158}
2159
2160/**
2161 * Skips ahead, past the current code point.
2162 *
2163 * @returns Pointer to the char after the current code point.
2164 * @param pwsz Pointer to the current code point.
2165 * @remark This will not move the next valid code point, only past the current one.
2166 */
2167DECLINLINE(PRTUTF16) RTUtf16NextCp(PCRTUTF16 pwsz)
2168{
2169 RTUNICP Cp;
2170 RTUtf16GetCpEx(&pwsz, &Cp);
2171 return (PRTUTF16)pwsz;
2172}
2173
2174/**
2175 * Skips backwards, to the previous code point.
2176 *
2177 * @returns Pointer to the char after the current code point.
2178 * @param pwszStart Pointer to the start of the string.
2179 * @param pwsz Pointer to the current code point.
2180 */
2181RTDECL(PRTUTF16) RTUtf16PrevCp(PCRTUTF16 pwszStart, PCRTUTF16 pwsz);
2182
2183
2184/**
2185 * Checks if the UTF-16 char is the high surrogate char (i.e.
2186 * the 1st char in the pair).
2187 *
2188 * @returns true if it is.
2189 * @returns false if it isn't.
2190 * @param wc The character to investigate.
2191 */
2192DECLINLINE(bool) RTUtf16IsHighSurrogate(RTUTF16 wc)
2193{
2194 return wc >= 0xd800 && wc <= 0xdbff;
2195}
2196
2197/**
2198 * Checks if the UTF-16 char is the low surrogate char (i.e.
2199 * the 2nd char in the pair).
2200 *
2201 * @returns true if it is.
2202 * @returns false if it isn't.
2203 * @param wc The character to investigate.
2204 */
2205DECLINLINE(bool) RTUtf16IsLowSurrogate(RTUTF16 wc)
2206{
2207 return wc >= 0xdc00 && wc <= 0xdfff;
2208}
2209
2210
2211/**
2212 * Checks if the two UTF-16 chars form a valid surrogate pair.
2213 *
2214 * @returns true if they do.
2215 * @returns false if they doesn't.
2216 * @param wcHigh The high (1st) character.
2217 * @param wcLow The low (2nd) character.
2218 */
2219DECLINLINE(bool) RTUtf16IsSurrogatePair(RTUTF16 wcHigh, RTUTF16 wcLow)
2220{
2221 return RTUtf16IsHighSurrogate(wcHigh)
2222 && RTUtf16IsLowSurrogate(wcLow);
2223}
2224
2225/** @} */
2226
2227
2228/** @defgroup rt_str_latin1 Latin-1 (ISO-8859-1) String Manipulation
2229 * @ingroup grp_rt_str
2230 * @{
2231 */
2232
2233/**
2234 * Calculates the length of the Latin-1 (ISO-8859-1) string in RTUTF16 items.
2235 *
2236 * @returns Number of RTUTF16 items.
2237 * @param psz The Latin-1 string.
2238 */
2239RTDECL(size_t) RTLatin1CalcUtf16Len(const char *psz);
2240
2241/**
2242 * Calculates the length of the Latin-1 (ISO-8859-1) string in RTUTF16 items.
2243 *
2244 * @returns iprt status code.
2245 * @param psz The Latin-1 string.
2246 * @param cch The max string length. Use RTSTR_MAX to process the
2247 * entire string.
2248 * @param pcwc Where to store the string length. Optional.
2249 * This is undefined on failure.
2250 */
2251RTDECL(int) RTLatin1CalcUtf16LenEx(const char *psz, size_t cch, size_t *pcwc);
2252
2253/**
2254 * Translate a Latin-1 (ISO-8859-1) string into a UTF-16 allocating the result
2255 * buffer.
2256 *
2257 * @returns iprt status code.
2258 * @param pszString The Latin-1 string to convert.
2259 * @param ppwszString Receives pointer to the allocated UTF-16 string. The
2260 * returned string must be freed using RTUtf16Free().
2261 */
2262RTDECL(int) RTLatin1ToUtf16(const char *pszString, PRTUTF16 *ppwszString);
2263
2264/**
2265 * Translates pszString from Latin-1 (ISO-8859-1) to UTF-16, allocating the
2266 * result buffer if requested.
2267 *
2268 * @returns iprt status code.
2269 * @param pszString The Latin-1 string to convert.
2270 * @param cchString The maximum size in chars (the type) to convert.
2271 * The conversion stops when it reaches cchString or
2272 * the string terminator ('\\0').
2273 * Use RTSTR_MAX to translate the entire string.
2274 * @param ppwsz If cwc is non-zero, this must either be pointing
2275 * to pointer to a buffer of the specified size, or
2276 * pointer to a NULL pointer.
2277 * If *ppwsz is NULL or cwc is zero a buffer of at
2278 * least cwc items will be allocated to hold the
2279 * translated string. If a buffer was requested it
2280 * must be freed using RTUtf16Free().
2281 * @param cwc The buffer size in RTUTF16s. This includes the
2282 * terminator.
2283 * @param pcwc Where to store the length of the translated string,
2284 * excluding the terminator. (Optional)
2285 *
2286 * This may be set under some error conditions,
2287 * however, only for VERR_BUFFER_OVERFLOW and
2288 * VERR_NO_STR_MEMORY will it contain a valid string
2289 * length that can be used to resize the buffer.
2290 */
2291RTDECL(int) RTLatin1ToUtf16Ex(const char *pszString, size_t cchString, PRTUTF16 *ppwsz, size_t cwc, size_t *pcwc);
2292
2293/** @} */
2294
2295
2296RT_C_DECLS_END
2297
2298/** @} */
2299
2300#endif
2301
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