VirtualBox

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

Last change on this file since 27347 was 26659, checked in by vboxsync, 15 years ago

IPRT: Added RTStrCopy and RTStrCopyEx for strcpy with overflow handling.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 88.1 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 * String copy with overflow handling.
1015 *
1016 * @retval VINF_SUCCESS on success.
1017 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
1018 * buffer will contain as much of the string as it can hold, fully
1019 * terminated.
1020 *
1021 * @param pszDst The destination buffer.
1022 * @param cbDst The size of the destination buffer (in bytes).
1023 * @param pszSrc The source string. NULL is not OK.
1024 */
1025RTDECL(int) RTStrCopy(char *pszDst, size_t cbDst, const char *pszSrc);
1026
1027/**
1028 * String copy with overflow handling.
1029 *
1030 * @retval VINF_SUCCESS on success.
1031 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
1032 * buffer will contain as much of the string as it can hold, fully
1033 * terminated.
1034 *
1035 * @param pszDst The destination buffer.
1036 * @param cbDst The size of the destination buffer (in bytes).
1037 * @param pszSrc The source string. NULL is not OK.
1038 * @param cchSrcMax The maximum number of chars (not code points) to
1039 * copy from the source string, not counting the
1040 * terminator as usual.
1041 */
1042RTDECL(int) RTStrCopyEx(char *pszDst, size_t cbDst, const char *pszSrc, size_t cchSrcMax);
1043
1044/**
1045 * Performs a case sensitive string compare between two UTF-8 strings.
1046 *
1047 * Encoding errors are ignored by the current implementation. So, the only
1048 * difference between this and the CRT strcmp function is the handling of
1049 * NULL arguments.
1050 *
1051 * @returns < 0 if the first string less than the second string.
1052 * @returns 0 if the first string identical to the second string.
1053 * @returns > 0 if the first string greater than the second string.
1054 * @param psz1 First UTF-8 string. Null is allowed.
1055 * @param psz2 Second UTF-8 string. Null is allowed.
1056 */
1057RTDECL(int) RTStrCmp(const char *psz1, const char *psz2);
1058
1059/**
1060 * Performs a case sensitive string compare between two UTF-8 strings, given
1061 * a maximum string length.
1062 *
1063 * Encoding errors are ignored by the current implementation. So, the only
1064 * difference between this and the CRT strncmp function is the handling of
1065 * NULL arguments.
1066 *
1067 * @returns < 0 if the first string less than the second string.
1068 * @returns 0 if the first string identical to the second string.
1069 * @returns > 0 if the first string greater than the second string.
1070 * @param psz1 First UTF-8 string. Null is allowed.
1071 * @param psz2 Second UTF-8 string. Null is allowed.
1072 * @param cchMax The maximum string length
1073 */
1074RTDECL(int) RTStrNCmp(const char *psz1, const char *psz2, size_t cchMax);
1075
1076/**
1077 * Performs a case insensitive string compare between two UTF-8 strings.
1078 *
1079 * This is a simplified compare, as only the simplified lower/upper case folding
1080 * specified by the unicode specs are used. It does not consider character pairs
1081 * as they are used in some languages, just simple upper & lower case compares.
1082 *
1083 * The result is the difference between the mismatching codepoints after they
1084 * both have been lower cased.
1085 *
1086 * If the string encoding is invalid the function will assert (strict builds)
1087 * and use RTStrCmp for the remainder of the string.
1088 *
1089 * @returns < 0 if the first string less than the second string.
1090 * @returns 0 if the first string identical to the second string.
1091 * @returns > 0 if the first string greater than the second string.
1092 * @param psz1 First UTF-8 string. Null is allowed.
1093 * @param psz2 Second UTF-8 string. Null is allowed.
1094 */
1095RTDECL(int) RTStrICmp(const char *psz1, const char *psz2);
1096
1097/**
1098 * Performs a case insensitive string compare between two UTF-8 strings, given a
1099 * maximum string length.
1100 *
1101 * This is a simplified compare, as only the simplified lower/upper case folding
1102 * specified by the unicode specs are used. It does not consider character pairs
1103 * as they are used in some languages, just simple upper & lower case compares.
1104 *
1105 * The result is the difference between the mismatching codepoints after they
1106 * both have been lower cased.
1107 *
1108 * If the string encoding is invalid the function will assert (strict builds)
1109 * and use RTStrCmp for the remainder of the string.
1110 *
1111 * @returns < 0 if the first string less than the second string.
1112 * @returns 0 if the first string identical to the second string.
1113 * @returns > 0 if the first string greater than the second string.
1114 * @param psz1 First UTF-8 string. Null is allowed.
1115 * @param psz2 Second UTF-8 string. Null is allowed.
1116 * @param cchMax Maximum string length
1117 */
1118RTDECL(int) RTStrNICmp(const char *psz1, const char *psz2, size_t cchMax);
1119
1120/**
1121 * Locates a case sensitive substring.
1122 *
1123 * If any of the two strings are NULL, then NULL is returned. If the needle is
1124 * an empty string, then the haystack is returned (i.e. matches anything).
1125 *
1126 * @returns Pointer to the first occurrence of the substring if found, NULL if
1127 * not.
1128 *
1129 * @param pszHaystack The string to search.
1130 * @param pszNeedle The substring to search for.
1131 *
1132 * @remarks The difference between this and strstr is the handling of NULL
1133 * pointers.
1134 */
1135RTDECL(char *) RTStrStr(const char *pszHaystack, const char *pszNeedle);
1136
1137/**
1138 * Locates a case insensitive substring.
1139 *
1140 * If any of the two strings are NULL, then NULL is returned. If the needle is
1141 * an empty string, then the haystack is returned (i.e. matches anything).
1142 *
1143 * @returns Pointer to the first occurrence of the substring if found, NULL if
1144 * not.
1145 *
1146 * @param pszHaystack The string to search.
1147 * @param pszNeedle The substring to search for.
1148 *
1149 */
1150RTDECL(char *) RTStrIStr(const char *pszHaystack, const char *pszNeedle);
1151
1152/**
1153 * Converts the string to lower case.
1154 *
1155 * @returns Pointer to the converted string.
1156 * @param psz The string to convert.
1157 */
1158RTDECL(char *) RTStrToLower(char *psz);
1159
1160/**
1161 * Converts the string to upper case.
1162 *
1163 * @returns Pointer to the converted string.
1164 * @param psz The string to convert.
1165 */
1166RTDECL(char *) RTStrToUpper(char *psz);
1167
1168/**
1169 * Find the length of a zero-terminated byte string, given
1170 * a max string length.
1171 *
1172 * See also RTStrNLenEx.
1173 *
1174 * @returns The string length or cbMax. The returned length does not include
1175 * the zero terminator if it was found.
1176 *
1177 * @param pszString The string.
1178 * @param cchMax The max string length.
1179 */
1180RTDECL(size_t) RTStrNLen(const char *pszString, size_t cchMax);
1181
1182/**
1183 * Find the length of a zero-terminated byte string, given
1184 * a max string length.
1185 *
1186 * See also RTStrNLen.
1187 *
1188 * @returns IPRT status code.
1189 * @retval VINF_SUCCESS if the string has a length less than cchMax.
1190 * @retval VERR_BUFFER_OVERFLOW if the end of the string wasn't found
1191 * before cchMax was reached.
1192 *
1193 * @param pszString The string.
1194 * @param cchMax The max string length.
1195 * @param pcch Where to store the string length excluding the
1196 * terminator. This is set to cchMax if the terminator
1197 * isn't found.
1198 */
1199RTDECL(int) RTStrNLenEx(const char *pszString, size_t cchMax, size_t *pcch);
1200
1201/**
1202 * Matches a simple string pattern.
1203 *
1204 * @returns true if the string matches the pattern, otherwise false.
1205 *
1206 * @param pszPattern The pattern. Special chars are '*' and '?', where the
1207 * asterisk matches zero or more characters and question
1208 * mark matches exactly one character.
1209 * @param pszString The string to match against the pattern.
1210 */
1211RTDECL(bool) RTStrSimplePatternMatch(const char *pszPattern, const char *pszString);
1212
1213/**
1214 * Matches a simple string pattern, neither which needs to be zero terminated.
1215 *
1216 * This is identical to RTStrSimplePatternMatch except that you can optionally
1217 * specify the length of both the pattern and the string. The function will
1218 * stop when it hits a string terminator or either of the lengths.
1219 *
1220 * @returns true if the string matches the pattern, otherwise false.
1221 *
1222 * @param pszPattern The pattern. Special chars are '*' and '?', where the
1223 * asterisk matches zero or more characters and question
1224 * mark matches exactly one character.
1225 * @param cchPattern The pattern length. Pass RTSTR_MAX if you don't know the
1226 * length and wish to stop at the string terminator.
1227 * @param pszString The string to match against the pattern.
1228 * @param cchString The string length. Pass RTSTR_MAX if you don't know the
1229 * length and wish to match up to the string terminator.
1230 */
1231RTDECL(bool) RTStrSimplePatternNMatch(const char *pszPattern, size_t cchPattern,
1232 const char *pszString, size_t cchString);
1233
1234/**
1235 * Matches multiple patterns against a string.
1236 *
1237 * The patterns are separated by the pipe character (|).
1238 *
1239 * @returns true if the string matches the pattern, otherwise false.
1240 *
1241 * @param pszPatterns The patterns.
1242 * @param cchPatterns The lengths of the patterns to use. Pass RTSTR_MAX to
1243 * stop at the terminator.
1244 * @param pszString The string to match against the pattern.
1245 * @param cchString The string length. Pass RTSTR_MAX stop stop at the
1246 * terminator.
1247 * @param poffPattern Offset into the patterns string of the patttern that
1248 * matched. If no match, this will be set to RTSTR_MAX.
1249 * This is optional, NULL is fine.
1250 */
1251RTDECL(bool) RTStrSimplePatternMultiMatch(const char *pszPatterns, size_t cchPatterns,
1252 const char *pszString, size_t cchString,
1253 size_t *poffPattern);
1254
1255/**
1256 * Compares two version strings RTStrICmp fashion.
1257 *
1258 * The version string is split up into sections at punctuation, spaces,
1259 * underscores, dashes and pluss signs. The sections are then split up into
1260 * numeric and string sub-sections. Finally, the sub-sections are compared
1261 * in a numeric or case insesntivie fashion depending on what they are.
1262 *
1263 * The following strings are considered to be equal: "1.0.0", "1.00.0", "1.0",
1264 * "1". These aren't: "1.0.0r993", "1.0", "1.0r993", "1.0_Beta3", "1.1"
1265 *
1266 * @returns < 0 if the first string less than the second string.
1267 * @returns 0 if the first string identical to the second string.
1268 * @returns > 0 if the first string greater than the second string.
1269 *
1270 * @param pszVer1 First version string to compare.
1271 * @param pszVer2 Second version string to compare first version with.
1272 */
1273RTDECL(int) RTStrVersionCompare(const char *pszVer1, const char *pszVer2);
1274
1275
1276/** @defgroup rt_str_conv String To/From Number Conversions
1277 * @ingroup grp_rt_str
1278 * @{ */
1279
1280/**
1281 * Converts a string representation of a number to a 64-bit unsigned number.
1282 *
1283 * @returns iprt status code.
1284 * Warnings are used to indicate conversion problems.
1285 * @retval VWRN_NUMBER_TOO_BIG
1286 * @retval VWRN_NEGATIVE_UNSIGNED
1287 * @retval VWRN_TRAILING_CHARS
1288 * @retval VWRN_TRAILING_SPACES
1289 * @retval VINF_SUCCESS
1290 * @retval VERR_NO_DIGITS
1291 *
1292 * @param pszValue Pointer to the string value.
1293 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1294 * @param uBase The base of the representation used.
1295 * If 0 the function will look for known prefixes before defaulting to 10.
1296 * @param pu64 Where to store the converted number. (optional)
1297 */
1298RTDECL(int) RTStrToUInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint64_t *pu64);
1299
1300/**
1301 * Converts a string representation of a number to a 64-bit unsigned number,
1302 * making sure the full string is converted.
1303 *
1304 * @returns iprt status code.
1305 * Warnings are used to indicate conversion problems.
1306 * @retval VWRN_NUMBER_TOO_BIG
1307 * @retval VWRN_NEGATIVE_UNSIGNED
1308 * @retval VINF_SUCCESS
1309 * @retval VERR_NO_DIGITS
1310 * @retval VERR_TRAILING_SPACES
1311 * @retval VERR_TRAILING_CHARS
1312 *
1313 * @param pszValue Pointer to the string value.
1314 * @param uBase The base of the representation used.
1315 * If 0 the function will look for known prefixes before defaulting to 10.
1316 * @param pu64 Where to store the converted number. (optional)
1317 */
1318RTDECL(int) RTStrToUInt64Full(const char *pszValue, unsigned uBase, uint64_t *pu64);
1319
1320/**
1321 * Converts a string representation of a number to a 64-bit unsigned number.
1322 * The base is guessed.
1323 *
1324 * @returns 64-bit unsigned number on success.
1325 * @returns 0 on failure.
1326 * @param pszValue Pointer to the string value.
1327 */
1328RTDECL(uint64_t) RTStrToUInt64(const char *pszValue);
1329
1330/**
1331 * Converts a string representation of a number to a 32-bit unsigned number.
1332 *
1333 * @returns iprt status code.
1334 * Warnings are used to indicate conversion problems.
1335 * @retval VWRN_NUMBER_TOO_BIG
1336 * @retval VWRN_NEGATIVE_UNSIGNED
1337 * @retval VWRN_TRAILING_CHARS
1338 * @retval VWRN_TRAILING_SPACES
1339 * @retval VINF_SUCCESS
1340 * @retval VERR_NO_DIGITS
1341 *
1342 * @param pszValue Pointer to the string value.
1343 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1344 * @param uBase The base of the representation used.
1345 * If 0 the function will look for known prefixes before defaulting to 10.
1346 * @param pu32 Where to store the converted number. (optional)
1347 */
1348RTDECL(int) RTStrToUInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint32_t *pu32);
1349
1350/**
1351 * Converts a string representation of a number to a 32-bit unsigned number,
1352 * making sure the full string is converted.
1353 *
1354 * @returns iprt status code.
1355 * Warnings are used to indicate conversion problems.
1356 * @retval VWRN_NUMBER_TOO_BIG
1357 * @retval VWRN_NEGATIVE_UNSIGNED
1358 * @retval VINF_SUCCESS
1359 * @retval VERR_NO_DIGITS
1360 * @retval VERR_TRAILING_SPACES
1361 * @retval VERR_TRAILING_CHARS
1362 *
1363 * @param pszValue Pointer to the string value.
1364 * @param uBase The base of the representation used.
1365 * If 0 the function will look for known prefixes before defaulting to 10.
1366 * @param pu32 Where to store the converted number. (optional)
1367 */
1368RTDECL(int) RTStrToUInt32Full(const char *pszValue, unsigned uBase, uint32_t *pu32);
1369
1370/**
1371 * Converts a string representation of a number to a 64-bit unsigned number.
1372 * The base is guessed.
1373 *
1374 * @returns 32-bit unsigned number on success.
1375 * @returns 0 on failure.
1376 * @param pszValue Pointer to the string value.
1377 */
1378RTDECL(uint32_t) RTStrToUInt32(const char *pszValue);
1379
1380/**
1381 * Converts a string representation of a number to a 16-bit unsigned number.
1382 *
1383 * @returns iprt status code.
1384 * Warnings are used to indicate conversion problems.
1385 * @retval VWRN_NUMBER_TOO_BIG
1386 * @retval VWRN_NEGATIVE_UNSIGNED
1387 * @retval VWRN_TRAILING_CHARS
1388 * @retval VWRN_TRAILING_SPACES
1389 * @retval VINF_SUCCESS
1390 * @retval VERR_NO_DIGITS
1391 *
1392 * @param pszValue Pointer to the string value.
1393 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1394 * @param uBase The base of the representation used.
1395 * If 0 the function will look for known prefixes before defaulting to 10.
1396 * @param pu16 Where to store the converted number. (optional)
1397 */
1398RTDECL(int) RTStrToUInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint16_t *pu16);
1399
1400/**
1401 * Converts a string representation of a number to a 16-bit unsigned number,
1402 * making sure the full string is converted.
1403 *
1404 * @returns iprt status code.
1405 * Warnings are used to indicate conversion problems.
1406 * @retval VWRN_NUMBER_TOO_BIG
1407 * @retval VWRN_NEGATIVE_UNSIGNED
1408 * @retval VINF_SUCCESS
1409 * @retval VERR_NO_DIGITS
1410 * @retval VERR_TRAILING_SPACES
1411 * @retval VERR_TRAILING_CHARS
1412 *
1413 * @param pszValue Pointer to the string value.
1414 * @param uBase The base of the representation used.
1415 * If 0 the function will look for known prefixes before defaulting to 10.
1416 * @param pu16 Where to store the converted number. (optional)
1417 */
1418RTDECL(int) RTStrToUInt16Full(const char *pszValue, unsigned uBase, uint16_t *pu16);
1419
1420/**
1421 * Converts a string representation of a number to a 16-bit unsigned number.
1422 * The base is guessed.
1423 *
1424 * @returns 16-bit unsigned number on success.
1425 * @returns 0 on failure.
1426 * @param pszValue Pointer to the string value.
1427 */
1428RTDECL(uint16_t) RTStrToUInt16(const char *pszValue);
1429
1430/**
1431 * Converts a string representation of a number to a 8-bit unsigned number.
1432 *
1433 * @returns iprt status code.
1434 * Warnings are used to indicate conversion problems.
1435 * @retval VWRN_NUMBER_TOO_BIG
1436 * @retval VWRN_NEGATIVE_UNSIGNED
1437 * @retval VWRN_TRAILING_CHARS
1438 * @retval VWRN_TRAILING_SPACES
1439 * @retval VINF_SUCCESS
1440 * @retval VERR_NO_DIGITS
1441 *
1442 * @param pszValue Pointer to the string value.
1443 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1444 * @param uBase The base of the representation used.
1445 * If 0 the function will look for known prefixes before defaulting to 10.
1446 * @param pu8 Where to store the converted number. (optional)
1447 */
1448RTDECL(int) RTStrToUInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint8_t *pu8);
1449
1450/**
1451 * Converts a string representation of a number to a 8-bit unsigned number,
1452 * making sure the full string is converted.
1453 *
1454 * @returns iprt status code.
1455 * Warnings are used to indicate conversion problems.
1456 * @retval VWRN_NUMBER_TOO_BIG
1457 * @retval VWRN_NEGATIVE_UNSIGNED
1458 * @retval VINF_SUCCESS
1459 * @retval VERR_NO_DIGITS
1460 * @retval VERR_TRAILING_SPACES
1461 * @retval VERR_TRAILING_CHARS
1462 *
1463 * @param pszValue Pointer to the string value.
1464 * @param uBase The base of the representation used.
1465 * If 0 the function will look for known prefixes before defaulting to 10.
1466 * @param pu8 Where to store the converted number. (optional)
1467 */
1468RTDECL(int) RTStrToUInt8Full(const char *pszValue, unsigned uBase, uint8_t *pu8);
1469
1470/**
1471 * Converts a string representation of a number to a 8-bit unsigned number.
1472 * The base is guessed.
1473 *
1474 * @returns 8-bit unsigned number on success.
1475 * @returns 0 on failure.
1476 * @param pszValue Pointer to the string value.
1477 */
1478RTDECL(uint8_t) RTStrToUInt8(const char *pszValue);
1479
1480/**
1481 * Converts a string representation of a number to a 64-bit signed number.
1482 *
1483 * @returns iprt status code.
1484 * Warnings are used to indicate conversion problems.
1485 * @retval VWRN_NUMBER_TOO_BIG
1486 * @retval VWRN_TRAILING_CHARS
1487 * @retval VWRN_TRAILING_SPACES
1488 * @retval VINF_SUCCESS
1489 * @retval VERR_NO_DIGITS
1490 *
1491 * @param pszValue Pointer to the string value.
1492 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1493 * @param uBase The base of the representation used.
1494 * If 0 the function will look for known prefixes before defaulting to 10.
1495 * @param pi64 Where to store the converted number. (optional)
1496 */
1497RTDECL(int) RTStrToInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, int64_t *pi64);
1498
1499/**
1500 * Converts a string representation of a number to a 64-bit signed number,
1501 * making sure the full string is converted.
1502 *
1503 * @returns iprt status code.
1504 * Warnings are used to indicate conversion problems.
1505 * @retval VWRN_NUMBER_TOO_BIG
1506 * @retval VINF_SUCCESS
1507 * @retval VERR_TRAILING_CHARS
1508 * @retval VERR_TRAILING_SPACES
1509 * @retval VERR_NO_DIGITS
1510 *
1511 * @param pszValue Pointer to the string value.
1512 * @param uBase The base of the representation used.
1513 * If 0 the function will look for known prefixes before defaulting to 10.
1514 * @param pi64 Where to store the converted number. (optional)
1515 */
1516RTDECL(int) RTStrToInt64Full(const char *pszValue, unsigned uBase, int64_t *pi64);
1517
1518/**
1519 * Converts a string representation of a number to a 64-bit signed number.
1520 * The base is guessed.
1521 *
1522 * @returns 64-bit signed number on success.
1523 * @returns 0 on failure.
1524 * @param pszValue Pointer to the string value.
1525 */
1526RTDECL(int64_t) RTStrToInt64(const char *pszValue);
1527
1528/**
1529 * Converts a string representation of a number to a 32-bit signed number.
1530 *
1531 * @returns iprt status code.
1532 * Warnings are used to indicate conversion problems.
1533 * @retval VWRN_NUMBER_TOO_BIG
1534 * @retval VWRN_TRAILING_CHARS
1535 * @retval VWRN_TRAILING_SPACES
1536 * @retval VINF_SUCCESS
1537 * @retval VERR_NO_DIGITS
1538 *
1539 * @param pszValue Pointer to the string value.
1540 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1541 * @param uBase The base of the representation used.
1542 * If 0 the function will look for known prefixes before defaulting to 10.
1543 * @param pi32 Where to store the converted number. (optional)
1544 */
1545RTDECL(int) RTStrToInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, int32_t *pi32);
1546
1547/**
1548 * Converts a string representation of a number to a 32-bit signed number,
1549 * making sure the full string is converted.
1550 *
1551 * @returns iprt status code.
1552 * Warnings are used to indicate conversion problems.
1553 * @retval VWRN_NUMBER_TOO_BIG
1554 * @retval VINF_SUCCESS
1555 * @retval VERR_TRAILING_CHARS
1556 * @retval VERR_TRAILING_SPACES
1557 * @retval VERR_NO_DIGITS
1558 *
1559 * @param pszValue Pointer to the string value.
1560 * @param uBase The base of the representation used.
1561 * If 0 the function will look for known prefixes before defaulting to 10.
1562 * @param pi32 Where to store the converted number. (optional)
1563 */
1564RTDECL(int) RTStrToInt32Full(const char *pszValue, unsigned uBase, int32_t *pi32);
1565
1566/**
1567 * Converts a string representation of a number to a 32-bit signed number.
1568 * The base is guessed.
1569 *
1570 * @returns 32-bit signed number on success.
1571 * @returns 0 on failure.
1572 * @param pszValue Pointer to the string value.
1573 */
1574RTDECL(int32_t) RTStrToInt32(const char *pszValue);
1575
1576/**
1577 * Converts a string representation of a number to a 16-bit signed number.
1578 *
1579 * @returns iprt status code.
1580 * Warnings are used to indicate conversion problems.
1581 * @retval VWRN_NUMBER_TOO_BIG
1582 * @retval VWRN_TRAILING_CHARS
1583 * @retval VWRN_TRAILING_SPACES
1584 * @retval VINF_SUCCESS
1585 * @retval VERR_NO_DIGITS
1586 *
1587 * @param pszValue Pointer to the string value.
1588 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1589 * @param uBase The base of the representation used.
1590 * If 0 the function will look for known prefixes before defaulting to 10.
1591 * @param pi16 Where to store the converted number. (optional)
1592 */
1593RTDECL(int) RTStrToInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, int16_t *pi16);
1594
1595/**
1596 * Converts a string representation of a number to a 16-bit signed number,
1597 * making sure the full string is converted.
1598 *
1599 * @returns iprt status code.
1600 * Warnings are used to indicate conversion problems.
1601 * @retval VWRN_NUMBER_TOO_BIG
1602 * @retval VINF_SUCCESS
1603 * @retval VERR_TRAILING_CHARS
1604 * @retval VERR_TRAILING_SPACES
1605 * @retval VERR_NO_DIGITS
1606 *
1607 * @param pszValue Pointer to the string value.
1608 * @param uBase The base of the representation used.
1609 * If 0 the function will look for known prefixes before defaulting to 10.
1610 * @param pi16 Where to store the converted number. (optional)
1611 */
1612RTDECL(int) RTStrToInt16Full(const char *pszValue, unsigned uBase, int16_t *pi16);
1613
1614/**
1615 * Converts a string representation of a number to a 16-bit signed number.
1616 * The base is guessed.
1617 *
1618 * @returns 16-bit signed number on success.
1619 * @returns 0 on failure.
1620 * @param pszValue Pointer to the string value.
1621 */
1622RTDECL(int16_t) RTStrToInt16(const char *pszValue);
1623
1624/**
1625 * Converts a string representation of a number to a 8-bit signed number.
1626 *
1627 * @returns iprt status code.
1628 * Warnings are used to indicate conversion problems.
1629 * @retval VWRN_NUMBER_TOO_BIG
1630 * @retval VWRN_TRAILING_CHARS
1631 * @retval VWRN_TRAILING_SPACES
1632 * @retval VINF_SUCCESS
1633 * @retval VERR_NO_DIGITS
1634 *
1635 * @param pszValue Pointer to the string value.
1636 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1637 * @param uBase The base of the representation used.
1638 * If 0 the function will look for known prefixes before defaulting to 10.
1639 * @param pi8 Where to store the converted number. (optional)
1640 */
1641RTDECL(int) RTStrToInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, int8_t *pi8);
1642
1643/**
1644 * Converts a string representation of a number to a 8-bit signed number,
1645 * making sure the full string is converted.
1646 *
1647 * @returns iprt status code.
1648 * Warnings are used to indicate conversion problems.
1649 * @retval VWRN_NUMBER_TOO_BIG
1650 * @retval VINF_SUCCESS
1651 * @retval VERR_TRAILING_CHARS
1652 * @retval VERR_TRAILING_SPACES
1653 * @retval VERR_NO_DIGITS
1654 *
1655 * @param pszValue Pointer to the string value.
1656 * @param uBase The base of the representation used.
1657 * If 0 the function will look for known prefixes before defaulting to 10.
1658 * @param pi8 Where to store the converted number. (optional)
1659 */
1660RTDECL(int) RTStrToInt8Full(const char *pszValue, unsigned uBase, int8_t *pi8);
1661
1662/**
1663 * Converts a string representation of a number to a 8-bit signed number.
1664 * The base is guessed.
1665 *
1666 * @returns 8-bit signed number on success.
1667 * @returns 0 on failure.
1668 * @param pszValue Pointer to the string value.
1669 */
1670RTDECL(int8_t) RTStrToInt8(const char *pszValue);
1671
1672/**
1673 * Formats a buffer stream as hex bytes.
1674 *
1675 * The default is no separating spaces or line breaks or anything.
1676 *
1677 * @returns IPRT status code.
1678 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
1679 * @retval VERR_BUFFER_OVERFLOW if the buffer is insufficent to hold the bytes.
1680 *
1681 * @param pszBuf Output string buffer.
1682 * @param cchBuf The size of the output buffer.
1683 * @param pv Pointer to the bytes to stringify.
1684 * @param cb The number of bytes to stringify.
1685 * @param fFlags Must be zero, reserved for future use.
1686 */
1687RTDECL(int) RTStrPrintHexBytes(char *pszBuf, size_t cchBuf, void const *pv, size_t cb, uint32_t fFlags);
1688
1689/**
1690 * Converts a string of hex bytes back into binary data.
1691 *
1692 * @returns IPRT status code.
1693 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
1694 * @retval VERR_BUFFER_OVERFLOW if the string contains too many hex bytes.
1695 * @retval VERR_BUFFER_UNDERFLOW if there aren't enough hex bytes to fill up
1696 * the output buffer.
1697 * @retval VERR_UNEVEN_INPUT if the input contains a half byte.
1698 * @retval VERR_NO_DIGITS
1699 * @retval VWRN_TRAILING_CHARS
1700 * @retval VWRN_TRAILING_SPACES
1701 *
1702 * @param pszHex The string containing the hex bytes.
1703 * @param pv Output buffer.
1704 * @param cb The size of the output buffer.
1705 * @param fFlags Must be zero, reserved for future use.
1706 */
1707RTDECL(int) RTStrConvertHexBytes(char const *pszHex, void *pv, size_t cb, uint32_t fFlags);
1708
1709/** @} */
1710
1711
1712/** @defgroup rt_str_space Unique String Space
1713 * @ingroup grp_rt_str
1714 * @{
1715 */
1716
1717/** Pointer to a string name space container node core. */
1718typedef struct RTSTRSPACECORE *PRTSTRSPACECORE;
1719/** Pointer to a pointer to a string name space container node core. */
1720typedef PRTSTRSPACECORE *PPRTSTRSPACECORE;
1721
1722/**
1723 * String name space container node core.
1724 */
1725typedef struct RTSTRSPACECORE
1726{
1727 /** Hash key. Don't touch. */
1728 uint32_t Key;
1729 /** Pointer to the left leaf node. Don't touch. */
1730 PRTSTRSPACECORE pLeft;
1731 /** Pointer to the left rigth node. Don't touch. */
1732 PRTSTRSPACECORE pRight;
1733 /** Pointer to the list of string with the same key. Don't touch. */
1734 PRTSTRSPACECORE pList;
1735 /** Height of this tree: max(heigth(left), heigth(right)) + 1. Don't touch */
1736 unsigned char uchHeight;
1737 /** The string length. Read only! */
1738 size_t cchString;
1739 /** Pointer to the string. Read only! */
1740 const char *pszString;
1741} RTSTRSPACECORE;
1742
1743/** String space. (Initialize with NULL.) */
1744typedef PRTSTRSPACECORE RTSTRSPACE;
1745/** Pointer to a string space. */
1746typedef PPRTSTRSPACECORE PRTSTRSPACE;
1747
1748
1749/**
1750 * Inserts a string into a unique string space.
1751 *
1752 * @returns true on success.
1753 * @returns false if the string collided with an existing string.
1754 * @param pStrSpace The space to insert it into.
1755 * @param pStr The string node.
1756 */
1757RTDECL(bool) RTStrSpaceInsert(PRTSTRSPACE pStrSpace, PRTSTRSPACECORE pStr);
1758
1759/**
1760 * Removes a string from a unique string space.
1761 *
1762 * @returns Pointer to the removed string node.
1763 * @returns NULL if the string was not found in the string space.
1764 * @param pStrSpace The space to insert it into.
1765 * @param pszString The string to remove.
1766 */
1767RTDECL(PRTSTRSPACECORE) RTStrSpaceRemove(PRTSTRSPACE pStrSpace, const char *pszString);
1768
1769/**
1770 * Gets a string from a unique string space.
1771 *
1772 * @returns Pointer to the string node.
1773 * @returns NULL if the string was not found in the string space.
1774 * @param pStrSpace The space to insert it into.
1775 * @param pszString The string to get.
1776 */
1777RTDECL(PRTSTRSPACECORE) RTStrSpaceGet(PRTSTRSPACE pStrSpace, const char *pszString);
1778
1779/**
1780 * Callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy().
1781 *
1782 * @returns 0 on continue.
1783 * @returns Non-zero to aborts the operation.
1784 * @param pStr The string node
1785 * @param pvUser The user specified argument.
1786 */
1787typedef DECLCALLBACK(int) FNRTSTRSPACECALLBACK(PRTSTRSPACECORE pStr, void *pvUser);
1788/** Pointer to callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy(). */
1789typedef FNRTSTRSPACECALLBACK *PFNRTSTRSPACECALLBACK;
1790
1791/**
1792 * Destroys the string space.
1793 * The caller supplies a callback which will be called for each of
1794 * the string nodes in for freeing their memory and other resources.
1795 *
1796 * @returns 0 or what ever non-zero return value pfnCallback returned
1797 * when aborting the destruction.
1798 * @param pStrSpace The space to insert it into.
1799 * @param pfnCallback The callback.
1800 * @param pvUser The user argument.
1801 */
1802RTDECL(int) RTStrSpaceDestroy(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
1803
1804/**
1805 * Enumerates the string space.
1806 * The caller supplies a callback which will be called for each of
1807 * the string nodes.
1808 *
1809 * @returns 0 or what ever non-zero return value pfnCallback returned
1810 * when aborting the destruction.
1811 * @param pStrSpace The space to insert it into.
1812 * @param pfnCallback The callback.
1813 * @param pvUser The user argument.
1814 */
1815RTDECL(int) RTStrSpaceEnumerate(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
1816
1817/** @} */
1818
1819
1820/** @defgroup rt_str_utf16 UTF-16 String Manipulation
1821 * @ingroup grp_rt_str
1822 * @{
1823 */
1824
1825/**
1826 * Free a UTF-16 string allocated by RTStrToUtf16(), RTStrToUtf16Ex(),
1827 * RTLatin1ToUtf16(), RTLatin1ToUtf16Ex(), RTUtf16Dup() or RTUtf16DupEx().
1828 *
1829 * @returns iprt status code.
1830 * @param pwszString The UTF-16 string to free. NULL is accepted.
1831 */
1832RTDECL(void) RTUtf16Free(PRTUTF16 pwszString);
1833
1834/**
1835 * Allocates a new copy of the specified UTF-16 string.
1836 *
1837 * @returns Pointer to the allocated string copy. Use RTUtf16Free() to free it.
1838 * @returns NULL when out of memory.
1839 * @param pwszString UTF-16 string to duplicate.
1840 * @remark This function will not make any attempt to validate the encoding.
1841 */
1842RTDECL(PRTUTF16) RTUtf16Dup(PCRTUTF16 pwszString);
1843
1844/**
1845 * Allocates a new copy of the specified UTF-16 string.
1846 *
1847 * @returns iprt status code.
1848 * @param ppwszString Receives pointer of the allocated UTF-16 string.
1849 * The returned pointer must be freed using RTUtf16Free().
1850 * @param pwszString UTF-16 string to duplicate.
1851 * @param cwcExtra Number of extra RTUTF16 items to allocate.
1852 * @remark This function will not make any attempt to validate the encoding.
1853 */
1854RTDECL(int) RTUtf16DupEx(PRTUTF16 *ppwszString, PCRTUTF16 pwszString, size_t cwcExtra);
1855
1856/**
1857 * Returns the length of a UTF-16 string in UTF-16 characters
1858 * without trailing '\\0'.
1859 *
1860 * Surrogate pairs counts as two UTF-16 characters here. Use RTUtf16CpCnt()
1861 * to get the exact number of code points in the string.
1862 *
1863 * @returns The number of RTUTF16 items in the string.
1864 * @param pwszString Pointer the UTF-16 string.
1865 * @remark This function will not make any attempt to validate the encoding.
1866 */
1867RTDECL(size_t) RTUtf16Len(PCRTUTF16 pwszString);
1868
1869/**
1870 * Performs a case sensitive string compare between two UTF-16 strings.
1871 *
1872 * @returns < 0 if the first string less than the second string.s
1873 * @returns 0 if the first string identical to the second string.
1874 * @returns > 0 if the first string greater than the second string.
1875 * @param pwsz1 First UTF-16 string. Null is allowed.
1876 * @param pwsz2 Second UTF-16 string. Null is allowed.
1877 * @remark This function will not make any attempt to validate the encoding.
1878 */
1879RTDECL(int) RTUtf16Cmp(register PCRTUTF16 pwsz1, register PCRTUTF16 pwsz2);
1880
1881/**
1882 * Performs a case insensitive string compare between two UTF-16 strings.
1883 *
1884 * This is a simplified compare, as only the simplified lower/upper case folding
1885 * specified by the unicode specs are used. It does not consider character pairs
1886 * as they are used in some languages, just simple upper & lower case compares.
1887 *
1888 * @returns < 0 if the first string less than the second string.
1889 * @returns 0 if the first string identical to the second string.
1890 * @returns > 0 if the first string greater than the second string.
1891 * @param pwsz1 First UTF-16 string. Null is allowed.
1892 * @param pwsz2 Second UTF-16 string. Null is allowed.
1893 */
1894RTDECL(int) RTUtf16ICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
1895
1896/**
1897 * Performs a case insensitive string compare between two UTF-16 strings
1898 * using the current locale of the process (if applicable).
1899 *
1900 * This differs from RTUtf16ICmp() in that it will try, if a locale with the
1901 * required data is available, to do a correct case-insensitive compare. It
1902 * follows that it is more complex and thereby likely to be more expensive.
1903 *
1904 * @returns < 0 if the first string less than the second string.
1905 * @returns 0 if the first string identical to the second string.
1906 * @returns > 0 if the first string greater than the second string.
1907 * @param pwsz1 First UTF-16 string. Null is allowed.
1908 * @param pwsz2 Second UTF-16 string. Null is allowed.
1909 */
1910RTDECL(int) RTUtf16LocaleICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
1911
1912/**
1913 * Folds a UTF-16 string to lowercase.
1914 *
1915 * This is a very simple folding; is uses the simple lowercase
1916 * code point, it is not related to any locale just the most common
1917 * lowercase codepoint setup by the unicode specs, and it will not
1918 * create new surrogate pairs or remove existing ones.
1919 *
1920 * @returns Pointer to the passed in string.
1921 * @param pwsz The string to fold.
1922 */
1923RTDECL(PRTUTF16) RTUtf16ToLower(PRTUTF16 pwsz);
1924
1925/**
1926 * Folds a UTF-16 string to uppercase.
1927 *
1928 * This is a very simple folding; is uses the simple uppercase
1929 * code point, it is not related to any locale just the most common
1930 * uppercase codepoint setup by the unicode specs, and it will not
1931 * create new surrogate pairs or remove existing ones.
1932 *
1933 * @returns Pointer to the passed in string.
1934 * @param pwsz The string to fold.
1935 */
1936RTDECL(PRTUTF16) RTUtf16ToUpper(PRTUTF16 pwsz);
1937
1938/**
1939 * Translate a UTF-16 string into a UTF-8 allocating the result buffer.
1940 *
1941 * @returns iprt status code.
1942 * @param pwszString UTF-16 string to convert.
1943 * @param ppszString Receives pointer of allocated UTF-8 string on
1944 * success, and is always set to NULL on failure.
1945 * The returned pointer must be freed using RTStrFree().
1946 */
1947RTDECL(int) RTUtf16ToUtf8(PCRTUTF16 pwszString, char **ppszString);
1948
1949/**
1950 * Translates UTF-16 to UTF-8 using buffer provided by the caller or
1951 * a fittingly sized buffer allocated by the function.
1952 *
1953 * @returns iprt status code.
1954 * @param pwszString The UTF-16 string to convert.
1955 * @param cwcString The number of RTUTF16 items to translate from pwszString.
1956 * The translation will stop when reaching cwcString or the terminator ('\\0').
1957 * Use RTSTR_MAX to translate the entire string.
1958 * @param ppsz If cch is non-zero, this must either be pointing to a pointer to
1959 * a buffer of the specified size, or pointer to a NULL pointer.
1960 * If *ppsz is NULL or cch is zero a buffer of at least cch chars
1961 * will be allocated to hold the translated string.
1962 * If a buffer was requested it must be freed using RTUtf16Free().
1963 * @param cch The buffer size in chars (the type). This includes the terminator.
1964 * @param pcch Where to store the length of the translated string,
1965 * excluding the terminator. (Optional)
1966 *
1967 * This may be set under some error conditions,
1968 * however, only for VERR_BUFFER_OVERFLOW and
1969 * VERR_NO_STR_MEMORY will it contain a valid string
1970 * length that can be used to resize the buffer.
1971 */
1972RTDECL(int) RTUtf16ToUtf8Ex(PCRTUTF16 pwszString, size_t cwcString, char **ppsz, size_t cch, size_t *pcch);
1973
1974/**
1975 * Calculates the length of the UTF-16 string in UTF-8 chars (bytes).
1976 *
1977 * This function will validate the string, and incorrectly encoded UTF-16
1978 * strings will be rejected. The primary purpose of this function is to
1979 * help allocate buffers for RTUtf16ToUtf8() of the correct size. For most
1980 * other purposes RTUtf16ToUtf8Ex() should be used.
1981 *
1982 * @returns Number of char (bytes).
1983 * @returns 0 if the string was incorrectly encoded.
1984 * @param pwsz The UTF-16 string.
1985 */
1986RTDECL(size_t) RTUtf16CalcUtf8Len(PCRTUTF16 pwsz);
1987
1988/**
1989 * Calculates the length of the UTF-16 string in UTF-8 chars (bytes).
1990 *
1991 * This function will validate the string, and incorrectly encoded UTF-16
1992 * strings will be rejected.
1993 *
1994 * @returns iprt status code.
1995 * @param pwsz The string.
1996 * @param cwc The max string length. Use RTSTR_MAX to process the entire string.
1997 * @param pcch Where to store the string length (in bytes). Optional.
1998 * This is undefined on failure.
1999 */
2000RTDECL(int) RTUtf16CalcUtf8LenEx(PCRTUTF16 pwsz, size_t cwc, size_t *pcch);
2001
2002/**
2003 * Translate a UTF-16 string into a Latin-1 (ISO-8859-1) allocating the result
2004 * buffer.
2005 *
2006 * @returns iprt status code.
2007 * @param pwszString UTF-16 string to convert.
2008 * @param ppszString Receives pointer of allocated Latin1 string on
2009 * success, and is always set to NULL on failure.
2010 * The returned pointer must be freed using RTStrFree().
2011 */
2012RTDECL(int) RTUtf16ToLatin1(PCRTUTF16 pwszString, char **ppszString);
2013
2014/**
2015 * Translates UTF-16 to Latin-1 (ISO-8859-1) using buffer provided by the caller
2016 * or a fittingly sized buffer allocated by the function.
2017 *
2018 * @returns iprt status code.
2019 * @param pwszString The UTF-16 string to convert.
2020 * @param cwcString The number of RTUTF16 items to translate from
2021 * pwszString. The translation will stop when reaching
2022 * cwcString or the terminator ('\\0'). Use RTSTR_MAX
2023 * to translate the entire string.
2024 * @param ppsz Pointer to the pointer to the Latin-1 string. The
2025 * buffer can optionally be preallocated by the caller.
2026 *
2027 * If cch is zero, *ppsz is undefined.
2028 *
2029 * If cch is non-zero and *ppsz is not NULL, then this
2030 * will be used as the output buffer.
2031 * VERR_BUFFER_OVERFLOW will be returned if this is
2032 * insufficient.
2033 *
2034 * If cch is zero or *ppsz is NULL, then a buffer of
2035 * sufficent size is allocated. cch can be used to
2036 * specify a minimum size of this buffer. Use
2037 * RTUtf16Free() to free the result.
2038 *
2039 * @param cch The buffer size in chars (the type). This includes
2040 * the terminator.
2041 * @param pcch Where to store the length of the translated string,
2042 * excluding the terminator. (Optional)
2043 *
2044 * This may be set under some error conditions,
2045 * however, only for VERR_BUFFER_OVERFLOW and
2046 * VERR_NO_STR_MEMORY will it contain a valid string
2047 * length that can be used to resize the buffer.
2048 */
2049RTDECL(int) RTUtf16ToLatin1Ex(PCRTUTF16 pwszString, size_t cwcString, char **ppsz, size_t cch, size_t *pcch);
2050
2051/**
2052 * Calculates the length of the UTF-16 string in Latin-1 (ISO-8859-1) chars.
2053 *
2054 * This function will validate the string, and incorrectly encoded UTF-16
2055 * strings will be rejected. The primary purpose of this function is to
2056 * help allocate buffers for RTUtf16ToLatin1() of the correct size. For most
2057 * other purposes RTUtf16ToLatin1Ex() should be used.
2058 *
2059 * @returns Number of char (bytes).
2060 * @returns 0 if the string was incorrectly encoded.
2061 * @param pwsz The UTF-16 string.
2062 */
2063RTDECL(size_t) RTUtf16CalcLatin1Len(PCRTUTF16 pwsz);
2064
2065/**
2066 * Calculates the length of the UTF-16 string in Latin-1 (ISO-8859-1) chars.
2067 *
2068 * This function will validate the string, and incorrectly encoded UTF-16
2069 * strings will be rejected.
2070 *
2071 * @returns iprt status code.
2072 * @param pwsz The string.
2073 * @param cwc The max string length. Use RTSTR_MAX to process the
2074 * entire string.
2075 * @param pcch Where to store the string length (in bytes). Optional.
2076 * This is undefined on failure.
2077 */
2078RTDECL(int) RTUtf16CalcLatin1LenEx(PCRTUTF16 pwsz, size_t cwc, size_t *pcch);
2079
2080/**
2081 * Get the unicode code point at the given string position.
2082 *
2083 * @returns unicode code point.
2084 * @returns RTUNICP_INVALID if the encoding is invalid.
2085 * @param pwsz The string.
2086 *
2087 * @remark This is an internal worker for RTUtf16GetCp().
2088 */
2089RTDECL(RTUNICP) RTUtf16GetCpInternal(PCRTUTF16 pwsz);
2090
2091/**
2092 * Get the unicode code point at the given string position.
2093 *
2094 * @returns iprt status code.
2095 * @param ppwsz Pointer to the string pointer. This will be updated to
2096 * point to the char following the current code point.
2097 * @param pCp Where to store the code point.
2098 * RTUNICP_INVALID is stored here on failure.
2099 *
2100 * @remark This is an internal worker for RTUtf16GetCpEx().
2101 */
2102RTDECL(int) RTUtf16GetCpExInternal(PCRTUTF16 *ppwsz, PRTUNICP pCp);
2103
2104/**
2105 * Put the unicode code point at the given string position
2106 * and return the pointer to the char following it.
2107 *
2108 * This function will not consider anything at or following the
2109 * buffer area pointed to by pwsz. It is therefore not suitable for
2110 * inserting code points into a string, only appending/overwriting.
2111 *
2112 * @returns pointer to the char following the written code point.
2113 * @param pwsz The string.
2114 * @param CodePoint The code point to write.
2115 * This should not be RTUNICP_INVALID or any other
2116 * character out of the UTF-16 range.
2117 *
2118 * @remark This is an internal worker for RTUtf16GetCpEx().
2119 */
2120RTDECL(PRTUTF16) RTUtf16PutCpInternal(PRTUTF16 pwsz, RTUNICP CodePoint);
2121
2122/**
2123 * Get the unicode code point at the given string position.
2124 *
2125 * @returns unicode code point.
2126 * @returns RTUNICP_INVALID if the encoding is invalid.
2127 * @param pwsz The string.
2128 *
2129 * @remark We optimize this operation by using an inline function for
2130 * everything which isn't a surrogate pair or an endian indicator.
2131 */
2132DECLINLINE(RTUNICP) RTUtf16GetCp(PCRTUTF16 pwsz)
2133{
2134 const RTUTF16 wc = *pwsz;
2135 if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
2136 return wc;
2137 return RTUtf16GetCpInternal(pwsz);
2138}
2139
2140/**
2141 * Get the unicode code point at the given string position.
2142 *
2143 * @returns iprt status code.
2144 * @param ppwsz Pointer to the string pointer. This will be updated to
2145 * point to the char following the current code point.
2146 * @param pCp Where to store the code point.
2147 * RTUNICP_INVALID is stored here on failure.
2148 *
2149 * @remark We optimize this operation by using an inline function for
2150 * everything which isn't a surrogate pair or and endian indicator.
2151 */
2152DECLINLINE(int) RTUtf16GetCpEx(PCRTUTF16 *ppwsz, PRTUNICP pCp)
2153{
2154 const RTUTF16 wc = **ppwsz;
2155 if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
2156 {
2157 (*ppwsz)++;
2158 *pCp = wc;
2159 return VINF_SUCCESS;
2160 }
2161 return RTUtf16GetCpExInternal(ppwsz, pCp);
2162}
2163
2164/**
2165 * Put the unicode code point at the given string position
2166 * and return the pointer to the char following it.
2167 *
2168 * This function will not consider anything at or following the
2169 * buffer area pointed to by pwsz. It is therefore not suitable for
2170 * inserting code points into a string, only appending/overwriting.
2171 *
2172 * @returns pointer to the char following the written code point.
2173 * @param pwsz The string.
2174 * @param CodePoint The code point to write.
2175 * This should not be RTUNICP_INVALID or any other
2176 * character out of the UTF-16 range.
2177 *
2178 * @remark We optimize this operation by using an inline function for
2179 * everything which isn't a surrogate pair or and endian indicator.
2180 */
2181DECLINLINE(PRTUTF16) RTUtf16PutCp(PRTUTF16 pwsz, RTUNICP CodePoint)
2182{
2183 if (CodePoint < 0xd800 || (CodePoint > 0xd800 && CodePoint < 0xfffe))
2184 {
2185 *pwsz++ = (RTUTF16)CodePoint;
2186 return pwsz;
2187 }
2188 return RTUtf16PutCpInternal(pwsz, CodePoint);
2189}
2190
2191/**
2192 * Skips ahead, past the current code point.
2193 *
2194 * @returns Pointer to the char after the current code point.
2195 * @param pwsz Pointer to the current code point.
2196 * @remark This will not move the next valid code point, only past the current one.
2197 */
2198DECLINLINE(PRTUTF16) RTUtf16NextCp(PCRTUTF16 pwsz)
2199{
2200 RTUNICP Cp;
2201 RTUtf16GetCpEx(&pwsz, &Cp);
2202 return (PRTUTF16)pwsz;
2203}
2204
2205/**
2206 * Skips backwards, to the previous code point.
2207 *
2208 * @returns Pointer to the char after the current code point.
2209 * @param pwszStart Pointer to the start of the string.
2210 * @param pwsz Pointer to the current code point.
2211 */
2212RTDECL(PRTUTF16) RTUtf16PrevCp(PCRTUTF16 pwszStart, PCRTUTF16 pwsz);
2213
2214
2215/**
2216 * Checks if the UTF-16 char is the high surrogate char (i.e.
2217 * the 1st char in the pair).
2218 *
2219 * @returns true if it is.
2220 * @returns false if it isn't.
2221 * @param wc The character to investigate.
2222 */
2223DECLINLINE(bool) RTUtf16IsHighSurrogate(RTUTF16 wc)
2224{
2225 return wc >= 0xd800 && wc <= 0xdbff;
2226}
2227
2228/**
2229 * Checks if the UTF-16 char is the low surrogate char (i.e.
2230 * the 2nd char in the pair).
2231 *
2232 * @returns true if it is.
2233 * @returns false if it isn't.
2234 * @param wc The character to investigate.
2235 */
2236DECLINLINE(bool) RTUtf16IsLowSurrogate(RTUTF16 wc)
2237{
2238 return wc >= 0xdc00 && wc <= 0xdfff;
2239}
2240
2241
2242/**
2243 * Checks if the two UTF-16 chars form a valid surrogate pair.
2244 *
2245 * @returns true if they do.
2246 * @returns false if they doesn't.
2247 * @param wcHigh The high (1st) character.
2248 * @param wcLow The low (2nd) character.
2249 */
2250DECLINLINE(bool) RTUtf16IsSurrogatePair(RTUTF16 wcHigh, RTUTF16 wcLow)
2251{
2252 return RTUtf16IsHighSurrogate(wcHigh)
2253 && RTUtf16IsLowSurrogate(wcLow);
2254}
2255
2256/** @} */
2257
2258
2259/** @defgroup rt_str_latin1 Latin-1 (ISO-8859-1) String Manipulation
2260 * @ingroup grp_rt_str
2261 * @{
2262 */
2263
2264/**
2265 * Calculates the length of the Latin-1 (ISO-8859-1) string in RTUTF16 items.
2266 *
2267 * @returns Number of RTUTF16 items.
2268 * @param psz The Latin-1 string.
2269 */
2270RTDECL(size_t) RTLatin1CalcUtf16Len(const char *psz);
2271
2272/**
2273 * Calculates the length of the Latin-1 (ISO-8859-1) string in RTUTF16 items.
2274 *
2275 * @returns iprt status code.
2276 * @param psz The Latin-1 string.
2277 * @param cch The max string length. Use RTSTR_MAX to process the
2278 * entire string.
2279 * @param pcwc Where to store the string length. Optional.
2280 * This is undefined on failure.
2281 */
2282RTDECL(int) RTLatin1CalcUtf16LenEx(const char *psz, size_t cch, size_t *pcwc);
2283
2284/**
2285 * Translate a Latin-1 (ISO-8859-1) string into a UTF-16 allocating the result
2286 * buffer.
2287 *
2288 * @returns iprt status code.
2289 * @param pszString The Latin-1 string to convert.
2290 * @param ppwszString Receives pointer to the allocated UTF-16 string. The
2291 * returned string must be freed using RTUtf16Free().
2292 */
2293RTDECL(int) RTLatin1ToUtf16(const char *pszString, PRTUTF16 *ppwszString);
2294
2295/**
2296 * Translates pszString from Latin-1 (ISO-8859-1) to UTF-16, allocating the
2297 * result buffer if requested.
2298 *
2299 * @returns iprt status code.
2300 * @param pszString The Latin-1 string to convert.
2301 * @param cchString The maximum size in chars (the type) to convert.
2302 * The conversion stops when it reaches cchString or
2303 * the string terminator ('\\0').
2304 * Use RTSTR_MAX to translate the entire string.
2305 * @param ppwsz If cwc is non-zero, this must either be pointing
2306 * to pointer to a buffer of the specified size, or
2307 * pointer to a NULL pointer.
2308 * If *ppwsz is NULL or cwc is zero a buffer of at
2309 * least cwc items will be allocated to hold the
2310 * translated string. If a buffer was requested it
2311 * must be freed using RTUtf16Free().
2312 * @param cwc The buffer size in RTUTF16s. This includes the
2313 * terminator.
2314 * @param pcwc Where to store the length of the translated string,
2315 * excluding the terminator. (Optional)
2316 *
2317 * This may be set under some error conditions,
2318 * however, only for VERR_BUFFER_OVERFLOW and
2319 * VERR_NO_STR_MEMORY will it contain a valid string
2320 * length that can be used to resize the buffer.
2321 */
2322RTDECL(int) RTLatin1ToUtf16Ex(const char *pszString, size_t cchString, PRTUTF16 *ppwsz, size_t cwc, size_t *pcwc);
2323
2324/** @} */
2325
2326
2327RT_C_DECLS_END
2328
2329/** @} */
2330
2331#endif
2332
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