VirtualBox

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

Last change on this file since 7200 was 7183, checked in by vboxsync, 17 years ago

Added RTStrFormatTypeRegister/SetUser/Deregister for runtime registration of custom format types. Example: RTStrFormat("%R[pgmpage]", pPage);

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 61.6 KB
Line 
1/** @file
2 * innotek Portable Runtime - String Manipluation.
3 */
4
5/*
6 * Copyright (C) 2006-2007 innotek GmbH
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
26#ifndef ___iprt_string_h
27#define ___iprt_string_h
28
29#include <iprt/cdefs.h>
30#include <iprt/types.h>
31#include <iprt/stdarg.h>
32#include <iprt/err.h> /* for VINF_SUCCESS */
33#if defined(RT_OS_LINUX) && defined(__KERNEL__)
34# include <linux/string.h>
35#elif defined(RT_OS_FREEBSD) && defined(_KERNEL)
36 /*
37 * Kludge for the FreeBSD kernel:
38 * Some of the string.h stuff clashes with sys/libkern.h, so just wrap
39 * it up while including string.h to keep things quiet. It's nothing
40 * important that's clashing, after all.
41 */
42# define strdup strdup_string_h
43# include <string.h>
44# undef strdup
45#elif defined(RT_OS_SOLARIS) && defined(_KERNEL)
46 /*
47 * Same case as with FreeBSD kernel:
48 * The string.h stuff clashes with sys/systm.h
49 * ffs = find first set bit.
50 */
51# define ffs ffs_string_h
52# include <string.h>
53# undef ffs
54# undef strpbrk
55#else
56# include <string.h>
57#endif
58
59/*
60 * Supply prototypes for standard string functions provided by
61 * IPRT instead of the operating environment.
62 */
63#if defined(RT_OS_DARWIN) && defined(KERNEL)
64__BEGIN_DECLS
65void *memchr(const void *pv, int ch, size_t cb);
66char *strpbrk(const char *pszStr, const char *pszChars);
67__END_DECLS
68#endif
69
70
71/** @defgroup grp_rt_str RTStr - String Manipulation
72 * Mostly UTF-8 related helpers where the standard string functions won't do.
73 * @ingroup grp_rt
74 * @{
75 */
76
77__BEGIN_DECLS
78
79
80/**
81 * The maximum string length.
82 */
83#define RTSTR_MAX (~(size_t)0)
84
85
86#ifdef IN_RING3
87
88/**
89 * Allocates tmp buffer, translates pszString from UTF8 to current codepage.
90 *
91 * @returns iprt status code.
92 * @param ppszString Receives pointer of allocated native CP string.
93 * The returned pointer must be freed using RTStrFree().
94 * @param pszString UTF-8 string to convert.
95 */
96RTR3DECL(int) RTStrUtf8ToCurrentCP(char **ppszString, const char *pszString);
97
98/**
99 * Allocates tmp buffer, translates pszString from current codepage to UTF-8.
100 *
101 * @returns iprt status code.
102 * @param ppszString Receives pointer of allocated UTF-8 string.
103 * The returned pointer must be freed using RTStrFree().
104 * @param pszString Native string to convert.
105 */
106RTR3DECL(int) RTStrCurrentCPToUtf8(char **ppszString, const char *pszString);
107
108#endif
109
110/**
111 * Free string allocated by any of the non-UCS-2 string functions.
112 *
113 * @returns iprt status code.
114 * @param pszString Pointer to buffer with string to free.
115 * NULL is accepted.
116 */
117RTDECL(void) RTStrFree(char *pszString);
118
119/**
120 * Allocates a new copy of the given UTF-8 string.
121 *
122 * @returns Pointer to the allocated UTF-8 string.
123 * @param pszString UTF-8 string to duplicate.
124 */
125RTDECL(char *) RTStrDup(const char *pszString);
126
127/**
128 * Allocates a new copy of the given UTF-8 string.
129 *
130 * @returns iprt status code.
131 * @param ppszString Receives pointer of the allocated UTF-8 string.
132 * The returned pointer must be freed using RTStrFree().
133 * @param pszString UTF-8 string to duplicate.
134 */
135RTDECL(int) RTStrDupEx(char **ppszString, const char *pszString);
136
137/**
138 * Gets the number of code points the string is made up of, excluding
139 * the terminator.
140 *
141 *
142 * @returns Number of code points (RTUNICP).
143 * @returns 0 if the string was incorrectly encoded.
144 * @param psz The string.
145 */
146RTDECL(size_t) RTStrUniLen(const char *psz);
147
148/**
149 * Gets the number of code points the string is made up of, excluding
150 * the terminator.
151 *
152 * This function will validate the string, and incorrectly encoded UTF-8
153 * strings will be rejected.
154 *
155 * @returns iprt status code.
156 * @param psz The string.
157 * @param cch The max string length. Use RTSTR_MAX to process the entire string.
158 * @param pcuc Where to store the code point count.
159 * This is undefined on failure.
160 */
161RTDECL(int) RTStrUniLenEx(const char *psz, size_t cch, size_t *pcuc);
162
163/**
164 * Translate a UTF-8 string into an unicode string (i.e. RTUNICPs), allocating the string buffer.
165 *
166 * @returns iprt status code.
167 * @param pszString UTF-8 string to convert.
168 * @param ppUniString Receives pointer to the allocated unicode string.
169 * The returned string must be freed using RTUniFree().
170 */
171RTDECL(int) RTStrToUni(const char *pszString, PRTUNICP *ppUniString);
172
173/**
174 * Translates pszString from UTF-8 to an array of code points, allocating the result
175 * array if requested.
176 *
177 * @returns iprt status code.
178 * @param pszString UTF-8 string to convert.
179 * @param cchString The maximum size in chars (the type) to convert. The conversion stop
180 * when it reaches cchString or the string terminator ('\\0').
181 * Use RTSTR_MAX to translate the entire string.
182 * @param ppaCps If cCps is non-zero, this must either be pointing to pointer to
183 * a buffer of the specified size, or pointer to a NULL pointer.
184 * If *ppusz is NULL or cCps is zero a buffer of at least cCps items
185 * will be allocated to hold the translated string.
186 * If a buffer was requirest it must be freed using RTUtf16Free().
187 * @param cCps The number of code points in the unicode string. This includes the terminator.
188 * @param pcCps Where to store the length of the translated string. (Optional)
189 * This field will be updated even on failure, however the value is only
190 * specified for the following two error codes. On VERR_BUFFER_OVERFLOW
191 * and VERR_NO_STR_MEMORY it contains the required buffer space.
192 */
193RTDECL(int) RTStrToUniEx(const char *pszString, size_t cchString, PRTUNICP *ppaCps, size_t cCps, size_t *pcCps);
194
195/**
196 * Calculates the length of the string in RTUTF16 items.
197 *
198 * This function will validate the string, and incorrectly encoded UTF-8
199 * strings will be rejected. The primary purpose of this function is to
200 * help allocate buffers for RTStrToUtf16Ex of the correct size. For most
201 * other puroses RTStrCalcUtf16LenEx() should be used.
202 *
203 * @returns Number of RTUTF16 items.
204 * @returns 0 if the string was incorrectly encoded.
205 * @param psz The string.
206 */
207RTDECL(size_t) RTStrCalcUtf16Len(const char *psz);
208
209/**
210 * Calculates the length of the string in RTUTF16 items.
211 *
212 * This function will validate the string, and incorrectly encoded UTF-8
213 * strings will be rejected.
214 *
215 * @returns iprt status code.
216 * @param psz The string.
217 * @param cch The max string length. Use RTSTR_MAX to process the entire string.
218 * @param pcwc Where to store the string length. Optional.
219 * This is undefined on failure.
220 */
221RTDECL(int) RTStrCalcUtf16LenEx(const char *psz, size_t cch, size_t *pcwc);
222
223/**
224 * Translate a UTF-8 string into a UTF-16 allocating the result buffer.
225 *
226 * @returns iprt status code.
227 * @param pszString UTF-8 string to convert.
228 * @param ppwszString Receives pointer to the allocated UTF-16 string.
229 * The returned string must be freed using RTUtf16Free().
230 */
231RTDECL(int) RTStrToUtf16(const char *pszString, PRTUTF16 *ppwszString);
232
233/**
234 * Translates pszString from UTF-8 to UTF-16, allocating the result buffer if requested.
235 *
236 * @returns iprt status code.
237 * @param pszString UTF-8 string to convert.
238 * @param cchString The maximum size in chars (the type) to convert. The conversion stop
239 * when it reaches cchString or the string terminator ('\\0').
240 * Use RTSTR_MAX to translate the entire string.
241 * @param ppwsz If cwc is non-zero, this must either be pointing to pointer to
242 * a buffer of the specified size, or pointer to a NULL pointer.
243 * If *ppwsz is NULL or cwc is zero a buffer of at least cwc items
244 * will be allocated to hold the translated string.
245 * If a buffer was requirest it must be freed using RTUtf16Free().
246 * @param cwc The buffer size in RTUTF16s. This includes the terminator.
247 * @param pcwc Where to store the length of the translated string. (Optional)
248 * This field will be updated even on failure, however the value is only
249 * specified for the following two error codes. On VERR_BUFFER_OVERFLOW
250 * and VERR_NO_STR_MEMORY it contains the required buffer space.
251 */
252RTDECL(int) RTStrToUtf16Ex(const char *pszString, size_t cchString, PRTUTF16 *ppwsz, size_t cwc, size_t *pcwc);
253
254/**
255 * Allocates tmp buffer, translates pszString from UTF8 to UCS-2.
256 *
257 * @returns iprt status code.
258 * @param ppwszString Receives pointer of allocated UCS-2 string.
259 * The returned pointer must be freed using RTStrUcs2Free().
260 * @param pszString UTF-8 string to convert.
261 * @deprecated Use RTStrToUtf16().
262 */
263DECLINLINE(int) RTStrUtf8ToUcs2(PRTUCS2 *ppwszString, const char *pszString)
264{
265 return RTStrToUtf16(pszString, ppwszString);
266}
267
268/**
269 * Translates pszString from UTF8 to backwater UCS-2, can allocate a temp buffer.
270 *
271 * @returns iprt status code.
272 * @param ppwszString Receives pointer of allocated UCS-2 string.
273 * The returned pointer must be freed using RTStrUcs2Free().
274 * @param cwc Length of target buffer in RTUCS2s including the trailing '\\0'.
275 * If 0 a temporary buffer is allocated.
276 * @param pszString UTF-8 string to convert.
277 * @deprecated Use RTStrToUtf16Ex().
278 */
279DECLINLINE(int) RTStrUtf8ToUcs2Ex(PRTUCS2 *ppwszString, unsigned cwc, const char *pszString)
280{
281 return RTStrToUtf16Ex(pszString, RTSTR_MAX, ppwszString, cwc, NULL);
282}
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 insensitive string compare between two UTF-8 strings.
1116 *
1117 * This is a simplified compare, as only the simplified lower/upper case folding
1118 * specified by the unicode specs are used. It does not consider character pairs
1119 * as they are used in some languages, just simple upper & lower case compares.
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.
1125 * @param psz2 Second UTF-8 string.
1126 */
1127RTDECL(int) RTStrICmp(const char *psz1, const char *psz2);
1128
1129/** @} */
1130
1131
1132/** @defgroup rt_str_space Unique String Space
1133 * @ingroup grp_rt_str
1134 * @{
1135 */
1136
1137/** Pointer to a string name space container node core. */
1138typedef struct RTSTRSPACECORE *PRTSTRSPACECORE;
1139/** Pointer to a pointer to a string name space container node core. */
1140typedef PRTSTRSPACECORE *PPRTSTRSPACECORE;
1141
1142/**
1143 * String name space container node core.
1144 */
1145typedef struct RTSTRSPACECORE
1146{
1147 /** Hash key. Don't touch. */
1148 uint32_t Key;
1149 /** Pointer to the left leaf node. Don't touch. */
1150 PRTSTRSPACECORE pLeft;
1151 /** Pointer to the left rigth node. Don't touch. */
1152 PRTSTRSPACECORE pRight;
1153 /** Pointer to the list of string with the same key. Don't touch. */
1154 PRTSTRSPACECORE pList;
1155 /** Height of this tree: max(heigth(left), heigth(right)) + 1. Don't touch */
1156 unsigned char uchHeight;
1157 /** The string length. Read only! */
1158 size_t cchString;
1159 /** Pointer to the string. Read only! */
1160 const char * pszString;
1161} RTSTRSPACECORE;
1162
1163/** String space. (Initialize with NULL.) */
1164typedef PRTSTRSPACECORE RTSTRSPACE;
1165/** Pointer to a string space. */
1166typedef PPRTSTRSPACECORE PRTSTRSPACE;
1167
1168
1169/**
1170 * Inserts a string into a unique string space.
1171 *
1172 * @returns true on success.
1173 * @returns false if the string collieded with an existing string.
1174 * @param pStrSpace The space to insert it into.
1175 * @param pStr The string node.
1176 */
1177RTDECL(bool) RTStrSpaceInsert(PRTSTRSPACE pStrSpace, PRTSTRSPACECORE pStr);
1178
1179/**
1180 * Removes a string from a unique string space.
1181 *
1182 * @returns Pointer to the removed string node.
1183 * @returns NULL if the string was not found in the string space.
1184 * @param pStrSpace The space to insert it into.
1185 * @param pszString The string to remove.
1186 */
1187RTDECL(PRTSTRSPACECORE) RTStrSpaceRemove(PRTSTRSPACE pStrSpace, const char *pszString);
1188
1189/**
1190 * Gets a string from a unique string space.
1191 *
1192 * @returns Pointer to the string node.
1193 * @returns NULL if the string was not found in the string space.
1194 * @param pStrSpace The space to insert it into.
1195 * @param pszString The string to get.
1196 */
1197RTDECL(PRTSTRSPACECORE) RTStrSpaceGet(PRTSTRSPACE pStrSpace, const char *pszString);
1198
1199/**
1200 * Callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy().
1201 *
1202 * @returns 0 on continue.
1203 * @returns Non-zero to aborts the operation.
1204 * @param pStr The string node
1205 * @param pvUser The user specified argument.
1206 */
1207typedef DECLCALLBACK(int) FNRTSTRSPACECALLBACK(PRTSTRSPACECORE pStr, void *pvUser);
1208/** Pointer to callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy(). */
1209typedef FNRTSTRSPACECALLBACK *PFNRTSTRSPACECALLBACK;
1210
1211/**
1212 * Destroys the string space.
1213 * The caller supplies a callback which will be called for each of
1214 * the string nodes in for freeing their memory and other resources.
1215 *
1216 * @returns 0 or what ever non-zero return value pfnCallback returned
1217 * when aborting the destruction.
1218 * @param pStrSpace The space to insert it into.
1219 * @param pfnCallback The callback.
1220 * @param pvUser The user argument.
1221 */
1222RTDECL(int) RTStrSpaceDestroy(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
1223
1224/**
1225 * Enumerates the string space.
1226 * The caller supplies a callback which will be called for each of
1227 * the string nodes.
1228 *
1229 * @returns 0 or what ever non-zero return value pfnCallback returned
1230 * when aborting the destruction.
1231 * @param pStrSpace The space to insert it into.
1232 * @param pfnCallback The callback.
1233 * @param pvUser The user argument.
1234 */
1235RTDECL(int) RTStrSpaceEnumerate(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
1236
1237/** @} */
1238
1239
1240/** @defgroup rt_str_utf16 UTF-16 String Manipulation
1241 * @ingroup grp_rt_str
1242 * @{
1243 */
1244
1245/**
1246 * Free a UTF-16 string allocated by RTStrUtf8ToUtf16(), RTStrUtf8ToUtf16Ex(),
1247 * RTUtf16Dup() or RTUtf16DupEx().
1248 *
1249 * @returns iprt status code.
1250 * @param pwszString The UTF-16 string to free. NULL is accepted.
1251 */
1252RTDECL(void) RTUtf16Free(PRTUTF16 pwszString);
1253
1254/**
1255 * Allocates a new copy of the specified UTF-16 string.
1256 *
1257 * @returns Pointer to the allocated string copy. Use RTUtf16Free() to free it.
1258 * @returns NULL when out of memory.
1259 * @param pwszString UTF-16 string to duplicate.
1260 * @remark This function will not make any attempt to validate the encoding.
1261 */
1262RTDECL(PRTUTF16) RTUtf16Dup(PCRTUTF16 pwszString);
1263
1264/**
1265 * Allocates a new copy of the specified UTF-16 string.
1266 *
1267 * @returns iprt status code.
1268 * @param ppwszString Receives pointer of the allocated UTF-16 string.
1269 * The returned pointer must be freed using RTUtf16Free().
1270 * @param pwszString UTF-16 string to duplicate.
1271 * @param cwcExtra Number of extra RTUTF16 items to allocate.
1272 * @remark This function will not make any attempt to validate the encoding.
1273 */
1274RTDECL(int) RTUtf16DupEx(PRTUTF16 *ppwszString, PCRTUTF16 pwszString, size_t cwcExtra);
1275
1276/**
1277 * Returns the length of a UTF-16 string in UTF-16 characters
1278 * without trailing '\\0'.
1279 *
1280 * Surrogate pairs counts as two UTF-16 characters here. Use RTUtf16CpCnt()
1281 * to get the exact number of code points in the string.
1282 *
1283 * @returns The number of RTUTF16 items in the string.
1284 * @param pwszString Pointer the UTF-16 string.
1285 * @remark This function will not make any attempt to validate the encoding.
1286 */
1287RTDECL(size_t) RTUtf16Len(PCRTUTF16 pwszString);
1288
1289/**
1290 * Performs a case sensitive string compare between two UTF-16 strings.
1291 *
1292 * @returns < 0 if the first string less than the second string.s
1293 * @returns 0 if the first string identical to the second string.
1294 * @returns > 0 if the first string greater than the second string.
1295 * @param pwsz1 First UTF-16 string.
1296 * @param pwsz2 Second UTF-16 string.
1297 * @remark This function will not make any attempt to validate the encoding.
1298 */
1299RTDECL(int) RTUtf16Cmp(register PCRTUTF16 pwsz1, register PCRTUTF16 pwsz2);
1300
1301/**
1302 * Performs a case insensitive string compare between two UTF-16 strings.
1303 *
1304 * This is a simplified compare, as only the simplified lower/upper case folding
1305 * specified by the unicode specs are used. It does not consider character pairs
1306 * as they are used in some languages, just simple upper & lower case compares.
1307 *
1308 * @returns < 0 if the first string less than the second string.
1309 * @returns 0 if the first string identical to the second string.
1310 * @returns > 0 if the first string greater than the second string.
1311 * @param pwsz1 First UTF-16 string.
1312 * @param pwsz2 Second UTF-16 string.
1313 */
1314RTDECL(int) RTUtf16ICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
1315
1316/**
1317 * Performs a case insensitive string compare between two UTF-16 strings
1318 * using the current locale of the process (if applicable).
1319 *
1320 * This differs from RTUtf16ICmp() in that it will try, if a locale with the
1321 * required data is available, to do a correct case-insensitive compare. It
1322 * follows that it is more complex and thereby likely to be more expensive.
1323 *
1324 * @returns < 0 if the first string less than the second string.
1325 * @returns 0 if the first string identical to the second string.
1326 * @returns > 0 if the first string greater than the second string.
1327 * @param pwsz1 First UTF-16 string.
1328 * @param pwsz2 Second UTF-16 string.
1329 */
1330RTDECL(int) RTUtf16LocaleICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
1331
1332/**
1333 * Folds a UTF-16 string to lowercase.
1334 *
1335 * This is a very simple folding; is uses the simple lowercase
1336 * code point, it is not related to any locale just the most common
1337 * lowercase codepoint setup by the unicode specs, and it will not
1338 * create new surrogate pairs or remove existing ones.
1339 *
1340 * @returns Pointer to the passed in string.
1341 * @param pwsz The string to fold.
1342 */
1343RTDECL(PRTUTF16) RTUtf16ToLower(PRTUTF16 pwsz);
1344
1345/**
1346 * Folds a UTF-16 string to uppercase.
1347 *
1348 * This is a very simple folding; is uses the simple uppercase
1349 * code point, it is not related to any locale just the most common
1350 * uppercase codepoint setup by the unicode specs, and it will not
1351 * create new surrogate pairs or remove existing ones.
1352 *
1353 * @returns Pointer to the passed in string.
1354 * @param pwsz The string to fold.
1355 */
1356RTDECL(PRTUTF16) RTUtf16ToUpper(PRTUTF16 pwsz);
1357
1358/**
1359 * Translate a UTF-16 string into a UTF-8 allocating the result buffer.
1360 *
1361 * @returns iprt status code.
1362 * @param pwszString UTF-16 string to convert.
1363 * @param ppszString Receives pointer of allocated UTF-8 string.
1364 * The returned pointer must be freed using RTStrFree().
1365 */
1366RTDECL(int) RTUtf16ToUtf8(PCRTUTF16 pwszString, char **ppszString);
1367
1368/**
1369 * Translates UTF-16 to UTF-8 using buffer provided by the caller or
1370 * a fittingly sized buffer allocated by the function.
1371 *
1372 * @returns iprt status code.
1373 * @param pwszString The UTF-16 string to convert.
1374 * @param cwcString The number of RTUTF16 items to translation from pwszString.
1375 * The translate will stop when reaching cwcString or the terminator ('\\0').
1376 * Use RTSTR_MAX to translate the entire string.
1377 * @param ppsz If cch is non-zero, this must either be pointing to pointer to
1378 * a buffer of the specified size, or pointer to a NULL pointer.
1379 * If *ppsz is NULL or cch is zero a buffer of at least cch chars
1380 * will be allocated to hold the translated string.
1381 * If a buffer was requirest it must be freed using RTUtf16Free().
1382 * @param cch The buffer size in chars (the type). This includes the terminator.
1383 * @param pcch Where to store the length of the translated string. (Optional)
1384 * This field will be updated even on failure, however the value is only
1385 * specified for the following two error codes. On VERR_BUFFER_OVERFLOW
1386 * and VERR_NO_STR_MEMORY it contains the required buffer space.
1387 */
1388RTDECL(int) RTUtf16ToUtf8Ex(PCRTUTF16 pwszString, size_t cwcString, char **ppsz, size_t cch, size_t *pcch);
1389
1390/**
1391 * Calculates the length of the UTF-16 string in UTF-8 chars (bytes).
1392 *
1393 * This function will validate the string, and incorrectly encoded UTF-16
1394 * strings will be rejected. The primary purpose of this function is to
1395 * help allocate buffers for RTUtf16ToUtf8() of the correct size. For most
1396 * other puroses RTUtf16ToUtf8Ex() should be used.
1397 *
1398 * @returns Number of char (bytes).
1399 * @returns 0 if the string was incorrectly encoded.
1400 * @param pwsz The UTF-16 string.
1401 */
1402RTDECL(size_t) RTUtf16CalcUtf8Len(PCRTUTF16 pwsz);
1403
1404/**
1405 * Calculates the length of the UTF-16 string in UTF-8 chars (bytes).
1406 *
1407 * This function will validate the string, and incorrectly encoded UTF-16
1408 * strings will be rejected.
1409 *
1410 * @returns iprt status code.
1411 * @param pwsz The string.
1412 * @param cwc The max string length. Use RTSTR_MAX to process the entire string.
1413 * @param pcch Where to store the string length (in bytes). Optional.
1414 * This is undefined on failure.
1415 */
1416RTDECL(int) RTUtf16CalcUtf8LenEx(PCRTUTF16 pwsz, size_t cwc, size_t *pcch);
1417
1418/**
1419 * Allocates tmp buffer, translates pwszString from UCS-2 to UTF8.
1420 *
1421 * @returns iprt status code.
1422 * @param ppszString Receives pointer of allocated UTF8 string.
1423 * The returned pointer must be freed using RTStrFree().
1424 * @param pwszString UCS-2 string to convert.
1425 * @deprecated Use RTUtf16ToUtf8().
1426 */
1427DECLINLINE(int) RTStrUcs2ToUtf8(char **ppszString, PCRTUCS2 pwszString)
1428{
1429 return RTUtf16ToUtf8(pwszString, ppszString);
1430}
1431
1432/**
1433 * Translates UCS-2 to UTF-8 using buffer provided by the caller or
1434 * a fittingly sized buffer allocated by the function.
1435 *
1436 * @returns iprt status code.
1437 * @param ppszString If cch is not zero, this points to the pointer to the
1438 * buffer where the converted string shall be resulted.
1439 * If cch is zero, this is where the pointer to the allocated
1440 * buffer with the converted string is stored. The allocated
1441 * buffer must be freed by using RTStrFree().
1442 * @param cch Size of the passed in buffer (*ppszString).
1443 * If 0 a fittingly sized buffer is allocated.
1444 * @param pwszString UCS-2 string to convert.
1445 * @deprecated
1446 */
1447DECLINLINE(int) RTStrUcs2ToUtf8Ex(char **ppszString, size_t cch, PCRTUCS2 pwszString)
1448{
1449 return RTUtf16ToUtf8Ex(pwszString, RTSTR_MAX, ppszString, cch, NULL);
1450}
1451
1452/**
1453 * Free a UCS-2 string allocated by RTStrUtf8ToUcs2().
1454 *
1455 * @returns iprt status code.
1456 * @param pwszString Pointer to buffer with unicode string to free.
1457 * NULL is accepted.
1458 * @deprecated
1459 */
1460DECLINLINE(void) RTStrUcs2Free(PRTUCS2 pwszString)
1461{
1462 RTUtf16Free(pwszString);
1463}
1464
1465/**
1466 * Allocates a new copy of the given UCS-2 string.
1467 *
1468 * @returns Pointer to the allocated string copy. Use RTStrUcs2Free() to free it.
1469 * @returns NULL when out of memory.
1470 * @param pwszString UCS-2 string to duplicate.
1471 * @deprecated
1472 */
1473DECLINLINE(PRTUCS2) RTStrUcs2Dup(PCRTUCS2 pwszString)
1474{
1475 return RTUtf16Dup(pwszString);
1476}
1477
1478/**
1479 * Allocates a new copy of the given UCS-2 string.
1480 *
1481 * @returns iprt status code.
1482 * @param ppwszString Receives pointer of the allocated UCS-2 string.
1483 * The returned pointer must be freed using RTStrUcs2Free().
1484 * @param pwszString UCS-2 string to duplicate.
1485 * @deprecated
1486 */
1487DECLINLINE(int) RTStrUcs2DupEx(PRTUCS2 *ppwszString, PCRTUCS2 pwszString)
1488{
1489 return RTUtf16DupEx(ppwszString, pwszString, 0);
1490}
1491
1492/**
1493 * Returns the length of a UCS-2 string in UCS-2 characters
1494 * without trailing '\\0'.
1495 *
1496 * @returns Length of input string in UCS-2 characters.
1497 * @param pwszString Pointer the UCS-2 string.
1498 * @deprecated
1499 */
1500DECLINLINE(size_t) RTStrUcs2Len(PCRTUCS2 pwszString)
1501{
1502 return RTUtf16Len(pwszString);
1503}
1504
1505/**
1506 * Performs a case sensitive string compare between two UCS-2 strings.
1507 *
1508 * @returns < 0 if the first string less than the second string.
1509 * @returns 0 if the first string identical to the second string.
1510 * @returns > 0 if the first string greater than the second string.
1511 * @param pwsz1 First UCS-2 string.
1512 * @param pwsz2 Second UCS-2 string.
1513 * @deprecated
1514 */
1515DECLINLINE(int) RTStrUcs2Cmp(register PCRTUCS2 pwsz1, register PCRTUCS2 pwsz2)
1516{
1517 return RTUtf16Cmp(pwsz1, pwsz2);
1518}
1519
1520
1521/**
1522 * Get the unicode code point at the given string position.
1523 *
1524 * @returns unicode code point.
1525 * @returns RTUNICP_INVALID if the encoding is invalid.
1526 * @param pwsz The string.
1527 *
1528 * @remark This is an internal worker for RTUtf16GetCp().
1529 */
1530RTDECL(RTUNICP) RTUtf16GetCpInternal(PCRTUTF16 pwsz);
1531
1532/**
1533 * Get the unicode code point at the given string position.
1534 *
1535 * @returns iprt status code.
1536 * @param ppwsz Pointer to the string pointer. This will be updated to
1537 * point to the char following the current code point.
1538 * @param pCp Where to store the code point.
1539 * RTUNICP_INVALID is stored here on failure.
1540 *
1541 * @remark This is an internal worker for RTUtf16GetCpEx().
1542 */
1543RTDECL(int) RTUtf16GetCpExInternal(PCRTUTF16 *ppwsz, PRTUNICP pCp);
1544
1545/**
1546 * Put the unicode code point at the given string position
1547 * and return the pointer to the char following it.
1548 *
1549 * This function will not consider anything at or following the the
1550 * buffer area pointed to by pwsz. It is therefore not suitable for
1551 * inserting code points into a string, only appending/overwriting.
1552 *
1553 * @returns pointer to the char following the written code point.
1554 * @param pwsz The string.
1555 * @param CodePoint The code point to write.
1556 * This sould not be RTUNICP_INVALID or any other charater
1557 * out of the UTF-16 range.
1558 *
1559 * @remark This is an internal worker for RTUtf16GetCpEx().
1560 */
1561RTDECL(PRTUTF16) RTUtf16PutCpInternal(PRTUTF16 pwsz, RTUNICP CodePoint);
1562
1563/**
1564 * Get the unicode code point at the given string position.
1565 *
1566 * @returns unicode code point.
1567 * @returns RTUNICP_INVALID if the encoding is invalid.
1568 * @param pwsz The string.
1569 *
1570 * @remark We optimize this operation by using an inline function for
1571 * everything which isn't a surrogate pair or an endian indicator.
1572 */
1573DECLINLINE(RTUNICP) RTUtf16GetCp(PCRTUTF16 pwsz)
1574{
1575 const RTUTF16 wc = *pwsz;
1576 if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
1577 return wc;
1578 return RTUtf16GetCpInternal(pwsz);
1579}
1580
1581/**
1582 * Get the unicode code point at the given string position.
1583 *
1584 * @returns iprt status code.
1585 * @param ppwsz Pointer to the string pointer. This will be updated to
1586 * point to the char following the current code point.
1587 * @param pCp Where to store the code point.
1588 * RTUNICP_INVALID is stored here on failure.
1589 *
1590 * @remark We optimize this operation by using an inline function for
1591 * everything which isn't a surrogate pair or and endian indicator.
1592 */
1593DECLINLINE(int) RTUtf16GetCpEx(PCRTUTF16 *ppwsz, PRTUNICP pCp)
1594{
1595 const RTUTF16 wc = **ppwsz;
1596 if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
1597 {
1598 (*ppwsz)++;
1599 *pCp = wc;
1600 return VINF_SUCCESS;
1601 }
1602 return RTUtf16GetCpExInternal(ppwsz, pCp);
1603}
1604
1605/**
1606 * Put the unicode code point at the given string position
1607 * and return the pointer to the char following it.
1608 *
1609 * This function will not consider anything at or following the the
1610 * buffer area pointed to by pwsz. It is therefore not suitable for
1611 * inserting code points into a string, only appending/overwriting.
1612 *
1613 * @returns pointer to the char following the written code point.
1614 * @param pwsz The string.
1615 * @param CodePoint The code point to write.
1616 * This sould not be RTUNICP_INVALID or any other charater
1617 * out of the UTF-16 range.
1618 *
1619 * @remark We optimize this operation by using an inline function for
1620 * everything which isn't a surrogate pair or and endian indicator.
1621 */
1622DECLINLINE(PRTUTF16) RTUtf16PutCp(PRTUTF16 pwsz, RTUNICP CodePoint)
1623{
1624 if (CodePoint < 0xd800 || (CodePoint > 0xd800 && CodePoint < 0xfffe))
1625 {
1626 *pwsz++ = (RTUTF16)CodePoint;
1627 return pwsz;
1628 }
1629 return RTUtf16PutCpInternal(pwsz, CodePoint);
1630}
1631
1632/**
1633 * Skips ahead, past the current code point.
1634 *
1635 * @returns Pointer to the char after the current code point.
1636 * @param pwsz Pointer to the current code point.
1637 * @remark This will not move the next valid code point, only past the current one.
1638 */
1639DECLINLINE(PRTUTF16) RTUtf16NextCp(PCRTUTF16 pwsz)
1640{
1641 RTUNICP Cp;
1642 RTUtf16GetCpEx(&pwsz, &Cp);
1643 return (PRTUTF16)pwsz;
1644}
1645
1646/**
1647 * Skips backwards, to the previous code point.
1648 *
1649 * @returns Pointer to the char after the current code point.
1650 * @param pwszStart Pointer to the start of the string.
1651 * @param pwsz Pointer to the current code point.
1652 */
1653RTDECL(PRTUTF16) RTUtf16PrevCp(PCRTUTF16 pwszStart, PCRTUTF16 pwsz);
1654
1655
1656/**
1657 * Checks if the UTF-16 char is the high surrogate char (i.e.
1658 * the 1st char in the pair).
1659 *
1660 * @returns true if it is.
1661 * @returns false if it isn't.
1662 * @param wc The character to investigate.
1663 */
1664DECLINLINE(bool) RTUtf16IsHighSurrogate(RTUTF16 wc)
1665{
1666 return wc >= 0xd800 && wc <= 0xdbff;
1667}
1668
1669/**
1670 * Checks if the UTF-16 char is the low surrogate char (i.e.
1671 * the 2nd char in the pair).
1672 *
1673 * @returns true if it is.
1674 * @returns false if it isn't.
1675 * @param wc The character to investigate.
1676 */
1677DECLINLINE(bool) RTUtf16IsLowSurrogate(RTUTF16 wc)
1678{
1679 return wc >= 0xdc00 && wc <= 0xdfff;
1680}
1681
1682
1683/**
1684 * Checks if the two UTF-16 chars form a valid surrogate pair.
1685 *
1686 * @returns true if they do.
1687 * @returns false if they doesn't.
1688 * @param wcHigh The high (1st) character.
1689 * @param wcLow The low (2nd) character.
1690 */
1691DECLINLINE(bool) RTUtf16IsSurrogatePair(RTUTF16 wcHigh, RTUTF16 wcLow)
1692{
1693 return RTUtf16IsHighSurrogate(wcHigh)
1694 && RTUtf16IsLowSurrogate(wcLow);
1695}
1696
1697/** @} */
1698
1699__END_DECLS
1700
1701/** @} */
1702
1703#endif
1704
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