VirtualBox

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

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

iprt: typo of the typo

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 69.2 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/system.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 Reserved 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 requested 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 purposes 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 requested 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
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 should not be RTUNICP_INVALID or any other
373 * character 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
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 should not be RTUNICP_INVALID or any other
471 * character 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 * Locates a case sensitive substring.
887 *
888 * If any of the two strings are NULL, then NULL is returned. If the needle is
889 * an empty string, then the haystack is returned (i.e. matches anything).
890 *
891 * @returns Pointer to the first occurrence of the substring if found, NULL if
892 * not.
893 *
894 * @param pszHaystack The string to search.
895 * @param pszNeedle The substring to search for.
896 *
897 * @remarks The difference between this and strstr is the handling of NULL
898 * pointers.
899 */
900RTDECL(char *) RTStrStr(const char *pszHaystack, const char *pszNeedle);
901
902/**
903 * Locates a case insensitive substring.
904 *
905 * If any of the two strings are NULL, then NULL is returned. If the needle is
906 * an empty string, then the haystack is returned (i.e. matches anything).
907 *
908 * @returns Pointer to the first occurrence of the substring if found, NULL if
909 * not.
910 *
911 * @param pszHaystack The string to search.
912 * @param pszNeedle The substring to search for.
913 *
914 */
915RTDECL(char *) RTStrIStr(const char *pszHaystack, const char *pszNeedle);
916
917/**
918 * Converts the string to lower case.
919 *
920 * @returns Pointer to the converted string.
921 * @param psz The string to convert.
922 */
923RTDECL(char *) RTStrToLower(char *psz);
924
925/**
926 * Converts the string to upper case.
927 *
928 * @returns Pointer to the converted string.
929 * @param psz The string to convert.
930 */
931RTDECL(char *) RTStrToUpper(char *psz);
932
933/**
934 * Find the length of a zero-terminated byte string, given
935 * a max string length.
936 *
937 * See also RTStrNLenEx.
938 *
939 * @returns The string length or cbMax. The returned length does not include
940 * the zero terminator if it was found.
941 *
942 * @param pszString The string.
943 * @param cchMax The max string length.
944 */
945RTDECL(size_t) RTStrNLen(const char *pszString, size_t cchMax);
946
947/**
948 * Find the length of a zero-terminated byte string, given
949 * a max string length.
950 *
951 * See also RTStrNLen.
952 *
953 * @returns IPRT status code.
954 * @retval VINF_SUCCESS if the string has a length less than cchMax.
955 * @retval VERR_BUFFER_OVERFLOW if the end of the string wasn't found
956 * before cchMax was reached.
957 *
958 * @param pszString The string.
959 * @param cchMax The max string length.
960 * @param pcch Where to store the string length excluding the
961 * terminator. This is set to cchMax if the terminator
962 * isn't found.
963 */
964RTDECL(int) RTStrNLenEx(const char *pszString, size_t cchMax, size_t *pcch);
965
966/**
967 * Matches a simple string pattern.
968 *
969 * @returns true if the string matches the pattern, otherwise false.
970 *
971 * @param pszPattern The pattern. Special chars are '*' and '?', where the
972 * asterisk matches zero or more characters and question
973 * mark matches exactly one character.
974 * @param pszString The string to match against the pattern.
975 */
976RTDECL(bool) RTStrSimplePatternMatch(const char *pszPattern, const char *pszString);
977
978/**
979 * Matches a simple string pattern, neither which needs to be zero terminated.
980 *
981 * This is identical to RTStrSimplePatternMatch except that you can optionally
982 * specify the length of both the pattern and the string. The function will
983 * stop when it hits a string terminator or either of the lengths.
984 *
985 * @returns true if the string matches the pattern, otherwise false.
986 *
987 * @param pszPattern The pattern. Special chars are '*' and '?', where the
988 * asterisk matches zero or more characters and question
989 * mark matches exactly one character.
990 * @param cchPattern The pattern length. Pass RTSTR_MAX if you don't know the
991 * length and wish to stop at the string terminator.
992 * @param pszString The string to match against the pattern.
993 * @param cchString The string length. Pass RTSTR_MAX if you don't know the
994 * length and wish to match up to the string terminator.
995 */
996RTDECL(bool) RTStrSimplePatternNMatch(const char *pszPattern, size_t cchPattern,
997 const char *pszString, size_t cchString);
998
999/**
1000 * Matches multiple patterns against a string.
1001 *
1002 * The patterns are separated by the pipe character (|).
1003 *
1004 * @returns true if the string matches the pattern, otherwise false.
1005 *
1006 * @param pszPatterns The patterns.
1007 * @param cchPatterns The lengths of the patterns to use. Pass RTSTR_MAX to
1008 * stop at the terminator.
1009 * @param pszString The string to match against the pattern.
1010 * @param cchString The string length. Pass RTSTR_MAX stop stop at the
1011 * terminator.
1012 * @param poffPattern Offset into the patterns string of the patttern that
1013 * matched. If no match, this will be set to RTSTR_MAX.
1014 * This is optional, NULL is fine.
1015 */
1016RTDECL(bool) RTStrSimplePatternMultiMatch(const char *pszPatterns, size_t cchPatterns,
1017 const char *pszString, size_t cchString,
1018 size_t *poffPattern);
1019
1020
1021/** @defgroup rt_str_conv String To/From Number Conversions
1022 * @ingroup grp_rt_str
1023 * @{ */
1024
1025/**
1026 * Converts a string representation of a number to a 64-bit unsigned number.
1027 *
1028 * @returns iprt status code.
1029 * Warnings are used to indicate conversion problems.
1030 * @retval VWRN_NUMBER_TOO_BIG
1031 * @retval VWRN_NEGATIVE_UNSIGNED
1032 * @retval VWRN_TRAILING_CHARS
1033 * @retval VWRN_TRAILING_SPACES
1034 * @retval VINF_SUCCESS
1035 * @retval VERR_NO_DIGITS
1036 *
1037 * @param pszValue Pointer to the string value.
1038 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1039 * @param uBase The base of the representation used.
1040 * If 0 the function will look for known prefixes before defaulting to 10.
1041 * @param pu64 Where to store the converted number. (optional)
1042 */
1043RTDECL(int) RTStrToUInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint64_t *pu64);
1044
1045/**
1046 * Converts a string representation of a number to a 64-bit unsigned number,
1047 * making sure the full string is converted.
1048 *
1049 * @returns iprt status code.
1050 * Warnings are used to indicate conversion problems.
1051 * @retval VWRN_NUMBER_TOO_BIG
1052 * @retval VWRN_NEGATIVE_UNSIGNED
1053 * @retval VINF_SUCCESS
1054 * @retval VERR_NO_DIGITS
1055 * @retval VERR_TRAILING_SPACES
1056 * @retval VERR_TRAILING_CHARS
1057 *
1058 * @param pszValue Pointer to the string value.
1059 * @param uBase The base of the representation used.
1060 * If 0 the function will look for known prefixes before defaulting to 10.
1061 * @param pu64 Where to store the converted number. (optional)
1062 */
1063RTDECL(int) RTStrToUInt64Full(const char *pszValue, unsigned uBase, uint64_t *pu64);
1064
1065/**
1066 * Converts a string representation of a number to a 64-bit unsigned number.
1067 * The base is guessed.
1068 *
1069 * @returns 64-bit unsigned number on success.
1070 * @returns 0 on failure.
1071 * @param pszValue Pointer to the string value.
1072 */
1073RTDECL(uint64_t) RTStrToUInt64(const char *pszValue);
1074
1075/**
1076 * Converts a string representation of a number to a 32-bit unsigned number.
1077 *
1078 * @returns iprt status code.
1079 * Warnings are used to indicate conversion problems.
1080 * @retval VWRN_NUMBER_TOO_BIG
1081 * @retval VWRN_NEGATIVE_UNSIGNED
1082 * @retval VWRN_TRAILING_CHARS
1083 * @retval VWRN_TRAILING_SPACES
1084 * @retval VINF_SUCCESS
1085 * @retval VERR_NO_DIGITS
1086 *
1087 * @param pszValue Pointer to the string value.
1088 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1089 * @param uBase The base of the representation used.
1090 * If 0 the function will look for known prefixes before defaulting to 10.
1091 * @param pu32 Where to store the converted number. (optional)
1092 */
1093RTDECL(int) RTStrToUInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint32_t *pu32);
1094
1095/**
1096 * Converts a string representation of a number to a 32-bit unsigned number,
1097 * making sure the full string is converted.
1098 *
1099 * @returns iprt status code.
1100 * Warnings are used to indicate conversion problems.
1101 * @retval VWRN_NUMBER_TOO_BIG
1102 * @retval VWRN_NEGATIVE_UNSIGNED
1103 * @retval VINF_SUCCESS
1104 * @retval VERR_NO_DIGITS
1105 * @retval VERR_TRAILING_SPACES
1106 * @retval VERR_TRAILING_CHARS
1107 *
1108 * @param pszValue Pointer to the string value.
1109 * @param uBase The base of the representation used.
1110 * If 0 the function will look for known prefixes before defaulting to 10.
1111 * @param pu32 Where to store the converted number. (optional)
1112 */
1113RTDECL(int) RTStrToUInt32Full(const char *pszValue, unsigned uBase, uint32_t *pu32);
1114
1115/**
1116 * Converts a string representation of a number to a 64-bit unsigned number.
1117 * The base is guessed.
1118 *
1119 * @returns 32-bit unsigned number on success.
1120 * @returns 0 on failure.
1121 * @param pszValue Pointer to the string value.
1122 */
1123RTDECL(uint32_t) RTStrToUInt32(const char *pszValue);
1124
1125/**
1126 * Converts a string representation of a number to a 16-bit unsigned number.
1127 *
1128 * @returns iprt status code.
1129 * Warnings are used to indicate conversion problems.
1130 * @retval VWRN_NUMBER_TOO_BIG
1131 * @retval VWRN_NEGATIVE_UNSIGNED
1132 * @retval VWRN_TRAILING_CHARS
1133 * @retval VWRN_TRAILING_SPACES
1134 * @retval VINF_SUCCESS
1135 * @retval VERR_NO_DIGITS
1136 *
1137 * @param pszValue Pointer to the string value.
1138 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1139 * @param uBase The base of the representation used.
1140 * If 0 the function will look for known prefixes before defaulting to 10.
1141 * @param pu16 Where to store the converted number. (optional)
1142 */
1143RTDECL(int) RTStrToUInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint16_t *pu16);
1144
1145/**
1146 * Converts a string representation of a number to a 16-bit unsigned number,
1147 * making sure the full string is converted.
1148 *
1149 * @returns iprt status code.
1150 * Warnings are used to indicate conversion problems.
1151 * @retval VWRN_NUMBER_TOO_BIG
1152 * @retval VWRN_NEGATIVE_UNSIGNED
1153 * @retval VINF_SUCCESS
1154 * @retval VERR_NO_DIGITS
1155 * @retval VERR_TRAILING_SPACES
1156 * @retval VERR_TRAILING_CHARS
1157 *
1158 * @param pszValue Pointer to the string value.
1159 * @param uBase The base of the representation used.
1160 * If 0 the function will look for known prefixes before defaulting to 10.
1161 * @param pu16 Where to store the converted number. (optional)
1162 */
1163RTDECL(int) RTStrToUInt16Full(const char *pszValue, unsigned uBase, uint16_t *pu16);
1164
1165/**
1166 * Converts a string representation of a number to a 16-bit unsigned number.
1167 * The base is guessed.
1168 *
1169 * @returns 16-bit unsigned number on success.
1170 * @returns 0 on failure.
1171 * @param pszValue Pointer to the string value.
1172 */
1173RTDECL(uint16_t) RTStrToUInt16(const char *pszValue);
1174
1175/**
1176 * Converts a string representation of a number to a 8-bit unsigned number.
1177 *
1178 * @returns iprt status code.
1179 * Warnings are used to indicate conversion problems.
1180 * @retval VWRN_NUMBER_TOO_BIG
1181 * @retval VWRN_NEGATIVE_UNSIGNED
1182 * @retval VWRN_TRAILING_CHARS
1183 * @retval VWRN_TRAILING_SPACES
1184 * @retval VINF_SUCCESS
1185 * @retval VERR_NO_DIGITS
1186 *
1187 * @param pszValue Pointer to the string value.
1188 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1189 * @param uBase The base of the representation used.
1190 * If 0 the function will look for known prefixes before defaulting to 10.
1191 * @param pu8 Where to store the converted number. (optional)
1192 */
1193RTDECL(int) RTStrToUInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint8_t *pu8);
1194
1195/**
1196 * Converts a string representation of a number to a 8-bit unsigned number,
1197 * making sure the full string is converted.
1198 *
1199 * @returns iprt status code.
1200 * Warnings are used to indicate conversion problems.
1201 * @retval VWRN_NUMBER_TOO_BIG
1202 * @retval VWRN_NEGATIVE_UNSIGNED
1203 * @retval VINF_SUCCESS
1204 * @retval VERR_NO_DIGITS
1205 * @retval VERR_TRAILING_SPACES
1206 * @retval VERR_TRAILING_CHARS
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 pu8 Where to store the converted number. (optional)
1212 */
1213RTDECL(int) RTStrToUInt8Full(const char *pszValue, unsigned uBase, uint8_t *pu8);
1214
1215/**
1216 * Converts a string representation of a number to a 8-bit unsigned number.
1217 * The base is guessed.
1218 *
1219 * @returns 8-bit unsigned number on success.
1220 * @returns 0 on failure.
1221 * @param pszValue Pointer to the string value.
1222 */
1223RTDECL(uint8_t) RTStrToUInt8(const char *pszValue);
1224
1225/**
1226 * Converts a string representation of a number to a 64-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 pi64 Where to store the converted number. (optional)
1241 */
1242RTDECL(int) RTStrToInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, int64_t *pi64);
1243
1244/**
1245 * Converts a string representation of a number to a 64-bit signed number,
1246 * making sure the full string is converted.
1247 *
1248 * @returns iprt status code.
1249 * Warnings are used to indicate conversion 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 pi64 Where to store the converted number. (optional)
1260 */
1261RTDECL(int) RTStrToInt64Full(const char *pszValue, unsigned uBase, int64_t *pi64);
1262
1263/**
1264 * Converts a string representation of a number to a 64-bit signed number.
1265 * The base is guessed.
1266 *
1267 * @returns 64-bit signed number on success.
1268 * @returns 0 on failure.
1269 * @param pszValue Pointer to the string value.
1270 */
1271RTDECL(int64_t) RTStrToInt64(const char *pszValue);
1272
1273/**
1274 * Converts a string representation of a number to a 32-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 pi32 Where to store the converted number. (optional)
1289 */
1290RTDECL(int) RTStrToInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, int32_t *pi32);
1291
1292/**
1293 * Converts a string representation of a number to a 32-bit signed number,
1294 * making sure the full string is converted.
1295 *
1296 * @returns iprt status code.
1297 * Warnings are used to indicate conversion 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 pi32 Where to store the converted number. (optional)
1308 */
1309RTDECL(int) RTStrToInt32Full(const char *pszValue, unsigned uBase, int32_t *pi32);
1310
1311/**
1312 * Converts a string representation of a number to a 32-bit signed number.
1313 * The base is guessed.
1314 *
1315 * @returns 32-bit signed number on success.
1316 * @returns 0 on failure.
1317 * @param pszValue Pointer to the string value.
1318 */
1319RTDECL(int32_t) RTStrToInt32(const char *pszValue);
1320
1321/**
1322 * Converts a string representation of a number to a 16-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 pi16 Where to store the converted number. (optional)
1337 */
1338RTDECL(int) RTStrToInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, int16_t *pi16);
1339
1340/**
1341 * Converts a string representation of a number to a 16-bit signed number,
1342 * making sure the full string is converted.
1343 *
1344 * @returns iprt status code.
1345 * Warnings are used to indicate conversion 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 pi16 Where to store the converted number. (optional)
1356 */
1357RTDECL(int) RTStrToInt16Full(const char *pszValue, unsigned uBase, int16_t *pi16);
1358
1359/**
1360 * Converts a string representation of a number to a 16-bit signed number.
1361 * The base is guessed.
1362 *
1363 * @returns 16-bit signed number on success.
1364 * @returns 0 on failure.
1365 * @param pszValue Pointer to the string value.
1366 */
1367RTDECL(int16_t) RTStrToInt16(const char *pszValue);
1368
1369/**
1370 * Converts a string representation of a number to a 8-bit signed number.
1371 *
1372 * @returns iprt status code.
1373 * Warnings are used to indicate conversion problems.
1374 * @retval VWRN_NUMBER_TOO_BIG
1375 * @retval VWRN_TRAILING_CHARS
1376 * @retval VWRN_TRAILING_SPACES
1377 * @retval VINF_SUCCESS
1378 * @retval VERR_NO_DIGITS
1379 *
1380 * @param pszValue Pointer to the string value.
1381 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1382 * @param uBase The base of the representation used.
1383 * If 0 the function will look for known prefixes before defaulting to 10.
1384 * @param pi8 Where to store the converted number. (optional)
1385 */
1386RTDECL(int) RTStrToInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, int8_t *pi8);
1387
1388/**
1389 * Converts a string representation of a number to a 8-bit signed number,
1390 * making sure the full string is converted.
1391 *
1392 * @returns iprt status code.
1393 * Warnings are used to indicate conversion problems.
1394 * @retval VWRN_NUMBER_TOO_BIG
1395 * @retval VINF_SUCCESS
1396 * @retval VERR_TRAILING_CHARS
1397 * @retval VERR_TRAILING_SPACES
1398 * @retval VERR_NO_DIGITS
1399 *
1400 * @param pszValue Pointer to the string value.
1401 * @param uBase The base of the representation used.
1402 * If 0 the function will look for known prefixes before defaulting to 10.
1403 * @param pi8 Where to store the converted number. (optional)
1404 */
1405RTDECL(int) RTStrToInt8Full(const char *pszValue, unsigned uBase, int8_t *pi8);
1406
1407/**
1408 * Converts a string representation of a number to a 8-bit signed number.
1409 * The base is guessed.
1410 *
1411 * @returns 8-bit signed number on success.
1412 * @returns 0 on failure.
1413 * @param pszValue Pointer to the string value.
1414 */
1415RTDECL(int8_t) RTStrToInt8(const char *pszValue);
1416
1417/** @} */
1418
1419
1420/** @defgroup rt_str_space Unique String Space
1421 * @ingroup grp_rt_str
1422 * @{
1423 */
1424
1425/** Pointer to a string name space container node core. */
1426typedef struct RTSTRSPACECORE *PRTSTRSPACECORE;
1427/** Pointer to a pointer to a string name space container node core. */
1428typedef PRTSTRSPACECORE *PPRTSTRSPACECORE;
1429
1430/**
1431 * String name space container node core.
1432 */
1433typedef struct RTSTRSPACECORE
1434{
1435 /** Hash key. Don't touch. */
1436 uint32_t Key;
1437 /** Pointer to the left leaf node. Don't touch. */
1438 PRTSTRSPACECORE pLeft;
1439 /** Pointer to the left rigth node. Don't touch. */
1440 PRTSTRSPACECORE pRight;
1441 /** Pointer to the list of string with the same key. Don't touch. */
1442 PRTSTRSPACECORE pList;
1443 /** Height of this tree: max(heigth(left), heigth(right)) + 1. Don't touch */
1444 unsigned char uchHeight;
1445 /** The string length. Read only! */
1446 size_t cchString;
1447 /** Pointer to the string. Read only! */
1448 const char * pszString;
1449} RTSTRSPACECORE;
1450
1451/** String space. (Initialize with NULL.) */
1452typedef PRTSTRSPACECORE RTSTRSPACE;
1453/** Pointer to a string space. */
1454typedef PPRTSTRSPACECORE PRTSTRSPACE;
1455
1456
1457/**
1458 * Inserts a string into a unique string space.
1459 *
1460 * @returns true on success.
1461 * @returns false if the string collided with an existing string.
1462 * @param pStrSpace The space to insert it into.
1463 * @param pStr The string node.
1464 */
1465RTDECL(bool) RTStrSpaceInsert(PRTSTRSPACE pStrSpace, PRTSTRSPACECORE pStr);
1466
1467/**
1468 * Removes a string from a unique string space.
1469 *
1470 * @returns Pointer to the removed string node.
1471 * @returns NULL if the string was not found in the string space.
1472 * @param pStrSpace The space to insert it into.
1473 * @param pszString The string to remove.
1474 */
1475RTDECL(PRTSTRSPACECORE) RTStrSpaceRemove(PRTSTRSPACE pStrSpace, const char *pszString);
1476
1477/**
1478 * Gets a string from a unique string space.
1479 *
1480 * @returns Pointer to the string node.
1481 * @returns NULL if the string was not found in the string space.
1482 * @param pStrSpace The space to insert it into.
1483 * @param pszString The string to get.
1484 */
1485RTDECL(PRTSTRSPACECORE) RTStrSpaceGet(PRTSTRSPACE pStrSpace, const char *pszString);
1486
1487/**
1488 * Callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy().
1489 *
1490 * @returns 0 on continue.
1491 * @returns Non-zero to aborts the operation.
1492 * @param pStr The string node
1493 * @param pvUser The user specified argument.
1494 */
1495typedef DECLCALLBACK(int) FNRTSTRSPACECALLBACK(PRTSTRSPACECORE pStr, void *pvUser);
1496/** Pointer to callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy(). */
1497typedef FNRTSTRSPACECALLBACK *PFNRTSTRSPACECALLBACK;
1498
1499/**
1500 * Destroys the string space.
1501 * The caller supplies a callback which will be called for each of
1502 * the string nodes in for freeing their memory and other resources.
1503 *
1504 * @returns 0 or what ever non-zero return value pfnCallback returned
1505 * when aborting the destruction.
1506 * @param pStrSpace The space to insert it into.
1507 * @param pfnCallback The callback.
1508 * @param pvUser The user argument.
1509 */
1510RTDECL(int) RTStrSpaceDestroy(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
1511
1512/**
1513 * Enumerates the string space.
1514 * The caller supplies a callback which will be called for each of
1515 * the string nodes.
1516 *
1517 * @returns 0 or what ever non-zero return value pfnCallback returned
1518 * when aborting the destruction.
1519 * @param pStrSpace The space to insert it into.
1520 * @param pfnCallback The callback.
1521 * @param pvUser The user argument.
1522 */
1523RTDECL(int) RTStrSpaceEnumerate(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
1524
1525/** @} */
1526
1527
1528/** @defgroup rt_str_utf16 UTF-16 String Manipulation
1529 * @ingroup grp_rt_str
1530 * @{
1531 */
1532
1533/**
1534 * Free a UTF-16 string allocated by RTStrUtf8ToUtf16(), RTStrUtf8ToUtf16Ex(),
1535 * RTUtf16Dup() or RTUtf16DupEx().
1536 *
1537 * @returns iprt status code.
1538 * @param pwszString The UTF-16 string to free. NULL is accepted.
1539 */
1540RTDECL(void) RTUtf16Free(PRTUTF16 pwszString);
1541
1542/**
1543 * Allocates a new copy of the specified UTF-16 string.
1544 *
1545 * @returns Pointer to the allocated string copy. Use RTUtf16Free() to free it.
1546 * @returns NULL when out of memory.
1547 * @param pwszString UTF-16 string to duplicate.
1548 * @remark This function will not make any attempt to validate the encoding.
1549 */
1550RTDECL(PRTUTF16) RTUtf16Dup(PCRTUTF16 pwszString);
1551
1552/**
1553 * Allocates a new copy of the specified UTF-16 string.
1554 *
1555 * @returns iprt status code.
1556 * @param ppwszString Receives pointer of the allocated UTF-16 string.
1557 * The returned pointer must be freed using RTUtf16Free().
1558 * @param pwszString UTF-16 string to duplicate.
1559 * @param cwcExtra Number of extra RTUTF16 items to allocate.
1560 * @remark This function will not make any attempt to validate the encoding.
1561 */
1562RTDECL(int) RTUtf16DupEx(PRTUTF16 *ppwszString, PCRTUTF16 pwszString, size_t cwcExtra);
1563
1564/**
1565 * Returns the length of a UTF-16 string in UTF-16 characters
1566 * without trailing '\\0'.
1567 *
1568 * Surrogate pairs counts as two UTF-16 characters here. Use RTUtf16CpCnt()
1569 * to get the exact number of code points in the string.
1570 *
1571 * @returns The number of RTUTF16 items in the string.
1572 * @param pwszString Pointer the UTF-16 string.
1573 * @remark This function will not make any attempt to validate the encoding.
1574 */
1575RTDECL(size_t) RTUtf16Len(PCRTUTF16 pwszString);
1576
1577/**
1578 * Performs a case sensitive string compare between two UTF-16 strings.
1579 *
1580 * @returns < 0 if the first string less than the second string.s
1581 * @returns 0 if the first string identical to the second string.
1582 * @returns > 0 if the first string greater than the second string.
1583 * @param pwsz1 First UTF-16 string. Null is allowed.
1584 * @param pwsz2 Second UTF-16 string. Null is allowed.
1585 * @remark This function will not make any attempt to validate the encoding.
1586 */
1587RTDECL(int) RTUtf16Cmp(register PCRTUTF16 pwsz1, register PCRTUTF16 pwsz2);
1588
1589/**
1590 * Performs a case insensitive string compare between two UTF-16 strings.
1591 *
1592 * This is a simplified compare, as only the simplified lower/upper case folding
1593 * specified by the unicode specs are used. It does not consider character pairs
1594 * as they are used in some languages, just simple upper & lower case compares.
1595 *
1596 * @returns < 0 if the first string less than the second string.
1597 * @returns 0 if the first string identical to the second string.
1598 * @returns > 0 if the first string greater than the second string.
1599 * @param pwsz1 First UTF-16 string. Null is allowed.
1600 * @param pwsz2 Second UTF-16 string. Null is allowed.
1601 */
1602RTDECL(int) RTUtf16ICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
1603
1604/**
1605 * Performs a case insensitive string compare between two UTF-16 strings
1606 * using the current locale of the process (if applicable).
1607 *
1608 * This differs from RTUtf16ICmp() in that it will try, if a locale with the
1609 * required data is available, to do a correct case-insensitive compare. It
1610 * follows that it is more complex and thereby likely to be more expensive.
1611 *
1612 * @returns < 0 if the first string less than the second string.
1613 * @returns 0 if the first string identical to the second string.
1614 * @returns > 0 if the first string greater than the second string.
1615 * @param pwsz1 First UTF-16 string. Null is allowed.
1616 * @param pwsz2 Second UTF-16 string. Null is allowed.
1617 */
1618RTDECL(int) RTUtf16LocaleICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
1619
1620/**
1621 * Folds a UTF-16 string to lowercase.
1622 *
1623 * This is a very simple folding; is uses the simple lowercase
1624 * code point, it is not related to any locale just the most common
1625 * lowercase codepoint setup by the unicode specs, and it will not
1626 * create new surrogate pairs or remove existing ones.
1627 *
1628 * @returns Pointer to the passed in string.
1629 * @param pwsz The string to fold.
1630 */
1631RTDECL(PRTUTF16) RTUtf16ToLower(PRTUTF16 pwsz);
1632
1633/**
1634 * Folds a UTF-16 string to uppercase.
1635 *
1636 * This is a very simple folding; is uses the simple uppercase
1637 * code point, it is not related to any locale just the most common
1638 * uppercase codepoint setup by the unicode specs, and it will not
1639 * create new surrogate pairs or remove existing ones.
1640 *
1641 * @returns Pointer to the passed in string.
1642 * @param pwsz The string to fold.
1643 */
1644RTDECL(PRTUTF16) RTUtf16ToUpper(PRTUTF16 pwsz);
1645
1646/**
1647 * Translate a UTF-16 string into a UTF-8 allocating the result buffer.
1648 *
1649 * @returns iprt status code.
1650 * @param pwszString UTF-16 string to convert.
1651 * @param ppszString Receives pointer of allocated UTF-8 string on
1652 * success, and is always set to NULL on failure.
1653 * The returned pointer must be freed using RTStrFree().
1654 */
1655RTDECL(int) RTUtf16ToUtf8(PCRTUTF16 pwszString, char **ppszString);
1656
1657/**
1658 * Translates UTF-16 to UTF-8 using buffer provided by the caller or
1659 * a fittingly sized buffer allocated by the function.
1660 *
1661 * @returns iprt status code.
1662 * @param pwszString The UTF-16 string to convert.
1663 * @param cwcString The number of RTUTF16 items to translate from pwszString.
1664 * The translation will stop when reaching cwcString or the terminator ('\\0').
1665 * Use RTSTR_MAX to translate the entire string.
1666 * @param ppsz If cch is non-zero, this must either be pointing to a pointer to
1667 * a buffer of the specified size, or pointer to a NULL pointer.
1668 * If *ppsz is NULL or cch is zero a buffer of at least cch chars
1669 * will be allocated to hold the translated string.
1670 * If a buffer was requested it must be freed using RTUtf16Free().
1671 * @param cch The buffer size in chars (the type). This includes the terminator.
1672 * @param pcch Where to store the length of the translated string. (Optional)
1673 * This field will be updated even on failure, however the value is only
1674 * specified for the following two error codes. On VERR_BUFFER_OVERFLOW
1675 * and VERR_NO_STR_MEMORY it contains the required buffer space.
1676 */
1677RTDECL(int) RTUtf16ToUtf8Ex(PCRTUTF16 pwszString, size_t cwcString, char **ppsz, size_t cch, size_t *pcch);
1678
1679/**
1680 * Calculates the length of the UTF-16 string in UTF-8 chars (bytes).
1681 *
1682 * This function will validate the string, and incorrectly encoded UTF-16
1683 * strings will be rejected. The primary purpose of this function is to
1684 * help allocate buffers for RTUtf16ToUtf8() of the correct size. For most
1685 * other purposes RTUtf16ToUtf8Ex() should be used.
1686 *
1687 * @returns Number of char (bytes).
1688 * @returns 0 if the string was incorrectly encoded.
1689 * @param pwsz The UTF-16 string.
1690 */
1691RTDECL(size_t) RTUtf16CalcUtf8Len(PCRTUTF16 pwsz);
1692
1693/**
1694 * Calculates the length of the UTF-16 string in UTF-8 chars (bytes).
1695 *
1696 * This function will validate the string, and incorrectly encoded UTF-16
1697 * strings will be rejected.
1698 *
1699 * @returns iprt status code.
1700 * @param pwsz The string.
1701 * @param cwc The max string length. Use RTSTR_MAX to process the entire string.
1702 * @param pcch Where to store the string length (in bytes). Optional.
1703 * This is undefined on failure.
1704 */
1705RTDECL(int) RTUtf16CalcUtf8LenEx(PCRTUTF16 pwsz, size_t cwc, size_t *pcch);
1706
1707/**
1708 * Get the unicode code point at the given string position.
1709 *
1710 * @returns unicode code point.
1711 * @returns RTUNICP_INVALID if the encoding is invalid.
1712 * @param pwsz The string.
1713 *
1714 * @remark This is an internal worker for RTUtf16GetCp().
1715 */
1716RTDECL(RTUNICP) RTUtf16GetCpInternal(PCRTUTF16 pwsz);
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 This is an internal worker for RTUtf16GetCpEx().
1728 */
1729RTDECL(int) RTUtf16GetCpExInternal(PCRTUTF16 *ppwsz, PRTUNICP pCp);
1730
1731/**
1732 * Put the unicode code point at the given string position
1733 * and return the pointer to the char following it.
1734 *
1735 * This function will not consider anything at or following the
1736 * buffer area pointed to by pwsz. It is therefore not suitable for
1737 * inserting code points into a string, only appending/overwriting.
1738 *
1739 * @returns pointer to the char following the written code point.
1740 * @param pwsz The string.
1741 * @param CodePoint The code point to write.
1742 * This should not be RTUNICP_INVALID or any other
1743 * character out of the UTF-16 range.
1744 *
1745 * @remark This is an internal worker for RTUtf16GetCpEx().
1746 */
1747RTDECL(PRTUTF16) RTUtf16PutCpInternal(PRTUTF16 pwsz, RTUNICP CodePoint);
1748
1749/**
1750 * Get the unicode code point at the given string position.
1751 *
1752 * @returns unicode code point.
1753 * @returns RTUNICP_INVALID if the encoding is invalid.
1754 * @param pwsz The string.
1755 *
1756 * @remark We optimize this operation by using an inline function for
1757 * everything which isn't a surrogate pair or an endian indicator.
1758 */
1759DECLINLINE(RTUNICP) RTUtf16GetCp(PCRTUTF16 pwsz)
1760{
1761 const RTUTF16 wc = *pwsz;
1762 if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
1763 return wc;
1764 return RTUtf16GetCpInternal(pwsz);
1765}
1766
1767/**
1768 * Get the unicode code point at the given string position.
1769 *
1770 * @returns iprt status code.
1771 * @param ppwsz Pointer to the string pointer. This will be updated to
1772 * point to the char following the current code point.
1773 * @param pCp Where to store the code point.
1774 * RTUNICP_INVALID is stored here on failure.
1775 *
1776 * @remark We optimize this operation by using an inline function for
1777 * everything which isn't a surrogate pair or and endian indicator.
1778 */
1779DECLINLINE(int) RTUtf16GetCpEx(PCRTUTF16 *ppwsz, PRTUNICP pCp)
1780{
1781 const RTUTF16 wc = **ppwsz;
1782 if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
1783 {
1784 (*ppwsz)++;
1785 *pCp = wc;
1786 return VINF_SUCCESS;
1787 }
1788 return RTUtf16GetCpExInternal(ppwsz, pCp);
1789}
1790
1791/**
1792 * Put the unicode code point at the given string position
1793 * and return the pointer to the char following it.
1794 *
1795 * This function will not consider anything at or following the
1796 * buffer area pointed to by pwsz. It is therefore not suitable for
1797 * inserting code points into a string, only appending/overwriting.
1798 *
1799 * @returns pointer to the char following the written code point.
1800 * @param pwsz The string.
1801 * @param CodePoint The code point to write.
1802 * This should not be RTUNICP_INVALID or any other
1803 * character out of the UTF-16 range.
1804 *
1805 * @remark We optimize this operation by using an inline function for
1806 * everything which isn't a surrogate pair or and endian indicator.
1807 */
1808DECLINLINE(PRTUTF16) RTUtf16PutCp(PRTUTF16 pwsz, RTUNICP CodePoint)
1809{
1810 if (CodePoint < 0xd800 || (CodePoint > 0xd800 && CodePoint < 0xfffe))
1811 {
1812 *pwsz++ = (RTUTF16)CodePoint;
1813 return pwsz;
1814 }
1815 return RTUtf16PutCpInternal(pwsz, CodePoint);
1816}
1817
1818/**
1819 * Skips ahead, past the current code point.
1820 *
1821 * @returns Pointer to the char after the current code point.
1822 * @param pwsz Pointer to the current code point.
1823 * @remark This will not move the next valid code point, only past the current one.
1824 */
1825DECLINLINE(PRTUTF16) RTUtf16NextCp(PCRTUTF16 pwsz)
1826{
1827 RTUNICP Cp;
1828 RTUtf16GetCpEx(&pwsz, &Cp);
1829 return (PRTUTF16)pwsz;
1830}
1831
1832/**
1833 * Skips backwards, to the previous code point.
1834 *
1835 * @returns Pointer to the char after the current code point.
1836 * @param pwszStart Pointer to the start of the string.
1837 * @param pwsz Pointer to the current code point.
1838 */
1839RTDECL(PRTUTF16) RTUtf16PrevCp(PCRTUTF16 pwszStart, PCRTUTF16 pwsz);
1840
1841
1842/**
1843 * Checks if the UTF-16 char is the high surrogate char (i.e.
1844 * the 1st char in the pair).
1845 *
1846 * @returns true if it is.
1847 * @returns false if it isn't.
1848 * @param wc The character to investigate.
1849 */
1850DECLINLINE(bool) RTUtf16IsHighSurrogate(RTUTF16 wc)
1851{
1852 return wc >= 0xd800 && wc <= 0xdbff;
1853}
1854
1855/**
1856 * Checks if the UTF-16 char is the low surrogate char (i.e.
1857 * the 2nd char in the pair).
1858 *
1859 * @returns true if it is.
1860 * @returns false if it isn't.
1861 * @param wc The character to investigate.
1862 */
1863DECLINLINE(bool) RTUtf16IsLowSurrogate(RTUTF16 wc)
1864{
1865 return wc >= 0xdc00 && wc <= 0xdfff;
1866}
1867
1868
1869/**
1870 * Checks if the two UTF-16 chars form a valid surrogate pair.
1871 *
1872 * @returns true if they do.
1873 * @returns false if they doesn't.
1874 * @param wcHigh The high (1st) character.
1875 * @param wcLow The low (2nd) character.
1876 */
1877DECLINLINE(bool) RTUtf16IsSurrogatePair(RTUTF16 wcHigh, RTUTF16 wcLow)
1878{
1879 return RTUtf16IsHighSurrogate(wcHigh)
1880 && RTUtf16IsLowSurrogate(wcLow);
1881}
1882
1883/** @} */
1884
1885__END_DECLS
1886
1887/** @} */
1888
1889#endif
1890
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