VirtualBox

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

Last change on this file since 10518 was 10106, checked in by vboxsync, 16 years ago

Added RTStrValidateEncoding, RTStrValidateEncodingEx and RTStrIsValidEncoding for explicit UTF-8 validation.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 59.0 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/systm.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)
68__BEGIN_DECLS
69void *memchr(const void *pv, int ch, size_t cb);
70char *strpbrk(const char *pszStr, const char *pszChars);
71__END_DECLS
72#endif
73
74
75/** @defgroup grp_rt_str RTStr - String Manipulation
76 * Mostly UTF-8 related helpers where the standard string functions won't do.
77 * @ingroup grp_rt
78 * @{
79 */
80
81__BEGIN_DECLS
82
83
84/**
85 * The maximum string length.
86 */
87#define RTSTR_MAX (~(size_t)0)
88
89
90#ifdef IN_RING3
91
92/**
93 * Allocates tmp buffer, translates pszString from UTF8 to current codepage.
94 *
95 * @returns iprt status code.
96 * @param ppszString Receives pointer of allocated native CP string.
97 * The returned pointer must be freed using RTStrFree().
98 * @param pszString UTF-8 string to convert.
99 */
100RTR3DECL(int) RTStrUtf8ToCurrentCP(char **ppszString, const char *pszString);
101
102/**
103 * Allocates tmp buffer, translates pszString from current codepage to UTF-8.
104 *
105 * @returns iprt status code.
106 * @param ppszString Receives pointer of allocated UTF-8 string.
107 * The returned pointer must be freed using RTStrFree().
108 * @param pszString Native string to convert.
109 */
110RTR3DECL(int) RTStrCurrentCPToUtf8(char **ppszString, const char *pszString);
111
112#endif
113
114/**
115 * Free string allocated by any of the non-UCS-2 string functions.
116 *
117 * @returns iprt status code.
118 * @param pszString Pointer to buffer with string to free.
119 * NULL is accepted.
120 */
121RTDECL(void) RTStrFree(char *pszString);
122
123/**
124 * Allocates a new copy of the given UTF-8 string.
125 *
126 * @returns Pointer to the allocated UTF-8 string.
127 * @param pszString UTF-8 string to duplicate.
128 */
129RTDECL(char *) RTStrDup(const char *pszString);
130
131/**
132 * Allocates a new copy of the given UTF-8 string.
133 *
134 * @returns iprt status code.
135 * @param ppszString Receives pointer of the allocated UTF-8 string.
136 * The returned pointer must be freed using RTStrFree().
137 * @param pszString UTF-8 string to duplicate.
138 */
139RTDECL(int) RTStrDupEx(char **ppszString, const char *pszString);
140
141/**
142 * Validates the UTF-8 encoding of the string.
143 *
144 * @returns iprt status code.
145 * @param psz The string.
146 */
147RTDECL(int) RTStrValidateEncoding(const char *psz);
148
149/**
150 * Validates the UTF-8 encoding of the string.
151 *
152 * @returns iprt status code.
153 * @param psz The string.
154 * @param cch The max string length. Use RTSTR_MAX to process the entire string.
155 * @param fFlags Teserved for future. Pass 0.
156 */
157RTDECL(int) RTStrValidateEncodingEx(const char *psz, size_t cch, unsigned fFlags);
158
159/**
160 * Checks if the UTF-8 encoding is valid.
161 *
162 * @returns true / false.
163 * @param psz The string.
164 */
165RTDECL(bool) RTStrIsValidEncoding(const char *psz);
166
167/**
168 * Gets the number of code points the string is made up of, excluding
169 * the terminator.
170 *
171 *
172 * @returns Number of code points (RTUNICP).
173 * @returns 0 if the string was incorrectly encoded.
174 * @param psz The string.
175 */
176RTDECL(size_t) RTStrUniLen(const char *psz);
177
178/**
179 * Gets the number of code points the string is made up of, excluding
180 * the terminator.
181 *
182 * This function will validate the string, and incorrectly encoded UTF-8
183 * strings will be rejected.
184 *
185 * @returns iprt status code.
186 * @param psz The string.
187 * @param cch The max string length. Use RTSTR_MAX to process the entire string.
188 * @param pcuc Where to store the code point count.
189 * This is undefined on failure.
190 */
191RTDECL(int) RTStrUniLenEx(const char *psz, size_t cch, size_t *pcuc);
192
193/**
194 * Translate a UTF-8 string into an unicode string (i.e. RTUNICPs), allocating the string buffer.
195 *
196 * @returns iprt status code.
197 * @param pszString UTF-8 string to convert.
198 * @param ppUniString Receives pointer to the allocated unicode string.
199 * The returned string must be freed using RTUniFree().
200 */
201RTDECL(int) RTStrToUni(const char *pszString, PRTUNICP *ppUniString);
202
203/**
204 * Translates pszString from UTF-8 to an array of code points, allocating the result
205 * array if requested.
206 *
207 * @returns iprt status code.
208 * @param pszString UTF-8 string to convert.
209 * @param cchString The maximum size in chars (the type) to convert. The conversion stop
210 * when it reaches cchString or the string terminator ('\\0').
211 * Use RTSTR_MAX to translate the entire string.
212 * @param ppaCps If cCps is non-zero, this must either be pointing to pointer to
213 * a buffer of the specified size, or pointer to a NULL pointer.
214 * If *ppusz is NULL or cCps is zero a buffer of at least cCps items
215 * will be allocated to hold the translated string.
216 * If a buffer was requirest it must be freed using RTUtf16Free().
217 * @param cCps The number of code points in the unicode string. This includes the terminator.
218 * @param pcCps Where to store the length of the translated string. (Optional)
219 * This field will be updated even on failure, however the value is only
220 * specified for the following two error codes. On VERR_BUFFER_OVERFLOW
221 * and VERR_NO_STR_MEMORY it contains the required buffer space.
222 */
223RTDECL(int) RTStrToUniEx(const char *pszString, size_t cchString, PRTUNICP *ppaCps, size_t cCps, size_t *pcCps);
224
225/**
226 * Calculates the length of the string in RTUTF16 items.
227 *
228 * This function will validate the string, and incorrectly encoded UTF-8
229 * strings will be rejected. The primary purpose of this function is to
230 * help allocate buffers for RTStrToUtf16Ex of the correct size. For most
231 * other puroses RTStrCalcUtf16LenEx() should be used.
232 *
233 * @returns Number of RTUTF16 items.
234 * @returns 0 if the string was incorrectly encoded.
235 * @param psz The string.
236 */
237RTDECL(size_t) RTStrCalcUtf16Len(const char *psz);
238
239/**
240 * Calculates the length of the string in RTUTF16 items.
241 *
242 * This function will validate the string, and incorrectly encoded UTF-8
243 * strings will be rejected.
244 *
245 * @returns iprt status code.
246 * @param psz The string.
247 * @param cch The max string length. Use RTSTR_MAX to process the entire string.
248 * @param pcwc Where to store the string length. Optional.
249 * This is undefined on failure.
250 */
251RTDECL(int) RTStrCalcUtf16LenEx(const char *psz, size_t cch, size_t *pcwc);
252
253/**
254 * Translate a UTF-8 string into a UTF-16 allocating the result buffer.
255 *
256 * @returns iprt status code.
257 * @param pszString UTF-8 string to convert.
258 * @param ppwszString Receives pointer to the allocated UTF-16 string.
259 * The returned string must be freed using RTUtf16Free().
260 */
261RTDECL(int) RTStrToUtf16(const char *pszString, PRTUTF16 *ppwszString);
262
263/**
264 * Translates pszString from UTF-8 to UTF-16, allocating the result buffer if requested.
265 *
266 * @returns iprt status code.
267 * @param pszString UTF-8 string to convert.
268 * @param cchString The maximum size in chars (the type) to convert. The conversion stop
269 * when it reaches cchString or the string terminator ('\\0').
270 * Use RTSTR_MAX to translate the entire string.
271 * @param ppwsz If cwc is non-zero, this must either be pointing to pointer to
272 * a buffer of the specified size, or pointer to a NULL pointer.
273 * If *ppwsz is NULL or cwc is zero a buffer of at least cwc items
274 * will be allocated to hold the translated string.
275 * If a buffer was requirest it must be freed using RTUtf16Free().
276 * @param cwc The buffer size in RTUTF16s. This includes the terminator.
277 * @param pcwc Where to store the length of the translated string. (Optional)
278 * This field will be updated even on failure, however the value is only
279 * specified for the following two error codes. On VERR_BUFFER_OVERFLOW
280 * and VERR_NO_STR_MEMORY it contains the required buffer space.
281 */
282RTDECL(int) RTStrToUtf16Ex(const char *pszString, size_t cchString, PRTUTF16 *ppwsz, size_t cwc, size_t *pcwc);
283
284
285/**
286 * Get the unicode code point at the given string position.
287 *
288 * @returns unicode code point.
289 * @returns RTUNICP_INVALID if the encoding is invalid.
290 * @param psz The string.
291 */
292RTDECL(RTUNICP) RTStrGetCpInternal(const char *psz);
293
294/**
295 * Get the unicode code point at the given string position.
296 *
297 * @returns unicode code point.
298 * @returns RTUNICP_INVALID if the encoding is invalid.
299 * @param ppsz The string.
300 * @param pCp Where to store the unicode code point.
301 */
302RTDECL(int) RTStrGetCpExInternal(const char **ppsz, PRTUNICP pCp);
303
304/**
305 * Put the unicode code point at the given string position
306 * and return the pointer to the char following it.
307 *
308 * This function will not consider anything at or following the the
309 * buffer area pointed to by psz. It is therefore not suitable for
310 * inserting code points into a string, only appending/overwriting.
311 *
312 * @returns pointer to the char following the written code point.
313 * @param psz The string.
314 * @param CodePoint The code point to write.
315 * This sould not be RTUNICP_INVALID or any other charater
316 * out of the UTF-8 range.
317 *
318 * @remark This is a worker function for RTStrPutCp().
319 *
320 */
321RTDECL(char *) RTStrPutCpInternal(char *psz, RTUNICP CodePoint);
322
323/**
324 * Get the unicode code point at the given string position.
325 *
326 * @returns unicode code point.
327 * @returns RTUNICP_INVALID if the encoding is invalid.
328 * @param psz The string.
329 *
330 * @remark We optimize this operation by using an inline function for
331 * the most frequent and simplest sequence, the rest is
332 * handled by RTStrGetCpInternal().
333 */
334DECLINLINE(RTUNICP) RTStrGetCp(const char *psz)
335{
336 const unsigned char uch = *(const unsigned char *)psz;
337 if (!(uch & RT_BIT(7)))
338 return uch;
339 return RTStrGetCpInternal(psz);
340}
341
342/**
343 * Get the unicode code point at the given string position.
344 *
345 * @returns iprt status code.
346 * @param ppsz Pointer to the string pointer. This will be updated to
347 * point to the char following the current code point.
348 * @param pCp Where to store the code point.
349 * RTUNICP_INVALID is stored here on failure.
350 *
351 * @remark We optimize this operation by using an inline function for
352 * the most frequent and simplest sequence, the rest is
353 * handled by RTStrGetCpExInternal().
354 */
355DECLINLINE(int) RTStrGetCpEx(const char **ppsz, PRTUNICP pCp)
356{
357 const unsigned char uch = **(const unsigned char **)ppsz;
358 if (!(uch & RT_BIT(7)))
359 {
360 (*ppsz)++;
361 *pCp = uch;
362 return VINF_SUCCESS;
363 }
364 return RTStrGetCpExInternal(ppsz, pCp);
365}
366
367/**
368 * Put the unicode code point at the given string position
369 * and return the pointer to the char following it.
370 *
371 * This function will not consider anything at or following the the
372 * buffer area pointed to by psz. It is therefore not suitable for
373 * inserting code points into a string, only appending/overwriting.
374 *
375 * @returns pointer to the char following the written code point.
376 * @param psz The string.
377 * @param CodePoint The code point to write.
378 * This sould not be RTUNICP_INVALID or any other charater
379 * out of the UTF-8 range.
380 *
381 * @remark We optimize this operation by using an inline function for
382 * the most frequent and simplest sequence, the rest is
383 * handled by RTStrPutCpInternal().
384 */
385DECLINLINE(char *) RTStrPutCp(char *psz, RTUNICP CodePoint)
386{
387 if (CodePoint < 0x80)
388 {
389 *psz++ = (unsigned char)CodePoint;
390 return psz;
391 }
392 return RTStrPutCpInternal(psz, CodePoint);
393}
394
395/**
396 * Skips ahead, past the current code point.
397 *
398 * @returns Pointer to the char after the current code point.
399 * @param psz Pointer to the current code point.
400 * @remark This will not move the next valid code point, only past the current one.
401 */
402DECLINLINE(char *) RTStrNextCp(const char *psz)
403{
404 RTUNICP Cp;
405 RTStrGetCpEx(&psz, &Cp);
406 return (char *)psz;
407}
408
409/**
410 * Skips back to the previous code point.
411 *
412 * @returns Pointer to the char before the current code point.
413 * @returns pszStart on failure.
414 * @param pszStart Pointer to the start of the string.
415 * @param psz Pointer to the current code point.
416 */
417RTDECL(char *) RTStrPrevCp(const char *pszStart, const char *psz);
418
419
420
421#ifndef DECLARED_FNRTSTROUTPUT /* duplicated in iprt/log.h */
422#define DECLARED_FNRTSTROUTPUT
423/**
424 * Output callback.
425 *
426 * @returns number of bytes written.
427 * @param pvArg User argument.
428 * @param pachChars Pointer to an array of utf-8 characters.
429 * @param cbChars Number of bytes in the character array pointed to by pachChars.
430 */
431typedef DECLCALLBACK(size_t) FNRTSTROUTPUT(void *pvArg, const char *pachChars, size_t cbChars);
432/** Pointer to callback function. */
433typedef FNRTSTROUTPUT *PFNRTSTROUTPUT;
434#endif
435
436/** Format flag.
437 * These are used by RTStrFormat extensions and RTStrFormatNumber, mind
438 * that not all flags makes sense to both of the functions.
439 * @{ */
440#define RTSTR_F_CAPITAL 0x0001
441#define RTSTR_F_LEFT 0x0002
442#define RTSTR_F_ZEROPAD 0x0004
443#define RTSTR_F_SPECIAL 0x0008
444#define RTSTR_F_VALSIGNED 0x0010
445#define RTSTR_F_PLUS 0x0020
446#define RTSTR_F_BLANK 0x0040
447#define RTSTR_F_WIDTH 0x0080
448#define RTSTR_F_PRECISION 0x0100
449
450#define RTSTR_F_BIT_MASK 0xf800
451#define RTSTR_F_8BIT 0x0800
452#define RTSTR_F_16BIT 0x1000
453#define RTSTR_F_32BIT 0x2000
454#define RTSTR_F_64BIT 0x4000
455#define RTSTR_F_128BIT 0x8000
456/** @} */
457
458/** @def RTSTR_GET_BIT_FLAG
459 * Gets the bit flag for the specified type.
460 */
461#define RTSTR_GET_BIT_FLAG(type) \
462 ( sizeof(type) == 32 ? RTSTR_F_32BIT \
463 : sizeof(type) == 64 ? RTSTR_F_64BIT \
464 : sizeof(type) == 16 ? RTSTR_F_16BIT \
465 : sizeof(type) == 8 ? RTSTR_F_8BIT \
466 : sizeof(type) == 128? RTSTR_F_128BIT \
467 : 0)
468
469
470/**
471 * Callback to format non-standard format specifiers.
472 *
473 * @returns The number of bytes formatted.
474 * @param pvArg Formatter argument.
475 * @param pfnOutput Pointer to output function.
476 * @param pvArgOutput Argument for the output function.
477 * @param ppszFormat Pointer to the format string pointer. Advance this till the char
478 * after the format specifier.
479 * @param pArgs Pointer to the argument list. Use this to fetch the arguments.
480 * @param cchWidth Format Width. -1 if not specified.
481 * @param cchPrecision Format Precision. -1 if not specified.
482 * @param fFlags Flags (RTSTR_NTFS_*).
483 * @param chArgSize The argument size specifier, 'l' or 'L'.
484 */
485typedef DECLCALLBACK(size_t) FNSTRFORMAT(void *pvArg, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
486 const char **ppszFormat, va_list *pArgs, int cchWidth,
487 int cchPrecision, unsigned fFlags, char chArgSize);
488/** Pointer to a FNSTRFORMAT() function. */
489typedef FNSTRFORMAT *PFNSTRFORMAT;
490
491
492/**
493 * Partial implementation of a printf like formatter.
494 * It doesn't do everything correct, and there is no floating point support.
495 * However, it supports custom formats by the means of a format callback.
496 *
497 * @returns number of bytes formatted.
498 * @param pfnOutput Output worker.
499 * Called in two ways. Normally with a string and its length.
500 * For termination, it's called with NULL for string, 0 for length.
501 * @param pvArgOutput Argument to the output worker.
502 * @param pfnFormat Custom format worker.
503 * @param pvArgFormat Argument to the format worker.
504 * @param pszFormat Format string pointer.
505 * @param InArgs Argument list.
506 */
507RTDECL(size_t) RTStrFormatV(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat, const char *pszFormat, va_list InArgs);
508
509/**
510 * Partial implementation of a printf like formatter.
511 * It doesn't do everything correct, and there is no floating point support.
512 * However, it supports custom formats by the means of a format callback.
513 *
514 * @returns number of bytes formatted.
515 * @param pfnOutput Output worker.
516 * Called in two ways. Normally with a string and its length.
517 * For termination, it's called with NULL for string, 0 for length.
518 * @param pvArgOutput Argument to the output worker.
519 * @param pfnFormat Custom format worker.
520 * @param pvArgFormat Argument to the format worker.
521 * @param pszFormat Format string.
522 * @param ... Argument list.
523 */
524RTDECL(size_t) RTStrFormat(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat, const char *pszFormat, ...);
525
526/**
527 * Formats an integer number according to the parameters.
528 *
529 * @returns Length of the formatted number.
530 * @param psz Pointer to output string buffer of sufficient size.
531 * @param u64Value Value to format.
532 * @param uiBase Number representation base.
533 * @param cchWidth Width.
534 * @param cchPrecision Precision.
535 * @param fFlags Flags (NTFS_*).
536 */
537RTDECL(int) RTStrFormatNumber(char *psz, uint64_t u64Value, unsigned int uiBase, signed int cchWidth, signed int cchPrecision, unsigned int fFlags);
538
539
540/**
541 * Callback for formatting a type.
542 *
543 * This is registered using the RTStrFormatTypeRegister function and will
544 * be called during string formatting to handle the specified %R[type].
545 * The argument for this format type is assumed to be a pointer and it's
546 * passed in the @a pvValue argument.
547 *
548 * @returns Length of the formatted output.
549 * @param pfnOutput Output worker.
550 * @param pvArgOutput Argument to the output worker.
551 * @param pszType The type name.
552 * @param pvValue The argument value.
553 * @param cchWidth Width.
554 * @param cchPrecision Precision.
555 * @param fFlags Flags (NTFS_*).
556 * @param pvUser The user argument.
557 */
558typedef DECLCALLBACK(size_t) FNRTSTRFORMATTYPE(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
559 const char *pszType, void const *pvValue,
560 int cchWidth, int cchPrecision, unsigned fFlags,
561 void *pvUser);
562/** Pointer to a FNRTSTRFORMATTYPE. */
563typedef FNRTSTRFORMATTYPE *PFNRTSTRFORMATTYPE;
564
565
566/**
567 * Register a format handler for a type.
568 *
569 * The format handler is used to handle '%R[type]' format types, where the argument
570 * in the vector is a pointer value (a bit restrictive, but keeps it simple).
571 *
572 * The caller must ensure that no other thread will be making use of any of
573 * the dynamic formatting type facilities simultaneously with this call.
574 *
575 * @returns IPRT status code.
576 * @retval VINF_SUCCESS on success.
577 * @retval VERR_ALREADY_EXISTS if the type has already been registered.
578 * @retval VERR_TOO_MANY_OPEN_FILES if all the type slots has been allocated already.
579 *
580 * @param pszType The type name.
581 * @param pfnHandler The handler address. See FNRTSTRFORMATTYPE for details.
582 * @param pvUser The user argument to pass to the handler. See RTStrFormatTypeSetUser
583 * for how to update this later.
584 */
585RTDECL(int) RTStrFormatTypeRegister(const char *pszType, PFNRTSTRFORMATTYPE pfnHandler, void *pvUser);
586
587/**
588 * Deregisters a format type.
589 *
590 * The caller must ensure that no other thread will be making use of any of
591 * the dynamic formatting type facilities simultaneously with this call.
592 *
593 * @returns IPRT status code.
594 * @retval VINF_SUCCESS on success.
595 * @retval VERR_FILE_NOT_FOUND if not found.
596 *
597 * @param pszType The type to deregister.
598 */
599RTDECL(int) RTStrFormatTypeDeregister(const char *pszType);
600
601/**
602 * Sets the user argument for a type.
603 *
604 * This can be used if a user argument needs relocating in GC.
605 *
606 * @returns IPRT status code.
607 * @retval VINF_SUCCESS on success.
608 * @retval VERR_FILE_NOT_FOUND if not found.
609 *
610 * @param pszType The type to update.
611 * @param pvUser The new user argument value.
612 */
613RTDECL(int) RTStrFormatTypeSetUser(const char *pszType, void *pvUser);
614
615
616/**
617 * String printf.
618 *
619 * @returns The length of the returned string (in pszBuffer).
620 * @param pszBuffer Output buffer.
621 * @param cchBuffer Size of the output buffer.
622 * @param pszFormat The format string.
623 * @param args The format argument.
624 */
625RTDECL(size_t) RTStrPrintfV(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args);
626
627/**
628 * String printf.
629 *
630 * @returns The length of the returned string (in pszBuffer).
631 * @param pszBuffer Output buffer.
632 * @param cchBuffer Size of the output buffer.
633 * @param pszFormat The format string.
634 * @param ... The format argument.
635 */
636RTDECL(size_t) RTStrPrintf(char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...);
637
638
639/**
640 * String printf with custom formatting.
641 *
642 * @returns The length of the returned string (in pszBuffer).
643 * @param pfnFormat Pointer to handler function for the custom formats.
644 * @param pvArg Argument to the pfnFormat function.
645 * @param pszBuffer Output buffer.
646 * @param cchBuffer Size of the output buffer.
647 * @param pszFormat The format string.
648 * @param args The format argument.
649 */
650RTDECL(size_t) RTStrPrintfExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args);
651
652/**
653 * String printf with custom formatting.
654 *
655 * @returns The length of the returned string (in pszBuffer).
656 * @param pfnFormat Pointer to handler function for the custom formats.
657 * @param pvArg Argument to the pfnFormat function.
658 * @param pszBuffer Output buffer.
659 * @param cchBuffer Size of the output buffer.
660 * @param pszFormat The format string.
661 * @param ... The format argument.
662 */
663RTDECL(size_t) RTStrPrintfEx(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...);
664
665
666/**
667 * Allocating string printf.
668 *
669 * @returns The length of the string in the returned *ppszBuffer.
670 * @returns -1 on failure.
671 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
672 * The buffer should be freed using RTStrFree().
673 * On failure *ppszBuffer will be set to NULL.
674 * @param pszFormat The format string.
675 * @param args The format argument.
676 */
677RTDECL(int) RTStrAPrintfV(char **ppszBuffer, const char *pszFormat, va_list args);
678
679/**
680 * Allocating string printf.
681 *
682 * @returns The length of the string in the returned *ppszBuffer.
683 * @returns -1 on failure.
684 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
685 * The buffer should be freed using RTStrFree().
686 * On failure *ppszBuffer will be set to NULL.
687 * @param pszFormat The format string.
688 * @param ... The format argument.
689 */
690RTDECL(int) RTStrAPrintf(char **ppszBuffer, const char *pszFormat, ...);
691
692
693/**
694 * Strips blankspaces from both ends of the string.
695 *
696 * @returns Pointer to first non-blank char in the string.
697 * @param psz The string to strip.
698 */
699RTDECL(char *) RTStrStrip(char *psz);
700
701/**
702 * Strips blankspaces from the start of the string.
703 *
704 * @returns Pointer to first non-blank char in the string.
705 * @param psz The string to strip.
706 */
707RTDECL(char *) RTStrStripL(const char *psz);
708
709/**
710 * Strips blankspaces from the end of the string.
711 *
712 * @returns psz.
713 * @param psz The string to strip.
714 */
715RTDECL(char *) RTStrStripR(char *psz);
716
717
718/** @defgroup rt_str_conv String To/From Number Conversions
719 * @ingroup grp_rt_str
720 * @{ */
721
722/**
723 * Converts a string representation of a number to a 64-bit unsigned number.
724 *
725 * @returns iprt status code.
726 * Warnings are used to indicate convertion problems.
727 * @retval VWRN_NUMBER_TOO_BIG
728 * @retval VWRN_NEGATIVE_UNSIGNED
729 * @retval VWRN_TRAILING_CHARS
730 * @retval VWRN_TRAILING_SPACES
731 * @retval VINF_SUCCESS
732 * @retval VERR_NO_DIGITS
733 *
734 * @param pszValue Pointer to the string value.
735 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
736 * @param uBase The base of the representation used.
737 * If the function will look for known prefixes before defaulting to 10.
738 * @param pu64 Where to store the converted number. (optional)
739 */
740RTDECL(int) RTStrToUInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint64_t *pu64);
741
742/**
743 * Converts a string representation of a number to a 64-bit unsigned number,
744 * making sure the full string is converted.
745 *
746 * @returns iprt status code.
747 * Warnings are used to indicate convertion problems.
748 * @retval VWRN_NUMBER_TOO_BIG
749 * @retval VWRN_NEGATIVE_UNSIGNED
750 * @retval VINF_SUCCESS
751 * @retval VERR_NO_DIGITS
752 * @retval VERR_TRAILING_SPACES
753 * @retval VERR_TRAILING_CHARS
754 *
755 * @param pszValue Pointer to the string value.
756 * @param uBase The base of the representation used.
757 * If the function will look for known prefixes before defaulting to 10.
758 * @param pu64 Where to store the converted number. (optional)
759 */
760RTDECL(int) RTStrToUInt64Full(const char *pszValue, unsigned uBase, uint64_t *pu64);
761
762/**
763 * Converts a string representation of a number to a 64-bit unsigned number.
764 * The base is guessed.
765 *
766 * @returns 64-bit unsigned number on success.
767 * @returns 0 on failure.
768 * @param pszValue Pointer to the string value.
769 */
770RTDECL(uint64_t) RTStrToUInt64(const char *pszValue);
771
772/**
773 * Converts a string representation of a number to a 32-bit unsigned number.
774 *
775 * @returns iprt status code.
776 * Warnings are used to indicate conversion problems.
777 * @retval VWRN_NUMBER_TOO_BIG
778 * @retval VWRN_NEGATIVE_UNSIGNED
779 * @retval VWRN_TRAILING_CHARS
780 * @retval VWRN_TRAILING_SPACES
781 * @retval VINF_SUCCESS
782 * @retval VERR_NO_DIGITS
783 *
784 * @param pszValue Pointer to the string value.
785 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
786 * @param uBase The base of the representation used.
787 * If 0 the function will look for known prefixes before defaulting to 10.
788 * @param pu32 Where to store the converted number. (optional)
789 */
790RTDECL(int) RTStrToUInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint32_t *pu32);
791
792/**
793 * Converts a string representation of a number to a 32-bit unsigned number,
794 * making sure the full string is converted.
795 *
796 * @returns iprt status code.
797 * Warnings are used to indicate convertion problems.
798 * @retval VWRN_NUMBER_TOO_BIG
799 * @retval VWRN_NEGATIVE_UNSIGNED
800 * @retval VINF_SUCCESS
801 * @retval VERR_NO_DIGITS
802 * @retval VERR_TRAILING_SPACES
803 * @retval VERR_TRAILING_CHARS
804 *
805 * @param pszValue Pointer to the string value.
806 * @param uBase The base of the representation used.
807 * If the function will look for known prefixes before defaulting to 10.
808 * @param pu32 Where to store the converted number. (optional)
809 */
810RTDECL(int) RTStrToUInt32Full(const char *pszValue, unsigned uBase, uint32_t *pu32);
811
812/**
813 * Converts a string representation of a number to a 64-bit unsigned number.
814 * The base is guessed.
815 *
816 * @returns 32-bit unsigned number on success.
817 * @returns 0 on failure.
818 * @param pszValue Pointer to the string value.
819 */
820RTDECL(uint32_t) RTStrToUInt32(const char *pszValue);
821
822/**
823 * Converts a string representation of a number to a 16-bit unsigned number.
824 *
825 * @returns iprt status code.
826 * Warnings are used to indicate conversion problems.
827 * @retval VWRN_NUMBER_TOO_BIG
828 * @retval VWRN_NEGATIVE_UNSIGNED
829 * @retval VWRN_TRAILING_CHARS
830 * @retval VWRN_TRAILING_SPACES
831 * @retval VINF_SUCCESS
832 * @retval VERR_NO_DIGITS
833 *
834 * @param pszValue Pointer to the string value.
835 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
836 * @param uBase The base of the representation used.
837 * If 0 the function will look for known prefixes before defaulting to 10.
838 * @param pu16 Where to store the converted number. (optional)
839 */
840RTDECL(int) RTStrToUInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint16_t *pu16);
841
842/**
843 * Converts a string representation of a number to a 16-bit unsigned number,
844 * making sure the full string is converted.
845 *
846 * @returns iprt status code.
847 * Warnings are used to indicate convertion problems.
848 * @retval VWRN_NUMBER_TOO_BIG
849 * @retval VWRN_NEGATIVE_UNSIGNED
850 * @retval VINF_SUCCESS
851 * @retval VERR_NO_DIGITS
852 * @retval VERR_TRAILING_SPACES
853 * @retval VERR_TRAILING_CHARS
854 *
855 * @param pszValue Pointer to the string value.
856 * @param uBase The base of the representation used.
857 * If the function will look for known prefixes before defaulting to 10.
858 * @param pu16 Where to store the converted number. (optional)
859 */
860RTDECL(int) RTStrToUInt16Full(const char *pszValue, unsigned uBase, uint16_t *pu16);
861
862/**
863 * Converts a string representation of a number to a 16-bit unsigned number.
864 * The base is guessed.
865 *
866 * @returns 16-bit unsigned number on success.
867 * @returns 0 on failure.
868 * @param pszValue Pointer to the string value.
869 */
870RTDECL(uint16_t) RTStrToUInt16(const char *pszValue);
871
872/**
873 * Converts a string representation of a number to a 8-bit unsigned number.
874 *
875 * @returns iprt status code.
876 * Warnings are used to indicate conversion problems.
877 * @retval VWRN_NUMBER_TOO_BIG
878 * @retval VWRN_NEGATIVE_UNSIGNED
879 * @retval VWRN_TRAILING_CHARS
880 * @retval VWRN_TRAILING_SPACES
881 * @retval VINF_SUCCESS
882 * @retval VERR_NO_DIGITS
883 *
884 * @param pszValue Pointer to the string value.
885 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
886 * @param uBase The base of the representation used.
887 * If 0 the function will look for known prefixes before defaulting to 10.
888 * @param pu8 Where to store the converted number. (optional)
889 */
890RTDECL(int) RTStrToUInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint8_t *pu8);
891
892/**
893 * Converts a string representation of a number to a 8-bit unsigned number,
894 * making sure the full string is converted.
895 *
896 * @returns iprt status code.
897 * Warnings are used to indicate convertion problems.
898 * @retval VWRN_NUMBER_TOO_BIG
899 * @retval VWRN_NEGATIVE_UNSIGNED
900 * @retval VINF_SUCCESS
901 * @retval VERR_NO_DIGITS
902 * @retval VERR_TRAILING_SPACES
903 * @retval VERR_TRAILING_CHARS
904 *
905 * @param pszValue Pointer to the string value.
906 * @param uBase The base of the representation used.
907 * If the function will look for known prefixes before defaulting to 10.
908 * @param pu8 Where to store the converted number. (optional)
909 */
910RTDECL(int) RTStrToUInt8Full(const char *pszValue, unsigned uBase, uint8_t *pu8);
911
912/**
913 * Converts a string representation of a number to a 8-bit unsigned number.
914 * The base is guessed.
915 *
916 * @returns 8-bit unsigned number on success.
917 * @returns 0 on failure.
918 * @param pszValue Pointer to the string value.
919 */
920RTDECL(uint8_t) RTStrToUInt8(const char *pszValue);
921
922/**
923 * Converts a string representation of a number to a 64-bit signed number.
924 *
925 * @returns iprt status code.
926 * Warnings are used to indicate conversion problems.
927 * @retval VWRN_NUMBER_TOO_BIG
928 * @retval VWRN_TRAILING_CHARS
929 * @retval VWRN_TRAILING_SPACES
930 * @retval VINF_SUCCESS
931 * @retval VERR_NO_DIGITS
932 *
933 * @param pszValue Pointer to the string value.
934 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
935 * @param uBase The base of the representation used.
936 * If 0 the function will look for known prefixes before defaulting to 10.
937 * @param pi64 Where to store the converted number. (optional)
938 */
939RTDECL(int) RTStrToInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, int64_t *pi64);
940
941/**
942 * Converts a string representation of a number to a 64-bit signed number,
943 * making sure the full string is converted.
944 *
945 * @returns iprt status code.
946 * Warnings are used to indicate convertion problems.
947 * @retval VWRN_NUMBER_TOO_BIG
948 * @retval VINF_SUCCESS
949 * @retval VERR_TRAILING_CHARS
950 * @retval VERR_TRAILING_SPACES
951 * @retval VERR_NO_DIGITS
952 *
953 * @param pszValue Pointer to the string value.
954 * @param uBase The base of the representation used.
955 * If the function will look for known prefixes before defaulting to 10.
956 * @param pi64 Where to store the converted number. (optional)
957 */
958RTDECL(int) RTStrToInt64Full(const char *pszValue, unsigned uBase, int64_t *pi64);
959
960/**
961 * Converts a string representation of a number to a 64-bit signed number.
962 * The base is guessed.
963 *
964 * @returns 64-bit signed number on success.
965 * @returns 0 on failure.
966 * @param pszValue Pointer to the string value.
967 */
968RTDECL(int64_t) RTStrToInt64(const char *pszValue);
969
970/**
971 * Converts a string representation of a number to a 32-bit signed number.
972 *
973 * @returns iprt status code.
974 * Warnings are used to indicate conversion problems.
975 * @retval VWRN_NUMBER_TOO_BIG
976 * @retval VWRN_TRAILING_CHARS
977 * @retval VWRN_TRAILING_SPACES
978 * @retval VINF_SUCCESS
979 * @retval VERR_NO_DIGITS
980 *
981 * @param pszValue Pointer to the string value.
982 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
983 * @param uBase The base of the representation used.
984 * If 0 the function will look for known prefixes before defaulting to 10.
985 * @param pi32 Where to store the converted number. (optional)
986 */
987RTDECL(int) RTStrToInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, int32_t *pi32);
988
989/**
990 * Converts a string representation of a number to a 32-bit signed number,
991 * making sure the full string is converted.
992 *
993 * @returns iprt status code.
994 * Warnings are used to indicate convertion problems.
995 * @retval VWRN_NUMBER_TOO_BIG
996 * @retval VINF_SUCCESS
997 * @retval VERR_TRAILING_CHARS
998 * @retval VERR_TRAILING_SPACES
999 * @retval VERR_NO_DIGITS
1000 *
1001 * @param pszValue Pointer to the string value.
1002 * @param uBase The base of the representation used.
1003 * If the function will look for known prefixes before defaulting to 10.
1004 * @param pi32 Where to store the converted number. (optional)
1005 */
1006RTDECL(int) RTStrToInt32Full(const char *pszValue, unsigned uBase, int32_t *pi32);
1007
1008/**
1009 * Converts a string representation of a number to a 32-bit signed number.
1010 * The base is guessed.
1011 *
1012 * @returns 32-bit signed number on success.
1013 * @returns 0 on failure.
1014 * @param pszValue Pointer to the string value.
1015 */
1016RTDECL(int32_t) RTStrToInt32(const char *pszValue);
1017
1018/**
1019 * Converts a string representation of a number to a 16-bit signed number.
1020 *
1021 * @returns iprt status code.
1022 * Warnings are used to indicate conversion problems.
1023 * @retval VWRN_NUMBER_TOO_BIG
1024 * @retval VWRN_TRAILING_CHARS
1025 * @retval VWRN_TRAILING_SPACES
1026 * @retval VINF_SUCCESS
1027 * @retval VERR_NO_DIGITS
1028 *
1029 * @param pszValue Pointer to the string value.
1030 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1031 * @param uBase The base of the representation used.
1032 * If 0 the function will look for known prefixes before defaulting to 10.
1033 * @param pi16 Where to store the converted number. (optional)
1034 */
1035RTDECL(int) RTStrToInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, int16_t *pi16);
1036
1037/**
1038 * Converts a string representation of a number to a 16-bit signed number,
1039 * making sure the full string is converted.
1040 *
1041 * @returns iprt status code.
1042 * Warnings are used to indicate convertion problems.
1043 * @retval VWRN_NUMBER_TOO_BIG
1044 * @retval VINF_SUCCESS
1045 * @retval VERR_TRAILING_CHARS
1046 * @retval VERR_TRAILING_SPACES
1047 * @retval VERR_NO_DIGITS
1048 *
1049 * @param pszValue Pointer to the string value.
1050 * @param uBase The base of the representation used.
1051 * If the function will look for known prefixes before defaulting to 10.
1052 * @param pi16 Where to store the converted number. (optional)
1053 */
1054RTDECL(int) RTStrToInt16Full(const char *pszValue, unsigned uBase, int16_t *pi16);
1055
1056/**
1057 * Converts a string representation of a number to a 16-bit signed number.
1058 * The base is guessed.
1059 *
1060 * @returns 16-bit signed number on success.
1061 * @returns 0 on failure.
1062 * @param pszValue Pointer to the string value.
1063 */
1064RTDECL(int16_t) RTStrToInt16(const char *pszValue);
1065
1066/**
1067 * Converts a string representation of a number to a 8-bit signed number.
1068 *
1069 * @returns iprt status code.
1070 * Warnings are used to indicate conversion problems.
1071 * @retval VWRN_NUMBER_TOO_BIG
1072 * @retval VWRN_TRAILING_CHARS
1073 * @retval VWRN_TRAILING_SPACES
1074 * @retval VINF_SUCCESS
1075 * @retval VERR_NO_DIGITS
1076 *
1077 * @param pszValue Pointer to the string value.
1078 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1079 * @param uBase The base of the representation used.
1080 * If 0 the function will look for known prefixes before defaulting to 10.
1081 * @param pi8 Where to store the converted number. (optional)
1082 */
1083RTDECL(int) RTStrToInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, int8_t *pi8);
1084
1085/**
1086 * Converts a string representation of a number to a 8-bit signed number,
1087 * making sure the full string is converted.
1088 *
1089 * @returns iprt status code.
1090 * Warnings are used to indicate convertion problems.
1091 * @retval VWRN_NUMBER_TOO_BIG
1092 * @retval VINF_SUCCESS
1093 * @retval VERR_TRAILING_CHARS
1094 * @retval VERR_TRAILING_SPACES
1095 * @retval VERR_NO_DIGITS
1096 *
1097 * @param pszValue Pointer to the string value.
1098 * @param uBase The base of the representation used.
1099 * If the function will look for known prefixes before defaulting to 10.
1100 * @param pi8 Where to store the converted number. (optional)
1101 */
1102RTDECL(int) RTStrToInt8Full(const char *pszValue, unsigned uBase, int8_t *pi8);
1103
1104/**
1105 * Converts a string representation of a number to a 8-bit signed number.
1106 * The base is guessed.
1107 *
1108 * @returns 8-bit signed number on success.
1109 * @returns 0 on failure.
1110 * @param pszValue Pointer to the string value.
1111 */
1112RTDECL(int8_t) RTStrToInt8(const char *pszValue);
1113
1114/**
1115 * Performs a case sensitive string compare between two UTF-8 strings.
1116 *
1117 * Encoding errors are ignored by the current implementation. So, the only
1118 * difference between this and the CRT strcmp function is the handling of
1119 * NULL arguments.
1120 *
1121 * @returns < 0 if the first string less than the second string.
1122 * @returns 0 if the first string identical to the second string.
1123 * @returns > 0 if the first string greater than the second string.
1124 * @param psz1 First UTF-8 string. Null is allowed.
1125 * @param psz2 Second UTF-8 string. Null is allowed.
1126 */
1127RTDECL(int) RTStrCmp(const char *psz1, const char *psz2);
1128
1129/**
1130 * Performs a case insensitive string compare between two UTF-8 strings.
1131 *
1132 * This is a simplified compare, as only the simplified lower/upper case folding
1133 * specified by the unicode specs are used. It does not consider character pairs
1134 * as they are used in some languages, just simple upper & lower case compares.
1135 *
1136 * The result is the difference between the mismatching codepoints after they
1137 * both have been lower cased.
1138 *
1139 * If the string encoding is invalid the function will assert (strict builds)
1140 * and use RTStrCmp for the remainder of the string.
1141 *
1142 * @returns < 0 if the first string less than the second string.
1143 * @returns 0 if the first string identical to the second string.
1144 * @returns > 0 if the first string greater than the second string.
1145 * @param psz1 First UTF-8 string. Null is allowed.
1146 * @param psz2 Second UTF-8 string. Null is allowed.
1147 */
1148RTDECL(int) RTStrICmp(const char *psz1, const char *psz2);
1149
1150/** @} */
1151
1152
1153/** @defgroup rt_str_space Unique String Space
1154 * @ingroup grp_rt_str
1155 * @{
1156 */
1157
1158/** Pointer to a string name space container node core. */
1159typedef struct RTSTRSPACECORE *PRTSTRSPACECORE;
1160/** Pointer to a pointer to a string name space container node core. */
1161typedef PRTSTRSPACECORE *PPRTSTRSPACECORE;
1162
1163/**
1164 * String name space container node core.
1165 */
1166typedef struct RTSTRSPACECORE
1167{
1168 /** Hash key. Don't touch. */
1169 uint32_t Key;
1170 /** Pointer to the left leaf node. Don't touch. */
1171 PRTSTRSPACECORE pLeft;
1172 /** Pointer to the left rigth node. Don't touch. */
1173 PRTSTRSPACECORE pRight;
1174 /** Pointer to the list of string with the same key. Don't touch. */
1175 PRTSTRSPACECORE pList;
1176 /** Height of this tree: max(heigth(left), heigth(right)) + 1. Don't touch */
1177 unsigned char uchHeight;
1178 /** The string length. Read only! */
1179 size_t cchString;
1180 /** Pointer to the string. Read only! */
1181 const char * pszString;
1182} RTSTRSPACECORE;
1183
1184/** String space. (Initialize with NULL.) */
1185typedef PRTSTRSPACECORE RTSTRSPACE;
1186/** Pointer to a string space. */
1187typedef PPRTSTRSPACECORE PRTSTRSPACE;
1188
1189
1190/**
1191 * Inserts a string into a unique string space.
1192 *
1193 * @returns true on success.
1194 * @returns false if the string collieded with an existing string.
1195 * @param pStrSpace The space to insert it into.
1196 * @param pStr The string node.
1197 */
1198RTDECL(bool) RTStrSpaceInsert(PRTSTRSPACE pStrSpace, PRTSTRSPACECORE pStr);
1199
1200/**
1201 * Removes a string from a unique string space.
1202 *
1203 * @returns Pointer to the removed string node.
1204 * @returns NULL if the string was not found in the string space.
1205 * @param pStrSpace The space to insert it into.
1206 * @param pszString The string to remove.
1207 */
1208RTDECL(PRTSTRSPACECORE) RTStrSpaceRemove(PRTSTRSPACE pStrSpace, const char *pszString);
1209
1210/**
1211 * Gets a string from a unique string space.
1212 *
1213 * @returns Pointer to the string node.
1214 * @returns NULL if the string was not found in the string space.
1215 * @param pStrSpace The space to insert it into.
1216 * @param pszString The string to get.
1217 */
1218RTDECL(PRTSTRSPACECORE) RTStrSpaceGet(PRTSTRSPACE pStrSpace, const char *pszString);
1219
1220/**
1221 * Callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy().
1222 *
1223 * @returns 0 on continue.
1224 * @returns Non-zero to aborts the operation.
1225 * @param pStr The string node
1226 * @param pvUser The user specified argument.
1227 */
1228typedef DECLCALLBACK(int) FNRTSTRSPACECALLBACK(PRTSTRSPACECORE pStr, void *pvUser);
1229/** Pointer to callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy(). */
1230typedef FNRTSTRSPACECALLBACK *PFNRTSTRSPACECALLBACK;
1231
1232/**
1233 * Destroys the string space.
1234 * The caller supplies a callback which will be called for each of
1235 * the string nodes in for freeing their memory and other resources.
1236 *
1237 * @returns 0 or what ever non-zero return value pfnCallback returned
1238 * when aborting the destruction.
1239 * @param pStrSpace The space to insert it into.
1240 * @param pfnCallback The callback.
1241 * @param pvUser The user argument.
1242 */
1243RTDECL(int) RTStrSpaceDestroy(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
1244
1245/**
1246 * Enumerates the string space.
1247 * The caller supplies a callback which will be called for each of
1248 * the string nodes.
1249 *
1250 * @returns 0 or what ever non-zero return value pfnCallback returned
1251 * when aborting the destruction.
1252 * @param pStrSpace The space to insert it into.
1253 * @param pfnCallback The callback.
1254 * @param pvUser The user argument.
1255 */
1256RTDECL(int) RTStrSpaceEnumerate(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
1257
1258/** @} */
1259
1260
1261/** @defgroup rt_str_utf16 UTF-16 String Manipulation
1262 * @ingroup grp_rt_str
1263 * @{
1264 */
1265
1266/**
1267 * Free a UTF-16 string allocated by RTStrUtf8ToUtf16(), RTStrUtf8ToUtf16Ex(),
1268 * RTUtf16Dup() or RTUtf16DupEx().
1269 *
1270 * @returns iprt status code.
1271 * @param pwszString The UTF-16 string to free. NULL is accepted.
1272 */
1273RTDECL(void) RTUtf16Free(PRTUTF16 pwszString);
1274
1275/**
1276 * Allocates a new copy of the specified UTF-16 string.
1277 *
1278 * @returns Pointer to the allocated string copy. Use RTUtf16Free() to free it.
1279 * @returns NULL when out of memory.
1280 * @param pwszString UTF-16 string to duplicate.
1281 * @remark This function will not make any attempt to validate the encoding.
1282 */
1283RTDECL(PRTUTF16) RTUtf16Dup(PCRTUTF16 pwszString);
1284
1285/**
1286 * Allocates a new copy of the specified UTF-16 string.
1287 *
1288 * @returns iprt status code.
1289 * @param ppwszString Receives pointer of the allocated UTF-16 string.
1290 * The returned pointer must be freed using RTUtf16Free().
1291 * @param pwszString UTF-16 string to duplicate.
1292 * @param cwcExtra Number of extra RTUTF16 items to allocate.
1293 * @remark This function will not make any attempt to validate the encoding.
1294 */
1295RTDECL(int) RTUtf16DupEx(PRTUTF16 *ppwszString, PCRTUTF16 pwszString, size_t cwcExtra);
1296
1297/**
1298 * Returns the length of a UTF-16 string in UTF-16 characters
1299 * without trailing '\\0'.
1300 *
1301 * Surrogate pairs counts as two UTF-16 characters here. Use RTUtf16CpCnt()
1302 * to get the exact number of code points in the string.
1303 *
1304 * @returns The number of RTUTF16 items in the string.
1305 * @param pwszString Pointer the UTF-16 string.
1306 * @remark This function will not make any attempt to validate the encoding.
1307 */
1308RTDECL(size_t) RTUtf16Len(PCRTUTF16 pwszString);
1309
1310/**
1311 * Performs a case sensitive string compare between two UTF-16 strings.
1312 *
1313 * @returns < 0 if the first string less than the second string.s
1314 * @returns 0 if the first string identical to the second string.
1315 * @returns > 0 if the first string greater than the second string.
1316 * @param pwsz1 First UTF-16 string. Null is allowed.
1317 * @param pwsz2 Second UTF-16 string. Null is allowed.
1318 * @remark This function will not make any attempt to validate the encoding.
1319 */
1320RTDECL(int) RTUtf16Cmp(register PCRTUTF16 pwsz1, register PCRTUTF16 pwsz2);
1321
1322/**
1323 * Performs a case insensitive string compare between two UTF-16 strings.
1324 *
1325 * This is a simplified compare, as only the simplified lower/upper case folding
1326 * specified by the unicode specs are used. It does not consider character pairs
1327 * as they are used in some languages, just simple upper & lower case compares.
1328 *
1329 * @returns < 0 if the first string less than the second string.
1330 * @returns 0 if the first string identical to the second string.
1331 * @returns > 0 if the first string greater than the second string.
1332 * @param pwsz1 First UTF-16 string. Null is allowed.
1333 * @param pwsz2 Second UTF-16 string. Null is allowed.
1334 */
1335RTDECL(int) RTUtf16ICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
1336
1337/**
1338 * Performs a case insensitive string compare between two UTF-16 strings
1339 * using the current locale of the process (if applicable).
1340 *
1341 * This differs from RTUtf16ICmp() in that it will try, if a locale with the
1342 * required data is available, to do a correct case-insensitive compare. It
1343 * follows that it is more complex and thereby likely to be more expensive.
1344 *
1345 * @returns < 0 if the first string less than the second string.
1346 * @returns 0 if the first string identical to the second string.
1347 * @returns > 0 if the first string greater than the second string.
1348 * @param pwsz1 First UTF-16 string. Null is allowed.
1349 * @param pwsz2 Second UTF-16 string. Null is allowed.
1350 */
1351RTDECL(int) RTUtf16LocaleICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
1352
1353/**
1354 * Folds a UTF-16 string to lowercase.
1355 *
1356 * This is a very simple folding; is uses the simple lowercase
1357 * code point, it is not related to any locale just the most common
1358 * lowercase codepoint setup by the unicode specs, and it will not
1359 * create new surrogate pairs or remove existing ones.
1360 *
1361 * @returns Pointer to the passed in string.
1362 * @param pwsz The string to fold.
1363 */
1364RTDECL(PRTUTF16) RTUtf16ToLower(PRTUTF16 pwsz);
1365
1366/**
1367 * Folds a UTF-16 string to uppercase.
1368 *
1369 * This is a very simple folding; is uses the simple uppercase
1370 * code point, it is not related to any locale just the most common
1371 * uppercase codepoint setup by the unicode specs, and it will not
1372 * create new surrogate pairs or remove existing ones.
1373 *
1374 * @returns Pointer to the passed in string.
1375 * @param pwsz The string to fold.
1376 */
1377RTDECL(PRTUTF16) RTUtf16ToUpper(PRTUTF16 pwsz);
1378
1379/**
1380 * Translate a UTF-16 string into a UTF-8 allocating the result buffer.
1381 *
1382 * @returns iprt status code.
1383 * @param pwszString UTF-16 string to convert.
1384 * @param ppszString Receives pointer of allocated UTF-8 string.
1385 * The returned pointer must be freed using RTStrFree().
1386 */
1387RTDECL(int) RTUtf16ToUtf8(PCRTUTF16 pwszString, char **ppszString);
1388
1389/**
1390 * Translates UTF-16 to UTF-8 using buffer provided by the caller or
1391 * a fittingly sized buffer allocated by the function.
1392 *
1393 * @returns iprt status code.
1394 * @param pwszString The UTF-16 string to convert.
1395 * @param cwcString The number of RTUTF16 items to translation from pwszString.
1396 * The translate will stop when reaching cwcString or the terminator ('\\0').
1397 * Use RTSTR_MAX to translate the entire string.
1398 * @param ppsz If cch is non-zero, this must either be pointing to pointer to
1399 * a buffer of the specified size, or pointer to a NULL pointer.
1400 * If *ppsz is NULL or cch is zero a buffer of at least cch chars
1401 * will be allocated to hold the translated string.
1402 * If a buffer was requirest it must be freed using RTUtf16Free().
1403 * @param cch The buffer size in chars (the type). This includes the terminator.
1404 * @param pcch Where to store the length of the translated string. (Optional)
1405 * This field will be updated even on failure, however the value is only
1406 * specified for the following two error codes. On VERR_BUFFER_OVERFLOW
1407 * and VERR_NO_STR_MEMORY it contains the required buffer space.
1408 */
1409RTDECL(int) RTUtf16ToUtf8Ex(PCRTUTF16 pwszString, size_t cwcString, char **ppsz, size_t cch, size_t *pcch);
1410
1411/**
1412 * Calculates the length of the UTF-16 string in UTF-8 chars (bytes).
1413 *
1414 * This function will validate the string, and incorrectly encoded UTF-16
1415 * strings will be rejected. The primary purpose of this function is to
1416 * help allocate buffers for RTUtf16ToUtf8() of the correct size. For most
1417 * other puroses RTUtf16ToUtf8Ex() should be used.
1418 *
1419 * @returns Number of char (bytes).
1420 * @returns 0 if the string was incorrectly encoded.
1421 * @param pwsz The UTF-16 string.
1422 */
1423RTDECL(size_t) RTUtf16CalcUtf8Len(PCRTUTF16 pwsz);
1424
1425/**
1426 * Calculates the length of the UTF-16 string in UTF-8 chars (bytes).
1427 *
1428 * This function will validate the string, and incorrectly encoded UTF-16
1429 * strings will be rejected.
1430 *
1431 * @returns iprt status code.
1432 * @param pwsz The string.
1433 * @param cwc The max string length. Use RTSTR_MAX to process the entire string.
1434 * @param pcch Where to store the string length (in bytes). Optional.
1435 * This is undefined on failure.
1436 */
1437RTDECL(int) RTUtf16CalcUtf8LenEx(PCRTUTF16 pwsz, size_t cwc, size_t *pcch);
1438
1439/**
1440 * Get the unicode code point at the given string position.
1441 *
1442 * @returns unicode code point.
1443 * @returns RTUNICP_INVALID if the encoding is invalid.
1444 * @param pwsz The string.
1445 *
1446 * @remark This is an internal worker for RTUtf16GetCp().
1447 */
1448RTDECL(RTUNICP) RTUtf16GetCpInternal(PCRTUTF16 pwsz);
1449
1450/**
1451 * Get the unicode code point at the given string position.
1452 *
1453 * @returns iprt status code.
1454 * @param ppwsz Pointer to the string pointer. This will be updated to
1455 * point to the char following the current code point.
1456 * @param pCp Where to store the code point.
1457 * RTUNICP_INVALID is stored here on failure.
1458 *
1459 * @remark This is an internal worker for RTUtf16GetCpEx().
1460 */
1461RTDECL(int) RTUtf16GetCpExInternal(PCRTUTF16 *ppwsz, PRTUNICP pCp);
1462
1463/**
1464 * Put the unicode code point at the given string position
1465 * and return the pointer to the char following it.
1466 *
1467 * This function will not consider anything at or following the the
1468 * buffer area pointed to by pwsz. It is therefore not suitable for
1469 * inserting code points into a string, only appending/overwriting.
1470 *
1471 * @returns pointer to the char following the written code point.
1472 * @param pwsz The string.
1473 * @param CodePoint The code point to write.
1474 * This sould not be RTUNICP_INVALID or any other charater
1475 * out of the UTF-16 range.
1476 *
1477 * @remark This is an internal worker for RTUtf16GetCpEx().
1478 */
1479RTDECL(PRTUTF16) RTUtf16PutCpInternal(PRTUTF16 pwsz, RTUNICP CodePoint);
1480
1481/**
1482 * Get the unicode code point at the given string position.
1483 *
1484 * @returns unicode code point.
1485 * @returns RTUNICP_INVALID if the encoding is invalid.
1486 * @param pwsz The string.
1487 *
1488 * @remark We optimize this operation by using an inline function for
1489 * everything which isn't a surrogate pair or an endian indicator.
1490 */
1491DECLINLINE(RTUNICP) RTUtf16GetCp(PCRTUTF16 pwsz)
1492{
1493 const RTUTF16 wc = *pwsz;
1494 if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
1495 return wc;
1496 return RTUtf16GetCpInternal(pwsz);
1497}
1498
1499/**
1500 * Get the unicode code point at the given string position.
1501 *
1502 * @returns iprt status code.
1503 * @param ppwsz Pointer to the string pointer. This will be updated to
1504 * point to the char following the current code point.
1505 * @param pCp Where to store the code point.
1506 * RTUNICP_INVALID is stored here on failure.
1507 *
1508 * @remark We optimize this operation by using an inline function for
1509 * everything which isn't a surrogate pair or and endian indicator.
1510 */
1511DECLINLINE(int) RTUtf16GetCpEx(PCRTUTF16 *ppwsz, PRTUNICP pCp)
1512{
1513 const RTUTF16 wc = **ppwsz;
1514 if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
1515 {
1516 (*ppwsz)++;
1517 *pCp = wc;
1518 return VINF_SUCCESS;
1519 }
1520 return RTUtf16GetCpExInternal(ppwsz, pCp);
1521}
1522
1523/**
1524 * Put the unicode code point at the given string position
1525 * and return the pointer to the char following it.
1526 *
1527 * This function will not consider anything at or following the the
1528 * buffer area pointed to by pwsz. It is therefore not suitable for
1529 * inserting code points into a string, only appending/overwriting.
1530 *
1531 * @returns pointer to the char following the written code point.
1532 * @param pwsz The string.
1533 * @param CodePoint The code point to write.
1534 * This sould not be RTUNICP_INVALID or any other charater
1535 * out of the UTF-16 range.
1536 *
1537 * @remark We optimize this operation by using an inline function for
1538 * everything which isn't a surrogate pair or and endian indicator.
1539 */
1540DECLINLINE(PRTUTF16) RTUtf16PutCp(PRTUTF16 pwsz, RTUNICP CodePoint)
1541{
1542 if (CodePoint < 0xd800 || (CodePoint > 0xd800 && CodePoint < 0xfffe))
1543 {
1544 *pwsz++ = (RTUTF16)CodePoint;
1545 return pwsz;
1546 }
1547 return RTUtf16PutCpInternal(pwsz, CodePoint);
1548}
1549
1550/**
1551 * Skips ahead, past the current code point.
1552 *
1553 * @returns Pointer to the char after the current code point.
1554 * @param pwsz Pointer to the current code point.
1555 * @remark This will not move the next valid code point, only past the current one.
1556 */
1557DECLINLINE(PRTUTF16) RTUtf16NextCp(PCRTUTF16 pwsz)
1558{
1559 RTUNICP Cp;
1560 RTUtf16GetCpEx(&pwsz, &Cp);
1561 return (PRTUTF16)pwsz;
1562}
1563
1564/**
1565 * Skips backwards, to the previous code point.
1566 *
1567 * @returns Pointer to the char after the current code point.
1568 * @param pwszStart Pointer to the start of the string.
1569 * @param pwsz Pointer to the current code point.
1570 */
1571RTDECL(PRTUTF16) RTUtf16PrevCp(PCRTUTF16 pwszStart, PCRTUTF16 pwsz);
1572
1573
1574/**
1575 * Checks if the UTF-16 char is the high surrogate char (i.e.
1576 * the 1st char in the pair).
1577 *
1578 * @returns true if it is.
1579 * @returns false if it isn't.
1580 * @param wc The character to investigate.
1581 */
1582DECLINLINE(bool) RTUtf16IsHighSurrogate(RTUTF16 wc)
1583{
1584 return wc >= 0xd800 && wc <= 0xdbff;
1585}
1586
1587/**
1588 * Checks if the UTF-16 char is the low surrogate char (i.e.
1589 * the 2nd char in the pair).
1590 *
1591 * @returns true if it is.
1592 * @returns false if it isn't.
1593 * @param wc The character to investigate.
1594 */
1595DECLINLINE(bool) RTUtf16IsLowSurrogate(RTUTF16 wc)
1596{
1597 return wc >= 0xdc00 && wc <= 0xdfff;
1598}
1599
1600
1601/**
1602 * Checks if the two UTF-16 chars form a valid surrogate pair.
1603 *
1604 * @returns true if they do.
1605 * @returns false if they doesn't.
1606 * @param wcHigh The high (1st) character.
1607 * @param wcLow The low (2nd) character.
1608 */
1609DECLINLINE(bool) RTUtf16IsSurrogatePair(RTUTF16 wcHigh, RTUTF16 wcLow)
1610{
1611 return RTUtf16IsHighSurrogate(wcHigh)
1612 && RTUtf16IsLowSurrogate(wcLow);
1613}
1614
1615/** @} */
1616
1617__END_DECLS
1618
1619/** @} */
1620
1621#endif
1622
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