VirtualBox

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

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

Added warnings about trailing chars & spaces in the RTStrTo*Int* functions. Also added a new variant, RTStrTo*Int*Full, that will fail if there are trailing chars or spaces.

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