VirtualBox

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

Last change on this file since 13928 was 13927, checked in by vboxsync, 17 years ago

Runtime: add RTStrNICmp and RTStrNCmp

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

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