VirtualBox

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

Last change on this file since 24025 was 23631, checked in by vboxsync, 15 years ago

string.h: Unconditionally define memmove on FreeBSD. FreeBSD_version is not defined during test builds

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