VirtualBox

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

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

The Big Sun Rebranding Header Change

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

© 2023 Oracle
ContactPrivacy policyTerms of Use