VirtualBox

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

Last change on this file since 24817 was 24639, checked in by vboxsync, 15 years ago

IPRT: Added RTStrVersionToUInt32() with testcase.

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

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