VirtualBox

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

Last change on this file since 22958 was 22736, checked in by vboxsync, 15 years ago

IPRT: Added RTStrDupN.

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