VirtualBox

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

Last change on this file since 26201 was 25014, checked in by vboxsync, 15 years ago

RTStrVersionCompare: made it return the same as RTStrICmp (-1,0,1).

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