VirtualBox

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

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

UCS-2 -> UTF-16.

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