VirtualBox

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

Last change on this file since 25003 was 24894, checked in by vboxsync, 15 years ago

IPRT: Removed outdated todos.

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