VirtualBox

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

Last change on this file since 23627 was 23627, checked in by vboxsync, 15 years ago

Fix building on FreeBSD < 7.2. No memmove in R0 there

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette