VirtualBox

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

Last change on this file since 29273 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

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