VirtualBox

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

Last change on this file since 11725 was 10951, checked in by vboxsync, 16 years ago

IPRT: Extended RTStrValidateEncodingEx with a RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED flag for verify that the string zero terminated correctly within the given size (cch).

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