VirtualBox

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

Last change on this file since 13497 was 13472, checked in by vboxsync, 16 years ago

IPRT: Added a couple of function for simple string pattern matching (from STAM).

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