VirtualBox

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

Last change on this file since 17669 was 16323, checked in by vboxsync, 16 years ago

typo

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 66.6 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 iprt status code
305 * @returns VERR_INVALID_UTF8_ENCODING if the encoding is invalid.
306 * @param ppsz The string.
307 * @param pCp Where to store the unicode code point.
308 * Stores RTUNICP_INVALID if the encoding is invalid.
309 */
310RTDECL(int) RTStrGetCpExInternal(const char **ppsz, PRTUNICP pCp);
311
312/**
313 * Get the unicode code point at the given string position for a string of a
314 * given length.
315 *
316 * @returns iprt status code
317 * @retval VERR_INVALID_UTF8_ENCODING if the encoding is invalid.
318 * @retval VERR_END_OF_STRING if *pcch is 0. *pCp is set to RTUNICP_INVALID.
319 *
320 * @param ppsz The string.
321 * @param pcch Pointer to the length of the string. This will be
322 * decremented by the size of the code point.
323 * @param pCp Where to store the unicode code point.
324 * Stores RTUNICP_INVALID if the encoding is invalid.
325 */
326RTDECL(int) RTStrGetCpNExInternal(const char **ppsz, size_t *pcch, PRTUNICP pCp);
327
328/**
329 * Put the unicode code point at the given string position
330 * and return the pointer to the char following it.
331 *
332 * This function will not consider anything at or following the the
333 * buffer area pointed to by psz. It is therefore not suitable for
334 * inserting code points into a string, only appending/overwriting.
335 *
336 * @returns pointer to the char following the written code point.
337 * @param psz The string.
338 * @param CodePoint The code point to write.
339 * This sould not be RTUNICP_INVALID or any other charater
340 * out of the UTF-8 range.
341 *
342 * @remark This is a worker function for RTStrPutCp().
343 *
344 */
345RTDECL(char *) RTStrPutCpInternal(char *psz, RTUNICP CodePoint);
346
347/**
348 * Get the unicode code point at the given string position.
349 *
350 * @returns unicode code point.
351 * @returns RTUNICP_INVALID if the encoding is invalid.
352 * @param psz The string.
353 *
354 * @remark We optimize this operation by using an inline function for
355 * the most frequent and simplest sequence, the rest is
356 * handled by RTStrGetCpInternal().
357 */
358DECLINLINE(RTUNICP) RTStrGetCp(const char *psz)
359{
360 const unsigned char uch = *(const unsigned char *)psz;
361 if (!(uch & RT_BIT(7)))
362 return uch;
363 return RTStrGetCpInternal(psz);
364}
365
366/**
367 * Get the unicode code point at the given string position.
368 *
369 * @returns iprt status code.
370 * @param ppsz Pointer to the string pointer. This will be updated to
371 * point to the char following the current code point.
372 * @param pCp Where to store the code point.
373 * RTUNICP_INVALID is stored here on failure.
374 *
375 * @remark We optimize this operation by using an inline function for
376 * the most frequent and simplest sequence, the rest is
377 * handled by RTStrGetCpExInternal().
378 */
379DECLINLINE(int) RTStrGetCpEx(const char **ppsz, PRTUNICP pCp)
380{
381 const unsigned char uch = **(const unsigned char **)ppsz;
382 if (!(uch & RT_BIT(7)))
383 {
384 (*ppsz)++;
385 *pCp = uch;
386 return VINF_SUCCESS;
387 }
388 return RTStrGetCpExInternal(ppsz, pCp);
389}
390
391/**
392 * Get the unicode code point at the given string position for a string of a
393 * given maximum length.
394 *
395 * @returns iprt status code.
396 * @retval VERR_INVALID_UTF8_ENCODING if the encoding is invalid.
397 * @retval VERR_END_OF_STRING if *pcch is 0. *pCp is set to RTUNICP_INVALID.
398 *
399 * @param ppsz Pointer to the string pointer. This will be updated to
400 * point to the char following the current code point.
401 * @param pcch Pointer to the maximum string length. This will be
402 * decremented by the size of the code point found.
403 * @param pCp Where to store the code point.
404 * RTUNICP_INVALID is stored here on failure.
405 *
406 * @remark We optimize this operation by using an inline function for
407 * the most frequent and simplest sequence, the rest is
408 * handled by RTStrGetCpNExInternal().
409 */
410DECLINLINE(int) RTStrGetCpNEx(const char **ppsz, size_t *pcch, PRTUNICP pCp)
411{
412 if (RT_LIKELY(*pcch != 0))
413 {
414 const unsigned char uch = **(const unsigned char **)ppsz;
415 if (!(uch & RT_BIT(7)))
416 {
417 (*ppsz)++;
418 (*pcch)--;
419 *pCp = uch;
420 return VINF_SUCCESS;
421 }
422 }
423 return RTStrGetCpNExInternal(ppsz, pcch, pCp);
424}
425
426/**
427 * Put the unicode code point at the given string position
428 * and return the pointer to the char following it.
429 *
430 * This function will not consider anything at or following the the
431 * buffer area pointed to by psz. It is therefore not suitable for
432 * inserting code points into a string, only appending/overwriting.
433 *
434 * @returns pointer to the char following the written code point.
435 * @param psz The string.
436 * @param CodePoint The code point to write.
437 * This sould not be RTUNICP_INVALID or any other charater
438 * out of the UTF-8 range.
439 *
440 * @remark We optimize this operation by using an inline function for
441 * the most frequent and simplest sequence, the rest is
442 * handled by RTStrPutCpInternal().
443 */
444DECLINLINE(char *) RTStrPutCp(char *psz, RTUNICP CodePoint)
445{
446 if (CodePoint < 0x80)
447 {
448 *psz++ = (unsigned char)CodePoint;
449 return psz;
450 }
451 return RTStrPutCpInternal(psz, CodePoint);
452}
453
454/**
455 * Skips ahead, past the current code point.
456 *
457 * @returns Pointer to the char after the current code point.
458 * @param psz Pointer to the current code point.
459 * @remark This will not move the next valid code point, only past the current one.
460 */
461DECLINLINE(char *) RTStrNextCp(const char *psz)
462{
463 RTUNICP Cp;
464 RTStrGetCpEx(&psz, &Cp);
465 return (char *)psz;
466}
467
468/**
469 * Skips back to the previous code point.
470 *
471 * @returns Pointer to the char before the current code point.
472 * @returns pszStart on failure.
473 * @param pszStart Pointer to the start of the string.
474 * @param psz Pointer to the current code point.
475 */
476RTDECL(char *) RTStrPrevCp(const char *pszStart, const char *psz);
477
478
479
480#ifndef DECLARED_FNRTSTROUTPUT /* duplicated in iprt/log.h */
481#define DECLARED_FNRTSTROUTPUT
482/**
483 * Output callback.
484 *
485 * @returns number of bytes written.
486 * @param pvArg User argument.
487 * @param pachChars Pointer to an array of utf-8 characters.
488 * @param cbChars Number of bytes in the character array pointed to by pachChars.
489 */
490typedef DECLCALLBACK(size_t) FNRTSTROUTPUT(void *pvArg, const char *pachChars, size_t cbChars);
491/** Pointer to callback function. */
492typedef FNRTSTROUTPUT *PFNRTSTROUTPUT;
493#endif
494
495/** Format flag.
496 * These are used by RTStrFormat extensions and RTStrFormatNumber, mind
497 * that not all flags makes sense to both of the functions.
498 * @{ */
499#define RTSTR_F_CAPITAL 0x0001
500#define RTSTR_F_LEFT 0x0002
501#define RTSTR_F_ZEROPAD 0x0004
502#define RTSTR_F_SPECIAL 0x0008
503#define RTSTR_F_VALSIGNED 0x0010
504#define RTSTR_F_PLUS 0x0020
505#define RTSTR_F_BLANK 0x0040
506#define RTSTR_F_WIDTH 0x0080
507#define RTSTR_F_PRECISION 0x0100
508
509#define RTSTR_F_BIT_MASK 0xf800
510#define RTSTR_F_8BIT 0x0800
511#define RTSTR_F_16BIT 0x1000
512#define RTSTR_F_32BIT 0x2000
513#define RTSTR_F_64BIT 0x4000
514#define RTSTR_F_128BIT 0x8000
515/** @} */
516
517/** @def RTSTR_GET_BIT_FLAG
518 * Gets the bit flag for the specified type.
519 */
520#define RTSTR_GET_BIT_FLAG(type) \
521 ( sizeof(type) == 32 ? RTSTR_F_32BIT \
522 : sizeof(type) == 64 ? RTSTR_F_64BIT \
523 : sizeof(type) == 16 ? RTSTR_F_16BIT \
524 : sizeof(type) == 8 ? RTSTR_F_8BIT \
525 : sizeof(type) == 128? RTSTR_F_128BIT \
526 : 0)
527
528
529/**
530 * Callback to format non-standard format specifiers.
531 *
532 * @returns The number of bytes formatted.
533 * @param pvArg Formatter argument.
534 * @param pfnOutput Pointer to output function.
535 * @param pvArgOutput Argument for the output function.
536 * @param ppszFormat Pointer to the format string pointer. Advance this till the char
537 * after the format specifier.
538 * @param pArgs Pointer to the argument list. Use this to fetch the arguments.
539 * @param cchWidth Format Width. -1 if not specified.
540 * @param cchPrecision Format Precision. -1 if not specified.
541 * @param fFlags Flags (RTSTR_NTFS_*).
542 * @param chArgSize The argument size specifier, 'l' or 'L'.
543 */
544typedef DECLCALLBACK(size_t) FNSTRFORMAT(void *pvArg, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
545 const char **ppszFormat, va_list *pArgs, int cchWidth,
546 int cchPrecision, unsigned fFlags, char chArgSize);
547/** Pointer to a FNSTRFORMAT() function. */
548typedef FNSTRFORMAT *PFNSTRFORMAT;
549
550
551/**
552 * Partial implementation of a printf like formatter.
553 * It doesn't do everything correct, and there is no floating point support.
554 * However, it supports custom formats by the means of a format callback.
555 *
556 * @returns number of bytes formatted.
557 * @param pfnOutput Output worker.
558 * Called in two ways. Normally with a string and its length.
559 * For termination, it's called with NULL for string, 0 for length.
560 * @param pvArgOutput Argument to the output worker.
561 * @param pfnFormat Custom format worker.
562 * @param pvArgFormat Argument to the format worker.
563 * @param pszFormat Format string pointer.
564 * @param InArgs Argument list.
565 */
566RTDECL(size_t) RTStrFormatV(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat, const char *pszFormat, va_list InArgs);
567
568/**
569 * Partial implementation of a printf like formatter.
570 * It doesn't do everything correct, and there is no floating point support.
571 * However, it supports custom formats by the means of a format callback.
572 *
573 * @returns number of bytes formatted.
574 * @param pfnOutput Output worker.
575 * Called in two ways. Normally with a string and its length.
576 * For termination, it's called with NULL for string, 0 for length.
577 * @param pvArgOutput Argument to the output worker.
578 * @param pfnFormat Custom format worker.
579 * @param pvArgFormat Argument to the format worker.
580 * @param pszFormat Format string.
581 * @param ... Argument list.
582 */
583RTDECL(size_t) RTStrFormat(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat, const char *pszFormat, ...);
584
585/**
586 * Formats an integer number according to the parameters.
587 *
588 * @returns Length of the formatted number.
589 * @param psz Pointer to output string buffer of sufficient size.
590 * @param u64Value Value to format.
591 * @param uiBase Number representation base.
592 * @param cchWidth Width.
593 * @param cchPrecision Precision.
594 * @param fFlags Flags (NTFS_*).
595 */
596RTDECL(int) RTStrFormatNumber(char *psz, uint64_t u64Value, unsigned int uiBase, signed int cchWidth, signed int cchPrecision, unsigned int fFlags);
597
598
599/**
600 * Callback for formatting a type.
601 *
602 * This is registered using the RTStrFormatTypeRegister function and will
603 * be called during string formatting to handle the specified %R[type].
604 * The argument for this format type is assumed to be a pointer and it's
605 * passed in the @a pvValue argument.
606 *
607 * @returns Length of the formatted output.
608 * @param pfnOutput Output worker.
609 * @param pvArgOutput Argument to the output worker.
610 * @param pszType The type name.
611 * @param pvValue The argument value.
612 * @param cchWidth Width.
613 * @param cchPrecision Precision.
614 * @param fFlags Flags (NTFS_*).
615 * @param pvUser The user argument.
616 */
617typedef DECLCALLBACK(size_t) FNRTSTRFORMATTYPE(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
618 const char *pszType, void const *pvValue,
619 int cchWidth, int cchPrecision, unsigned fFlags,
620 void *pvUser);
621/** Pointer to a FNRTSTRFORMATTYPE. */
622typedef FNRTSTRFORMATTYPE *PFNRTSTRFORMATTYPE;
623
624
625/**
626 * Register a format handler for a type.
627 *
628 * The format handler is used to handle '%R[type]' format types, where the argument
629 * in the vector is a pointer value (a bit restrictive, but keeps it simple).
630 *
631 * The caller must ensure that no other thread will be making use of any of
632 * the dynamic formatting type facilities simultaneously with this call.
633 *
634 * @returns IPRT status code.
635 * @retval VINF_SUCCESS on success.
636 * @retval VERR_ALREADY_EXISTS if the type has already been registered.
637 * @retval VERR_TOO_MANY_OPEN_FILES if all the type slots has been allocated already.
638 *
639 * @param pszType The type name.
640 * @param pfnHandler The handler address. See FNRTSTRFORMATTYPE for details.
641 * @param pvUser The user argument to pass to the handler. See RTStrFormatTypeSetUser
642 * for how to update this later.
643 */
644RTDECL(int) RTStrFormatTypeRegister(const char *pszType, PFNRTSTRFORMATTYPE pfnHandler, void *pvUser);
645
646/**
647 * Deregisters a format type.
648 *
649 * The caller must ensure that no other thread will be making use of any of
650 * the dynamic formatting type facilities simultaneously with this call.
651 *
652 * @returns IPRT status code.
653 * @retval VINF_SUCCESS on success.
654 * @retval VERR_FILE_NOT_FOUND if not found.
655 *
656 * @param pszType The type to deregister.
657 */
658RTDECL(int) RTStrFormatTypeDeregister(const char *pszType);
659
660/**
661 * Sets the user argument for a type.
662 *
663 * This can be used if a user argument needs relocating in GC.
664 *
665 * @returns IPRT status code.
666 * @retval VINF_SUCCESS on success.
667 * @retval VERR_FILE_NOT_FOUND if not found.
668 *
669 * @param pszType The type to update.
670 * @param pvUser The new user argument value.
671 */
672RTDECL(int) RTStrFormatTypeSetUser(const char *pszType, void *pvUser);
673
674
675/**
676 * String printf.
677 *
678 * @returns The length of the returned string (in pszBuffer).
679 * @param pszBuffer Output buffer.
680 * @param cchBuffer Size of the output buffer.
681 * @param pszFormat The format string.
682 * @param args The format argument.
683 */
684RTDECL(size_t) RTStrPrintfV(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args);
685
686/**
687 * String printf.
688 *
689 * @returns The length of the returned string (in pszBuffer).
690 * @param pszBuffer Output buffer.
691 * @param cchBuffer Size of the output buffer.
692 * @param pszFormat The format string.
693 * @param ... The format argument.
694 */
695RTDECL(size_t) RTStrPrintf(char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...);
696
697
698/**
699 * String printf with custom formatting.
700 *
701 * @returns The length of the returned string (in pszBuffer).
702 * @param pfnFormat Pointer to handler function for the custom formats.
703 * @param pvArg Argument to the pfnFormat function.
704 * @param pszBuffer Output buffer.
705 * @param cchBuffer Size of the output buffer.
706 * @param pszFormat The format string.
707 * @param args The format argument.
708 */
709RTDECL(size_t) RTStrPrintfExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args);
710
711/**
712 * String printf with custom formatting.
713 *
714 * @returns The length of the returned string (in pszBuffer).
715 * @param pfnFormat Pointer to handler function for the custom formats.
716 * @param pvArg Argument to the pfnFormat function.
717 * @param pszBuffer Output buffer.
718 * @param cchBuffer Size of the output buffer.
719 * @param pszFormat The format string.
720 * @param ... The format argument.
721 */
722RTDECL(size_t) RTStrPrintfEx(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...);
723
724
725/**
726 * Allocating string printf.
727 *
728 * @returns The length of the string in the returned *ppszBuffer.
729 * @returns -1 on failure.
730 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
731 * The buffer should be freed using RTStrFree().
732 * On failure *ppszBuffer will be set to NULL.
733 * @param pszFormat The format string.
734 * @param args The format argument.
735 */
736RTDECL(int) RTStrAPrintfV(char **ppszBuffer, const char *pszFormat, va_list args);
737
738/**
739 * Allocating string printf.
740 *
741 * @returns The length of the string in the returned *ppszBuffer.
742 * @returns -1 on failure.
743 * @param ppszBuffer Where to store the pointer to the allocated output buffer.
744 * The buffer should be freed using RTStrFree().
745 * On failure *ppszBuffer will be set to NULL.
746 * @param pszFormat The format string.
747 * @param ... The format argument.
748 */
749RTDECL(int) RTStrAPrintf(char **ppszBuffer, const char *pszFormat, ...);
750
751
752/**
753 * Strips blankspaces from both ends of the string.
754 *
755 * @returns Pointer to first non-blank char in the string.
756 * @param psz The string to strip.
757 */
758RTDECL(char *) RTStrStrip(char *psz);
759
760/**
761 * Strips blankspaces from the start of the string.
762 *
763 * @returns Pointer to first non-blank char in the string.
764 * @param psz The string to strip.
765 */
766RTDECL(char *) RTStrStripL(const char *psz);
767
768/**
769 * Strips blankspaces from the end of the string.
770 *
771 * @returns psz.
772 * @param psz The string to strip.
773 */
774RTDECL(char *) RTStrStripR(char *psz);
775
776/**
777 * Performs a case sensitive string compare between two UTF-8 strings.
778 *
779 * Encoding errors are ignored by the current implementation. So, the only
780 * difference between this and the CRT strcmp function is the handling of
781 * NULL arguments.
782 *
783 * @returns < 0 if the first string less than the second string.
784 * @returns 0 if the first string identical to the second string.
785 * @returns > 0 if the first string greater than the second string.
786 * @param psz1 First UTF-8 string. Null is allowed.
787 * @param psz2 Second UTF-8 string. Null is allowed.
788 */
789RTDECL(int) RTStrCmp(const char *psz1, const char *psz2);
790
791/**
792 * Performs a case sensitive string compare between two UTF-8 strings, given
793 * a maximum string length.
794 *
795 * Encoding errors are ignored by the current implementation. So, the only
796 * difference between this and the CRT strncmp function is the handling of
797 * NULL arguments.
798 *
799 * @returns < 0 if the first string less than the second string.
800 * @returns 0 if the first string identical to the second string.
801 * @returns > 0 if the first string greater than the second string.
802 * @param psz1 First UTF-8 string. Null is allowed.
803 * @param psz2 Second UTF-8 string. Null is allowed.
804 * @param cchMax The maximum string length
805 */
806RTDECL(int) RTStrNCmp(const char *psz1, const char *psz2, size_t cchMax);
807
808/**
809 * Performs a case insensitive string compare between two UTF-8 strings.
810 *
811 * This is a simplified compare, as only the simplified lower/upper case folding
812 * specified by the unicode specs are used. It does not consider character pairs
813 * as they are used in some languages, just simple upper & lower case compares.
814 *
815 * The result is the difference between the mismatching codepoints after they
816 * both have been lower cased.
817 *
818 * If the string encoding is invalid the function will assert (strict builds)
819 * and use RTStrCmp for the remainder of the string.
820 *
821 * @returns < 0 if the first string less than the second string.
822 * @returns 0 if the first string identical to the second string.
823 * @returns > 0 if the first string greater than the second string.
824 * @param psz1 First UTF-8 string. Null is allowed.
825 * @param psz2 Second UTF-8 string. Null is allowed.
826 */
827RTDECL(int) RTStrICmp(const char *psz1, const char *psz2);
828
829/**
830 * Performs a case insensitive string compare between two UTF-8 strings, given a
831 * maximum string length.
832 *
833 * This is a simplified compare, as only the simplified lower/upper case folding
834 * specified by the unicode specs are used. It does not consider character pairs
835 * as they are used in some languages, just simple upper & lower case compares.
836 *
837 * The result is the difference between the mismatching codepoints after they
838 * both have been lower cased.
839 *
840 * If the string encoding is invalid the function will assert (strict builds)
841 * and use RTStrCmp for the remainder of the string.
842 *
843 * @returns < 0 if the first string less than the second string.
844 * @returns 0 if the first string identical to the second string.
845 * @returns > 0 if the first string greater than the second string.
846 * @param psz1 First UTF-8 string. Null is allowed.
847 * @param psz2 Second UTF-8 string. Null is allowed.
848 * @param cchMax Maximum string length
849 */
850RTDECL(int) RTStrNICmp(const char *psz1, const char *psz2, size_t cchMax);
851
852/**
853 * Find the length of a zero-terminated byte string, given
854 * a max string length.
855 *
856 * See also RTStrNLenEx.
857 *
858 * @returns The string length or cbMax. The returned length does not include
859 * the zero terminator if it was found.
860 *
861 * @param pszString The string.
862 * @param cchMax The max string length.
863 */
864RTDECL(size_t) RTStrNLen(const char *pszString, size_t cchMax);
865
866/**
867 * Find the length of a zero-terminated byte string, given
868 * a max string length.
869 *
870 * See also RTStrNLen.
871 *
872 * @returns IPRT status code.
873 * @retval VINF_SUCCESS if the string has a length less than cchMax.
874 * @retval VERR_BUFFER_OVERFLOW if the end of the string wasn't found
875 * before cchMax was reached.
876 *
877 * @param pszString The string.
878 * @param cchMax The max string length.
879 * @param pcch Where to store the string length excluding the
880 * terminator. This is set to cchMax if the terminator
881 * isn't found.
882 */
883RTDECL(int) RTStrNLenEx(const char *pszString, size_t cchMax, size_t *pcch);
884
885/**
886 * Matches a simple string pattern.
887 *
888 * @returns true if the string matches the pattern, otherwise false.
889 *
890 * @param pszPattern The pattern. Special chars are '*' and '?', where the
891 * asterisk matches zero or more characters and question
892 * mark matches exactly one character.
893 * @param pszString The string to match against the pattern.
894 */
895RTDECL(bool) RTStrSimplePatternMatch(const char *pszPattern, const char *pszString);
896
897/**
898 * Matches a simple string pattern, neither which needs to be zero terminated.
899 *
900 * This is identical to RTStrSimplePatternMatch except that you can optionally
901 * specify the length of both the pattern and the string. The function will
902 * stop when it hits a string terminator or either of the lengths.
903 *
904 * @returns true if the string matches the pattern, otherwise false.
905 *
906 * @param pszPattern The pattern. Special chars are '*' and '?', where the
907 * asterisk matches zero or more characters and question
908 * mark matches exactly one character.
909 * @param cchPattern The pattern length. Pass RTSTR_MAX if you don't know the
910 * length and wish to stop at the string terminator.
911 * @param pszString The string to match against the pattern.
912 * @param cchString The string length. Pass RTSTR_MAX if you don't know the
913 * length and wish to match up to the string terminator.
914 */
915RTDECL(bool) RTStrSimplePatternNMatch(const char *pszPattern, size_t cchPattern,
916 const char *pszString, size_t cchString);
917
918/**
919 * Matches multiple patterns against a string.
920 *
921 * The patterns are separated by the pipe character (|).
922 *
923 * @returns true if the string matches the pattern, otherwise false.
924 *
925 * @param pszPatterns The patterns.
926 * @param cchPatterns The lengths of the patterns to use. Pass RTSTR_MAX to
927 * stop at the terminator.
928 * @param pszString The string to match against the pattern.
929 * @param cchString The string length. Pass RTSTR_MAX stop stop at the
930 * terminator.
931 * @param poffPattern Offset into the patterns string of the patttern that
932 * matched. If no match, this will be set to RTSTR_MAX.
933 * This is optional, NULL is fine.
934 */
935RTDECL(bool) RTStrSimplePatternMultiMatch(const char *pszPatterns, size_t cchPatterns,
936 const char *pszString, size_t cchString,
937 size_t *poffPattern);
938
939
940/** @defgroup rt_str_conv String To/From Number Conversions
941 * @ingroup grp_rt_str
942 * @{ */
943
944/**
945 * Converts a string representation of a number to a 64-bit unsigned number.
946 *
947 * @returns iprt status code.
948 * Warnings are used to indicate convertion problems.
949 * @retval VWRN_NUMBER_TOO_BIG
950 * @retval VWRN_NEGATIVE_UNSIGNED
951 * @retval VWRN_TRAILING_CHARS
952 * @retval VWRN_TRAILING_SPACES
953 * @retval VINF_SUCCESS
954 * @retval VERR_NO_DIGITS
955 *
956 * @param pszValue Pointer to the string value.
957 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
958 * @param uBase The base of the representation used.
959 * If 0 the function will look for known prefixes before defaulting to 10.
960 * @param pu64 Where to store the converted number. (optional)
961 */
962RTDECL(int) RTStrToUInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint64_t *pu64);
963
964/**
965 * Converts a string representation of a number to a 64-bit unsigned number,
966 * making sure the full string is converted.
967 *
968 * @returns iprt status code.
969 * Warnings are used to indicate convertion problems.
970 * @retval VWRN_NUMBER_TOO_BIG
971 * @retval VWRN_NEGATIVE_UNSIGNED
972 * @retval VINF_SUCCESS
973 * @retval VERR_NO_DIGITS
974 * @retval VERR_TRAILING_SPACES
975 * @retval VERR_TRAILING_CHARS
976 *
977 * @param pszValue Pointer to the string value.
978 * @param uBase The base of the representation used.
979 * If 0 the function will look for known prefixes before defaulting to 10.
980 * @param pu64 Where to store the converted number. (optional)
981 */
982RTDECL(int) RTStrToUInt64Full(const char *pszValue, unsigned uBase, uint64_t *pu64);
983
984/**
985 * Converts a string representation of a number to a 64-bit unsigned number.
986 * The base is guessed.
987 *
988 * @returns 64-bit unsigned number on success.
989 * @returns 0 on failure.
990 * @param pszValue Pointer to the string value.
991 */
992RTDECL(uint64_t) RTStrToUInt64(const char *pszValue);
993
994/**
995 * Converts a string representation of a number to a 32-bit unsigned number.
996 *
997 * @returns iprt status code.
998 * Warnings are used to indicate conversion problems.
999 * @retval VWRN_NUMBER_TOO_BIG
1000 * @retval VWRN_NEGATIVE_UNSIGNED
1001 * @retval VWRN_TRAILING_CHARS
1002 * @retval VWRN_TRAILING_SPACES
1003 * @retval VINF_SUCCESS
1004 * @retval VERR_NO_DIGITS
1005 *
1006 * @param pszValue Pointer to the string value.
1007 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1008 * @param uBase The base of the representation used.
1009 * If 0 the function will look for known prefixes before defaulting to 10.
1010 * @param pu32 Where to store the converted number. (optional)
1011 */
1012RTDECL(int) RTStrToUInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint32_t *pu32);
1013
1014/**
1015 * Converts a string representation of a number to a 32-bit unsigned number,
1016 * making sure the full string is converted.
1017 *
1018 * @returns iprt status code.
1019 * Warnings are used to indicate convertion problems.
1020 * @retval VWRN_NUMBER_TOO_BIG
1021 * @retval VWRN_NEGATIVE_UNSIGNED
1022 * @retval VINF_SUCCESS
1023 * @retval VERR_NO_DIGITS
1024 * @retval VERR_TRAILING_SPACES
1025 * @retval VERR_TRAILING_CHARS
1026 *
1027 * @param pszValue Pointer to the string value.
1028 * @param uBase The base of the representation used.
1029 * If 0 the function will look for known prefixes before defaulting to 10.
1030 * @param pu32 Where to store the converted number. (optional)
1031 */
1032RTDECL(int) RTStrToUInt32Full(const char *pszValue, unsigned uBase, uint32_t *pu32);
1033
1034/**
1035 * Converts a string representation of a number to a 64-bit unsigned number.
1036 * The base is guessed.
1037 *
1038 * @returns 32-bit unsigned number on success.
1039 * @returns 0 on failure.
1040 * @param pszValue Pointer to the string value.
1041 */
1042RTDECL(uint32_t) RTStrToUInt32(const char *pszValue);
1043
1044/**
1045 * Converts a string representation of a number to a 16-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 pu16 Where to store the converted number. (optional)
1061 */
1062RTDECL(int) RTStrToUInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint16_t *pu16);
1063
1064/**
1065 * Converts a string representation of a number to a 16-bit unsigned number,
1066 * making sure the full string is converted.
1067 *
1068 * @returns iprt status code.
1069 * Warnings are used to indicate convertion 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 pu16 Where to store the converted number. (optional)
1081 */
1082RTDECL(int) RTStrToUInt16Full(const char *pszValue, unsigned uBase, uint16_t *pu16);
1083
1084/**
1085 * Converts a string representation of a number to a 16-bit unsigned number.
1086 * The base is guessed.
1087 *
1088 * @returns 16-bit unsigned number on success.
1089 * @returns 0 on failure.
1090 * @param pszValue Pointer to the string value.
1091 */
1092RTDECL(uint16_t) RTStrToUInt16(const char *pszValue);
1093
1094/**
1095 * Converts a string representation of a number to a 8-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 pu8 Where to store the converted number. (optional)
1111 */
1112RTDECL(int) RTStrToUInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint8_t *pu8);
1113
1114/**
1115 * Converts a string representation of a number to a 8-bit unsigned number,
1116 * making sure the full string is converted.
1117 *
1118 * @returns iprt status code.
1119 * Warnings are used to indicate convertion 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 pu8 Where to store the converted number. (optional)
1131 */
1132RTDECL(int) RTStrToUInt8Full(const char *pszValue, unsigned uBase, uint8_t *pu8);
1133
1134/**
1135 * Converts a string representation of a number to a 8-bit unsigned number.
1136 * The base is guessed.
1137 *
1138 * @returns 8-bit unsigned number on success.
1139 * @returns 0 on failure.
1140 * @param pszValue Pointer to the string value.
1141 */
1142RTDECL(uint8_t) RTStrToUInt8(const char *pszValue);
1143
1144/**
1145 * Converts a string representation of a number to a 64-bit signed number.
1146 *
1147 * @returns iprt status code.
1148 * Warnings are used to indicate conversion problems.
1149 * @retval VWRN_NUMBER_TOO_BIG
1150 * @retval VWRN_TRAILING_CHARS
1151 * @retval VWRN_TRAILING_SPACES
1152 * @retval VINF_SUCCESS
1153 * @retval VERR_NO_DIGITS
1154 *
1155 * @param pszValue Pointer to the string value.
1156 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1157 * @param uBase The base of the representation used.
1158 * If 0 the function will look for known prefixes before defaulting to 10.
1159 * @param pi64 Where to store the converted number. (optional)
1160 */
1161RTDECL(int) RTStrToInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, int64_t *pi64);
1162
1163/**
1164 * Converts a string representation of a number to a 64-bit signed number,
1165 * making sure the full string is converted.
1166 *
1167 * @returns iprt status code.
1168 * Warnings are used to indicate convertion problems.
1169 * @retval VWRN_NUMBER_TOO_BIG
1170 * @retval VINF_SUCCESS
1171 * @retval VERR_TRAILING_CHARS
1172 * @retval VERR_TRAILING_SPACES
1173 * @retval VERR_NO_DIGITS
1174 *
1175 * @param pszValue Pointer to the string value.
1176 * @param uBase The base of the representation used.
1177 * If 0 the function will look for known prefixes before defaulting to 10.
1178 * @param pi64 Where to store the converted number. (optional)
1179 */
1180RTDECL(int) RTStrToInt64Full(const char *pszValue, unsigned uBase, int64_t *pi64);
1181
1182/**
1183 * Converts a string representation of a number to a 64-bit signed number.
1184 * The base is guessed.
1185 *
1186 * @returns 64-bit signed number on success.
1187 * @returns 0 on failure.
1188 * @param pszValue Pointer to the string value.
1189 */
1190RTDECL(int64_t) RTStrToInt64(const char *pszValue);
1191
1192/**
1193 * Converts a string representation of a number to a 32-bit signed number.
1194 *
1195 * @returns iprt status code.
1196 * Warnings are used to indicate conversion problems.
1197 * @retval VWRN_NUMBER_TOO_BIG
1198 * @retval VWRN_TRAILING_CHARS
1199 * @retval VWRN_TRAILING_SPACES
1200 * @retval VINF_SUCCESS
1201 * @retval VERR_NO_DIGITS
1202 *
1203 * @param pszValue Pointer to the string value.
1204 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1205 * @param uBase The base of the representation used.
1206 * If 0 the function will look for known prefixes before defaulting to 10.
1207 * @param pi32 Where to store the converted number. (optional)
1208 */
1209RTDECL(int) RTStrToInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, int32_t *pi32);
1210
1211/**
1212 * Converts a string representation of a number to a 32-bit signed number,
1213 * making sure the full string is converted.
1214 *
1215 * @returns iprt status code.
1216 * Warnings are used to indicate convertion problems.
1217 * @retval VWRN_NUMBER_TOO_BIG
1218 * @retval VINF_SUCCESS
1219 * @retval VERR_TRAILING_CHARS
1220 * @retval VERR_TRAILING_SPACES
1221 * @retval VERR_NO_DIGITS
1222 *
1223 * @param pszValue Pointer to the string value.
1224 * @param uBase The base of the representation used.
1225 * If 0 the function will look for known prefixes before defaulting to 10.
1226 * @param pi32 Where to store the converted number. (optional)
1227 */
1228RTDECL(int) RTStrToInt32Full(const char *pszValue, unsigned uBase, int32_t *pi32);
1229
1230/**
1231 * Converts a string representation of a number to a 32-bit signed number.
1232 * The base is guessed.
1233 *
1234 * @returns 32-bit signed number on success.
1235 * @returns 0 on failure.
1236 * @param pszValue Pointer to the string value.
1237 */
1238RTDECL(int32_t) RTStrToInt32(const char *pszValue);
1239
1240/**
1241 * Converts a string representation of a number to a 16-bit signed number.
1242 *
1243 * @returns iprt status code.
1244 * Warnings are used to indicate conversion problems.
1245 * @retval VWRN_NUMBER_TOO_BIG
1246 * @retval VWRN_TRAILING_CHARS
1247 * @retval VWRN_TRAILING_SPACES
1248 * @retval VINF_SUCCESS
1249 * @retval VERR_NO_DIGITS
1250 *
1251 * @param pszValue Pointer to the string value.
1252 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1253 * @param uBase The base of the representation used.
1254 * If 0 the function will look for known prefixes before defaulting to 10.
1255 * @param pi16 Where to store the converted number. (optional)
1256 */
1257RTDECL(int) RTStrToInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, int16_t *pi16);
1258
1259/**
1260 * Converts a string representation of a number to a 16-bit signed number,
1261 * making sure the full string is converted.
1262 *
1263 * @returns iprt status code.
1264 * Warnings are used to indicate convertion problems.
1265 * @retval VWRN_NUMBER_TOO_BIG
1266 * @retval VINF_SUCCESS
1267 * @retval VERR_TRAILING_CHARS
1268 * @retval VERR_TRAILING_SPACES
1269 * @retval VERR_NO_DIGITS
1270 *
1271 * @param pszValue Pointer to the string value.
1272 * @param uBase The base of the representation used.
1273 * If 0 the function will look for known prefixes before defaulting to 10.
1274 * @param pi16 Where to store the converted number. (optional)
1275 */
1276RTDECL(int) RTStrToInt16Full(const char *pszValue, unsigned uBase, int16_t *pi16);
1277
1278/**
1279 * Converts a string representation of a number to a 16-bit signed number.
1280 * The base is guessed.
1281 *
1282 * @returns 16-bit signed number on success.
1283 * @returns 0 on failure.
1284 * @param pszValue Pointer to the string value.
1285 */
1286RTDECL(int16_t) RTStrToInt16(const char *pszValue);
1287
1288/**
1289 * Converts a string representation of a number to a 8-bit signed number.
1290 *
1291 * @returns iprt status code.
1292 * Warnings are used to indicate conversion problems.
1293 * @retval VWRN_NUMBER_TOO_BIG
1294 * @retval VWRN_TRAILING_CHARS
1295 * @retval VWRN_TRAILING_SPACES
1296 * @retval VINF_SUCCESS
1297 * @retval VERR_NO_DIGITS
1298 *
1299 * @param pszValue Pointer to the string value.
1300 * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
1301 * @param uBase The base of the representation used.
1302 * If 0 the function will look for known prefixes before defaulting to 10.
1303 * @param pi8 Where to store the converted number. (optional)
1304 */
1305RTDECL(int) RTStrToInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, int8_t *pi8);
1306
1307/**
1308 * Converts a string representation of a number to a 8-bit signed number,
1309 * making sure the full string is converted.
1310 *
1311 * @returns iprt status code.
1312 * Warnings are used to indicate convertion problems.
1313 * @retval VWRN_NUMBER_TOO_BIG
1314 * @retval VINF_SUCCESS
1315 * @retval VERR_TRAILING_CHARS
1316 * @retval VERR_TRAILING_SPACES
1317 * @retval VERR_NO_DIGITS
1318 *
1319 * @param pszValue Pointer to the string value.
1320 * @param uBase The base of the representation used.
1321 * If 0 the function will look for known prefixes before defaulting to 10.
1322 * @param pi8 Where to store the converted number. (optional)
1323 */
1324RTDECL(int) RTStrToInt8Full(const char *pszValue, unsigned uBase, int8_t *pi8);
1325
1326/**
1327 * Converts a string representation of a number to a 8-bit signed number.
1328 * The base is guessed.
1329 *
1330 * @returns 8-bit signed number on success.
1331 * @returns 0 on failure.
1332 * @param pszValue Pointer to the string value.
1333 */
1334RTDECL(int8_t) RTStrToInt8(const char *pszValue);
1335
1336/** @} */
1337
1338
1339/** @defgroup rt_str_space Unique String Space
1340 * @ingroup grp_rt_str
1341 * @{
1342 */
1343
1344/** Pointer to a string name space container node core. */
1345typedef struct RTSTRSPACECORE *PRTSTRSPACECORE;
1346/** Pointer to a pointer to a string name space container node core. */
1347typedef PRTSTRSPACECORE *PPRTSTRSPACECORE;
1348
1349/**
1350 * String name space container node core.
1351 */
1352typedef struct RTSTRSPACECORE
1353{
1354 /** Hash key. Don't touch. */
1355 uint32_t Key;
1356 /** Pointer to the left leaf node. Don't touch. */
1357 PRTSTRSPACECORE pLeft;
1358 /** Pointer to the left rigth node. Don't touch. */
1359 PRTSTRSPACECORE pRight;
1360 /** Pointer to the list of string with the same key. Don't touch. */
1361 PRTSTRSPACECORE pList;
1362 /** Height of this tree: max(heigth(left), heigth(right)) + 1. Don't touch */
1363 unsigned char uchHeight;
1364 /** The string length. Read only! */
1365 size_t cchString;
1366 /** Pointer to the string. Read only! */
1367 const char * pszString;
1368} RTSTRSPACECORE;
1369
1370/** String space. (Initialize with NULL.) */
1371typedef PRTSTRSPACECORE RTSTRSPACE;
1372/** Pointer to a string space. */
1373typedef PPRTSTRSPACECORE PRTSTRSPACE;
1374
1375
1376/**
1377 * Inserts a string into a unique string space.
1378 *
1379 * @returns true on success.
1380 * @returns false if the string collieded with an existing string.
1381 * @param pStrSpace The space to insert it into.
1382 * @param pStr The string node.
1383 */
1384RTDECL(bool) RTStrSpaceInsert(PRTSTRSPACE pStrSpace, PRTSTRSPACECORE pStr);
1385
1386/**
1387 * Removes a string from a unique string space.
1388 *
1389 * @returns Pointer to the removed string node.
1390 * @returns NULL if the string was not found in the string space.
1391 * @param pStrSpace The space to insert it into.
1392 * @param pszString The string to remove.
1393 */
1394RTDECL(PRTSTRSPACECORE) RTStrSpaceRemove(PRTSTRSPACE pStrSpace, const char *pszString);
1395
1396/**
1397 * Gets a string from a unique string space.
1398 *
1399 * @returns Pointer to the string node.
1400 * @returns NULL if the string was not found in the string space.
1401 * @param pStrSpace The space to insert it into.
1402 * @param pszString The string to get.
1403 */
1404RTDECL(PRTSTRSPACECORE) RTStrSpaceGet(PRTSTRSPACE pStrSpace, const char *pszString);
1405
1406/**
1407 * Callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy().
1408 *
1409 * @returns 0 on continue.
1410 * @returns Non-zero to aborts the operation.
1411 * @param pStr The string node
1412 * @param pvUser The user specified argument.
1413 */
1414typedef DECLCALLBACK(int) FNRTSTRSPACECALLBACK(PRTSTRSPACECORE pStr, void *pvUser);
1415/** Pointer to callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy(). */
1416typedef FNRTSTRSPACECALLBACK *PFNRTSTRSPACECALLBACK;
1417
1418/**
1419 * Destroys the string space.
1420 * The caller supplies a callback which will be called for each of
1421 * the string nodes in for freeing their memory and other resources.
1422 *
1423 * @returns 0 or what ever non-zero return value pfnCallback returned
1424 * when aborting the destruction.
1425 * @param pStrSpace The space to insert it into.
1426 * @param pfnCallback The callback.
1427 * @param pvUser The user argument.
1428 */
1429RTDECL(int) RTStrSpaceDestroy(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
1430
1431/**
1432 * Enumerates the string space.
1433 * The caller supplies a callback which will be called for each of
1434 * the string nodes.
1435 *
1436 * @returns 0 or what ever non-zero return value pfnCallback returned
1437 * when aborting the destruction.
1438 * @param pStrSpace The space to insert it into.
1439 * @param pfnCallback The callback.
1440 * @param pvUser The user argument.
1441 */
1442RTDECL(int) RTStrSpaceEnumerate(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
1443
1444/** @} */
1445
1446
1447/** @defgroup rt_str_utf16 UTF-16 String Manipulation
1448 * @ingroup grp_rt_str
1449 * @{
1450 */
1451
1452/**
1453 * Free a UTF-16 string allocated by RTStrUtf8ToUtf16(), RTStrUtf8ToUtf16Ex(),
1454 * RTUtf16Dup() or RTUtf16DupEx().
1455 *
1456 * @returns iprt status code.
1457 * @param pwszString The UTF-16 string to free. NULL is accepted.
1458 */
1459RTDECL(void) RTUtf16Free(PRTUTF16 pwszString);
1460
1461/**
1462 * Allocates a new copy of the specified UTF-16 string.
1463 *
1464 * @returns Pointer to the allocated string copy. Use RTUtf16Free() to free it.
1465 * @returns NULL when out of memory.
1466 * @param pwszString UTF-16 string to duplicate.
1467 * @remark This function will not make any attempt to validate the encoding.
1468 */
1469RTDECL(PRTUTF16) RTUtf16Dup(PCRTUTF16 pwszString);
1470
1471/**
1472 * Allocates a new copy of the specified UTF-16 string.
1473 *
1474 * @returns iprt status code.
1475 * @param ppwszString Receives pointer of the allocated UTF-16 string.
1476 * The returned pointer must be freed using RTUtf16Free().
1477 * @param pwszString UTF-16 string to duplicate.
1478 * @param cwcExtra Number of extra RTUTF16 items to allocate.
1479 * @remark This function will not make any attempt to validate the encoding.
1480 */
1481RTDECL(int) RTUtf16DupEx(PRTUTF16 *ppwszString, PCRTUTF16 pwszString, size_t cwcExtra);
1482
1483/**
1484 * Returns the length of a UTF-16 string in UTF-16 characters
1485 * without trailing '\\0'.
1486 *
1487 * Surrogate pairs counts as two UTF-16 characters here. Use RTUtf16CpCnt()
1488 * to get the exact number of code points in the string.
1489 *
1490 * @returns The number of RTUTF16 items in the string.
1491 * @param pwszString Pointer the UTF-16 string.
1492 * @remark This function will not make any attempt to validate the encoding.
1493 */
1494RTDECL(size_t) RTUtf16Len(PCRTUTF16 pwszString);
1495
1496/**
1497 * Performs a case sensitive string compare between two UTF-16 strings.
1498 *
1499 * @returns < 0 if the first string less than the second string.s
1500 * @returns 0 if the first string identical to the second string.
1501 * @returns > 0 if the first string greater than the second string.
1502 * @param pwsz1 First UTF-16 string. Null is allowed.
1503 * @param pwsz2 Second UTF-16 string. Null is allowed.
1504 * @remark This function will not make any attempt to validate the encoding.
1505 */
1506RTDECL(int) RTUtf16Cmp(register PCRTUTF16 pwsz1, register PCRTUTF16 pwsz2);
1507
1508/**
1509 * Performs a case insensitive string compare between two UTF-16 strings.
1510 *
1511 * This is a simplified compare, as only the simplified lower/upper case folding
1512 * specified by the unicode specs are used. It does not consider character pairs
1513 * as they are used in some languages, just simple upper & lower case compares.
1514 *
1515 * @returns < 0 if the first string less than the second string.
1516 * @returns 0 if the first string identical to the second string.
1517 * @returns > 0 if the first string greater than the second string.
1518 * @param pwsz1 First UTF-16 string. Null is allowed.
1519 * @param pwsz2 Second UTF-16 string. Null is allowed.
1520 */
1521RTDECL(int) RTUtf16ICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
1522
1523/**
1524 * Performs a case insensitive string compare between two UTF-16 strings
1525 * using the current locale of the process (if applicable).
1526 *
1527 * This differs from RTUtf16ICmp() in that it will try, if a locale with the
1528 * required data is available, to do a correct case-insensitive compare. It
1529 * follows that it is more complex and thereby likely to be more expensive.
1530 *
1531 * @returns < 0 if the first string less than the second string.
1532 * @returns 0 if the first string identical to the second string.
1533 * @returns > 0 if the first string greater than the second string.
1534 * @param pwsz1 First UTF-16 string. Null is allowed.
1535 * @param pwsz2 Second UTF-16 string. Null is allowed.
1536 */
1537RTDECL(int) RTUtf16LocaleICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
1538
1539/**
1540 * Folds a UTF-16 string to lowercase.
1541 *
1542 * This is a very simple folding; is uses the simple lowercase
1543 * code point, it is not related to any locale just the most common
1544 * lowercase codepoint setup by the unicode specs, and it will not
1545 * create new surrogate pairs or remove existing ones.
1546 *
1547 * @returns Pointer to the passed in string.
1548 * @param pwsz The string to fold.
1549 */
1550RTDECL(PRTUTF16) RTUtf16ToLower(PRTUTF16 pwsz);
1551
1552/**
1553 * Folds a UTF-16 string to uppercase.
1554 *
1555 * This is a very simple folding; is uses the simple uppercase
1556 * code point, it is not related to any locale just the most common
1557 * uppercase codepoint setup by the unicode specs, and it will not
1558 * create new surrogate pairs or remove existing ones.
1559 *
1560 * @returns Pointer to the passed in string.
1561 * @param pwsz The string to fold.
1562 */
1563RTDECL(PRTUTF16) RTUtf16ToUpper(PRTUTF16 pwsz);
1564
1565/**
1566 * Translate a UTF-16 string into a UTF-8 allocating the result buffer.
1567 *
1568 * @returns iprt status code.
1569 * @param pwszString UTF-16 string to convert.
1570 * @param ppszString Receives pointer of allocated UTF-8 string.
1571 * The returned pointer must be freed using RTStrFree().
1572 */
1573RTDECL(int) RTUtf16ToUtf8(PCRTUTF16 pwszString, char **ppszString);
1574
1575/**
1576 * Translates UTF-16 to UTF-8 using buffer provided by the caller or
1577 * a fittingly sized buffer allocated by the function.
1578 *
1579 * @returns iprt status code.
1580 * @param pwszString The UTF-16 string to convert.
1581 * @param cwcString The number of RTUTF16 items to translate from pwszString.
1582 * The translation will stop when reaching cwcString or the terminator ('\\0').
1583 * Use RTSTR_MAX to translate the entire string.
1584 * @param ppsz If cch is non-zero, this must either be pointing to a pointer to
1585 * a buffer of the specified size, or pointer to a NULL pointer.
1586 * If *ppsz is NULL or cch is zero a buffer of at least cch chars
1587 * will be allocated to hold the translated string.
1588 * If a buffer was requirest it must be freed using RTUtf16Free().
1589 * @param cch The buffer size in chars (the type). This includes the terminator.
1590 * @param pcch Where to store the length of the translated string. (Optional)
1591 * This field will be updated even on failure, however the value is only
1592 * specified for the following two error codes. On VERR_BUFFER_OVERFLOW
1593 * and VERR_NO_STR_MEMORY it contains the required buffer space.
1594 */
1595RTDECL(int) RTUtf16ToUtf8Ex(PCRTUTF16 pwszString, size_t cwcString, char **ppsz, size_t cch, size_t *pcch);
1596
1597/**
1598 * Calculates the length of the UTF-16 string in UTF-8 chars (bytes).
1599 *
1600 * This function will validate the string, and incorrectly encoded UTF-16
1601 * strings will be rejected. The primary purpose of this function is to
1602 * help allocate buffers for RTUtf16ToUtf8() of the correct size. For most
1603 * other puroses RTUtf16ToUtf8Ex() should be used.
1604 *
1605 * @returns Number of char (bytes).
1606 * @returns 0 if the string was incorrectly encoded.
1607 * @param pwsz The UTF-16 string.
1608 */
1609RTDECL(size_t) RTUtf16CalcUtf8Len(PCRTUTF16 pwsz);
1610
1611/**
1612 * Calculates the length of the UTF-16 string in UTF-8 chars (bytes).
1613 *
1614 * This function will validate the string, and incorrectly encoded UTF-16
1615 * strings will be rejected.
1616 *
1617 * @returns iprt status code.
1618 * @param pwsz The string.
1619 * @param cwc The max string length. Use RTSTR_MAX to process the entire string.
1620 * @param pcch Where to store the string length (in bytes). Optional.
1621 * This is undefined on failure.
1622 */
1623RTDECL(int) RTUtf16CalcUtf8LenEx(PCRTUTF16 pwsz, size_t cwc, size_t *pcch);
1624
1625/**
1626 * Get the unicode code point at the given string position.
1627 *
1628 * @returns unicode code point.
1629 * @returns RTUNICP_INVALID if the encoding is invalid.
1630 * @param pwsz The string.
1631 *
1632 * @remark This is an internal worker for RTUtf16GetCp().
1633 */
1634RTDECL(RTUNICP) RTUtf16GetCpInternal(PCRTUTF16 pwsz);
1635
1636/**
1637 * Get the unicode code point at the given string position.
1638 *
1639 * @returns iprt status code.
1640 * @param ppwsz Pointer to the string pointer. This will be updated to
1641 * point to the char following the current code point.
1642 * @param pCp Where to store the code point.
1643 * RTUNICP_INVALID is stored here on failure.
1644 *
1645 * @remark This is an internal worker for RTUtf16GetCpEx().
1646 */
1647RTDECL(int) RTUtf16GetCpExInternal(PCRTUTF16 *ppwsz, PRTUNICP pCp);
1648
1649/**
1650 * Put the unicode code point at the given string position
1651 * and return the pointer to the char following it.
1652 *
1653 * This function will not consider anything at or following the the
1654 * buffer area pointed to by pwsz. It is therefore not suitable for
1655 * inserting code points into a string, only appending/overwriting.
1656 *
1657 * @returns pointer to the char following the written code point.
1658 * @param pwsz The string.
1659 * @param CodePoint The code point to write.
1660 * This sould not be RTUNICP_INVALID or any other charater
1661 * out of the UTF-16 range.
1662 *
1663 * @remark This is an internal worker for RTUtf16GetCpEx().
1664 */
1665RTDECL(PRTUTF16) RTUtf16PutCpInternal(PRTUTF16 pwsz, RTUNICP CodePoint);
1666
1667/**
1668 * Get the unicode code point at the given string position.
1669 *
1670 * @returns unicode code point.
1671 * @returns RTUNICP_INVALID if the encoding is invalid.
1672 * @param pwsz The string.
1673 *
1674 * @remark We optimize this operation by using an inline function for
1675 * everything which isn't a surrogate pair or an endian indicator.
1676 */
1677DECLINLINE(RTUNICP) RTUtf16GetCp(PCRTUTF16 pwsz)
1678{
1679 const RTUTF16 wc = *pwsz;
1680 if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
1681 return wc;
1682 return RTUtf16GetCpInternal(pwsz);
1683}
1684
1685/**
1686 * Get the unicode code point at the given string position.
1687 *
1688 * @returns iprt status code.
1689 * @param ppwsz Pointer to the string pointer. This will be updated to
1690 * point to the char following the current code point.
1691 * @param pCp Where to store the code point.
1692 * RTUNICP_INVALID is stored here on failure.
1693 *
1694 * @remark We optimize this operation by using an inline function for
1695 * everything which isn't a surrogate pair or and endian indicator.
1696 */
1697DECLINLINE(int) RTUtf16GetCpEx(PCRTUTF16 *ppwsz, PRTUNICP pCp)
1698{
1699 const RTUTF16 wc = **ppwsz;
1700 if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
1701 {
1702 (*ppwsz)++;
1703 *pCp = wc;
1704 return VINF_SUCCESS;
1705 }
1706 return RTUtf16GetCpExInternal(ppwsz, pCp);
1707}
1708
1709/**
1710 * Put the unicode code point at the given string position
1711 * and return the pointer to the char following it.
1712 *
1713 * This function will not consider anything at or following the the
1714 * buffer area pointed to by pwsz. It is therefore not suitable for
1715 * inserting code points into a string, only appending/overwriting.
1716 *
1717 * @returns pointer to the char following the written code point.
1718 * @param pwsz The string.
1719 * @param CodePoint The code point to write.
1720 * This sould not be RTUNICP_INVALID or any other charater
1721 * out of the UTF-16 range.
1722 *
1723 * @remark We optimize this operation by using an inline function for
1724 * everything which isn't a surrogate pair or and endian indicator.
1725 */
1726DECLINLINE(PRTUTF16) RTUtf16PutCp(PRTUTF16 pwsz, RTUNICP CodePoint)
1727{
1728 if (CodePoint < 0xd800 || (CodePoint > 0xd800 && CodePoint < 0xfffe))
1729 {
1730 *pwsz++ = (RTUTF16)CodePoint;
1731 return pwsz;
1732 }
1733 return RTUtf16PutCpInternal(pwsz, CodePoint);
1734}
1735
1736/**
1737 * Skips ahead, past the current code point.
1738 *
1739 * @returns Pointer to the char after the current code point.
1740 * @param pwsz Pointer to the current code point.
1741 * @remark This will not move the next valid code point, only past the current one.
1742 */
1743DECLINLINE(PRTUTF16) RTUtf16NextCp(PCRTUTF16 pwsz)
1744{
1745 RTUNICP Cp;
1746 RTUtf16GetCpEx(&pwsz, &Cp);
1747 return (PRTUTF16)pwsz;
1748}
1749
1750/**
1751 * Skips backwards, to the previous code point.
1752 *
1753 * @returns Pointer to the char after the current code point.
1754 * @param pwszStart Pointer to the start of the string.
1755 * @param pwsz Pointer to the current code point.
1756 */
1757RTDECL(PRTUTF16) RTUtf16PrevCp(PCRTUTF16 pwszStart, PCRTUTF16 pwsz);
1758
1759
1760/**
1761 * Checks if the UTF-16 char is the high surrogate char (i.e.
1762 * the 1st char in the pair).
1763 *
1764 * @returns true if it is.
1765 * @returns false if it isn't.
1766 * @param wc The character to investigate.
1767 */
1768DECLINLINE(bool) RTUtf16IsHighSurrogate(RTUTF16 wc)
1769{
1770 return wc >= 0xd800 && wc <= 0xdbff;
1771}
1772
1773/**
1774 * Checks if the UTF-16 char is the low surrogate char (i.e.
1775 * the 2nd char in the pair).
1776 *
1777 * @returns true if it is.
1778 * @returns false if it isn't.
1779 * @param wc The character to investigate.
1780 */
1781DECLINLINE(bool) RTUtf16IsLowSurrogate(RTUTF16 wc)
1782{
1783 return wc >= 0xdc00 && wc <= 0xdfff;
1784}
1785
1786
1787/**
1788 * Checks if the two UTF-16 chars form a valid surrogate pair.
1789 *
1790 * @returns true if they do.
1791 * @returns false if they doesn't.
1792 * @param wcHigh The high (1st) character.
1793 * @param wcLow The low (2nd) character.
1794 */
1795DECLINLINE(bool) RTUtf16IsSurrogatePair(RTUTF16 wcHigh, RTUTF16 wcLow)
1796{
1797 return RTUtf16IsHighSurrogate(wcHigh)
1798 && RTUtf16IsLowSurrogate(wcLow);
1799}
1800
1801/** @} */
1802
1803__END_DECLS
1804
1805/** @} */
1806
1807#endif
1808
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