VirtualBox

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

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

iprt/string.h: Added the macros RT_ZERO(Obj) and RT_BZERO(pv, cb) for shortening memset(&Obj, 0, sizeof(Obj)) and memset(pv, 0, cb).

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