1 | /** @file
|
---|
2 | * IPRT - String Manipluation.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2010 Oracle Corporation
|
---|
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 |
|
---|
26 | #ifndef ___iprt_string_h
|
---|
27 | #define ___iprt_string_h
|
---|
28 |
|
---|
29 | #include <iprt/cdefs.h>
|
---|
30 | #include <iprt/types.h>
|
---|
31 | #include <iprt/assert.h>
|
---|
32 | #include <iprt/stdarg.h>
|
---|
33 | #include <iprt/err.h> /* for VINF_SUCCESS */
|
---|
34 | #if defined(RT_OS_LINUX) && defined(__KERNEL__)
|
---|
35 | # include <linux/string.h>
|
---|
36 | #elif defined(RT_OS_FREEBSD) && defined(_KERNEL)
|
---|
37 | /** @todo
|
---|
38 | * XXX: Very ugly hack to get things build on recent FreeBSD builds. They have
|
---|
39 | * memchr now and we need to include param.h to get __FreeBSD_version and make
|
---|
40 | * memchr available based on the version below or we can't compile the kernel
|
---|
41 | * module on older versions anymore.
|
---|
42 | *
|
---|
43 | * But including param.h here opens Pandora's box because we clash with a few
|
---|
44 | * defines namely PVM and PAGE_SIZE. We can safely undefine PVM here but not
|
---|
45 | * PAGE_SIZE because this results in build errors sooner or later. Luckily this
|
---|
46 | * define is in a header included by param.h (machine/param.h). We define the
|
---|
47 | * guards here to prevent inclusion of it if PAGE_SIZE was defined already.
|
---|
48 | *
|
---|
49 | * @todo aeichner: Search for an elegant solution and cleanup this mess ASAP!
|
---|
50 | */
|
---|
51 | # ifdef PAGE_SIZE
|
---|
52 | # define _AMD64_INCLUDE_PARAM_H_
|
---|
53 | # define _I386_INCLUDE_PARAM_H_
|
---|
54 | # define _MACHINE_PARAM_H_
|
---|
55 | # endif
|
---|
56 | # include <sys/param.h> /* __FreeBSD_version */
|
---|
57 | # undef PVM
|
---|
58 | # include <sys/libkern.h>
|
---|
59 | /*
|
---|
60 | * No memmove on versions < 7.2
|
---|
61 | * Defining a macro using bcopy here
|
---|
62 | */
|
---|
63 | # define memmove(dst, src, size) bcopy(src, dst, size)
|
---|
64 | #elif defined(RT_OS_SOLARIS) && defined(_KERNEL)
|
---|
65 | /*
|
---|
66 | * Same case as with FreeBSD kernel:
|
---|
67 | * The string.h stuff clashes with sys/system.h
|
---|
68 | * ffs = find first set bit.
|
---|
69 | */
|
---|
70 | # define ffs ffs_string_h
|
---|
71 | # include <string.h>
|
---|
72 | # undef ffs
|
---|
73 | # undef strpbrk
|
---|
74 | #else
|
---|
75 | # include <string.h>
|
---|
76 | #endif
|
---|
77 |
|
---|
78 | /*
|
---|
79 | * Supply prototypes for standard string functions provided by
|
---|
80 | * IPRT instead of the operating environment.
|
---|
81 | */
|
---|
82 | #if defined(RT_OS_DARWIN) && defined(KERNEL)
|
---|
83 | RT_C_DECLS_BEGIN
|
---|
84 | void *memchr(const void *pv, int ch, size_t cb);
|
---|
85 | char *strpbrk(const char *pszStr, const char *pszChars);
|
---|
86 | RT_C_DECLS_END
|
---|
87 | #endif
|
---|
88 |
|
---|
89 | #if defined(RT_OS_FREEBSD) && defined(_KERNEL)
|
---|
90 | RT_C_DECLS_BEGIN
|
---|
91 | #if __FreeBSD_version < 900000
|
---|
92 | void *memchr(const void *pv, int ch, size_t cb);
|
---|
93 | #endif
|
---|
94 | char *strpbrk(const char *pszStr, const char *pszChars);
|
---|
95 | RT_C_DECLS_END
|
---|
96 | #endif
|
---|
97 |
|
---|
98 | /** @def RT_USE_RTC_3629
|
---|
99 | * When defined the UTF-8 range will stop at 0x10ffff. If not defined, the
|
---|
100 | * range stops at 0x7fffffff.
|
---|
101 | * @remarks Must be defined both when building and using the IPRT. */
|
---|
102 | #ifdef DOXYGEN_RUNNING
|
---|
103 | # define RT_USE_RTC_3629
|
---|
104 | #endif
|
---|
105 |
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Byte zero the specified object.
|
---|
109 | *
|
---|
110 | * This will use sizeof(Obj) to figure the size and will call memset, bzero
|
---|
111 | * or some compiler intrinsic to perform the actual zeroing.
|
---|
112 | *
|
---|
113 | * @param Obj The object to zero. Make sure to dereference pointers.
|
---|
114 | *
|
---|
115 | * @remarks Because the macro may use memset it has been placed in string.h
|
---|
116 | * instead of cdefs.h to avoid build issues because someone forgot
|
---|
117 | * to include this header.
|
---|
118 | *
|
---|
119 | * @ingroup grp_rt_cdefs
|
---|
120 | */
|
---|
121 | #define RT_ZERO(Obj) RT_BZERO(&(Obj), sizeof(Obj))
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Byte zero the specified memory area.
|
---|
125 | *
|
---|
126 | * This will call memset, bzero or some compiler intrinsic to clear the
|
---|
127 | * specified bytes of memory.
|
---|
128 | *
|
---|
129 | * @param pv Pointer to the memory.
|
---|
130 | * @param cb The number of bytes to clear. Please, don't pass 0.
|
---|
131 | *
|
---|
132 | * @remarks Because the macro may use memset it has been placed in string.h
|
---|
133 | * instead of cdefs.h to avoid build issues because someone forgot
|
---|
134 | * to include this header.
|
---|
135 | *
|
---|
136 | * @ingroup grp_rt_cdefs
|
---|
137 | */
|
---|
138 | #define RT_BZERO(pv, cb) do { memset((pv), 0, cb); } while (0)
|
---|
139 |
|
---|
140 |
|
---|
141 |
|
---|
142 | /** @defgroup grp_rt_str RTStr - String Manipulation
|
---|
143 | * Mostly UTF-8 related helpers where the standard string functions won't do.
|
---|
144 | * @ingroup grp_rt
|
---|
145 | * @{
|
---|
146 | */
|
---|
147 |
|
---|
148 | RT_C_DECLS_BEGIN
|
---|
149 |
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * The maximum string length.
|
---|
153 | */
|
---|
154 | #define RTSTR_MAX (~(size_t)0)
|
---|
155 |
|
---|
156 |
|
---|
157 | /** @def RTMEM_TAG
|
---|
158 | * The default allocation tag used by the RTStr allocation APIs.
|
---|
159 | *
|
---|
160 | * When not defined before the inclusion of iprt/string.h, this will default to
|
---|
161 | * the pointer to the current file name. The string API will make of use of
|
---|
162 | * this as pointer to a volatile but read-only string.
|
---|
163 | */
|
---|
164 | #ifndef RTSTR_TAG
|
---|
165 | # define RTSTR_TAG (__FILE__)
|
---|
166 | #endif
|
---|
167 |
|
---|
168 |
|
---|
169 | #ifdef IN_RING3
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Allocates tmp buffer with default tag, translates pszString from UTF8 to
|
---|
173 | * current codepage.
|
---|
174 | *
|
---|
175 | * @returns iprt status code.
|
---|
176 | * @param ppszString Receives pointer of allocated native CP string.
|
---|
177 | * The returned pointer must be freed using RTStrFree().
|
---|
178 | * @param pszString UTF-8 string to convert.
|
---|
179 | */
|
---|
180 | #define RTStrUtf8ToCurrentCP(ppszString, pszString) RTStrUtf8ToCurrentCPTag((ppszString), (pszString), RTSTR_TAG)
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Allocates tmp buffer with custom tag, translates pszString from UTF8 to
|
---|
184 | * current codepage.
|
---|
185 | *
|
---|
186 | * @returns iprt status code.
|
---|
187 | * @param ppszString Receives pointer of allocated native CP string.
|
---|
188 | * The returned pointer must be freed using
|
---|
189 | * RTStrFree()., const char *pszTag
|
---|
190 | * @param pszString UTF-8 string to convert.
|
---|
191 | * @param pszTag Allocation tag used for statistics and such.
|
---|
192 | */
|
---|
193 | RTR3DECL(int) RTStrUtf8ToCurrentCPTag(char **ppszString, const char *pszString, const char *pszTag);
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Allocates tmp buffer, translates pszString from current codepage to UTF-8.
|
---|
197 | *
|
---|
198 | * @returns iprt status code.
|
---|
199 | * @param ppszString Receives pointer of allocated UTF-8 string.
|
---|
200 | * The returned pointer must be freed using RTStrFree().
|
---|
201 | * @param pszString Native string to convert.
|
---|
202 | */
|
---|
203 | #define RTStrCurrentCPToUtf8(ppszString, pszString) RTStrCurrentCPToUtf8Tag((ppszString), (pszString), RTSTR_TAG)
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Allocates tmp buffer, translates pszString from current codepage to UTF-8.
|
---|
207 | *
|
---|
208 | * @returns iprt status code.
|
---|
209 | * @param ppszString Receives pointer of allocated UTF-8 string.
|
---|
210 | * The returned pointer must be freed using RTStrFree().
|
---|
211 | * @param pszString Native string to convert.
|
---|
212 | * @param pszTag Allocation tag used for statistics and such.
|
---|
213 | */
|
---|
214 | RTR3DECL(int) RTStrCurrentCPToUtf8Tag(char **ppszString, const char *pszString, const char *pszTag);
|
---|
215 |
|
---|
216 | #endif /* IN_RING3 */
|
---|
217 |
|
---|
218 | /**
|
---|
219 | * Free string allocated by any of the non-UCS-2 string functions.
|
---|
220 | *
|
---|
221 | * @returns iprt status code.
|
---|
222 | * @param pszString Pointer to buffer with string to free.
|
---|
223 | * NULL is accepted.
|
---|
224 | */
|
---|
225 | RTDECL(void) RTStrFree(char *pszString);
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Allocates a new copy of the given UTF-8 string (default tag).
|
---|
229 | *
|
---|
230 | * @returns Pointer to the allocated UTF-8 string.
|
---|
231 | * @param pszString UTF-8 string to duplicate.
|
---|
232 | */
|
---|
233 | #define RTStrDup(pszString) RTStrDupTag((pszString), RTSTR_TAG)
|
---|
234 |
|
---|
235 | /**
|
---|
236 | * Allocates a new copy of the given UTF-8 string (custom tag).
|
---|
237 | *
|
---|
238 | * @returns Pointer to the allocated UTF-8 string.
|
---|
239 | * @param pszString UTF-8 string to duplicate.
|
---|
240 | * @param pszTag Allocation tag used for statistics and such.
|
---|
241 | */
|
---|
242 | RTDECL(char *) RTStrDupTag(const char *pszString, const char *pszTag);
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Allocates a new copy of the given UTF-8 string (default tag).
|
---|
246 | *
|
---|
247 | * @returns iprt status code.
|
---|
248 | * @param ppszString Receives pointer of the allocated UTF-8 string.
|
---|
249 | * The returned pointer must be freed using RTStrFree().
|
---|
250 | * @param pszString UTF-8 string to duplicate.
|
---|
251 | */
|
---|
252 | #define RTStrDupEx(ppszString, pszString) RTStrDupExTag((ppszString), (pszString), RTSTR_TAG)
|
---|
253 |
|
---|
254 | /**
|
---|
255 | * Allocates a new copy of the given UTF-8 string (custom tag).
|
---|
256 | *
|
---|
257 | * @returns iprt status code.
|
---|
258 | * @param ppszString Receives pointer of the allocated UTF-8 string.
|
---|
259 | * The returned pointer must be freed using RTStrFree().
|
---|
260 | * @param pszString UTF-8 string to duplicate.
|
---|
261 | * @param pszTag Allocation tag used for statistics and such.
|
---|
262 | */
|
---|
263 | RTDECL(int) RTStrDupExTag(char **ppszString, const char *pszString, const char *pszTag);
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * Allocates a new copy of the given UTF-8 substring (default tag).
|
---|
267 | *
|
---|
268 | * @returns Pointer to the allocated UTF-8 substring.
|
---|
269 | * @param pszString UTF-8 string to duplicate.
|
---|
270 | * @param cchMax The max number of chars to duplicate, not counting
|
---|
271 | * the terminator.
|
---|
272 | */
|
---|
273 | #define RTStrDupN(pszString, cchMax) RTStrDupNTag((pszString), (cchMax), RTSTR_TAG)
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Allocates a new copy of the given UTF-8 substring (custom tag).
|
---|
277 | *
|
---|
278 | * @returns Pointer to the allocated UTF-8 substring.
|
---|
279 | * @param pszString UTF-8 string to duplicate.
|
---|
280 | * @param cchMax The max number of chars to duplicate, not counting
|
---|
281 | * the terminator.
|
---|
282 | * @param pszTag Allocation tag used for statistics and such.
|
---|
283 | */
|
---|
284 | RTDECL(char *) RTStrDupNTag(const char *pszString, size_t cchMax, const char *pszTag);
|
---|
285 |
|
---|
286 | /**
|
---|
287 | * Appends a string onto an existing IPRT allocated string (defaul tag).
|
---|
288 | *
|
---|
289 | * @retval VINF_SUCCESS
|
---|
290 | * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
|
---|
291 | * remains unchanged.
|
---|
292 | *
|
---|
293 | * @param ppsz Pointer to the string pointer. The string
|
---|
294 | * pointer must either be NULL or point to a string
|
---|
295 | * returned by an IPRT string API. (In/Out)
|
---|
296 | * @param pszAppend The string to append. NULL and empty strings
|
---|
297 | * are quietly ignored.
|
---|
298 | */
|
---|
299 | #define RTStrAAppend(ppsz, pszAppend) RTStrAAppendTag((ppsz), (pszAppend), RTSTR_TAG)
|
---|
300 |
|
---|
301 | /**
|
---|
302 | * Appends a string onto an existing IPRT allocated string (custom tag).
|
---|
303 | *
|
---|
304 | * @retval VINF_SUCCESS
|
---|
305 | * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
|
---|
306 | * remains unchanged.
|
---|
307 | *
|
---|
308 | * @param ppsz Pointer to the string pointer. The string
|
---|
309 | * pointer must either be NULL or point to a string
|
---|
310 | * returned by an IPRT string API. (In/Out)
|
---|
311 | * @param pszAppend The string to append. NULL and empty strings
|
---|
312 | * are quietly ignored.
|
---|
313 | * @param pszTag Allocation tag used for statistics and such.
|
---|
314 | */
|
---|
315 | RTDECL(int) RTStrAAppendTag(char **ppsz, const char *pszAppend, const char *pszTag);
|
---|
316 |
|
---|
317 | /**
|
---|
318 | * Appends N bytes from a strings onto an existing IPRT allocated string
|
---|
319 | * (default tag).
|
---|
320 | *
|
---|
321 | * @retval VINF_SUCCESS
|
---|
322 | * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
|
---|
323 | * remains unchanged.
|
---|
324 | *
|
---|
325 | * @param ppsz Pointer to the string pointer. The string
|
---|
326 | * pointer must either be NULL or point to a string
|
---|
327 | * returned by an IPRT string API. (In/Out)
|
---|
328 | * @param pszAppend The string to append. Can be NULL if cchAppend
|
---|
329 | * is NULL.
|
---|
330 | * @param cchAppend The number of chars (not code points) to append
|
---|
331 | * from pszAppend. Must not be more than
|
---|
332 | * @a pszAppend contains, except for the special
|
---|
333 | * value RTSTR_MAX that can be used to indicate all
|
---|
334 | * of @a pszAppend without having to strlen it.
|
---|
335 | */
|
---|
336 | #define RTStrAAppendN(ppsz, pszAppend, cchAppend) RTStrAAppendNTag((ppsz), (pszAppend), (cchAppend), RTSTR_TAG)
|
---|
337 |
|
---|
338 | /**
|
---|
339 | * Appends N bytes from a strings onto an existing IPRT allocated string (custom
|
---|
340 | * tag).
|
---|
341 | *
|
---|
342 | * @retval VINF_SUCCESS
|
---|
343 | * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
|
---|
344 | * remains unchanged.
|
---|
345 | *
|
---|
346 | * @param ppsz Pointer to the string pointer. The string
|
---|
347 | * pointer must either be NULL or point to a string
|
---|
348 | * returned by an IPRT string API. (In/Out)
|
---|
349 | * @param pszAppend The string to append. Can be NULL if cchAppend
|
---|
350 | * is NULL.
|
---|
351 | * @param cchAppend The number of chars (not code points) to append
|
---|
352 | * from pszAppend. Must not be more than
|
---|
353 | * @a pszAppend contains, except for the special
|
---|
354 | * value RTSTR_MAX that can be used to indicate all
|
---|
355 | * of @a pszAppend without having to strlen it.
|
---|
356 | * @param pszTag Allocation tag used for statistics and such.
|
---|
357 | */
|
---|
358 | RTDECL(int) RTStrAAppendNTag(char **ppsz, const char *pszAppend, size_t cchAppend, const char *pszTag);
|
---|
359 |
|
---|
360 | /**
|
---|
361 | * Appends one or more strings onto an existing IPRT allocated string.
|
---|
362 | *
|
---|
363 | * This is a very flexible and efficient alternative to using RTStrAPrintf to
|
---|
364 | * combine several strings together.
|
---|
365 | *
|
---|
366 | * @retval VINF_SUCCESS
|
---|
367 | * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
|
---|
368 | * remains unchanged.
|
---|
369 | *
|
---|
370 | * @param ppsz Pointer to the string pointer. The string
|
---|
371 | * pointer must either be NULL or point to a string
|
---|
372 | * returned by an IPRT string API. (In/Out)
|
---|
373 | * @param cPairs The number of string / length pairs in the
|
---|
374 | * @a va.
|
---|
375 | * @param va List of string (const char *) and length
|
---|
376 | * (size_t) pairs. The strings will be appended to
|
---|
377 | * the string in the first argument.
|
---|
378 | */
|
---|
379 | #define RTStrAAppendExNV(ppsz, cPairs, va) RTStrAAppendExNVTag((ppsz), (cPairs), (va), RTSTR_TAG)
|
---|
380 |
|
---|
381 | /**
|
---|
382 | * Appends one or more strings onto an existing IPRT allocated string.
|
---|
383 | *
|
---|
384 | * This is a very flexible and efficient alternative to using RTStrAPrintf to
|
---|
385 | * combine several strings together.
|
---|
386 | *
|
---|
387 | * @retval VINF_SUCCESS
|
---|
388 | * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
|
---|
389 | * remains unchanged.
|
---|
390 | *
|
---|
391 | * @param ppsz Pointer to the string pointer. The string
|
---|
392 | * pointer must either be NULL or point to a string
|
---|
393 | * returned by an IPRT string API. (In/Out)
|
---|
394 | * @param cPairs The number of string / length pairs in the
|
---|
395 | * @a va.
|
---|
396 | * @param va List of string (const char *) and length
|
---|
397 | * (size_t) pairs. The strings will be appended to
|
---|
398 | * the string in the first argument.
|
---|
399 | * @param pszTag Allocation tag used for statistics and such.
|
---|
400 | */
|
---|
401 | RTDECL(int) RTStrAAppendExNVTag(char **ppsz, size_t cPairs, va_list va, const char *pszTag);
|
---|
402 |
|
---|
403 | /**
|
---|
404 | * Appends one or more strings onto an existing IPRT allocated string
|
---|
405 | * (untagged).
|
---|
406 | *
|
---|
407 | * This is a very flexible and efficient alternative to using RTStrAPrintf to
|
---|
408 | * combine several strings together.
|
---|
409 | *
|
---|
410 | * @retval VINF_SUCCESS
|
---|
411 | * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
|
---|
412 | * remains unchanged.
|
---|
413 | *
|
---|
414 | * @param ppsz Pointer to the string pointer. The string
|
---|
415 | * pointer must either be NULL or point to a string
|
---|
416 | * returned by an IPRT string API. (In/Out)
|
---|
417 | * @param cPairs The number of string / length pairs in the
|
---|
418 | * ellipsis.
|
---|
419 | * @param ... List of string (const char *) and length
|
---|
420 | * (size_t) pairs. The strings will be appended to
|
---|
421 | * the string in the first argument.
|
---|
422 | */
|
---|
423 | DECLINLINE(int) RTStrAAppendExN(char **ppsz, size_t cPairs, ...)
|
---|
424 | {
|
---|
425 | int rc;
|
---|
426 | va_list va;
|
---|
427 | va_start(va, cPairs);
|
---|
428 | rc = RTStrAAppendExNVTag(ppsz, cPairs, va, RTSTR_TAG);
|
---|
429 | va_end(va);
|
---|
430 | return rc;
|
---|
431 | }
|
---|
432 |
|
---|
433 | /**
|
---|
434 | * Appends one or more strings onto an existing IPRT allocated string (custom
|
---|
435 | * tag).
|
---|
436 | *
|
---|
437 | * This is a very flexible and efficient alternative to using RTStrAPrintf to
|
---|
438 | * combine several strings together.
|
---|
439 | *
|
---|
440 | * @retval VINF_SUCCESS
|
---|
441 | * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
|
---|
442 | * remains unchanged.
|
---|
443 | *
|
---|
444 | * @param ppsz Pointer to the string pointer. The string
|
---|
445 | * pointer must either be NULL or point to a string
|
---|
446 | * returned by an IPRT string API. (In/Out)
|
---|
447 | * @param pszTag Allocation tag used for statistics and such.
|
---|
448 | * @param cPairs The number of string / length pairs in the
|
---|
449 | * ellipsis.
|
---|
450 | * @param ... List of string (const char *) and length
|
---|
451 | * (size_t) pairs. The strings will be appended to
|
---|
452 | * the string in the first argument.
|
---|
453 | */
|
---|
454 | DECLINLINE(int) RTStrAAppendExNTag(char **ppsz, const char *pszTag, size_t cPairs, ...)
|
---|
455 | {
|
---|
456 | int rc;
|
---|
457 | va_list va;
|
---|
458 | va_start(va, cPairs);
|
---|
459 | rc = RTStrAAppendExNVTag(ppsz, cPairs, va, pszTag);
|
---|
460 | va_end(va);
|
---|
461 | return rc;
|
---|
462 | }
|
---|
463 |
|
---|
464 | /**
|
---|
465 | * Truncates an IPRT allocated string (default tag).
|
---|
466 | *
|
---|
467 | * @retval VINF_SUCCESS.
|
---|
468 | * @retval VERR_OUT_OF_RANGE if cchNew is too long. Nothing is done.
|
---|
469 | *
|
---|
470 | * @param ppsz Pointer to the string pointer. The string
|
---|
471 | * pointer can be NULL if @a cchNew is 0, no change
|
---|
472 | * is made then. If we actually reallocate the
|
---|
473 | * string, the string pointer might be changed by
|
---|
474 | * this call. (In/Out)
|
---|
475 | * @param cchNew The new string length (excluding the
|
---|
476 | * terminator). The string must be at least this
|
---|
477 | * long or we'll return VERR_OUT_OF_RANGE and
|
---|
478 | * assert on you.
|
---|
479 | */
|
---|
480 | #define RTStrATruncate(ppsz, cchNew) RTStrATruncateTag((ppsz), (cchNew), RTSTR_TAG)
|
---|
481 |
|
---|
482 | /**
|
---|
483 | * Truncates an IPRT allocated string.
|
---|
484 | *
|
---|
485 | * @retval VINF_SUCCESS.
|
---|
486 | * @retval VERR_OUT_OF_RANGE if cchNew is too long. Nothing is done.
|
---|
487 | *
|
---|
488 | * @param ppsz Pointer to the string pointer. The string
|
---|
489 | * pointer can be NULL if @a cchNew is 0, no change
|
---|
490 | * is made then. If we actually reallocate the
|
---|
491 | * string, the string pointer might be changed by
|
---|
492 | * this call. (In/Out)
|
---|
493 | * @param cchNew The new string length (excluding the
|
---|
494 | * terminator). The string must be at least this
|
---|
495 | * long or we'll return VERR_OUT_OF_RANGE and
|
---|
496 | * assert on you.
|
---|
497 | * @param pszTag Allocation tag used for statistics and such.
|
---|
498 | */
|
---|
499 | RTDECL(int) RTStrATruncateTag(char **ppsz, size_t cchNew, const char *pszTag);
|
---|
500 |
|
---|
501 | /**
|
---|
502 | * Allocates memory for string storage (default tag).
|
---|
503 | *
|
---|
504 | * You should normally not use this function, except if there is some very
|
---|
505 | * custom string handling you need doing that isn't covered by any of the other
|
---|
506 | * APIs.
|
---|
507 | *
|
---|
508 | * @returns Pointer to the allocated string. The first byte is always set
|
---|
509 | * to the string terminator char, the contents of the remainder of the
|
---|
510 | * memory is undefined. The string must be freed by calling RTStrFree.
|
---|
511 | *
|
---|
512 | * NULL is returned if the allocation failed. Please translate this to
|
---|
513 | * VERR_NO_STR_MEMORY and not VERR_NO_MEMORY. Also consider
|
---|
514 | * RTStrAllocEx if an IPRT status code is required.
|
---|
515 | *
|
---|
516 | * @param cb How many bytes to allocate. If this is zero, we
|
---|
517 | * will allocate a terminator byte anyway.
|
---|
518 | */
|
---|
519 | #define RTStrAlloc(cb) RTStrAllocTag((cb), RTSTR_TAG)
|
---|
520 |
|
---|
521 | /**
|
---|
522 | * Allocates memory for string storage (custom tag).
|
---|
523 | *
|
---|
524 | * You should normally not use this function, except if there is some very
|
---|
525 | * custom string handling you need doing that isn't covered by any of the other
|
---|
526 | * APIs.
|
---|
527 | *
|
---|
528 | * @returns Pointer to the allocated string. The first byte is always set
|
---|
529 | * to the string terminator char, the contents of the remainder of the
|
---|
530 | * memory is undefined. The string must be freed by calling RTStrFree.
|
---|
531 | *
|
---|
532 | * NULL is returned if the allocation failed. Please translate this to
|
---|
533 | * VERR_NO_STR_MEMORY and not VERR_NO_MEMORY. Also consider
|
---|
534 | * RTStrAllocEx if an IPRT status code is required.
|
---|
535 | *
|
---|
536 | * @param cb How many bytes to allocate. If this is zero, we
|
---|
537 | * will allocate a terminator byte anyway.
|
---|
538 | * @param pszTag Allocation tag used for statistics and such.
|
---|
539 | */
|
---|
540 | RTDECL(char *) RTStrAllocTag(size_t cb, const char *pszTag);
|
---|
541 |
|
---|
542 | /**
|
---|
543 | * Allocates memory for string storage, with status code (default tag).
|
---|
544 | *
|
---|
545 | * You should normally not use this function, except if there is some very
|
---|
546 | * custom string handling you need doing that isn't covered by any of the other
|
---|
547 | * APIs.
|
---|
548 | *
|
---|
549 | * @retval VINF_SUCCESS
|
---|
550 | * @retval VERR_NO_STR_MEMORY
|
---|
551 | *
|
---|
552 | * @param ppsz Where to return the allocated string. This will
|
---|
553 | * be set to NULL on failure. On success, the
|
---|
554 | * returned memory will always start with a
|
---|
555 | * terminator char so that it is considered a valid
|
---|
556 | * C string, the contents of rest of the memory is
|
---|
557 | * undefined.
|
---|
558 | * @param cb How many bytes to allocate. If this is zero, we
|
---|
559 | * will allocate a terminator byte anyway.
|
---|
560 | */
|
---|
561 | #define RTStrAllocEx(ppsz, cb) RTStrAllocExTag((ppsz), (cb), RTSTR_TAG)
|
---|
562 |
|
---|
563 | /**
|
---|
564 | * Allocates memory for string storage, with status code (custom tag).
|
---|
565 | *
|
---|
566 | * You should normally not use this function, except if there is some very
|
---|
567 | * custom string handling you need doing that isn't covered by any of the other
|
---|
568 | * APIs.
|
---|
569 | *
|
---|
570 | * @retval VINF_SUCCESS
|
---|
571 | * @retval VERR_NO_STR_MEMORY
|
---|
572 | *
|
---|
573 | * @param ppsz Where to return the allocated string. This will
|
---|
574 | * be set to NULL on failure. On success, the
|
---|
575 | * returned memory will always start with a
|
---|
576 | * terminator char so that it is considered a valid
|
---|
577 | * C string, the contents of rest of the memory is
|
---|
578 | * undefined.
|
---|
579 | * @param cb How many bytes to allocate. If this is zero, we
|
---|
580 | * will allocate a terminator byte anyway.
|
---|
581 | * @param pszTag Allocation tag used for statistics and such.
|
---|
582 | */
|
---|
583 | RTDECL(int) RTStrAllocExTag(char **ppsz, size_t cb, const char *pszTag);
|
---|
584 |
|
---|
585 | /**
|
---|
586 | * Reallocates the specified string (default tag).
|
---|
587 | *
|
---|
588 | * You should normally not have use this function, except perhaps to truncate a
|
---|
589 | * really long string you've got from some IPRT string API, but then you should
|
---|
590 | * use RTStrATruncate.
|
---|
591 | *
|
---|
592 | * @returns VINF_SUCCESS.
|
---|
593 | * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
|
---|
594 | * remains unchanged.
|
---|
595 | *
|
---|
596 | * @param ppsz Pointer to the string variable containing the
|
---|
597 | * input and output string.
|
---|
598 | *
|
---|
599 | * When not freeing the string, the result will
|
---|
600 | * always have the last byte set to the terminator
|
---|
601 | * character so that when used for string
|
---|
602 | * truncation the result will be a valid C string
|
---|
603 | * (your job to keep it a valid UTF-8 string).
|
---|
604 | *
|
---|
605 | * When the input string is NULL and we're supposed
|
---|
606 | * to reallocate, the returned string will also
|
---|
607 | * have the first byte set to the terminator char
|
---|
608 | * so it will be a valid C string.
|
---|
609 | *
|
---|
610 | * @param cbNew When @a cbNew is zero, we'll behave like
|
---|
611 | * RTStrFree and @a *ppsz will be set to NULL.
|
---|
612 | *
|
---|
613 | * When not zero, this will be the new size of the
|
---|
614 | * memory backing the string, i.e. it includes the
|
---|
615 | * terminator char.
|
---|
616 | */
|
---|
617 | #define RTStrRealloc(ppsz, cbNew) RTStrReallocTag((ppsz), (cbNew), RTSTR_TAG)
|
---|
618 |
|
---|
619 | /**
|
---|
620 | * Reallocates the specified string (custom tag).
|
---|
621 | *
|
---|
622 | * You should normally not have use this function, except perhaps to truncate a
|
---|
623 | * really long string you've got from some IPRT string API, but then you should
|
---|
624 | * use RTStrATruncate.
|
---|
625 | *
|
---|
626 | * @returns VINF_SUCCESS.
|
---|
627 | * @retval VERR_NO_STR_MEMORY if we failed to reallocate the string, @a *ppsz
|
---|
628 | * remains unchanged.
|
---|
629 | *
|
---|
630 | * @param ppsz Pointer to the string variable containing the
|
---|
631 | * input and output string.
|
---|
632 | *
|
---|
633 | * When not freeing the string, the result will
|
---|
634 | * always have the last byte set to the terminator
|
---|
635 | * character so that when used for string
|
---|
636 | * truncation the result will be a valid C string
|
---|
637 | * (your job to keep it a valid UTF-8 string).
|
---|
638 | *
|
---|
639 | * When the input string is NULL and we're supposed
|
---|
640 | * to reallocate, the returned string will also
|
---|
641 | * have the first byte set to the terminator char
|
---|
642 | * so it will be a valid C string.
|
---|
643 | *
|
---|
644 | * @param cbNew When @a cbNew is zero, we'll behave like
|
---|
645 | * RTStrFree and @a *ppsz will be set to NULL.
|
---|
646 | *
|
---|
647 | * When not zero, this will be the new size of the
|
---|
648 | * memory backing the string, i.e. it includes the
|
---|
649 | * terminator char.
|
---|
650 | * @param pszTag Allocation tag used for statistics and such.
|
---|
651 | */
|
---|
652 | RTDECL(int) RTStrReallocTag(char **ppsz, size_t cbNew, const char *pszTag);
|
---|
653 |
|
---|
654 | /**
|
---|
655 | * Validates the UTF-8 encoding of the string.
|
---|
656 | *
|
---|
657 | * @returns iprt status code.
|
---|
658 | * @param psz The string.
|
---|
659 | */
|
---|
660 | RTDECL(int) RTStrValidateEncoding(const char *psz);
|
---|
661 |
|
---|
662 | /** @name Flags for RTStrValidateEncodingEx
|
---|
663 | */
|
---|
664 | /** Check that the string is zero terminated within the given size.
|
---|
665 | * VERR_BUFFER_OVERFLOW will be returned if the check fails. */
|
---|
666 | #define RTSTR_VALIDATE_ENCODING_ZERO_TERMINATED RT_BIT_32(0)
|
---|
667 | /** @} */
|
---|
668 |
|
---|
669 | /**
|
---|
670 | * Validates the UTF-8 encoding of the string.
|
---|
671 | *
|
---|
672 | * @returns iprt status code.
|
---|
673 | * @param psz The string.
|
---|
674 | * @param cch The max string length. Use RTSTR_MAX to process the entire string.
|
---|
675 | * @param fFlags Reserved for future. Pass 0.
|
---|
676 | */
|
---|
677 | RTDECL(int) RTStrValidateEncodingEx(const char *psz, size_t cch, uint32_t fFlags);
|
---|
678 |
|
---|
679 | /**
|
---|
680 | * Checks if the UTF-8 encoding is valid.
|
---|
681 | *
|
---|
682 | * @returns true / false.
|
---|
683 | * @param psz The string.
|
---|
684 | */
|
---|
685 | RTDECL(bool) RTStrIsValidEncoding(const char *psz);
|
---|
686 |
|
---|
687 | /**
|
---|
688 | * Purge all bad UTF-8 encoding in the string, replacing it with '?'.
|
---|
689 | *
|
---|
690 | * @returns The number of bad characters (0 if nothing was done).
|
---|
691 | * @param psz The string to purge.
|
---|
692 | */
|
---|
693 | RTDECL(size_t) RTStrPurgeEncoding(char *psz);
|
---|
694 |
|
---|
695 | /**
|
---|
696 | * Gets the number of code points the string is made up of, excluding
|
---|
697 | * the terminator.
|
---|
698 | *
|
---|
699 | *
|
---|
700 | * @returns Number of code points (RTUNICP).
|
---|
701 | * @returns 0 if the string was incorrectly encoded.
|
---|
702 | * @param psz The string.
|
---|
703 | */
|
---|
704 | RTDECL(size_t) RTStrUniLen(const char *psz);
|
---|
705 |
|
---|
706 | /**
|
---|
707 | * Gets the number of code points the string is made up of, excluding
|
---|
708 | * the terminator.
|
---|
709 | *
|
---|
710 | * This function will validate the string, and incorrectly encoded UTF-8
|
---|
711 | * strings will be rejected.
|
---|
712 | *
|
---|
713 | * @returns iprt status code.
|
---|
714 | * @param psz The string.
|
---|
715 | * @param cch The max string length. Use RTSTR_MAX to process the entire string.
|
---|
716 | * @param pcuc Where to store the code point count.
|
---|
717 | * This is undefined on failure.
|
---|
718 | */
|
---|
719 | RTDECL(int) RTStrUniLenEx(const char *psz, size_t cch, size_t *pcuc);
|
---|
720 |
|
---|
721 | /**
|
---|
722 | * Translate a UTF-8 string into an unicode string (i.e. RTUNICPs), allocating the string buffer.
|
---|
723 | *
|
---|
724 | * @returns iprt status code.
|
---|
725 | * @param pszString UTF-8 string to convert.
|
---|
726 | * @param ppUniString Receives pointer to the allocated unicode string.
|
---|
727 | * The returned string must be freed using RTUniFree().
|
---|
728 | */
|
---|
729 | RTDECL(int) RTStrToUni(const char *pszString, PRTUNICP *ppUniString);
|
---|
730 |
|
---|
731 | /**
|
---|
732 | * Translates pszString from UTF-8 to an array of code points, allocating the result
|
---|
733 | * array if requested.
|
---|
734 | *
|
---|
735 | * @returns iprt status code.
|
---|
736 | * @param pszString UTF-8 string to convert.
|
---|
737 | * @param cchString The maximum size in chars (the type) to convert. The conversion stop
|
---|
738 | * when it reaches cchString or the string terminator ('\\0').
|
---|
739 | * Use RTSTR_MAX to translate the entire string.
|
---|
740 | * @param ppaCps If cCps is non-zero, this must either be pointing to pointer to
|
---|
741 | * a buffer of the specified size, or pointer to a NULL pointer.
|
---|
742 | * If *ppusz is NULL or cCps is zero a buffer of at least cCps items
|
---|
743 | * will be allocated to hold the translated string.
|
---|
744 | * If a buffer was requested it must be freed using RTUtf16Free().
|
---|
745 | * @param cCps The number of code points in the unicode string. This includes the terminator.
|
---|
746 | * @param pcCps Where to store the length of the translated string,
|
---|
747 | * excluding the terminator. (Optional)
|
---|
748 | *
|
---|
749 | * This may be set under some error conditions,
|
---|
750 | * however, only for VERR_BUFFER_OVERFLOW and
|
---|
751 | * VERR_NO_STR_MEMORY will it contain a valid string
|
---|
752 | * length that can be used to resize the buffer.
|
---|
753 | */
|
---|
754 | RTDECL(int) RTStrToUniEx(const char *pszString, size_t cchString, PRTUNICP *ppaCps, size_t cCps, size_t *pcCps);
|
---|
755 |
|
---|
756 | /**
|
---|
757 | * Calculates the length of the string in RTUTF16 items.
|
---|
758 | *
|
---|
759 | * This function will validate the string, and incorrectly encoded UTF-8
|
---|
760 | * strings will be rejected. The primary purpose of this function is to
|
---|
761 | * help allocate buffers for RTStrToUtf16Ex of the correct size. For most
|
---|
762 | * other purposes RTStrCalcUtf16LenEx() should be used.
|
---|
763 | *
|
---|
764 | * @returns Number of RTUTF16 items.
|
---|
765 | * @returns 0 if the string was incorrectly encoded.
|
---|
766 | * @param psz The string.
|
---|
767 | */
|
---|
768 | RTDECL(size_t) RTStrCalcUtf16Len(const char *psz);
|
---|
769 |
|
---|
770 | /**
|
---|
771 | * Calculates the length of the string in RTUTF16 items.
|
---|
772 | *
|
---|
773 | * This function will validate the string, and incorrectly encoded UTF-8
|
---|
774 | * strings will be rejected.
|
---|
775 | *
|
---|
776 | * @returns iprt status code.
|
---|
777 | * @param psz The string.
|
---|
778 | * @param cch The max string length. Use RTSTR_MAX to process the entire string.
|
---|
779 | * @param pcwc Where to store the string length. Optional.
|
---|
780 | * This is undefined on failure.
|
---|
781 | */
|
---|
782 | RTDECL(int) RTStrCalcUtf16LenEx(const char *psz, size_t cch, size_t *pcwc);
|
---|
783 |
|
---|
784 | /**
|
---|
785 | * Translate a UTF-8 string into a UTF-16 allocating the result buffer (default
|
---|
786 | * tag).
|
---|
787 | *
|
---|
788 | * @returns iprt status code.
|
---|
789 | * @param pszString UTF-8 string to convert.
|
---|
790 | * @param ppwszString Receives pointer to the allocated UTF-16 string.
|
---|
791 | * The returned string must be freed using RTUtf16Free().
|
---|
792 | */
|
---|
793 | #define RTStrToUtf16(pszString, ppwszString) RTStrToUtf16Tag((pszString), (ppwszString), RTSTR_TAG)
|
---|
794 |
|
---|
795 | /**
|
---|
796 | * Translate a UTF-8 string into a UTF-16 allocating the result buffer (custom
|
---|
797 | * tag).
|
---|
798 | *
|
---|
799 | * @returns iprt status code.
|
---|
800 | * @param pszString UTF-8 string to convert.
|
---|
801 | * @param ppwszString Receives pointer to the allocated UTF-16 string.
|
---|
802 | * The returned string must be freed using RTUtf16Free().
|
---|
803 | * @param pszTag Allocation tag used for statistics and such.
|
---|
804 | */
|
---|
805 | RTDECL(int) RTStrToUtf16Tag(const char *pszString, PRTUTF16 *ppwszString, const char *pszTag);
|
---|
806 |
|
---|
807 | /**
|
---|
808 | * Translates pszString from UTF-8 to UTF-16, allocating the result buffer if requested.
|
---|
809 | *
|
---|
810 | * @returns iprt status code.
|
---|
811 | * @param pszString UTF-8 string to convert.
|
---|
812 | * @param cchString The maximum size in chars (the type) to convert. The conversion stop
|
---|
813 | * when it reaches cchString or the string terminator ('\\0').
|
---|
814 | * Use RTSTR_MAX to translate the entire string.
|
---|
815 | * @param ppwsz If cwc is non-zero, this must either be pointing to pointer to
|
---|
816 | * a buffer of the specified size, or pointer to a NULL pointer.
|
---|
817 | * If *ppwsz is NULL or cwc is zero a buffer of at least cwc items
|
---|
818 | * will be allocated to hold the translated string.
|
---|
819 | * If a buffer was requested it must be freed using RTUtf16Free().
|
---|
820 | * @param cwc The buffer size in RTUTF16s. This includes the terminator.
|
---|
821 | * @param pcwc Where to store the length of the translated string,
|
---|
822 | * excluding the terminator. (Optional)
|
---|
823 | *
|
---|
824 | * This may be set under some error conditions,
|
---|
825 | * however, only for VERR_BUFFER_OVERFLOW and
|
---|
826 | * VERR_NO_STR_MEMORY will it contain a valid string
|
---|
827 | * length that can be used to resize the buffer.
|
---|
828 | */
|
---|
829 | #define RTStrToUtf16Ex(pszString, cchString, ppwsz, cwc, pcwc) \
|
---|
830 | RTStrToUtf16ExTag((pszString), (cchString), (ppwsz), (cwc), (pcwc), RTSTR_TAG)
|
---|
831 |
|
---|
832 | /**
|
---|
833 | * Translates pszString from UTF-8 to UTF-16, allocating the result buffer if
|
---|
834 | * requested (custom tag).
|
---|
835 | *
|
---|
836 | * @returns iprt status code.
|
---|
837 | * @param pszString UTF-8 string to convert.
|
---|
838 | * @param cchString The maximum size in chars (the type) to convert. The conversion stop
|
---|
839 | * when it reaches cchString or the string terminator ('\\0').
|
---|
840 | * Use RTSTR_MAX to translate the entire string.
|
---|
841 | * @param ppwsz If cwc is non-zero, this must either be pointing to pointer to
|
---|
842 | * a buffer of the specified size, or pointer to a NULL pointer.
|
---|
843 | * If *ppwsz is NULL or cwc is zero a buffer of at least cwc items
|
---|
844 | * will be allocated to hold the translated string.
|
---|
845 | * If a buffer was requested it must be freed using RTUtf16Free().
|
---|
846 | * @param cwc The buffer size in RTUTF16s. This includes the terminator.
|
---|
847 | * @param pcwc Where to store the length of the translated string,
|
---|
848 | * excluding the terminator. (Optional)
|
---|
849 | *
|
---|
850 | * This may be set under some error conditions,
|
---|
851 | * however, only for VERR_BUFFER_OVERFLOW and
|
---|
852 | * VERR_NO_STR_MEMORY will it contain a valid string
|
---|
853 | * length that can be used to resize the buffer.
|
---|
854 | * @param pszTag Allocation tag used for statistics and such.
|
---|
855 | */
|
---|
856 | RTDECL(int) RTStrToUtf16ExTag(const char *pszString, size_t cchString, PRTUTF16 *ppwsz, size_t cwc, size_t *pcwc, const char *pszTag);
|
---|
857 |
|
---|
858 |
|
---|
859 | /**
|
---|
860 | * Calculates the length of the string in Latin-1 characters.
|
---|
861 | *
|
---|
862 | * This function will validate the string, and incorrectly encoded UTF-8
|
---|
863 | * strings as well as string with codepoints outside the latin-1 range will be
|
---|
864 | * rejected. The primary purpose of this function is to help allocate buffers
|
---|
865 | * for RTStrToLatin1Ex of the correct size. For most other purposes
|
---|
866 | * RTStrCalcLatin1LenEx() should be used.
|
---|
867 | *
|
---|
868 | * @returns Number of Latin-1 characters.
|
---|
869 | * @returns 0 if the string was incorrectly encoded.
|
---|
870 | * @param psz The string.
|
---|
871 | */
|
---|
872 | RTDECL(size_t) RTStrCalcLatin1Len(const char *psz);
|
---|
873 |
|
---|
874 | /**
|
---|
875 | * Calculates the length of the string in Latin-1 characters.
|
---|
876 | *
|
---|
877 | * This function will validate the string, and incorrectly encoded UTF-8
|
---|
878 | * strings as well as string with codepoints outside the latin-1 range will be
|
---|
879 | * rejected.
|
---|
880 | *
|
---|
881 | * @returns iprt status code.
|
---|
882 | * @param psz The string.
|
---|
883 | * @param cch The max string length. Use RTSTR_MAX to process the
|
---|
884 | * entire string.
|
---|
885 | * @param pcch Where to store the string length. Optional.
|
---|
886 | * This is undefined on failure.
|
---|
887 | */
|
---|
888 | RTDECL(int) RTStrCalcLatin1LenEx(const char *psz, size_t cch, size_t *pcwc);
|
---|
889 |
|
---|
890 | /**
|
---|
891 | * Translate a UTF-8 string into a Latin-1 allocating the result buffer (default
|
---|
892 | * tag).
|
---|
893 | *
|
---|
894 | * @returns iprt status code.
|
---|
895 | * @param pszString UTF-8 string to convert.
|
---|
896 | * @param ppszString Receives pointer to the allocated Latin-1 string.
|
---|
897 | * The returned string must be freed using RTStrFree().
|
---|
898 | */
|
---|
899 | #define RTStrToLatin1(pszString, ppszString) RTStrToLatin1Tag((pszString), (ppszString), RTSTR_TAG)
|
---|
900 |
|
---|
901 | /**
|
---|
902 | * Translate a UTF-8 string into a Latin-1 allocating the result buffer (custom
|
---|
903 | * tag).
|
---|
904 | *
|
---|
905 | * @returns iprt status code.
|
---|
906 | * @param pszString UTF-8 string to convert.
|
---|
907 | * @param ppszString Receives pointer to the allocated Latin-1 string.
|
---|
908 | * The returned string must be freed using RTStrFree().
|
---|
909 | * @param pszTag Allocation tag used for statistics and such.
|
---|
910 | */
|
---|
911 | RTDECL(int) RTStrToLatin1Tag(const char *pszString, char **ppszString, const char *pszTag);
|
---|
912 |
|
---|
913 | /**
|
---|
914 | * Translates pszString from UTF-8 to Latin-1, allocating the result buffer if requested.
|
---|
915 | *
|
---|
916 | * @returns iprt status code.
|
---|
917 | * @param pszString UTF-8 string to convert.
|
---|
918 | * @param cchString The maximum size in chars (the type) to convert.
|
---|
919 | * The conversion stop when it reaches cchString or
|
---|
920 | * the string terminator ('\\0'). Use RTSTR_MAX to
|
---|
921 | * translate the entire string.
|
---|
922 | * @param ppsz If cch is non-zero, this must either be pointing to
|
---|
923 | * pointer to a buffer of the specified size, or
|
---|
924 | * pointer to a NULL pointer. If *ppsz is NULL or cch
|
---|
925 | * is zero a buffer of at least cch items will be
|
---|
926 | * allocated to hold the translated string. If a
|
---|
927 | * buffer was requested it must be freed using
|
---|
928 | * RTStrFree().
|
---|
929 | * @param cch The buffer size in bytes. This includes the
|
---|
930 | * terminator.
|
---|
931 | * @param pcch Where to store the length of the translated string,
|
---|
932 | * excluding the terminator. (Optional)
|
---|
933 | *
|
---|
934 | * This may be set under some error conditions,
|
---|
935 | * however, only for VERR_BUFFER_OVERFLOW and
|
---|
936 | * VERR_NO_STR_MEMORY will it contain a valid string
|
---|
937 | * length that can be used to resize the buffer.
|
---|
938 | */
|
---|
939 | #define RTStrToLatin1Ex(pszString, cchString, ppsz, cch, pcch) \
|
---|
940 | RTStrToLatin1ExTag((pszString), (cchString), (ppsz), (cch), (pcch), RTSTR_TAG)
|
---|
941 |
|
---|
942 | /**
|
---|
943 | * Translates pszString from UTF-8 to Latin1, allocating the result buffer if
|
---|
944 | * requested (custom tag).
|
---|
945 | *
|
---|
946 | * @returns iprt status code.
|
---|
947 | * @param pszString UTF-8 string to convert.
|
---|
948 | * @param cchString The maximum size in chars (the type) to convert.
|
---|
949 | * The conversion stop when it reaches cchString or
|
---|
950 | * the string terminator ('\\0'). Use RTSTR_MAX to
|
---|
951 | * translate the entire string.
|
---|
952 | * @param ppsz If cch is non-zero, this must either be pointing to
|
---|
953 | * pointer to a buffer of the specified size, or
|
---|
954 | * pointer to a NULL pointer. If *ppsz is NULL or cch
|
---|
955 | * is zero a buffer of at least cch items will be
|
---|
956 | * allocated to hold the translated string. If a
|
---|
957 | * buffer was requested it must be freed using
|
---|
958 | * RTStrFree().
|
---|
959 | * @param cch The buffer size in bytes. This includes the
|
---|
960 | * terminator.
|
---|
961 | * @param pcch Where to store the length of the translated string,
|
---|
962 | * excluding the terminator. (Optional)
|
---|
963 | *
|
---|
964 | * This may be set under some error conditions,
|
---|
965 | * however, only for VERR_BUFFER_OVERFLOW and
|
---|
966 | * VERR_NO_STR_MEMORY will it contain a valid string
|
---|
967 | * length that can be used to resize the buffer.
|
---|
968 | * @param pszTag Allocation tag used for statistics and such.
|
---|
969 | */
|
---|
970 | RTDECL(int) RTStrToLatin1ExTag(const char *pszString, size_t cchString, char **ppsz, size_t cch, size_t *pcch, const char *pszTag);
|
---|
971 |
|
---|
972 |
|
---|
973 | /**
|
---|
974 | * Translate a Latin1 string into a UTF-8 allocating the result buffer (default
|
---|
975 | * tag).
|
---|
976 | *
|
---|
977 | * @returns iprt status code.
|
---|
978 | * @param pszString Latin1 string to convert.
|
---|
979 | * @param ppszString Receives pointer of allocated UTF-8 string on
|
---|
980 | * success, and is always set to NULL on failure.
|
---|
981 | * The returned pointer must be freed using RTStrFree().
|
---|
982 | */
|
---|
983 | #define RTLatin1ToUtf8(pszString, ppszString) RTLatin1ToUtf8Tag((pszString), (ppszString), RTSTR_TAG)
|
---|
984 |
|
---|
985 | /**
|
---|
986 | * Translate a Latin-1 string into a UTF-8 allocating the result buffer.
|
---|
987 | *
|
---|
988 | * @returns iprt status code.
|
---|
989 | * @param pszString Latin-1 string to convert.
|
---|
990 | * @param ppszString Receives pointer of allocated UTF-8 string on
|
---|
991 | * success, and is always set to NULL on failure.
|
---|
992 | * The returned pointer must be freed using RTStrFree().
|
---|
993 | * @param pszTag Allocation tag used for statistics and such.
|
---|
994 | */
|
---|
995 | RTDECL(int) RTLatin1ToUtf8Tag(const char *pszString, char **ppszString, const char *pszTag);
|
---|
996 |
|
---|
997 | /**
|
---|
998 | * Translates Latin-1 to UTF-8 using buffer provided by the caller or a fittingly
|
---|
999 | * sized buffer allocated by the function (default tag).
|
---|
1000 | *
|
---|
1001 | * @returns iprt status code.
|
---|
1002 | * @param pszString The Latin-1 string to convert.
|
---|
1003 | * @param cchString The number of Latin-1 characters to translate from
|
---|
1004 | * pszString. The translation will stop when reaching
|
---|
1005 | * cchString or the terminator ('\\0'). Use RTSTR_MAX
|
---|
1006 | * to translate the entire string.
|
---|
1007 | * @param ppsz If cch is non-zero, this must either be pointing to
|
---|
1008 | * a pointer to a buffer of the specified size, or
|
---|
1009 | * pointer to a NULL pointer. If *ppsz is NULL or cch
|
---|
1010 | * is zero a buffer of at least cch chars will be
|
---|
1011 | * allocated to hold the translated string. If a
|
---|
1012 | * buffer was requested it must be freed using
|
---|
1013 | * RTStrFree().
|
---|
1014 | * @param cch The buffer size in chars (the type). This includes the terminator.
|
---|
1015 | * @param pcch Where to store the length of the translated string,
|
---|
1016 | * excluding the terminator. (Optional)
|
---|
1017 | *
|
---|
1018 | * This may be set under some error conditions,
|
---|
1019 | * however, only for VERR_BUFFER_OVERFLOW and
|
---|
1020 | * VERR_NO_STR_MEMORY will it contain a valid string
|
---|
1021 | * length that can be used to resize the buffer.
|
---|
1022 | */
|
---|
1023 | #define RTLatin1ToUtf8Ex(pszString, cchString, ppsz, cch, pcch) \
|
---|
1024 | RTLatin1ToUtf8ExTag((pszString), (cchString), (ppsz), (cch), (pcch), RTSTR_TAG)
|
---|
1025 |
|
---|
1026 | /**
|
---|
1027 | * Translates Latin1 to UTF-8 using buffer provided by the caller or a fittingly
|
---|
1028 | * sized buffer allocated by the function (custom tag).
|
---|
1029 | *
|
---|
1030 | * @returns iprt status code.
|
---|
1031 | * @param pszString The Latin1 string to convert.
|
---|
1032 | * @param cchString The number of Latin1 characters to translate from
|
---|
1033 | * pwszString. The translation will stop when
|
---|
1034 | * reaching cchString or the terminator ('\\0'). Use
|
---|
1035 | * RTSTR_MAX to translate the entire string.
|
---|
1036 | * @param ppsz If cch is non-zero, this must either be pointing to
|
---|
1037 | * a pointer to a buffer of the specified size, or
|
---|
1038 | * pointer to a NULL pointer. If *ppsz is NULL or cch
|
---|
1039 | * is zero a buffer of at least cch chars will be
|
---|
1040 | * allocated to hold the translated string. If a
|
---|
1041 | * buffer was requested it must be freed using
|
---|
1042 | * RTStrFree().
|
---|
1043 | * @param cch The buffer size in chars (the type). This includes
|
---|
1044 | * the terminator.
|
---|
1045 | * @param pcch Where to store the length of the translated string,
|
---|
1046 | * excluding the terminator. (Optional)
|
---|
1047 | *
|
---|
1048 | * This may be set under some error conditions,
|
---|
1049 | * however, only for VERR_BUFFER_OVERFLOW and
|
---|
1050 | * VERR_NO_STR_MEMORY will it contain a valid string
|
---|
1051 | * length that can be used to resize the buffer.
|
---|
1052 | * @param pszTag Allocation tag used for statistics and such.
|
---|
1053 | */
|
---|
1054 | RTDECL(int) RTLatin1ToUtf8ExTag(const char *pszString, size_t cchString, char **ppsz, size_t cch, size_t *pcch, const char *pszTag);
|
---|
1055 |
|
---|
1056 | /**
|
---|
1057 | * Calculates the length of the Latin-1 string in UTF-8 chars (bytes).
|
---|
1058 | *
|
---|
1059 | * The primary purpose of this function is to help allocate buffers for
|
---|
1060 | * RTLatin1ToUtf8() of the correct size. For most other purposes
|
---|
1061 | * RTLatin1ToUtf8Ex() should be used.
|
---|
1062 | *
|
---|
1063 | * @returns Number of chars (bytes).
|
---|
1064 | * @returns 0 if the string was incorrectly encoded.
|
---|
1065 | * @param psz The Latin-1 string.
|
---|
1066 | */
|
---|
1067 | RTDECL(size_t) RTLatin1CalcUtf8Len(const char *psz);
|
---|
1068 |
|
---|
1069 | /**
|
---|
1070 | * Calculates the length of the Latin-1 string in UTF-8 chars (bytes).
|
---|
1071 | *
|
---|
1072 | * @returns iprt status code.
|
---|
1073 | * @param psz The string.
|
---|
1074 | * @param cch The max string length. Use RTSTR_MAX to process the entire string.
|
---|
1075 | * @param pcch Where to store the string length (in bytes). Optional.
|
---|
1076 | * This is undefined on failure.
|
---|
1077 | */
|
---|
1078 | RTDECL(int) RTLatin1CalcUtf8LenEx(const char *psz, size_t cch, size_t *pcch);
|
---|
1079 |
|
---|
1080 | /**
|
---|
1081 | * Get the unicode code point at the given string position.
|
---|
1082 | *
|
---|
1083 | * @returns unicode code point.
|
---|
1084 | * @returns RTUNICP_INVALID if the encoding is invalid.
|
---|
1085 | * @param psz The string.
|
---|
1086 | */
|
---|
1087 | RTDECL(RTUNICP) RTStrGetCpInternal(const char *psz);
|
---|
1088 |
|
---|
1089 | /**
|
---|
1090 | * Get the unicode code point at the given string position.
|
---|
1091 | *
|
---|
1092 | * @returns iprt status code
|
---|
1093 | * @returns VERR_INVALID_UTF8_ENCODING if the encoding is invalid.
|
---|
1094 | * @param ppsz The string cursor.
|
---|
1095 | * This is advanced one character forward on failure.
|
---|
1096 | * @param pCp Where to store the unicode code point.
|
---|
1097 | * Stores RTUNICP_INVALID if the encoding is invalid.
|
---|
1098 | */
|
---|
1099 | RTDECL(int) RTStrGetCpExInternal(const char **ppsz, PRTUNICP pCp);
|
---|
1100 |
|
---|
1101 | /**
|
---|
1102 | * Get the unicode code point at the given string position for a string of a
|
---|
1103 | * given length.
|
---|
1104 | *
|
---|
1105 | * @returns iprt status code
|
---|
1106 | * @retval VERR_INVALID_UTF8_ENCODING if the encoding is invalid.
|
---|
1107 | * @retval VERR_END_OF_STRING if *pcch is 0. *pCp is set to RTUNICP_INVALID.
|
---|
1108 | *
|
---|
1109 | * @param ppsz The string.
|
---|
1110 | * @param pcch Pointer to the length of the string. This will be
|
---|
1111 | * decremented by the size of the code point.
|
---|
1112 | * @param pCp Where to store the unicode code point.
|
---|
1113 | * Stores RTUNICP_INVALID if the encoding is invalid.
|
---|
1114 | */
|
---|
1115 | RTDECL(int) RTStrGetCpNExInternal(const char **ppsz, size_t *pcch, PRTUNICP pCp);
|
---|
1116 |
|
---|
1117 | /**
|
---|
1118 | * Put the unicode code point at the given string position
|
---|
1119 | * and return the pointer to the char following it.
|
---|
1120 | *
|
---|
1121 | * This function will not consider anything at or following the
|
---|
1122 | * buffer area pointed to by psz. It is therefore not suitable for
|
---|
1123 | * inserting code points into a string, only appending/overwriting.
|
---|
1124 | *
|
---|
1125 | * @returns pointer to the char following the written code point.
|
---|
1126 | * @param psz The string.
|
---|
1127 | * @param CodePoint The code point to write.
|
---|
1128 | * This should not be RTUNICP_INVALID or any other
|
---|
1129 | * character out of the UTF-8 range.
|
---|
1130 | *
|
---|
1131 | * @remark This is a worker function for RTStrPutCp().
|
---|
1132 | *
|
---|
1133 | */
|
---|
1134 | RTDECL(char *) RTStrPutCpInternal(char *psz, RTUNICP CodePoint);
|
---|
1135 |
|
---|
1136 | /**
|
---|
1137 | * Get the unicode code point at the given string position.
|
---|
1138 | *
|
---|
1139 | * @returns unicode code point.
|
---|
1140 | * @returns RTUNICP_INVALID if the encoding is invalid.
|
---|
1141 | * @param psz The string.
|
---|
1142 | *
|
---|
1143 | * @remark We optimize this operation by using an inline function for
|
---|
1144 | * the most frequent and simplest sequence, the rest is
|
---|
1145 | * handled by RTStrGetCpInternal().
|
---|
1146 | */
|
---|
1147 | DECLINLINE(RTUNICP) RTStrGetCp(const char *psz)
|
---|
1148 | {
|
---|
1149 | const unsigned char uch = *(const unsigned char *)psz;
|
---|
1150 | if (!(uch & RT_BIT(7)))
|
---|
1151 | return uch;
|
---|
1152 | return RTStrGetCpInternal(psz);
|
---|
1153 | }
|
---|
1154 |
|
---|
1155 | /**
|
---|
1156 | * Get the unicode code point at the given string position.
|
---|
1157 | *
|
---|
1158 | * @returns iprt status code.
|
---|
1159 | * @param ppsz Pointer to the string pointer. This will be updated to
|
---|
1160 | * point to the char following the current code point.
|
---|
1161 | * This is advanced one character forward on failure.
|
---|
1162 | * @param pCp Where to store the code point.
|
---|
1163 | * RTUNICP_INVALID is stored here on failure.
|
---|
1164 | *
|
---|
1165 | * @remark We optimize this operation by using an inline function for
|
---|
1166 | * the most frequent and simplest sequence, the rest is
|
---|
1167 | * handled by RTStrGetCpExInternal().
|
---|
1168 | */
|
---|
1169 | DECLINLINE(int) RTStrGetCpEx(const char **ppsz, PRTUNICP pCp)
|
---|
1170 | {
|
---|
1171 | const unsigned char uch = **(const unsigned char **)ppsz;
|
---|
1172 | if (!(uch & RT_BIT(7)))
|
---|
1173 | {
|
---|
1174 | (*ppsz)++;
|
---|
1175 | *pCp = uch;
|
---|
1176 | return VINF_SUCCESS;
|
---|
1177 | }
|
---|
1178 | return RTStrGetCpExInternal(ppsz, pCp);
|
---|
1179 | }
|
---|
1180 |
|
---|
1181 | /**
|
---|
1182 | * Get the unicode code point at the given string position for a string of a
|
---|
1183 | * given maximum length.
|
---|
1184 | *
|
---|
1185 | * @returns iprt status code.
|
---|
1186 | * @retval VERR_INVALID_UTF8_ENCODING if the encoding is invalid.
|
---|
1187 | * @retval VERR_END_OF_STRING if *pcch is 0. *pCp is set to RTUNICP_INVALID.
|
---|
1188 | *
|
---|
1189 | * @param ppsz Pointer to the string pointer. This will be updated to
|
---|
1190 | * point to the char following the current code point.
|
---|
1191 | * @param pcch Pointer to the maximum string length. This will be
|
---|
1192 | * decremented by the size of the code point found.
|
---|
1193 | * @param pCp Where to store the code point.
|
---|
1194 | * RTUNICP_INVALID is stored here on failure.
|
---|
1195 | *
|
---|
1196 | * @remark We optimize this operation by using an inline function for
|
---|
1197 | * the most frequent and simplest sequence, the rest is
|
---|
1198 | * handled by RTStrGetCpNExInternal().
|
---|
1199 | */
|
---|
1200 | DECLINLINE(int) RTStrGetCpNEx(const char **ppsz, size_t *pcch, PRTUNICP pCp)
|
---|
1201 | {
|
---|
1202 | if (RT_LIKELY(*pcch != 0))
|
---|
1203 | {
|
---|
1204 | const unsigned char uch = **(const unsigned char **)ppsz;
|
---|
1205 | if (!(uch & RT_BIT(7)))
|
---|
1206 | {
|
---|
1207 | (*ppsz)++;
|
---|
1208 | (*pcch)--;
|
---|
1209 | *pCp = uch;
|
---|
1210 | return VINF_SUCCESS;
|
---|
1211 | }
|
---|
1212 | }
|
---|
1213 | return RTStrGetCpNExInternal(ppsz, pcch, pCp);
|
---|
1214 | }
|
---|
1215 |
|
---|
1216 | /**
|
---|
1217 | * Get the UTF-8 size in characters of a given Unicode code point.
|
---|
1218 | *
|
---|
1219 | * The code point is expected to be a valid Unicode one, but not necessarily in
|
---|
1220 | * the range supported by UTF-8.
|
---|
1221 | *
|
---|
1222 | * @returns The number of chars (bytes) required to encode the code point, or
|
---|
1223 | * zero if there is no UTF-8 encoding.
|
---|
1224 | * @param CodePoint The unicode code point.
|
---|
1225 | */
|
---|
1226 | DECLINLINE(size_t) RTStrCpSize(RTUNICP CodePoint)
|
---|
1227 | {
|
---|
1228 | if (CodePoint < 0x00000080)
|
---|
1229 | return 1;
|
---|
1230 | if (CodePoint < 0x00000800)
|
---|
1231 | return 2;
|
---|
1232 | if (CodePoint < 0x00010000)
|
---|
1233 | return 3;
|
---|
1234 | #ifdef RT_USE_RTC_3629
|
---|
1235 | if (CodePoint < 0x00011000)
|
---|
1236 | return 4;
|
---|
1237 | #else
|
---|
1238 | if (CodePoint < 0x00200000)
|
---|
1239 | return 4;
|
---|
1240 | if (CodePoint < 0x04000000)
|
---|
1241 | return 5;
|
---|
1242 | if (CodePoint < 0x7fffffff)
|
---|
1243 | return 6;
|
---|
1244 | #endif
|
---|
1245 | return 0;
|
---|
1246 | }
|
---|
1247 |
|
---|
1248 | /**
|
---|
1249 | * Put the unicode code point at the given string position
|
---|
1250 | * and return the pointer to the char following it.
|
---|
1251 | *
|
---|
1252 | * This function will not consider anything at or following the
|
---|
1253 | * buffer area pointed to by psz. It is therefore not suitable for
|
---|
1254 | * inserting code points into a string, only appending/overwriting.
|
---|
1255 | *
|
---|
1256 | * @returns pointer to the char following the written code point.
|
---|
1257 | * @param psz The string.
|
---|
1258 | * @param CodePoint The code point to write.
|
---|
1259 | * This should not be RTUNICP_INVALID or any other
|
---|
1260 | * character out of the UTF-8 range.
|
---|
1261 | *
|
---|
1262 | * @remark We optimize this operation by using an inline function for
|
---|
1263 | * the most frequent and simplest sequence, the rest is
|
---|
1264 | * handled by RTStrPutCpInternal().
|
---|
1265 | */
|
---|
1266 | DECLINLINE(char *) RTStrPutCp(char *psz, RTUNICP CodePoint)
|
---|
1267 | {
|
---|
1268 | if (CodePoint < 0x80)
|
---|
1269 | {
|
---|
1270 | *psz++ = (unsigned char)CodePoint;
|
---|
1271 | return psz;
|
---|
1272 | }
|
---|
1273 | return RTStrPutCpInternal(psz, CodePoint);
|
---|
1274 | }
|
---|
1275 |
|
---|
1276 | /**
|
---|
1277 | * Skips ahead, past the current code point.
|
---|
1278 | *
|
---|
1279 | * @returns Pointer to the char after the current code point.
|
---|
1280 | * @param psz Pointer to the current code point.
|
---|
1281 | * @remark This will not move the next valid code point, only past the current one.
|
---|
1282 | */
|
---|
1283 | DECLINLINE(char *) RTStrNextCp(const char *psz)
|
---|
1284 | {
|
---|
1285 | RTUNICP Cp;
|
---|
1286 | RTStrGetCpEx(&psz, &Cp);
|
---|
1287 | return (char *)psz;
|
---|
1288 | }
|
---|
1289 |
|
---|
1290 | /**
|
---|
1291 | * Skips back to the previous code point.
|
---|
1292 | *
|
---|
1293 | * @returns Pointer to the char before the current code point.
|
---|
1294 | * @returns pszStart on failure.
|
---|
1295 | * @param pszStart Pointer to the start of the string.
|
---|
1296 | * @param psz Pointer to the current code point.
|
---|
1297 | */
|
---|
1298 | RTDECL(char *) RTStrPrevCp(const char *pszStart, const char *psz);
|
---|
1299 |
|
---|
1300 | /**
|
---|
1301 | * Get the unicode code point at the given string position.
|
---|
1302 | *
|
---|
1303 | * @returns unicode code point.
|
---|
1304 | * @returns RTUNICP_INVALID if the encoding is invalid.
|
---|
1305 | * @param psz The string.
|
---|
1306 | */
|
---|
1307 | DECLINLINE(RTUNICP) RTLatin1GetCp(const char *psz)
|
---|
1308 | {
|
---|
1309 | return *(const unsigned char *)psz;
|
---|
1310 | }
|
---|
1311 |
|
---|
1312 | /**
|
---|
1313 | * Get the unicode code point at the given string position.
|
---|
1314 | *
|
---|
1315 | * @returns iprt status code.
|
---|
1316 | * @param ppsz Pointer to the string pointer. This will be updated to
|
---|
1317 | * point to the char following the current code point.
|
---|
1318 | * This is advanced one character forward on failure.
|
---|
1319 | * @param pCp Where to store the code point.
|
---|
1320 | * RTUNICP_INVALID is stored here on failure.
|
---|
1321 | *
|
---|
1322 | * @remark We optimize this operation by using an inline function for
|
---|
1323 | * the most frequent and simplest sequence, the rest is
|
---|
1324 | * handled by RTStrGetCpExInternal().
|
---|
1325 | */
|
---|
1326 | DECLINLINE(int) RTLatin1GetCpEx(const char **ppsz, PRTUNICP pCp)
|
---|
1327 | {
|
---|
1328 | const unsigned char uch = **(const unsigned char **)ppsz;
|
---|
1329 | (*ppsz)++;
|
---|
1330 | *pCp = uch;
|
---|
1331 | return VINF_SUCCESS;
|
---|
1332 | }
|
---|
1333 |
|
---|
1334 | /**
|
---|
1335 | * Get the unicode code point at the given string position for a string of a
|
---|
1336 | * given maximum length.
|
---|
1337 | *
|
---|
1338 | * @returns iprt status code.
|
---|
1339 | * @retval VERR_END_OF_STRING if *pcch is 0. *pCp is set to RTUNICP_INVALID.
|
---|
1340 | *
|
---|
1341 | * @param ppsz Pointer to the string pointer. This will be updated to
|
---|
1342 | * point to the char following the current code point.
|
---|
1343 | * @param pcch Pointer to the maximum string length. This will be
|
---|
1344 | * decremented by the size of the code point found.
|
---|
1345 | * @param pCp Where to store the code point.
|
---|
1346 | * RTUNICP_INVALID is stored here on failure.
|
---|
1347 | */
|
---|
1348 | DECLINLINE(int) RTLatin1GetCpNEx(const char **ppsz, size_t *pcch, PRTUNICP pCp)
|
---|
1349 | {
|
---|
1350 | if (RT_LIKELY(*pcch != 0))
|
---|
1351 | {
|
---|
1352 | const unsigned char uch = **(const unsigned char **)ppsz;
|
---|
1353 | (*ppsz)++;
|
---|
1354 | (*pcch)--;
|
---|
1355 | *pCp = uch;
|
---|
1356 | return VINF_SUCCESS;
|
---|
1357 | }
|
---|
1358 | *pCp = RTUNICP_INVALID;
|
---|
1359 | return VERR_END_OF_STRING;
|
---|
1360 | }
|
---|
1361 |
|
---|
1362 | /**
|
---|
1363 | * Get the Latin-1 size in characters of a given Unicode code point.
|
---|
1364 | *
|
---|
1365 | * The code point is expected to be a valid Unicode one, but not necessarily in
|
---|
1366 | * the range supported by Latin-1.
|
---|
1367 | *
|
---|
1368 | * @returns the size in characters, or zero if there is no Latin-1 encoding
|
---|
1369 | */
|
---|
1370 | DECLINLINE(size_t) RTLatin1CpSize(RTUNICP CodePoint)
|
---|
1371 | {
|
---|
1372 | if (CodePoint < 0x100)
|
---|
1373 | return 1;
|
---|
1374 | return 0;
|
---|
1375 | }
|
---|
1376 |
|
---|
1377 | /**
|
---|
1378 | * Put the unicode code point at the given string position
|
---|
1379 | * and return the pointer to the char following it.
|
---|
1380 | *
|
---|
1381 | * This function will not consider anything at or following the
|
---|
1382 | * buffer area pointed to by psz. It is therefore not suitable for
|
---|
1383 | * inserting code points into a string, only appending/overwriting.
|
---|
1384 | *
|
---|
1385 | * @returns pointer to the char following the written code point.
|
---|
1386 | * @param psz The string.
|
---|
1387 | * @param CodePoint The code point to write.
|
---|
1388 | * This should not be RTUNICP_INVALID or any other
|
---|
1389 | * character out of the Latin-1 range.
|
---|
1390 | */
|
---|
1391 | DECLINLINE(char *) RTLatin1PutCp(char *psz, RTUNICP CodePoint)
|
---|
1392 | {
|
---|
1393 | AssertReturn(CodePoint < 0x100, NULL);
|
---|
1394 | *psz++ = (unsigned char)CodePoint;
|
---|
1395 | return psz;
|
---|
1396 | }
|
---|
1397 |
|
---|
1398 | /**
|
---|
1399 | * Skips ahead, past the current code point.
|
---|
1400 | *
|
---|
1401 | * @returns Pointer to the char after the current code point.
|
---|
1402 | * @param psz Pointer to the current code point.
|
---|
1403 | * @remark This will not move the next valid code point, only past the current one.
|
---|
1404 | */
|
---|
1405 | DECLINLINE(char *) RTLatin1NextCp(const char *psz)
|
---|
1406 | {
|
---|
1407 | psz++;
|
---|
1408 | return (char *)psz;
|
---|
1409 | }
|
---|
1410 |
|
---|
1411 | /**
|
---|
1412 | * Skips back to the previous code point.
|
---|
1413 | *
|
---|
1414 | * @returns Pointer to the char before the current code point.
|
---|
1415 | * @returns pszStart on failure.
|
---|
1416 | * @param pszStart Pointer to the start of the string.
|
---|
1417 | * @param psz Pointer to the current code point.
|
---|
1418 | */
|
---|
1419 | DECLINLINE(char *) RTLatin1PrevCp(const char *psz)
|
---|
1420 | {
|
---|
1421 | psz--;
|
---|
1422 | return (char *)psz;
|
---|
1423 | }
|
---|
1424 |
|
---|
1425 |
|
---|
1426 | /** @page pg_rt_str_format The IPRT Format Strings
|
---|
1427 | *
|
---|
1428 | * IPRT implements most of the commonly used format types and flags with the
|
---|
1429 | * exception of floating point which is completely missing. In addition IPRT
|
---|
1430 | * provides a number of IPRT specific format types for the IPRT typedefs and
|
---|
1431 | * other useful things. Note that several of these extensions are similar to
|
---|
1432 | * \%p and doesn't care much if you try add formating flags/width/precision.
|
---|
1433 | *
|
---|
1434 | *
|
---|
1435 | * Group 0a, The commonly used format types:
|
---|
1436 | * - \%s - Takes a pointer to a zero terminated string (UTF-8) and
|
---|
1437 | * prints it with the optionally adjustment (width, -) and
|
---|
1438 | * length restriction (precision).
|
---|
1439 | * - \%ls - Same as \%s except that the input is UTF-16 (output UTF-8).
|
---|
1440 | * - \%Ls - Same as \%s except that the input is UCS-32 (output UTF-8).
|
---|
1441 | * - \%S - R3: Same as \%s except it is printed in the current codeset
|
---|
1442 | * instead of UTF-8 (source is still UTF-8).
|
---|
1443 | * Other contexts: Same as \%s.
|
---|
1444 | * - \%lS - Same as \%S except that the input is UTF-16 (output UTF-8).
|
---|
1445 | * - \%LS - Same as \%S except that the input is UCS-32 (output UTF-8).
|
---|
1446 | * - \%c - Takes a char and prints it.
|
---|
1447 | * - \%d - Takes a signed integer and prints it as decimal. Thousand
|
---|
1448 | * separator (\'), zero padding (0), adjustment (-+), width,
|
---|
1449 | * precision
|
---|
1450 | * - \%i - Same as \%d.
|
---|
1451 | * - \%u - Takes an unsigned integer and prints it as decimal. Thousand
|
---|
1452 | * separator (\'), zero padding (0), adjustment (-+), width,
|
---|
1453 | * precision
|
---|
1454 | * - \%x - Takes an unsigned integer and prints it as lowercased
|
---|
1455 | * hexadecimal. The special hash (\#) flag causes a '0x'
|
---|
1456 | * prefixed to be printed. Zero padding (0), adjustment (-+),
|
---|
1457 | * width, precision.
|
---|
1458 | * - \%X - Same as \%x except that it is uppercased.
|
---|
1459 | * - \%o - Takes an unsigned (?) integer and prints it as octal. Zero
|
---|
1460 | * padding (0), adjustment (-+), width, precision.
|
---|
1461 | * - \%p - Takes a pointer (void technically) and prints it. Zero
|
---|
1462 | * padding (0), adjustment (-+), width, precision.
|
---|
1463 | *
|
---|
1464 | * The \%d, \%i, \%u, \%x, \%X and \%o format types support the following
|
---|
1465 | * argument type specifiers:
|
---|
1466 | * - \%ll - long long (uint64_t).
|
---|
1467 | * - \%L - long long (uint64_t).
|
---|
1468 | * - \%l - long (uint32_t, uint64_t)
|
---|
1469 | * - \%h - short (int16_t).
|
---|
1470 | * - \%hh - char (int8_t).
|
---|
1471 | * - \%H - char (int8_t).
|
---|
1472 | * - \%z - size_t.
|
---|
1473 | * - \%j - intmax_t (int64_t).
|
---|
1474 | * - \%t - ptrdiff_t.
|
---|
1475 | * The type in parentheses is typical sizes, however when printing those types
|
---|
1476 | * you are better off using the special group 2 format types below (\%RX32 and
|
---|
1477 | * such).
|
---|
1478 | *
|
---|
1479 | *
|
---|
1480 | * Group 0b, IPRT format tricks:
|
---|
1481 | * - %M - Replaces the format string, takes a string pointer.
|
---|
1482 | * - %N - Nested formatting, takes a pointer to a format string
|
---|
1483 | * followed by the pointer to a va_list variable. The va_list
|
---|
1484 | * variable will not be modified and the caller must do va_end()
|
---|
1485 | * on it. Make sure the va_list variable is NOT in a parameter
|
---|
1486 | * list or some gcc versions/targets may get it all wrong.
|
---|
1487 | *
|
---|
1488 | *
|
---|
1489 | * Group 1, the basic runtime typedefs (excluding those which obviously are
|
---|
1490 | * pointer):
|
---|
1491 | * - \%RTbool - Takes a bool value and prints 'true', 'false', or '!%d!'.
|
---|
1492 | * - \%RTfile - Takes a #RTFILE value.
|
---|
1493 | * - \%RTfmode - Takes a #RTFMODE value.
|
---|
1494 | * - \%RTfoff - Takes a #RTFOFF value.
|
---|
1495 | * - \%RTfp16 - Takes a #RTFAR16 value.
|
---|
1496 | * - \%RTfp32 - Takes a #RTFAR32 value.
|
---|
1497 | * - \%RTfp64 - Takes a #RTFAR64 value.
|
---|
1498 | * - \%RTgid - Takes a #RTGID value.
|
---|
1499 | * - \%RTino - Takes a #RTINODE value.
|
---|
1500 | * - \%RTint - Takes a #RTINT value.
|
---|
1501 | * - \%RTiop - Takes a #RTIOPORT value.
|
---|
1502 | * - \%RTldrm - Takes a #RTLDRMOD value.
|
---|
1503 | * - \%RTmac - Takes a #PCRTMAC pointer.
|
---|
1504 | * - \%RTnaddr - Takes a #PCRTNETADDR value.
|
---|
1505 | * - \%RTnaipv4 - Takes a #RTNETADDRIPV4 value.
|
---|
1506 | * - \%RTnaipv6 - Takes a #PCRTNETADDRIPV6 value.
|
---|
1507 | * - \%RTnthrd - Takes a #RTNATIVETHREAD value.
|
---|
1508 | * - \%RTnthrd - Takes a #RTNATIVETHREAD value.
|
---|
1509 | * - \%RTproc - Takes a #RTPROCESS value.
|
---|
1510 | * - \%RTptr - Takes a #RTINTPTR or #RTUINTPTR value (but not void *).
|
---|
1511 | * - \%RTreg - Takes a #RTCCUINTREG value.
|
---|
1512 | * - \%RTsel - Takes a #RTSEL value.
|
---|
1513 | * - \%RTsem - Takes a #RTSEMEVENT, #RTSEMEVENTMULTI, #RTSEMMUTEX, #RTSEMFASTMUTEX, or #RTSEMRW value.
|
---|
1514 | * - \%RTsock - Takes a #RTSOCKET value.
|
---|
1515 | * - \%RTthrd - Takes a #RTTHREAD value.
|
---|
1516 | * - \%RTuid - Takes a #RTUID value.
|
---|
1517 | * - \%RTuint - Takes a #RTUINT value.
|
---|
1518 | * - \%RTunicp - Takes a #RTUNICP value.
|
---|
1519 | * - \%RTutf16 - Takes a #RTUTF16 value.
|
---|
1520 | * - \%RTuuid - Takes a #PCRTUUID and will print the UUID as a string.
|
---|
1521 | * - \%RTxuint - Takes a #RTUINT or #RTINT value, formatting it as hex.
|
---|
1522 | * - \%RGi - Takes a #RTGCINT value.
|
---|
1523 | * - \%RGp - Takes a #RTGCPHYS value.
|
---|
1524 | * - \%RGr - Takes a #RTGCUINTREG value.
|
---|
1525 | * - \%RGu - Takes a #RTGCUINT value.
|
---|
1526 | * - \%RGv - Takes a #RTGCPTR, #RTGCINTPTR or #RTGCUINTPTR value.
|
---|
1527 | * - \%RGx - Takes a #RTGCUINT or #RTGCINT value, formatting it as hex.
|
---|
1528 | * - \%RHi - Takes a #RTHCINT value.
|
---|
1529 | * - \%RHp - Takes a #RTHCPHYS value.
|
---|
1530 | * - \%RHr - Takes a #RTHCUINTREG value.
|
---|
1531 | * - \%RHu - Takes a #RTHCUINT value.
|
---|
1532 | * - \%RHv - Takes a #RTHCPTR, #RTHCINTPTR or #RTHCUINTPTR value.
|
---|
1533 | * - \%RHx - Takes a #RTHCUINT or #RTHCINT value, formatting it as hex.
|
---|
1534 | * - \%RRv - Takes a #RTRCPTR, #RTRCINTPTR or #RTRCUINTPTR value.
|
---|
1535 | * - \%RCi - Takes a #RTINT value.
|
---|
1536 | * - \%RCp - Takes a #RTCCPHYS value.
|
---|
1537 | * - \%RCr - Takes a #RTCCUINTREG value.
|
---|
1538 | * - \%RCu - Takes a #RTUINT value.
|
---|
1539 | * - \%RCv - Takes a #uintptr_t, #intptr_t, void * value.
|
---|
1540 | * - \%RCx - Takes a #RTUINT or #RTINT value, formatting it as hex.
|
---|
1541 | *
|
---|
1542 | *
|
---|
1543 | * Group 2, the generic integer types which are prefered over relying on what
|
---|
1544 | * bit-count a 'long', 'short', or 'long long' has on a platform. This are
|
---|
1545 | * highly prefered for the [u]intXX_t kind of types:
|
---|
1546 | * - \%RI[8|16|32|64] - Signed integer value of the specifed bit count.
|
---|
1547 | * - \%RU[8|16|32|64] - Unsigned integer value of the specifed bit count.
|
---|
1548 | * - \%RX[8|16|32|64] - Hexadecimal integer value of the specifed bit count.
|
---|
1549 | *
|
---|
1550 | *
|
---|
1551 | * Group 3, hex dumpers and other complex stuff which requires more than simple
|
---|
1552 | * formatting:
|
---|
1553 | * - \%Rhxd - Takes a pointer to the memory which is to be dumped in typical
|
---|
1554 | * hex format. Use the precision to specify the length, and the width to
|
---|
1555 | * set the number of bytes per line. Default width and precision is 16.
|
---|
1556 | * - \%Rhxs - Takes a pointer to the memory to be displayed as a hex string,
|
---|
1557 | * i.e. a series of space separated bytes formatted as two digit hex value.
|
---|
1558 | * Use the precision to specify the length. Default length is 16 bytes.
|
---|
1559 | * The width, if specified, is ignored.
|
---|
1560 | * - \%Rrc - Takes an integer iprt status code as argument. Will insert the
|
---|
1561 | * status code define corresponding to the iprt status code.
|
---|
1562 | * - \%Rrs - Takes an integer iprt status code as argument. Will insert the
|
---|
1563 | * short description of the specified status code.
|
---|
1564 | * - \%Rrf - Takes an integer iprt status code as argument. Will insert the
|
---|
1565 | * full description of the specified status code.
|
---|
1566 | * - \%Rra - Takes an integer iprt status code as argument. Will insert the
|
---|
1567 | * status code define + full description.
|
---|
1568 | * - \%Rwc - Takes a long Windows error code as argument. Will insert the status
|
---|
1569 | * code define corresponding to the Windows error code.
|
---|
1570 | * - \%Rwf - Takes a long Windows error code as argument. Will insert the
|
---|
1571 | * full description of the specified status code.
|
---|
1572 | * - \%Rwa - Takes a long Windows error code as argument. Will insert the
|
---|
1573 | * error code define + full description.
|
---|
1574 | *
|
---|
1575 | * - \%Rhrc - Takes a COM/XPCOM status code as argument. Will insert the status
|
---|
1576 | * code define corresponding to the Windows error code.
|
---|
1577 | * - \%Rhrf - Takes a COM/XPCOM status code as argument. Will insert the
|
---|
1578 | * full description of the specified status code.
|
---|
1579 | * - \%Rhra - Takes a COM/XPCOM error code as argument. Will insert the
|
---|
1580 | * error code define + full description.
|
---|
1581 | *
|
---|
1582 | * - \%Rfn - Pretty printing of a function or method. It drops the
|
---|
1583 | * return code and parameter list.
|
---|
1584 | * - \%Rbn - Prints the base name. For dropping the path in
|
---|
1585 | * order to save space when printing a path name.
|
---|
1586 | *
|
---|
1587 | * On other platforms, \%Rw? simply prints the argument in a form of 0xXXXXXXXX.
|
---|
1588 | *
|
---|
1589 | *
|
---|
1590 | * Group 4, structure dumpers:
|
---|
1591 | * - \%RDtimespec - Takes a PCRTTIMESPEC.
|
---|
1592 | *
|
---|
1593 | *
|
---|
1594 | * Group 5, XML / HTML escapers:
|
---|
1595 | * - \%RMas - Takes a string pointer (const char *) and outputs
|
---|
1596 | * it as an attribute value with the proper escaping.
|
---|
1597 | * This typically ends up in double quotes.
|
---|
1598 | *
|
---|
1599 | * - \%RMes - Takes a string pointer (const char *) and outputs
|
---|
1600 | * it as an element with the necessary escaping.
|
---|
1601 | *
|
---|
1602 | *
|
---|
1603 | */
|
---|
1604 |
|
---|
1605 | #ifndef DECLARED_FNRTSTROUTPUT /* duplicated in iprt/log.h */
|
---|
1606 | # define DECLARED_FNRTSTROUTPUT
|
---|
1607 | /**
|
---|
1608 | * Output callback.
|
---|
1609 | *
|
---|
1610 | * @returns number of bytes written.
|
---|
1611 | * @param pvArg User argument.
|
---|
1612 | * @param pachChars Pointer to an array of utf-8 characters.
|
---|
1613 | * @param cbChars Number of bytes in the character array pointed to by pachChars.
|
---|
1614 | */
|
---|
1615 | typedef DECLCALLBACK(size_t) FNRTSTROUTPUT(void *pvArg, const char *pachChars, size_t cbChars);
|
---|
1616 | /** Pointer to callback function. */
|
---|
1617 | typedef FNRTSTROUTPUT *PFNRTSTROUTPUT;
|
---|
1618 | #endif
|
---|
1619 |
|
---|
1620 | /** Format flag.
|
---|
1621 | * These are used by RTStrFormat extensions and RTStrFormatNumber, mind
|
---|
1622 | * that not all flags makes sense to both of the functions.
|
---|
1623 | * @{ */
|
---|
1624 | #define RTSTR_F_CAPITAL 0x0001
|
---|
1625 | #define RTSTR_F_LEFT 0x0002
|
---|
1626 | #define RTSTR_F_ZEROPAD 0x0004
|
---|
1627 | #define RTSTR_F_SPECIAL 0x0008
|
---|
1628 | #define RTSTR_F_VALSIGNED 0x0010
|
---|
1629 | #define RTSTR_F_PLUS 0x0020
|
---|
1630 | #define RTSTR_F_BLANK 0x0040
|
---|
1631 | #define RTSTR_F_WIDTH 0x0080
|
---|
1632 | #define RTSTR_F_PRECISION 0x0100
|
---|
1633 | #define RTSTR_F_THOUSAND_SEP 0x0200
|
---|
1634 |
|
---|
1635 | #define RTSTR_F_BIT_MASK 0xf800
|
---|
1636 | #define RTSTR_F_8BIT 0x0800
|
---|
1637 | #define RTSTR_F_16BIT 0x1000
|
---|
1638 | #define RTSTR_F_32BIT 0x2000
|
---|
1639 | #define RTSTR_F_64BIT 0x4000
|
---|
1640 | #define RTSTR_F_128BIT 0x8000
|
---|
1641 | /** @} */
|
---|
1642 |
|
---|
1643 | /** @def RTSTR_GET_BIT_FLAG
|
---|
1644 | * Gets the bit flag for the specified type.
|
---|
1645 | */
|
---|
1646 | #define RTSTR_GET_BIT_FLAG(type) \
|
---|
1647 | ( sizeof(type) * 8 == 32 ? RTSTR_F_32BIT \
|
---|
1648 | : sizeof(type) * 8 == 64 ? RTSTR_F_64BIT \
|
---|
1649 | : sizeof(type) * 8 == 16 ? RTSTR_F_16BIT \
|
---|
1650 | : sizeof(type) * 8 == 8 ? RTSTR_F_8BIT \
|
---|
1651 | : sizeof(type) * 8 == 128 ? RTSTR_F_128BIT \
|
---|
1652 | : 0)
|
---|
1653 |
|
---|
1654 |
|
---|
1655 | /**
|
---|
1656 | * Callback to format non-standard format specifiers.
|
---|
1657 | *
|
---|
1658 | * @returns The number of bytes formatted.
|
---|
1659 | * @param pvArg Formatter argument.
|
---|
1660 | * @param pfnOutput Pointer to output function.
|
---|
1661 | * @param pvArgOutput Argument for the output function.
|
---|
1662 | * @param ppszFormat Pointer to the format string pointer. Advance this till the char
|
---|
1663 | * after the format specifier.
|
---|
1664 | * @param pArgs Pointer to the argument list. Use this to fetch the arguments.
|
---|
1665 | * @param cchWidth Format Width. -1 if not specified.
|
---|
1666 | * @param cchPrecision Format Precision. -1 if not specified.
|
---|
1667 | * @param fFlags Flags (RTSTR_NTFS_*).
|
---|
1668 | * @param chArgSize The argument size specifier, 'l' or 'L'.
|
---|
1669 | */
|
---|
1670 | typedef DECLCALLBACK(size_t) FNSTRFORMAT(void *pvArg, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
1671 | const char **ppszFormat, va_list *pArgs, int cchWidth,
|
---|
1672 | int cchPrecision, unsigned fFlags, char chArgSize);
|
---|
1673 | /** Pointer to a FNSTRFORMAT() function. */
|
---|
1674 | typedef FNSTRFORMAT *PFNSTRFORMAT;
|
---|
1675 |
|
---|
1676 |
|
---|
1677 | /**
|
---|
1678 | * Partial implementation of a printf like formatter.
|
---|
1679 | * It doesn't do everything correct, and there is no floating point support.
|
---|
1680 | * However, it supports custom formats by the means of a format callback.
|
---|
1681 | *
|
---|
1682 | * @returns number of bytes formatted.
|
---|
1683 | * @param pfnOutput Output worker.
|
---|
1684 | * Called in two ways. Normally with a string and its length.
|
---|
1685 | * For termination, it's called with NULL for string, 0 for length.
|
---|
1686 | * @param pvArgOutput Argument to the output worker.
|
---|
1687 | * @param pfnFormat Custom format worker.
|
---|
1688 | * @param pvArgFormat Argument to the format worker.
|
---|
1689 | * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
|
---|
1690 | * @param InArgs Argument list.
|
---|
1691 | */
|
---|
1692 | RTDECL(size_t) RTStrFormatV(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat, const char *pszFormat, va_list InArgs);
|
---|
1693 |
|
---|
1694 | /**
|
---|
1695 | * Partial implementation of a printf like formatter.
|
---|
1696 | * It doesn't do everything correct, and there is no floating point support.
|
---|
1697 | * However, it supports custom formats by the means of a format callback.
|
---|
1698 | *
|
---|
1699 | * @returns number of bytes formatted.
|
---|
1700 | * @param pfnOutput Output worker.
|
---|
1701 | * Called in two ways. Normally with a string and its length.
|
---|
1702 | * For termination, it's called with NULL for string, 0 for length.
|
---|
1703 | * @param pvArgOutput Argument to the output worker.
|
---|
1704 | * @param pfnFormat Custom format worker.
|
---|
1705 | * @param pvArgFormat Argument to the format worker.
|
---|
1706 | * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
|
---|
1707 | * @param ... Argument list.
|
---|
1708 | */
|
---|
1709 | RTDECL(size_t) RTStrFormat(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, PFNSTRFORMAT pfnFormat, void *pvArgFormat, const char *pszFormat, ...);
|
---|
1710 |
|
---|
1711 | /**
|
---|
1712 | * Formats an integer number according to the parameters.
|
---|
1713 | *
|
---|
1714 | * @returns Length of the formatted number.
|
---|
1715 | * @param psz Pointer to output string buffer of sufficient size.
|
---|
1716 | * @param u64Value Value to format.
|
---|
1717 | * @param uiBase Number representation base.
|
---|
1718 | * @param cchWidth Width.
|
---|
1719 | * @param cchPrecision Precision.
|
---|
1720 | * @param fFlags Flags (NTFS_*).
|
---|
1721 | */
|
---|
1722 | RTDECL(int) RTStrFormatNumber(char *psz, uint64_t u64Value, unsigned int uiBase, signed int cchWidth, signed int cchPrecision, unsigned int fFlags);
|
---|
1723 |
|
---|
1724 |
|
---|
1725 | /**
|
---|
1726 | * Callback for formatting a type.
|
---|
1727 | *
|
---|
1728 | * This is registered using the RTStrFormatTypeRegister function and will
|
---|
1729 | * be called during string formatting to handle the specified %R[type].
|
---|
1730 | * The argument for this format type is assumed to be a pointer and it's
|
---|
1731 | * passed in the @a pvValue argument.
|
---|
1732 | *
|
---|
1733 | * @returns Length of the formatted output.
|
---|
1734 | * @param pfnOutput Output worker.
|
---|
1735 | * @param pvArgOutput Argument to the output worker.
|
---|
1736 | * @param pszType The type name.
|
---|
1737 | * @param pvValue The argument value.
|
---|
1738 | * @param cchWidth Width.
|
---|
1739 | * @param cchPrecision Precision.
|
---|
1740 | * @param fFlags Flags (NTFS_*).
|
---|
1741 | * @param pvUser The user argument.
|
---|
1742 | */
|
---|
1743 | typedef DECLCALLBACK(size_t) FNRTSTRFORMATTYPE(PFNRTSTROUTPUT pfnOutput, void *pvArgOutput,
|
---|
1744 | const char *pszType, void const *pvValue,
|
---|
1745 | int cchWidth, int cchPrecision, unsigned fFlags,
|
---|
1746 | void *pvUser);
|
---|
1747 | /** Pointer to a FNRTSTRFORMATTYPE. */
|
---|
1748 | typedef FNRTSTRFORMATTYPE *PFNRTSTRFORMATTYPE;
|
---|
1749 |
|
---|
1750 |
|
---|
1751 | /**
|
---|
1752 | * Register a format handler for a type.
|
---|
1753 | *
|
---|
1754 | * The format handler is used to handle '%R[type]' format types, where the argument
|
---|
1755 | * in the vector is a pointer value (a bit restrictive, but keeps it simple).
|
---|
1756 | *
|
---|
1757 | * The caller must ensure that no other thread will be making use of any of
|
---|
1758 | * the dynamic formatting type facilities simultaneously with this call.
|
---|
1759 | *
|
---|
1760 | * @returns IPRT status code.
|
---|
1761 | * @retval VINF_SUCCESS on success.
|
---|
1762 | * @retval VERR_ALREADY_EXISTS if the type has already been registered.
|
---|
1763 | * @retval VERR_TOO_MANY_OPEN_FILES if all the type slots has been allocated already.
|
---|
1764 | *
|
---|
1765 | * @param pszType The type name.
|
---|
1766 | * @param pfnHandler The handler address. See FNRTSTRFORMATTYPE for details.
|
---|
1767 | * @param pvUser The user argument to pass to the handler. See RTStrFormatTypeSetUser
|
---|
1768 | * for how to update this later.
|
---|
1769 | */
|
---|
1770 | RTDECL(int) RTStrFormatTypeRegister(const char *pszType, PFNRTSTRFORMATTYPE pfnHandler, void *pvUser);
|
---|
1771 |
|
---|
1772 | /**
|
---|
1773 | * Deregisters a format type.
|
---|
1774 | *
|
---|
1775 | * The caller must ensure that no other thread will be making use of any of
|
---|
1776 | * the dynamic formatting type facilities simultaneously with this call.
|
---|
1777 | *
|
---|
1778 | * @returns IPRT status code.
|
---|
1779 | * @retval VINF_SUCCESS on success.
|
---|
1780 | * @retval VERR_FILE_NOT_FOUND if not found.
|
---|
1781 | *
|
---|
1782 | * @param pszType The type to deregister.
|
---|
1783 | */
|
---|
1784 | RTDECL(int) RTStrFormatTypeDeregister(const char *pszType);
|
---|
1785 |
|
---|
1786 | /**
|
---|
1787 | * Sets the user argument for a type.
|
---|
1788 | *
|
---|
1789 | * This can be used if a user argument needs relocating in GC.
|
---|
1790 | *
|
---|
1791 | * @returns IPRT status code.
|
---|
1792 | * @retval VINF_SUCCESS on success.
|
---|
1793 | * @retval VERR_FILE_NOT_FOUND if not found.
|
---|
1794 | *
|
---|
1795 | * @param pszType The type to update.
|
---|
1796 | * @param pvUser The new user argument value.
|
---|
1797 | */
|
---|
1798 | RTDECL(int) RTStrFormatTypeSetUser(const char *pszType, void *pvUser);
|
---|
1799 |
|
---|
1800 |
|
---|
1801 | /**
|
---|
1802 | * String printf.
|
---|
1803 | *
|
---|
1804 | * @returns The length of the returned string (in pszBuffer) excluding the
|
---|
1805 | * terminator.
|
---|
1806 | * @param pszBuffer Output buffer.
|
---|
1807 | * @param cchBuffer Size of the output buffer.
|
---|
1808 | * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
|
---|
1809 | * @param args The format argument.
|
---|
1810 | */
|
---|
1811 | RTDECL(size_t) RTStrPrintfV(char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args);
|
---|
1812 |
|
---|
1813 | /**
|
---|
1814 | * String printf.
|
---|
1815 | *
|
---|
1816 | * @returns The length of the returned string (in pszBuffer) excluding the
|
---|
1817 | * terminator.
|
---|
1818 | * @param pszBuffer Output buffer.
|
---|
1819 | * @param cchBuffer Size of the output buffer.
|
---|
1820 | * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
|
---|
1821 | * @param ... The format argument.
|
---|
1822 | */
|
---|
1823 | RTDECL(size_t) RTStrPrintf(char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...);
|
---|
1824 |
|
---|
1825 |
|
---|
1826 | /**
|
---|
1827 | * String printf with custom formatting.
|
---|
1828 | *
|
---|
1829 | * @returns The length of the returned string (in pszBuffer) excluding the
|
---|
1830 | * terminator.
|
---|
1831 | * @param pfnFormat Pointer to handler function for the custom formats.
|
---|
1832 | * @param pvArg Argument to the pfnFormat function.
|
---|
1833 | * @param pszBuffer Output buffer.
|
---|
1834 | * @param cchBuffer Size of the output buffer.
|
---|
1835 | * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
|
---|
1836 | * @param args The format argument.
|
---|
1837 | */
|
---|
1838 | RTDECL(size_t) RTStrPrintfExV(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, va_list args);
|
---|
1839 |
|
---|
1840 | /**
|
---|
1841 | * String printf with custom formatting.
|
---|
1842 | *
|
---|
1843 | * @returns The length of the returned string (in pszBuffer) excluding the
|
---|
1844 | * terminator.
|
---|
1845 | * @param pfnFormat Pointer to handler function for the custom formats.
|
---|
1846 | * @param pvArg Argument to the pfnFormat function.
|
---|
1847 | * @param pszBuffer Output buffer.
|
---|
1848 | * @param cchBuffer Size of the output buffer.
|
---|
1849 | * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
|
---|
1850 | * @param ... The format argument.
|
---|
1851 | */
|
---|
1852 | RTDECL(size_t) RTStrPrintfEx(PFNSTRFORMAT pfnFormat, void *pvArg, char *pszBuffer, size_t cchBuffer, const char *pszFormat, ...);
|
---|
1853 |
|
---|
1854 |
|
---|
1855 | /**
|
---|
1856 | * Allocating string printf (default tag).
|
---|
1857 | *
|
---|
1858 | * @returns The length of the string in the returned *ppszBuffer excluding the
|
---|
1859 | * terminator.
|
---|
1860 | * @returns -1 on failure.
|
---|
1861 | * @param ppszBuffer Where to store the pointer to the allocated output buffer.
|
---|
1862 | * The buffer should be freed using RTStrFree().
|
---|
1863 | * On failure *ppszBuffer will be set to NULL.
|
---|
1864 | * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
|
---|
1865 | * @param args The format argument.
|
---|
1866 | */
|
---|
1867 | #define RTStrAPrintfV(ppszBuffer, pszFormat, args) RTStrAPrintfVTag((ppszBuffer), (pszFormat), (args), RTSTR_TAG)
|
---|
1868 |
|
---|
1869 | /**
|
---|
1870 | * Allocating string printf (custom tag).
|
---|
1871 | *
|
---|
1872 | * @returns The length of the string in the returned *ppszBuffer excluding the
|
---|
1873 | * terminator.
|
---|
1874 | * @returns -1 on failure.
|
---|
1875 | * @param ppszBuffer Where to store the pointer to the allocated output buffer.
|
---|
1876 | * The buffer should be freed using RTStrFree().
|
---|
1877 | * On failure *ppszBuffer will be set to NULL.
|
---|
1878 | * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
|
---|
1879 | * @param args The format argument.
|
---|
1880 | * @param pszTag Allocation tag used for statistics and such.
|
---|
1881 | */
|
---|
1882 | RTDECL(int) RTStrAPrintfVTag(char **ppszBuffer, const char *pszFormat, va_list args, const char *pszTag);
|
---|
1883 |
|
---|
1884 | /**
|
---|
1885 | * Allocating string printf.
|
---|
1886 | *
|
---|
1887 | * @returns The length of the string in the returned *ppszBuffer excluding the
|
---|
1888 | * terminator.
|
---|
1889 | * @returns -1 on failure.
|
---|
1890 | * @param ppszBuffer Where to store the pointer to the allocated output buffer.
|
---|
1891 | * The buffer should be freed using RTStrFree().
|
---|
1892 | * On failure *ppszBuffer will be set to NULL.
|
---|
1893 | * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
|
---|
1894 | * @param ... The format argument.
|
---|
1895 | */
|
---|
1896 | DECLINLINE(int) RTStrAPrintf(char **ppszBuffer, const char *pszFormat, ...)
|
---|
1897 | {
|
---|
1898 | int cbRet;
|
---|
1899 | va_list va;
|
---|
1900 | va_start(va, pszFormat);
|
---|
1901 | cbRet = RTStrAPrintfVTag(ppszBuffer, pszFormat, va, RTSTR_TAG);
|
---|
1902 | va_end(va);
|
---|
1903 | return cbRet;
|
---|
1904 | }
|
---|
1905 |
|
---|
1906 | /**
|
---|
1907 | * Allocating string printf (custom tag).
|
---|
1908 | *
|
---|
1909 | * @returns The length of the string in the returned *ppszBuffer excluding the
|
---|
1910 | * terminator.
|
---|
1911 | * @returns -1 on failure.
|
---|
1912 | * @param ppszBuffer Where to store the pointer to the allocated output buffer.
|
---|
1913 | * The buffer should be freed using RTStrFree().
|
---|
1914 | * On failure *ppszBuffer will be set to NULL.
|
---|
1915 | * @param pszTag Allocation tag used for statistics and such.
|
---|
1916 | * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
|
---|
1917 | * @param ... The format argument.
|
---|
1918 | */
|
---|
1919 | DECLINLINE(int) RTStrAPrintfTag(char **ppszBuffer, const char *pszTag, const char *pszFormat, ...)
|
---|
1920 | {
|
---|
1921 | int cbRet;
|
---|
1922 | va_list va;
|
---|
1923 | va_start(va, pszFormat);
|
---|
1924 | cbRet = RTStrAPrintfVTag(ppszBuffer, pszFormat, va, pszTag);
|
---|
1925 | va_end(va);
|
---|
1926 | return cbRet;
|
---|
1927 | }
|
---|
1928 |
|
---|
1929 | /**
|
---|
1930 | * Allocating string printf, version 2.
|
---|
1931 | *
|
---|
1932 | * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
|
---|
1933 | * memory.
|
---|
1934 | * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
|
---|
1935 | * @param args The format argument.
|
---|
1936 | */
|
---|
1937 | #define RTStrAPrintf2V(pszFormat, args) RTStrAPrintf2VTag((pszFormat), (args), RTSTR_TAG)
|
---|
1938 |
|
---|
1939 | /**
|
---|
1940 | * Allocating string printf, version 2.
|
---|
1941 | *
|
---|
1942 | * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
|
---|
1943 | * memory.
|
---|
1944 | * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
|
---|
1945 | * @param args The format argument.
|
---|
1946 | * @param pszTag Allocation tag used for statistics and such.
|
---|
1947 | */
|
---|
1948 | RTDECL(char *) RTStrAPrintf2VTag(const char *pszFormat, va_list args, const char *pszTag);
|
---|
1949 |
|
---|
1950 | /**
|
---|
1951 | * Allocating string printf, version 2 (default tag).
|
---|
1952 | *
|
---|
1953 | * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
|
---|
1954 | * memory.
|
---|
1955 | * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
|
---|
1956 | * @param ... The format argument.
|
---|
1957 | */
|
---|
1958 | DECLINLINE(char *) RTStrAPrintf2(const char *pszFormat, ...)
|
---|
1959 | {
|
---|
1960 | char *pszRet;
|
---|
1961 | va_list va;
|
---|
1962 | va_start(va, pszFormat);
|
---|
1963 | pszRet = RTStrAPrintf2VTag(pszFormat, va, RTSTR_TAG);
|
---|
1964 | va_end(va);
|
---|
1965 | return pszRet;
|
---|
1966 | }
|
---|
1967 |
|
---|
1968 | /**
|
---|
1969 | * Allocating string printf, version 2 (custom tag).
|
---|
1970 | *
|
---|
1971 | * @returns Formatted string. Use RTStrFree() to free it. NULL when out of
|
---|
1972 | * memory.
|
---|
1973 | * @param pszTag Allocation tag used for statistics and such.
|
---|
1974 | * @param pszFormat Pointer to the format string, @see pg_rt_str_format.
|
---|
1975 | * @param ... The format argument.
|
---|
1976 | */
|
---|
1977 | DECLINLINE(char *) RTStrAPrintf2Tag(const char *pszTag, const char *pszFormat, ...)
|
---|
1978 | {
|
---|
1979 | char *pszRet;
|
---|
1980 | va_list va;
|
---|
1981 | va_start(va, pszFormat);
|
---|
1982 | pszRet = RTStrAPrintf2VTag(pszFormat, va, pszTag);
|
---|
1983 | va_end(va);
|
---|
1984 | return pszRet;
|
---|
1985 | }
|
---|
1986 |
|
---|
1987 | /**
|
---|
1988 | * Strips blankspaces from both ends of the string.
|
---|
1989 | *
|
---|
1990 | * @returns Pointer to first non-blank char in the string.
|
---|
1991 | * @param psz The string to strip.
|
---|
1992 | */
|
---|
1993 | RTDECL(char *) RTStrStrip(char *psz);
|
---|
1994 |
|
---|
1995 | /**
|
---|
1996 | * Strips blankspaces from the start of the string.
|
---|
1997 | *
|
---|
1998 | * @returns Pointer to first non-blank char in the string.
|
---|
1999 | * @param psz The string to strip.
|
---|
2000 | */
|
---|
2001 | RTDECL(char *) RTStrStripL(const char *psz);
|
---|
2002 |
|
---|
2003 | /**
|
---|
2004 | * Strips blankspaces from the end of the string.
|
---|
2005 | *
|
---|
2006 | * @returns psz.
|
---|
2007 | * @param psz The string to strip.
|
---|
2008 | */
|
---|
2009 | RTDECL(char *) RTStrStripR(char *psz);
|
---|
2010 |
|
---|
2011 | /**
|
---|
2012 | * String copy with overflow handling.
|
---|
2013 | *
|
---|
2014 | * @retval VINF_SUCCESS on success.
|
---|
2015 | * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
|
---|
2016 | * buffer will contain as much of the string as it can hold, fully
|
---|
2017 | * terminated.
|
---|
2018 | *
|
---|
2019 | * @param pszDst The destination buffer.
|
---|
2020 | * @param cbDst The size of the destination buffer (in bytes).
|
---|
2021 | * @param pszSrc The source string. NULL is not OK.
|
---|
2022 | */
|
---|
2023 | RTDECL(int) RTStrCopy(char *pszDst, size_t cbDst, const char *pszSrc);
|
---|
2024 |
|
---|
2025 | /**
|
---|
2026 | * String copy with overflow handling.
|
---|
2027 | *
|
---|
2028 | * @retval VINF_SUCCESS on success.
|
---|
2029 | * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
|
---|
2030 | * buffer will contain as much of the string as it can hold, fully
|
---|
2031 | * terminated.
|
---|
2032 | *
|
---|
2033 | * @param pszDst The destination buffer.
|
---|
2034 | * @param cbDst The size of the destination buffer (in bytes).
|
---|
2035 | * @param pszSrc The source string. NULL is not OK.
|
---|
2036 | * @param cchSrcMax The maximum number of chars (not code points) to
|
---|
2037 | * copy from the source string, not counting the
|
---|
2038 | * terminator as usual.
|
---|
2039 | */
|
---|
2040 | RTDECL(int) RTStrCopyEx(char *pszDst, size_t cbDst, const char *pszSrc, size_t cchSrcMax);
|
---|
2041 |
|
---|
2042 | /**
|
---|
2043 | * Performs a case sensitive string compare between two UTF-8 strings.
|
---|
2044 | *
|
---|
2045 | * Encoding errors are ignored by the current implementation. So, the only
|
---|
2046 | * difference between this and the CRT strcmp function is the handling of
|
---|
2047 | * NULL arguments.
|
---|
2048 | *
|
---|
2049 | * @returns < 0 if the first string less than the second string.
|
---|
2050 | * @returns 0 if the first string identical to the second string.
|
---|
2051 | * @returns > 0 if the first string greater than the second string.
|
---|
2052 | * @param psz1 First UTF-8 string. Null is allowed.
|
---|
2053 | * @param psz2 Second UTF-8 string. Null is allowed.
|
---|
2054 | */
|
---|
2055 | RTDECL(int) RTStrCmp(const char *psz1, const char *psz2);
|
---|
2056 |
|
---|
2057 | /**
|
---|
2058 | * Performs a case sensitive string compare between two UTF-8 strings, given
|
---|
2059 | * a maximum string length.
|
---|
2060 | *
|
---|
2061 | * Encoding errors are ignored by the current implementation. So, the only
|
---|
2062 | * difference between this and the CRT strncmp function is the handling of
|
---|
2063 | * NULL arguments.
|
---|
2064 | *
|
---|
2065 | * @returns < 0 if the first string less than the second string.
|
---|
2066 | * @returns 0 if the first string identical to the second string.
|
---|
2067 | * @returns > 0 if the first string greater than the second string.
|
---|
2068 | * @param psz1 First UTF-8 string. Null is allowed.
|
---|
2069 | * @param psz2 Second UTF-8 string. Null is allowed.
|
---|
2070 | * @param cchMax The maximum string length
|
---|
2071 | */
|
---|
2072 | RTDECL(int) RTStrNCmp(const char *psz1, const char *psz2, size_t cchMax);
|
---|
2073 |
|
---|
2074 | /**
|
---|
2075 | * Performs a case insensitive string compare between two UTF-8 strings.
|
---|
2076 | *
|
---|
2077 | * This is a simplified compare, as only the simplified lower/upper case folding
|
---|
2078 | * specified by the unicode specs are used. It does not consider character pairs
|
---|
2079 | * as they are used in some languages, just simple upper & lower case compares.
|
---|
2080 | *
|
---|
2081 | * The result is the difference between the mismatching codepoints after they
|
---|
2082 | * both have been lower cased.
|
---|
2083 | *
|
---|
2084 | * If the string encoding is invalid the function will assert (strict builds)
|
---|
2085 | * and use RTStrCmp for the remainder of the string.
|
---|
2086 | *
|
---|
2087 | * @returns < 0 if the first string less than the second string.
|
---|
2088 | * @returns 0 if the first string identical to the second string.
|
---|
2089 | * @returns > 0 if the first string greater than the second string.
|
---|
2090 | * @param psz1 First UTF-8 string. Null is allowed.
|
---|
2091 | * @param psz2 Second UTF-8 string. Null is allowed.
|
---|
2092 | */
|
---|
2093 | RTDECL(int) RTStrICmp(const char *psz1, const char *psz2);
|
---|
2094 |
|
---|
2095 | /**
|
---|
2096 | * Performs a case insensitive string compare between two UTF-8 strings, given a
|
---|
2097 | * maximum string length.
|
---|
2098 | *
|
---|
2099 | * This is a simplified compare, as only the simplified lower/upper case folding
|
---|
2100 | * specified by the unicode specs are used. It does not consider character pairs
|
---|
2101 | * as they are used in some languages, just simple upper & lower case compares.
|
---|
2102 | *
|
---|
2103 | * The result is the difference between the mismatching codepoints after they
|
---|
2104 | * both have been lower cased.
|
---|
2105 | *
|
---|
2106 | * If the string encoding is invalid the function will assert (strict builds)
|
---|
2107 | * and use RTStrCmp for the remainder of the string.
|
---|
2108 | *
|
---|
2109 | * @returns < 0 if the first string less than the second string.
|
---|
2110 | * @returns 0 if the first string identical to the second string.
|
---|
2111 | * @returns > 0 if the first string greater than the second string.
|
---|
2112 | * @param psz1 First UTF-8 string. Null is allowed.
|
---|
2113 | * @param psz2 Second UTF-8 string. Null is allowed.
|
---|
2114 | * @param cchMax Maximum string length
|
---|
2115 | */
|
---|
2116 | RTDECL(int) RTStrNICmp(const char *psz1, const char *psz2, size_t cchMax);
|
---|
2117 |
|
---|
2118 | /**
|
---|
2119 | * Locates a case sensitive substring.
|
---|
2120 | *
|
---|
2121 | * If any of the two strings are NULL, then NULL is returned. If the needle is
|
---|
2122 | * an empty string, then the haystack is returned (i.e. matches anything).
|
---|
2123 | *
|
---|
2124 | * @returns Pointer to the first occurrence of the substring if found, NULL if
|
---|
2125 | * not.
|
---|
2126 | *
|
---|
2127 | * @param pszHaystack The string to search.
|
---|
2128 | * @param pszNeedle The substring to search for.
|
---|
2129 | *
|
---|
2130 | * @remarks The difference between this and strstr is the handling of NULL
|
---|
2131 | * pointers.
|
---|
2132 | */
|
---|
2133 | RTDECL(char *) RTStrStr(const char *pszHaystack, const char *pszNeedle);
|
---|
2134 |
|
---|
2135 | /**
|
---|
2136 | * Locates a case insensitive substring.
|
---|
2137 | *
|
---|
2138 | * If any of the two strings are NULL, then NULL is returned. If the needle is
|
---|
2139 | * an empty string, then the haystack is returned (i.e. matches anything).
|
---|
2140 | *
|
---|
2141 | * @returns Pointer to the first occurrence of the substring if found, NULL if
|
---|
2142 | * not.
|
---|
2143 | *
|
---|
2144 | * @param pszHaystack The string to search.
|
---|
2145 | * @param pszNeedle The substring to search for.
|
---|
2146 | *
|
---|
2147 | */
|
---|
2148 | RTDECL(char *) RTStrIStr(const char *pszHaystack, const char *pszNeedle);
|
---|
2149 |
|
---|
2150 | /**
|
---|
2151 | * Converts the string to lower case.
|
---|
2152 | *
|
---|
2153 | * @returns Pointer to the converted string.
|
---|
2154 | * @param psz The string to convert.
|
---|
2155 | */
|
---|
2156 | RTDECL(char *) RTStrToLower(char *psz);
|
---|
2157 |
|
---|
2158 | /**
|
---|
2159 | * Converts the string to upper case.
|
---|
2160 | *
|
---|
2161 | * @returns Pointer to the converted string.
|
---|
2162 | * @param psz The string to convert.
|
---|
2163 | */
|
---|
2164 | RTDECL(char *) RTStrToUpper(char *psz);
|
---|
2165 |
|
---|
2166 | /**
|
---|
2167 | * Find the length of a zero-terminated byte string, given
|
---|
2168 | * a max string length.
|
---|
2169 | *
|
---|
2170 | * See also RTStrNLenEx.
|
---|
2171 | *
|
---|
2172 | * @returns The string length or cbMax. The returned length does not include
|
---|
2173 | * the zero terminator if it was found.
|
---|
2174 | *
|
---|
2175 | * @param pszString The string.
|
---|
2176 | * @param cchMax The max string length.
|
---|
2177 | */
|
---|
2178 | RTDECL(size_t) RTStrNLen(const char *pszString, size_t cchMax);
|
---|
2179 |
|
---|
2180 | /**
|
---|
2181 | * Find the length of a zero-terminated byte string, given
|
---|
2182 | * a max string length.
|
---|
2183 | *
|
---|
2184 | * See also RTStrNLen.
|
---|
2185 | *
|
---|
2186 | * @returns IPRT status code.
|
---|
2187 | * @retval VINF_SUCCESS if the string has a length less than cchMax.
|
---|
2188 | * @retval VERR_BUFFER_OVERFLOW if the end of the string wasn't found
|
---|
2189 | * before cchMax was reached.
|
---|
2190 | *
|
---|
2191 | * @param pszString The string.
|
---|
2192 | * @param cchMax The max string length.
|
---|
2193 | * @param pcch Where to store the string length excluding the
|
---|
2194 | * terminator. This is set to cchMax if the terminator
|
---|
2195 | * isn't found.
|
---|
2196 | */
|
---|
2197 | RTDECL(int) RTStrNLenEx(const char *pszString, size_t cchMax, size_t *pcch);
|
---|
2198 |
|
---|
2199 | RT_C_DECLS_END
|
---|
2200 |
|
---|
2201 | /** The maximum size argument of a memchr call. */
|
---|
2202 | #define RTSTR_MEMCHR_MAX (~(size_t)0x10000)
|
---|
2203 |
|
---|
2204 | /**
|
---|
2205 | * Find the zero terminator in a string with a limited length.
|
---|
2206 | *
|
---|
2207 | * @returns Pointer to the zero terminator.
|
---|
2208 | * @returns NULL if the zero terminator was not found.
|
---|
2209 | *
|
---|
2210 | * @param pszString The string.
|
---|
2211 | * @param cchMax The max string length. RTSTR_MAX is fine.
|
---|
2212 | */
|
---|
2213 | #if defined(__cplusplus) && !defined(DOXYGEN_RUNNING)
|
---|
2214 | DECLINLINE(char const *) RTStrEnd(char const *pszString, size_t cchMax)
|
---|
2215 | {
|
---|
2216 | /* Avoid potential issues with memchr seen in glibc. */
|
---|
2217 | if (cchMax > RTSTR_MEMCHR_MAX)
|
---|
2218 | {
|
---|
2219 | char const *pszRet = (char const *)memchr(pszString, '\0', RTSTR_MEMCHR_MAX);
|
---|
2220 | if (RT_LIKELY(pszRet))
|
---|
2221 | return pszRet;
|
---|
2222 | pszString += RTSTR_MEMCHR_MAX;
|
---|
2223 | cchMax -= RTSTR_MEMCHR_MAX;
|
---|
2224 | }
|
---|
2225 | return (char const *)memchr(pszString, '\0', cchMax);
|
---|
2226 | }
|
---|
2227 |
|
---|
2228 | DECLINLINE(char *) RTStrEnd(char *pszString, size_t cchMax)
|
---|
2229 | #else
|
---|
2230 | DECLINLINE(char *) RTStrEnd(const char *pszString, size_t cchMax)
|
---|
2231 | #endif
|
---|
2232 | {
|
---|
2233 | /* Avoid potential issues with memchr seen in glibc. */
|
---|
2234 | if (cchMax > RTSTR_MEMCHR_MAX)
|
---|
2235 | {
|
---|
2236 | char *pszRet = (char *)memchr(pszString, '\0', RTSTR_MEMCHR_MAX);
|
---|
2237 | if (RT_LIKELY(pszRet))
|
---|
2238 | return pszRet;
|
---|
2239 | pszString += RTSTR_MEMCHR_MAX;
|
---|
2240 | cchMax -= RTSTR_MEMCHR_MAX;
|
---|
2241 | }
|
---|
2242 | return (char *)memchr(pszString, '\0', cchMax);
|
---|
2243 | }
|
---|
2244 |
|
---|
2245 | RT_C_DECLS_BEGIN
|
---|
2246 |
|
---|
2247 | /**
|
---|
2248 | * Matches a simple string pattern.
|
---|
2249 | *
|
---|
2250 | * @returns true if the string matches the pattern, otherwise false.
|
---|
2251 | *
|
---|
2252 | * @param pszPattern The pattern. Special chars are '*' and '?', where the
|
---|
2253 | * asterisk matches zero or more characters and question
|
---|
2254 | * mark matches exactly one character.
|
---|
2255 | * @param pszString The string to match against the pattern.
|
---|
2256 | */
|
---|
2257 | RTDECL(bool) RTStrSimplePatternMatch(const char *pszPattern, const char *pszString);
|
---|
2258 |
|
---|
2259 | /**
|
---|
2260 | * Matches a simple string pattern, neither which needs to be zero terminated.
|
---|
2261 | *
|
---|
2262 | * This is identical to RTStrSimplePatternMatch except that you can optionally
|
---|
2263 | * specify the length of both the pattern and the string. The function will
|
---|
2264 | * stop when it hits a string terminator or either of the lengths.
|
---|
2265 | *
|
---|
2266 | * @returns true if the string matches the pattern, otherwise false.
|
---|
2267 | *
|
---|
2268 | * @param pszPattern The pattern. Special chars are '*' and '?', where the
|
---|
2269 | * asterisk matches zero or more characters and question
|
---|
2270 | * mark matches exactly one character.
|
---|
2271 | * @param cchPattern The pattern length. Pass RTSTR_MAX if you don't know the
|
---|
2272 | * length and wish to stop at the string terminator.
|
---|
2273 | * @param pszString The string to match against the pattern.
|
---|
2274 | * @param cchString The string length. Pass RTSTR_MAX if you don't know the
|
---|
2275 | * length and wish to match up to the string terminator.
|
---|
2276 | */
|
---|
2277 | RTDECL(bool) RTStrSimplePatternNMatch(const char *pszPattern, size_t cchPattern,
|
---|
2278 | const char *pszString, size_t cchString);
|
---|
2279 |
|
---|
2280 | /**
|
---|
2281 | * Matches multiple patterns against a string.
|
---|
2282 | *
|
---|
2283 | * The patterns are separated by the pipe character (|).
|
---|
2284 | *
|
---|
2285 | * @returns true if the string matches the pattern, otherwise false.
|
---|
2286 | *
|
---|
2287 | * @param pszPatterns The patterns.
|
---|
2288 | * @param cchPatterns The lengths of the patterns to use. Pass RTSTR_MAX to
|
---|
2289 | * stop at the terminator.
|
---|
2290 | * @param pszString The string to match against the pattern.
|
---|
2291 | * @param cchString The string length. Pass RTSTR_MAX stop stop at the
|
---|
2292 | * terminator.
|
---|
2293 | * @param poffPattern Offset into the patterns string of the patttern that
|
---|
2294 | * matched. If no match, this will be set to RTSTR_MAX.
|
---|
2295 | * This is optional, NULL is fine.
|
---|
2296 | */
|
---|
2297 | RTDECL(bool) RTStrSimplePatternMultiMatch(const char *pszPatterns, size_t cchPatterns,
|
---|
2298 | const char *pszString, size_t cchString,
|
---|
2299 | size_t *poffPattern);
|
---|
2300 |
|
---|
2301 | /**
|
---|
2302 | * Compares two version strings RTStrICmp fashion.
|
---|
2303 | *
|
---|
2304 | * The version string is split up into sections at punctuation, spaces,
|
---|
2305 | * underscores, dashes and pluss signs. The sections are then split up into
|
---|
2306 | * numeric and string sub-sections. Finally, the sub-sections are compared
|
---|
2307 | * in a numeric or case insesntivie fashion depending on what they are.
|
---|
2308 | *
|
---|
2309 | * The following strings are considered to be equal: "1.0.0", "1.00.0", "1.0",
|
---|
2310 | * "1". These aren't: "1.0.0r993", "1.0", "1.0r993", "1.0_Beta3", "1.1"
|
---|
2311 | *
|
---|
2312 | * @returns < 0 if the first string less than the second string.
|
---|
2313 | * @returns 0 if the first string identical to the second string.
|
---|
2314 | * @returns > 0 if the first string greater than the second string.
|
---|
2315 | *
|
---|
2316 | * @param pszVer1 First version string to compare.
|
---|
2317 | * @param pszVer2 Second version string to compare first version with.
|
---|
2318 | */
|
---|
2319 | RTDECL(int) RTStrVersionCompare(const char *pszVer1, const char *pszVer2);
|
---|
2320 |
|
---|
2321 |
|
---|
2322 | /** @defgroup rt_str_conv String To/From Number Conversions
|
---|
2323 | * @ingroup grp_rt_str
|
---|
2324 | * @{ */
|
---|
2325 |
|
---|
2326 | /**
|
---|
2327 | * Converts a string representation of a number to a 64-bit unsigned number.
|
---|
2328 | *
|
---|
2329 | * @returns iprt status code.
|
---|
2330 | * Warnings are used to indicate conversion problems.
|
---|
2331 | * @retval VWRN_NUMBER_TOO_BIG
|
---|
2332 | * @retval VWRN_NEGATIVE_UNSIGNED
|
---|
2333 | * @retval VWRN_TRAILING_CHARS
|
---|
2334 | * @retval VWRN_TRAILING_SPACES
|
---|
2335 | * @retval VINF_SUCCESS
|
---|
2336 | * @retval VERR_NO_DIGITS
|
---|
2337 | *
|
---|
2338 | * @param pszValue Pointer to the string value.
|
---|
2339 | * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
|
---|
2340 | * @param uBase The base of the representation used.
|
---|
2341 | * If 0 the function will look for known prefixes before defaulting to 10.
|
---|
2342 | * @param pu64 Where to store the converted number. (optional)
|
---|
2343 | */
|
---|
2344 | RTDECL(int) RTStrToUInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint64_t *pu64);
|
---|
2345 |
|
---|
2346 | /**
|
---|
2347 | * Converts a string representation of a number to a 64-bit unsigned number,
|
---|
2348 | * making sure the full string is converted.
|
---|
2349 | *
|
---|
2350 | * @returns iprt status code.
|
---|
2351 | * Warnings are used to indicate conversion problems.
|
---|
2352 | * @retval VWRN_NUMBER_TOO_BIG
|
---|
2353 | * @retval VWRN_NEGATIVE_UNSIGNED
|
---|
2354 | * @retval VINF_SUCCESS
|
---|
2355 | * @retval VERR_NO_DIGITS
|
---|
2356 | * @retval VERR_TRAILING_SPACES
|
---|
2357 | * @retval VERR_TRAILING_CHARS
|
---|
2358 | *
|
---|
2359 | * @param pszValue Pointer to the string value.
|
---|
2360 | * @param uBase The base of the representation used.
|
---|
2361 | * If 0 the function will look for known prefixes before defaulting to 10.
|
---|
2362 | * @param pu64 Where to store the converted number. (optional)
|
---|
2363 | */
|
---|
2364 | RTDECL(int) RTStrToUInt64Full(const char *pszValue, unsigned uBase, uint64_t *pu64);
|
---|
2365 |
|
---|
2366 | /**
|
---|
2367 | * Converts a string representation of a number to a 64-bit unsigned number.
|
---|
2368 | * The base is guessed.
|
---|
2369 | *
|
---|
2370 | * @returns 64-bit unsigned number on success.
|
---|
2371 | * @returns 0 on failure.
|
---|
2372 | * @param pszValue Pointer to the string value.
|
---|
2373 | */
|
---|
2374 | RTDECL(uint64_t) RTStrToUInt64(const char *pszValue);
|
---|
2375 |
|
---|
2376 | /**
|
---|
2377 | * Converts a string representation of a number to a 32-bit unsigned number.
|
---|
2378 | *
|
---|
2379 | * @returns iprt status code.
|
---|
2380 | * Warnings are used to indicate conversion problems.
|
---|
2381 | * @retval VWRN_NUMBER_TOO_BIG
|
---|
2382 | * @retval VWRN_NEGATIVE_UNSIGNED
|
---|
2383 | * @retval VWRN_TRAILING_CHARS
|
---|
2384 | * @retval VWRN_TRAILING_SPACES
|
---|
2385 | * @retval VINF_SUCCESS
|
---|
2386 | * @retval VERR_NO_DIGITS
|
---|
2387 | *
|
---|
2388 | * @param pszValue Pointer to the string value.
|
---|
2389 | * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
|
---|
2390 | * @param uBase The base of the representation used.
|
---|
2391 | * If 0 the function will look for known prefixes before defaulting to 10.
|
---|
2392 | * @param pu32 Where to store the converted number. (optional)
|
---|
2393 | */
|
---|
2394 | RTDECL(int) RTStrToUInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint32_t *pu32);
|
---|
2395 |
|
---|
2396 | /**
|
---|
2397 | * Converts a string representation of a number to a 32-bit unsigned number,
|
---|
2398 | * making sure the full string is converted.
|
---|
2399 | *
|
---|
2400 | * @returns iprt status code.
|
---|
2401 | * Warnings are used to indicate conversion problems.
|
---|
2402 | * @retval VWRN_NUMBER_TOO_BIG
|
---|
2403 | * @retval VWRN_NEGATIVE_UNSIGNED
|
---|
2404 | * @retval VINF_SUCCESS
|
---|
2405 | * @retval VERR_NO_DIGITS
|
---|
2406 | * @retval VERR_TRAILING_SPACES
|
---|
2407 | * @retval VERR_TRAILING_CHARS
|
---|
2408 | *
|
---|
2409 | * @param pszValue Pointer to the string value.
|
---|
2410 | * @param uBase The base of the representation used.
|
---|
2411 | * If 0 the function will look for known prefixes before defaulting to 10.
|
---|
2412 | * @param pu32 Where to store the converted number. (optional)
|
---|
2413 | */
|
---|
2414 | RTDECL(int) RTStrToUInt32Full(const char *pszValue, unsigned uBase, uint32_t *pu32);
|
---|
2415 |
|
---|
2416 | /**
|
---|
2417 | * Converts a string representation of a number to a 64-bit unsigned number.
|
---|
2418 | * The base is guessed.
|
---|
2419 | *
|
---|
2420 | * @returns 32-bit unsigned number on success.
|
---|
2421 | * @returns 0 on failure.
|
---|
2422 | * @param pszValue Pointer to the string value.
|
---|
2423 | */
|
---|
2424 | RTDECL(uint32_t) RTStrToUInt32(const char *pszValue);
|
---|
2425 |
|
---|
2426 | /**
|
---|
2427 | * Converts a string representation of a number to a 16-bit unsigned number.
|
---|
2428 | *
|
---|
2429 | * @returns iprt status code.
|
---|
2430 | * Warnings are used to indicate conversion problems.
|
---|
2431 | * @retval VWRN_NUMBER_TOO_BIG
|
---|
2432 | * @retval VWRN_NEGATIVE_UNSIGNED
|
---|
2433 | * @retval VWRN_TRAILING_CHARS
|
---|
2434 | * @retval VWRN_TRAILING_SPACES
|
---|
2435 | * @retval VINF_SUCCESS
|
---|
2436 | * @retval VERR_NO_DIGITS
|
---|
2437 | *
|
---|
2438 | * @param pszValue Pointer to the string value.
|
---|
2439 | * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
|
---|
2440 | * @param uBase The base of the representation used.
|
---|
2441 | * If 0 the function will look for known prefixes before defaulting to 10.
|
---|
2442 | * @param pu16 Where to store the converted number. (optional)
|
---|
2443 | */
|
---|
2444 | RTDECL(int) RTStrToUInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint16_t *pu16);
|
---|
2445 |
|
---|
2446 | /**
|
---|
2447 | * Converts a string representation of a number to a 16-bit unsigned number,
|
---|
2448 | * making sure the full string is converted.
|
---|
2449 | *
|
---|
2450 | * @returns iprt status code.
|
---|
2451 | * Warnings are used to indicate conversion problems.
|
---|
2452 | * @retval VWRN_NUMBER_TOO_BIG
|
---|
2453 | * @retval VWRN_NEGATIVE_UNSIGNED
|
---|
2454 | * @retval VINF_SUCCESS
|
---|
2455 | * @retval VERR_NO_DIGITS
|
---|
2456 | * @retval VERR_TRAILING_SPACES
|
---|
2457 | * @retval VERR_TRAILING_CHARS
|
---|
2458 | *
|
---|
2459 | * @param pszValue Pointer to the string value.
|
---|
2460 | * @param uBase The base of the representation used.
|
---|
2461 | * If 0 the function will look for known prefixes before defaulting to 10.
|
---|
2462 | * @param pu16 Where to store the converted number. (optional)
|
---|
2463 | */
|
---|
2464 | RTDECL(int) RTStrToUInt16Full(const char *pszValue, unsigned uBase, uint16_t *pu16);
|
---|
2465 |
|
---|
2466 | /**
|
---|
2467 | * Converts a string representation of a number to a 16-bit unsigned number.
|
---|
2468 | * The base is guessed.
|
---|
2469 | *
|
---|
2470 | * @returns 16-bit unsigned number on success.
|
---|
2471 | * @returns 0 on failure.
|
---|
2472 | * @param pszValue Pointer to the string value.
|
---|
2473 | */
|
---|
2474 | RTDECL(uint16_t) RTStrToUInt16(const char *pszValue);
|
---|
2475 |
|
---|
2476 | /**
|
---|
2477 | * Converts a string representation of a number to a 8-bit unsigned number.
|
---|
2478 | *
|
---|
2479 | * @returns iprt status code.
|
---|
2480 | * Warnings are used to indicate conversion problems.
|
---|
2481 | * @retval VWRN_NUMBER_TOO_BIG
|
---|
2482 | * @retval VWRN_NEGATIVE_UNSIGNED
|
---|
2483 | * @retval VWRN_TRAILING_CHARS
|
---|
2484 | * @retval VWRN_TRAILING_SPACES
|
---|
2485 | * @retval VINF_SUCCESS
|
---|
2486 | * @retval VERR_NO_DIGITS
|
---|
2487 | *
|
---|
2488 | * @param pszValue Pointer to the string value.
|
---|
2489 | * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
|
---|
2490 | * @param uBase The base of the representation used.
|
---|
2491 | * If 0 the function will look for known prefixes before defaulting to 10.
|
---|
2492 | * @param pu8 Where to store the converted number. (optional)
|
---|
2493 | */
|
---|
2494 | RTDECL(int) RTStrToUInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, uint8_t *pu8);
|
---|
2495 |
|
---|
2496 | /**
|
---|
2497 | * Converts a string representation of a number to a 8-bit unsigned number,
|
---|
2498 | * making sure the full string is converted.
|
---|
2499 | *
|
---|
2500 | * @returns iprt status code.
|
---|
2501 | * Warnings are used to indicate conversion problems.
|
---|
2502 | * @retval VWRN_NUMBER_TOO_BIG
|
---|
2503 | * @retval VWRN_NEGATIVE_UNSIGNED
|
---|
2504 | * @retval VINF_SUCCESS
|
---|
2505 | * @retval VERR_NO_DIGITS
|
---|
2506 | * @retval VERR_TRAILING_SPACES
|
---|
2507 | * @retval VERR_TRAILING_CHARS
|
---|
2508 | *
|
---|
2509 | * @param pszValue Pointer to the string value.
|
---|
2510 | * @param uBase The base of the representation used.
|
---|
2511 | * If 0 the function will look for known prefixes before defaulting to 10.
|
---|
2512 | * @param pu8 Where to store the converted number. (optional)
|
---|
2513 | */
|
---|
2514 | RTDECL(int) RTStrToUInt8Full(const char *pszValue, unsigned uBase, uint8_t *pu8);
|
---|
2515 |
|
---|
2516 | /**
|
---|
2517 | * Converts a string representation of a number to a 8-bit unsigned number.
|
---|
2518 | * The base is guessed.
|
---|
2519 | *
|
---|
2520 | * @returns 8-bit unsigned number on success.
|
---|
2521 | * @returns 0 on failure.
|
---|
2522 | * @param pszValue Pointer to the string value.
|
---|
2523 | */
|
---|
2524 | RTDECL(uint8_t) RTStrToUInt8(const char *pszValue);
|
---|
2525 |
|
---|
2526 | /**
|
---|
2527 | * Converts a string representation of a number to a 64-bit signed number.
|
---|
2528 | *
|
---|
2529 | * @returns iprt status code.
|
---|
2530 | * Warnings are used to indicate conversion problems.
|
---|
2531 | * @retval VWRN_NUMBER_TOO_BIG
|
---|
2532 | * @retval VWRN_TRAILING_CHARS
|
---|
2533 | * @retval VWRN_TRAILING_SPACES
|
---|
2534 | * @retval VINF_SUCCESS
|
---|
2535 | * @retval VERR_NO_DIGITS
|
---|
2536 | *
|
---|
2537 | * @param pszValue Pointer to the string value.
|
---|
2538 | * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
|
---|
2539 | * @param uBase The base of the representation used.
|
---|
2540 | * If 0 the function will look for known prefixes before defaulting to 10.
|
---|
2541 | * @param pi64 Where to store the converted number. (optional)
|
---|
2542 | */
|
---|
2543 | RTDECL(int) RTStrToInt64Ex(const char *pszValue, char **ppszNext, unsigned uBase, int64_t *pi64);
|
---|
2544 |
|
---|
2545 | /**
|
---|
2546 | * Converts a string representation of a number to a 64-bit signed number,
|
---|
2547 | * making sure the full string is converted.
|
---|
2548 | *
|
---|
2549 | * @returns iprt status code.
|
---|
2550 | * Warnings are used to indicate conversion problems.
|
---|
2551 | * @retval VWRN_NUMBER_TOO_BIG
|
---|
2552 | * @retval VINF_SUCCESS
|
---|
2553 | * @retval VERR_TRAILING_CHARS
|
---|
2554 | * @retval VERR_TRAILING_SPACES
|
---|
2555 | * @retval VERR_NO_DIGITS
|
---|
2556 | *
|
---|
2557 | * @param pszValue Pointer to the string value.
|
---|
2558 | * @param uBase The base of the representation used.
|
---|
2559 | * If 0 the function will look for known prefixes before defaulting to 10.
|
---|
2560 | * @param pi64 Where to store the converted number. (optional)
|
---|
2561 | */
|
---|
2562 | RTDECL(int) RTStrToInt64Full(const char *pszValue, unsigned uBase, int64_t *pi64);
|
---|
2563 |
|
---|
2564 | /**
|
---|
2565 | * Converts a string representation of a number to a 64-bit signed number.
|
---|
2566 | * The base is guessed.
|
---|
2567 | *
|
---|
2568 | * @returns 64-bit signed number on success.
|
---|
2569 | * @returns 0 on failure.
|
---|
2570 | * @param pszValue Pointer to the string value.
|
---|
2571 | */
|
---|
2572 | RTDECL(int64_t) RTStrToInt64(const char *pszValue);
|
---|
2573 |
|
---|
2574 | /**
|
---|
2575 | * Converts a string representation of a number to a 32-bit signed number.
|
---|
2576 | *
|
---|
2577 | * @returns iprt status code.
|
---|
2578 | * Warnings are used to indicate conversion problems.
|
---|
2579 | * @retval VWRN_NUMBER_TOO_BIG
|
---|
2580 | * @retval VWRN_TRAILING_CHARS
|
---|
2581 | * @retval VWRN_TRAILING_SPACES
|
---|
2582 | * @retval VINF_SUCCESS
|
---|
2583 | * @retval VERR_NO_DIGITS
|
---|
2584 | *
|
---|
2585 | * @param pszValue Pointer to the string value.
|
---|
2586 | * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
|
---|
2587 | * @param uBase The base of the representation used.
|
---|
2588 | * If 0 the function will look for known prefixes before defaulting to 10.
|
---|
2589 | * @param pi32 Where to store the converted number. (optional)
|
---|
2590 | */
|
---|
2591 | RTDECL(int) RTStrToInt32Ex(const char *pszValue, char **ppszNext, unsigned uBase, int32_t *pi32);
|
---|
2592 |
|
---|
2593 | /**
|
---|
2594 | * Converts a string representation of a number to a 32-bit signed number,
|
---|
2595 | * making sure the full string is converted.
|
---|
2596 | *
|
---|
2597 | * @returns iprt status code.
|
---|
2598 | * Warnings are used to indicate conversion problems.
|
---|
2599 | * @retval VWRN_NUMBER_TOO_BIG
|
---|
2600 | * @retval VINF_SUCCESS
|
---|
2601 | * @retval VERR_TRAILING_CHARS
|
---|
2602 | * @retval VERR_TRAILING_SPACES
|
---|
2603 | * @retval VERR_NO_DIGITS
|
---|
2604 | *
|
---|
2605 | * @param pszValue Pointer to the string value.
|
---|
2606 | * @param uBase The base of the representation used.
|
---|
2607 | * If 0 the function will look for known prefixes before defaulting to 10.
|
---|
2608 | * @param pi32 Where to store the converted number. (optional)
|
---|
2609 | */
|
---|
2610 | RTDECL(int) RTStrToInt32Full(const char *pszValue, unsigned uBase, int32_t *pi32);
|
---|
2611 |
|
---|
2612 | /**
|
---|
2613 | * Converts a string representation of a number to a 32-bit signed number.
|
---|
2614 | * The base is guessed.
|
---|
2615 | *
|
---|
2616 | * @returns 32-bit signed number on success.
|
---|
2617 | * @returns 0 on failure.
|
---|
2618 | * @param pszValue Pointer to the string value.
|
---|
2619 | */
|
---|
2620 | RTDECL(int32_t) RTStrToInt32(const char *pszValue);
|
---|
2621 |
|
---|
2622 | /**
|
---|
2623 | * Converts a string representation of a number to a 16-bit signed number.
|
---|
2624 | *
|
---|
2625 | * @returns iprt status code.
|
---|
2626 | * Warnings are used to indicate conversion problems.
|
---|
2627 | * @retval VWRN_NUMBER_TOO_BIG
|
---|
2628 | * @retval VWRN_TRAILING_CHARS
|
---|
2629 | * @retval VWRN_TRAILING_SPACES
|
---|
2630 | * @retval VINF_SUCCESS
|
---|
2631 | * @retval VERR_NO_DIGITS
|
---|
2632 | *
|
---|
2633 | * @param pszValue Pointer to the string value.
|
---|
2634 | * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
|
---|
2635 | * @param uBase The base of the representation used.
|
---|
2636 | * If 0 the function will look for known prefixes before defaulting to 10.
|
---|
2637 | * @param pi16 Where to store the converted number. (optional)
|
---|
2638 | */
|
---|
2639 | RTDECL(int) RTStrToInt16Ex(const char *pszValue, char **ppszNext, unsigned uBase, int16_t *pi16);
|
---|
2640 |
|
---|
2641 | /**
|
---|
2642 | * Converts a string representation of a number to a 16-bit signed number,
|
---|
2643 | * making sure the full string is converted.
|
---|
2644 | *
|
---|
2645 | * @returns iprt status code.
|
---|
2646 | * Warnings are used to indicate conversion problems.
|
---|
2647 | * @retval VWRN_NUMBER_TOO_BIG
|
---|
2648 | * @retval VINF_SUCCESS
|
---|
2649 | * @retval VERR_TRAILING_CHARS
|
---|
2650 | * @retval VERR_TRAILING_SPACES
|
---|
2651 | * @retval VERR_NO_DIGITS
|
---|
2652 | *
|
---|
2653 | * @param pszValue Pointer to the string value.
|
---|
2654 | * @param uBase The base of the representation used.
|
---|
2655 | * If 0 the function will look for known prefixes before defaulting to 10.
|
---|
2656 | * @param pi16 Where to store the converted number. (optional)
|
---|
2657 | */
|
---|
2658 | RTDECL(int) RTStrToInt16Full(const char *pszValue, unsigned uBase, int16_t *pi16);
|
---|
2659 |
|
---|
2660 | /**
|
---|
2661 | * Converts a string representation of a number to a 16-bit signed number.
|
---|
2662 | * The base is guessed.
|
---|
2663 | *
|
---|
2664 | * @returns 16-bit signed number on success.
|
---|
2665 | * @returns 0 on failure.
|
---|
2666 | * @param pszValue Pointer to the string value.
|
---|
2667 | */
|
---|
2668 | RTDECL(int16_t) RTStrToInt16(const char *pszValue);
|
---|
2669 |
|
---|
2670 | /**
|
---|
2671 | * Converts a string representation of a number to a 8-bit signed number.
|
---|
2672 | *
|
---|
2673 | * @returns iprt status code.
|
---|
2674 | * Warnings are used to indicate conversion problems.
|
---|
2675 | * @retval VWRN_NUMBER_TOO_BIG
|
---|
2676 | * @retval VWRN_TRAILING_CHARS
|
---|
2677 | * @retval VWRN_TRAILING_SPACES
|
---|
2678 | * @retval VINF_SUCCESS
|
---|
2679 | * @retval VERR_NO_DIGITS
|
---|
2680 | *
|
---|
2681 | * @param pszValue Pointer to the string value.
|
---|
2682 | * @param ppszNext Where to store the pointer to the first char following the number. (Optional)
|
---|
2683 | * @param uBase The base of the representation used.
|
---|
2684 | * If 0 the function will look for known prefixes before defaulting to 10.
|
---|
2685 | * @param pi8 Where to store the converted number. (optional)
|
---|
2686 | */
|
---|
2687 | RTDECL(int) RTStrToInt8Ex(const char *pszValue, char **ppszNext, unsigned uBase, int8_t *pi8);
|
---|
2688 |
|
---|
2689 | /**
|
---|
2690 | * Converts a string representation of a number to a 8-bit signed number,
|
---|
2691 | * making sure the full string is converted.
|
---|
2692 | *
|
---|
2693 | * @returns iprt status code.
|
---|
2694 | * Warnings are used to indicate conversion problems.
|
---|
2695 | * @retval VWRN_NUMBER_TOO_BIG
|
---|
2696 | * @retval VINF_SUCCESS
|
---|
2697 | * @retval VERR_TRAILING_CHARS
|
---|
2698 | * @retval VERR_TRAILING_SPACES
|
---|
2699 | * @retval VERR_NO_DIGITS
|
---|
2700 | *
|
---|
2701 | * @param pszValue Pointer to the string value.
|
---|
2702 | * @param uBase The base of the representation used.
|
---|
2703 | * If 0 the function will look for known prefixes before defaulting to 10.
|
---|
2704 | * @param pi8 Where to store the converted number. (optional)
|
---|
2705 | */
|
---|
2706 | RTDECL(int) RTStrToInt8Full(const char *pszValue, unsigned uBase, int8_t *pi8);
|
---|
2707 |
|
---|
2708 | /**
|
---|
2709 | * Converts a string representation of a number to a 8-bit signed number.
|
---|
2710 | * The base is guessed.
|
---|
2711 | *
|
---|
2712 | * @returns 8-bit signed number on success.
|
---|
2713 | * @returns 0 on failure.
|
---|
2714 | * @param pszValue Pointer to the string value.
|
---|
2715 | */
|
---|
2716 | RTDECL(int8_t) RTStrToInt8(const char *pszValue);
|
---|
2717 |
|
---|
2718 | /**
|
---|
2719 | * Formats a buffer stream as hex bytes.
|
---|
2720 | *
|
---|
2721 | * The default is no separating spaces or line breaks or anything.
|
---|
2722 | *
|
---|
2723 | * @returns IPRT status code.
|
---|
2724 | * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
|
---|
2725 | * @retval VERR_BUFFER_OVERFLOW if the buffer is insufficent to hold the bytes.
|
---|
2726 | *
|
---|
2727 | * @param pszBuf Output string buffer.
|
---|
2728 | * @param cchBuf The size of the output buffer.
|
---|
2729 | * @param pv Pointer to the bytes to stringify.
|
---|
2730 | * @param cb The number of bytes to stringify.
|
---|
2731 | * @param fFlags Must be zero, reserved for future use.
|
---|
2732 | */
|
---|
2733 | RTDECL(int) RTStrPrintHexBytes(char *pszBuf, size_t cchBuf, void const *pv, size_t cb, uint32_t fFlags);
|
---|
2734 |
|
---|
2735 | /**
|
---|
2736 | * Converts a string of hex bytes back into binary data.
|
---|
2737 | *
|
---|
2738 | * @returns IPRT status code.
|
---|
2739 | * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
|
---|
2740 | * @retval VERR_BUFFER_OVERFLOW if the string contains too many hex bytes.
|
---|
2741 | * @retval VERR_BUFFER_UNDERFLOW if there aren't enough hex bytes to fill up
|
---|
2742 | * the output buffer.
|
---|
2743 | * @retval VERR_UNEVEN_INPUT if the input contains a half byte.
|
---|
2744 | * @retval VERR_NO_DIGITS
|
---|
2745 | * @retval VWRN_TRAILING_CHARS
|
---|
2746 | * @retval VWRN_TRAILING_SPACES
|
---|
2747 | *
|
---|
2748 | * @param pszHex The string containing the hex bytes.
|
---|
2749 | * @param pv Output buffer.
|
---|
2750 | * @param cb The size of the output buffer.
|
---|
2751 | * @param fFlags Must be zero, reserved for future use.
|
---|
2752 | */
|
---|
2753 | RTDECL(int) RTStrConvertHexBytes(char const *pszHex, void *pv, size_t cb, uint32_t fFlags);
|
---|
2754 |
|
---|
2755 | /** @} */
|
---|
2756 |
|
---|
2757 |
|
---|
2758 | /** @defgroup rt_str_space Unique String Space
|
---|
2759 | * @ingroup grp_rt_str
|
---|
2760 | * @{
|
---|
2761 | */
|
---|
2762 |
|
---|
2763 | /** Pointer to a string name space container node core. */
|
---|
2764 | typedef struct RTSTRSPACECORE *PRTSTRSPACECORE;
|
---|
2765 | /** Pointer to a pointer to a string name space container node core. */
|
---|
2766 | typedef PRTSTRSPACECORE *PPRTSTRSPACECORE;
|
---|
2767 |
|
---|
2768 | /**
|
---|
2769 | * String name space container node core.
|
---|
2770 | */
|
---|
2771 | typedef struct RTSTRSPACECORE
|
---|
2772 | {
|
---|
2773 | /** Hash key. Don't touch. */
|
---|
2774 | uint32_t Key;
|
---|
2775 | /** Pointer to the left leaf node. Don't touch. */
|
---|
2776 | PRTSTRSPACECORE pLeft;
|
---|
2777 | /** Pointer to the left rigth node. Don't touch. */
|
---|
2778 | PRTSTRSPACECORE pRight;
|
---|
2779 | /** Pointer to the list of string with the same key. Don't touch. */
|
---|
2780 | PRTSTRSPACECORE pList;
|
---|
2781 | /** Height of this tree: max(heigth(left), heigth(right)) + 1. Don't touch */
|
---|
2782 | unsigned char uchHeight;
|
---|
2783 | /** The string length. Read only! */
|
---|
2784 | size_t cchString;
|
---|
2785 | /** Pointer to the string. Read only! */
|
---|
2786 | const char *pszString;
|
---|
2787 | } RTSTRSPACECORE;
|
---|
2788 |
|
---|
2789 | /** String space. (Initialize with NULL.) */
|
---|
2790 | typedef PRTSTRSPACECORE RTSTRSPACE;
|
---|
2791 | /** Pointer to a string space. */
|
---|
2792 | typedef PPRTSTRSPACECORE PRTSTRSPACE;
|
---|
2793 |
|
---|
2794 |
|
---|
2795 | /**
|
---|
2796 | * Inserts a string into a unique string space.
|
---|
2797 | *
|
---|
2798 | * @returns true on success.
|
---|
2799 | * @returns false if the string collided with an existing string.
|
---|
2800 | * @param pStrSpace The space to insert it into.
|
---|
2801 | * @param pStr The string node.
|
---|
2802 | */
|
---|
2803 | RTDECL(bool) RTStrSpaceInsert(PRTSTRSPACE pStrSpace, PRTSTRSPACECORE pStr);
|
---|
2804 |
|
---|
2805 | /**
|
---|
2806 | * Removes a string from a unique string space.
|
---|
2807 | *
|
---|
2808 | * @returns Pointer to the removed string node.
|
---|
2809 | * @returns NULL if the string was not found in the string space.
|
---|
2810 | * @param pStrSpace The space to insert it into.
|
---|
2811 | * @param pszString The string to remove.
|
---|
2812 | */
|
---|
2813 | RTDECL(PRTSTRSPACECORE) RTStrSpaceRemove(PRTSTRSPACE pStrSpace, const char *pszString);
|
---|
2814 |
|
---|
2815 | /**
|
---|
2816 | * Gets a string from a unique string space.
|
---|
2817 | *
|
---|
2818 | * @returns Pointer to the string node.
|
---|
2819 | * @returns NULL if the string was not found in the string space.
|
---|
2820 | * @param pStrSpace The space to insert it into.
|
---|
2821 | * @param pszString The string to get.
|
---|
2822 | */
|
---|
2823 | RTDECL(PRTSTRSPACECORE) RTStrSpaceGet(PRTSTRSPACE pStrSpace, const char *pszString);
|
---|
2824 |
|
---|
2825 | /**
|
---|
2826 | * Callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy().
|
---|
2827 | *
|
---|
2828 | * @returns 0 on continue.
|
---|
2829 | * @returns Non-zero to aborts the operation.
|
---|
2830 | * @param pStr The string node
|
---|
2831 | * @param pvUser The user specified argument.
|
---|
2832 | */
|
---|
2833 | typedef DECLCALLBACK(int) FNRTSTRSPACECALLBACK(PRTSTRSPACECORE pStr, void *pvUser);
|
---|
2834 | /** Pointer to callback function for RTStrSpaceEnumerate() and RTStrSpaceDestroy(). */
|
---|
2835 | typedef FNRTSTRSPACECALLBACK *PFNRTSTRSPACECALLBACK;
|
---|
2836 |
|
---|
2837 | /**
|
---|
2838 | * Destroys the string space.
|
---|
2839 | * The caller supplies a callback which will be called for each of
|
---|
2840 | * the string nodes in for freeing their memory and other resources.
|
---|
2841 | *
|
---|
2842 | * @returns 0 or what ever non-zero return value pfnCallback returned
|
---|
2843 | * when aborting the destruction.
|
---|
2844 | * @param pStrSpace The space to insert it into.
|
---|
2845 | * @param pfnCallback The callback.
|
---|
2846 | * @param pvUser The user argument.
|
---|
2847 | */
|
---|
2848 | RTDECL(int) RTStrSpaceDestroy(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
|
---|
2849 |
|
---|
2850 | /**
|
---|
2851 | * Enumerates the string space.
|
---|
2852 | * The caller supplies a callback which will be called for each of
|
---|
2853 | * the string nodes.
|
---|
2854 | *
|
---|
2855 | * @returns 0 or what ever non-zero return value pfnCallback returned
|
---|
2856 | * when aborting the destruction.
|
---|
2857 | * @param pStrSpace The space to insert it into.
|
---|
2858 | * @param pfnCallback The callback.
|
---|
2859 | * @param pvUser The user argument.
|
---|
2860 | */
|
---|
2861 | RTDECL(int) RTStrSpaceEnumerate(PRTSTRSPACE pStrSpace, PFNRTSTRSPACECALLBACK pfnCallback, void *pvUser);
|
---|
2862 |
|
---|
2863 | /** @} */
|
---|
2864 |
|
---|
2865 |
|
---|
2866 | /** @defgroup rt_str_utf16 UTF-16 String Manipulation
|
---|
2867 | * @ingroup grp_rt_str
|
---|
2868 | * @{
|
---|
2869 | */
|
---|
2870 |
|
---|
2871 | /**
|
---|
2872 | * Free a UTF-16 string allocated by RTStrToUtf16(), RTStrToUtf16Ex(),
|
---|
2873 | * RTLatin1ToUtf16(), RTLatin1ToUtf16Ex(), RTUtf16Dup() or RTUtf16DupEx().
|
---|
2874 | *
|
---|
2875 | * @returns iprt status code.
|
---|
2876 | * @param pwszString The UTF-16 string to free. NULL is accepted.
|
---|
2877 | */
|
---|
2878 | RTDECL(void) RTUtf16Free(PRTUTF16 pwszString);
|
---|
2879 |
|
---|
2880 | /**
|
---|
2881 | * Allocates a new copy of the specified UTF-16 string (default tag).
|
---|
2882 | *
|
---|
2883 | * @returns Pointer to the allocated string copy. Use RTUtf16Free() to free it.
|
---|
2884 | * @returns NULL when out of memory.
|
---|
2885 | * @param pwszString UTF-16 string to duplicate.
|
---|
2886 | * @remark This function will not make any attempt to validate the encoding.
|
---|
2887 | */
|
---|
2888 | #define RTUtf16Dup(pwszString) RTUtf16DupTag((pwszString), RTSTR_TAG)
|
---|
2889 |
|
---|
2890 | /**
|
---|
2891 | * Allocates a new copy of the specified UTF-16 string (custom tag).
|
---|
2892 | *
|
---|
2893 | * @returns Pointer to the allocated string copy. Use RTUtf16Free() to free it.
|
---|
2894 | * @returns NULL when out of memory.
|
---|
2895 | * @param pwszString UTF-16 string to duplicate.
|
---|
2896 | * @param pszTag Allocation tag used for statistics and such.
|
---|
2897 | * @remark This function will not make any attempt to validate the encoding.
|
---|
2898 | */
|
---|
2899 | RTDECL(PRTUTF16) RTUtf16DupTag(PCRTUTF16 pwszString, const char *pszTag);
|
---|
2900 |
|
---|
2901 | /**
|
---|
2902 | * Allocates a new copy of the specified UTF-16 string (default tag).
|
---|
2903 | *
|
---|
2904 | * @returns iprt status code.
|
---|
2905 | * @param ppwszString Receives pointer of the allocated UTF-16 string.
|
---|
2906 | * The returned pointer must be freed using RTUtf16Free().
|
---|
2907 | * @param pwszString UTF-16 string to duplicate.
|
---|
2908 | * @param cwcExtra Number of extra RTUTF16 items to allocate.
|
---|
2909 | * @remark This function will not make any attempt to validate the encoding.
|
---|
2910 | */
|
---|
2911 | #define RTUtf16DupEx(ppwszString, pwszString, cwcExtra) \
|
---|
2912 | RTUtf16DupExTag((ppwszString), (pwszString), (cwcExtra), RTSTR_TAG)
|
---|
2913 |
|
---|
2914 | /**
|
---|
2915 | * Allocates a new copy of the specified UTF-16 string (custom tag).
|
---|
2916 | *
|
---|
2917 | * @returns iprt status code.
|
---|
2918 | * @param ppwszString Receives pointer of the allocated UTF-16 string.
|
---|
2919 | * The returned pointer must be freed using RTUtf16Free().
|
---|
2920 | * @param pwszString UTF-16 string to duplicate.
|
---|
2921 | * @param cwcExtra Number of extra RTUTF16 items to allocate.
|
---|
2922 | * @param pszTag Allocation tag used for statistics and such.
|
---|
2923 | * @remark This function will not make any attempt to validate the encoding.
|
---|
2924 | */
|
---|
2925 | RTDECL(int) RTUtf16DupExTag(PRTUTF16 *ppwszString, PCRTUTF16 pwszString, size_t cwcExtra, const char *pszTag);
|
---|
2926 |
|
---|
2927 | /**
|
---|
2928 | * Returns the length of a UTF-16 string in UTF-16 characters
|
---|
2929 | * without trailing '\\0'.
|
---|
2930 | *
|
---|
2931 | * Surrogate pairs counts as two UTF-16 characters here. Use RTUtf16CpCnt()
|
---|
2932 | * to get the exact number of code points in the string.
|
---|
2933 | *
|
---|
2934 | * @returns The number of RTUTF16 items in the string.
|
---|
2935 | * @param pwszString Pointer the UTF-16 string.
|
---|
2936 | * @remark This function will not make any attempt to validate the encoding.
|
---|
2937 | */
|
---|
2938 | RTDECL(size_t) RTUtf16Len(PCRTUTF16 pwszString);
|
---|
2939 |
|
---|
2940 | /**
|
---|
2941 | * Performs a case sensitive string compare between two UTF-16 strings.
|
---|
2942 | *
|
---|
2943 | * @returns < 0 if the first string less than the second string.s
|
---|
2944 | * @returns 0 if the first string identical to the second string.
|
---|
2945 | * @returns > 0 if the first string greater than the second string.
|
---|
2946 | * @param pwsz1 First UTF-16 string. Null is allowed.
|
---|
2947 | * @param pwsz2 Second UTF-16 string. Null is allowed.
|
---|
2948 | * @remark This function will not make any attempt to validate the encoding.
|
---|
2949 | */
|
---|
2950 | RTDECL(int) RTUtf16Cmp(register PCRTUTF16 pwsz1, register PCRTUTF16 pwsz2);
|
---|
2951 |
|
---|
2952 | /**
|
---|
2953 | * Performs a case insensitive string compare between two UTF-16 strings.
|
---|
2954 | *
|
---|
2955 | * This is a simplified compare, as only the simplified lower/upper case folding
|
---|
2956 | * specified by the unicode specs are used. It does not consider character pairs
|
---|
2957 | * as they are used in some languages, just simple upper & lower case compares.
|
---|
2958 | *
|
---|
2959 | * @returns < 0 if the first string less than the second string.
|
---|
2960 | * @returns 0 if the first string identical to the second string.
|
---|
2961 | * @returns > 0 if the first string greater than the second string.
|
---|
2962 | * @param pwsz1 First UTF-16 string. Null is allowed.
|
---|
2963 | * @param pwsz2 Second UTF-16 string. Null is allowed.
|
---|
2964 | */
|
---|
2965 | RTDECL(int) RTUtf16ICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
|
---|
2966 |
|
---|
2967 | /**
|
---|
2968 | * Performs a case insensitive string compare between two UTF-16 strings
|
---|
2969 | * using the current locale of the process (if applicable).
|
---|
2970 | *
|
---|
2971 | * This differs from RTUtf16ICmp() in that it will try, if a locale with the
|
---|
2972 | * required data is available, to do a correct case-insensitive compare. It
|
---|
2973 | * follows that it is more complex and thereby likely to be more expensive.
|
---|
2974 | *
|
---|
2975 | * @returns < 0 if the first string less than the second string.
|
---|
2976 | * @returns 0 if the first string identical to the second string.
|
---|
2977 | * @returns > 0 if the first string greater than the second string.
|
---|
2978 | * @param pwsz1 First UTF-16 string. Null is allowed.
|
---|
2979 | * @param pwsz2 Second UTF-16 string. Null is allowed.
|
---|
2980 | */
|
---|
2981 | RTDECL(int) RTUtf16LocaleICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
|
---|
2982 |
|
---|
2983 | /**
|
---|
2984 | * Folds a UTF-16 string to lowercase.
|
---|
2985 | *
|
---|
2986 | * This is a very simple folding; is uses the simple lowercase
|
---|
2987 | * code point, it is not related to any locale just the most common
|
---|
2988 | * lowercase codepoint setup by the unicode specs, and it will not
|
---|
2989 | * create new surrogate pairs or remove existing ones.
|
---|
2990 | *
|
---|
2991 | * @returns Pointer to the passed in string.
|
---|
2992 | * @param pwsz The string to fold.
|
---|
2993 | */
|
---|
2994 | RTDECL(PRTUTF16) RTUtf16ToLower(PRTUTF16 pwsz);
|
---|
2995 |
|
---|
2996 | /**
|
---|
2997 | * Folds a UTF-16 string to uppercase.
|
---|
2998 | *
|
---|
2999 | * This is a very simple folding; is uses the simple uppercase
|
---|
3000 | * code point, it is not related to any locale just the most common
|
---|
3001 | * uppercase codepoint setup by the unicode specs, and it will not
|
---|
3002 | * create new surrogate pairs or remove existing ones.
|
---|
3003 | *
|
---|
3004 | * @returns Pointer to the passed in string.
|
---|
3005 | * @param pwsz The string to fold.
|
---|
3006 | */
|
---|
3007 | RTDECL(PRTUTF16) RTUtf16ToUpper(PRTUTF16 pwsz);
|
---|
3008 |
|
---|
3009 | /**
|
---|
3010 | * Translate a UTF-16 string into a UTF-8 allocating the result buffer (default
|
---|
3011 | * tag).
|
---|
3012 | *
|
---|
3013 | * @returns iprt status code.
|
---|
3014 | * @param pwszString UTF-16 string to convert.
|
---|
3015 | * @param ppszString Receives pointer of allocated UTF-8 string on
|
---|
3016 | * success, and is always set to NULL on failure.
|
---|
3017 | * The returned pointer must be freed using RTStrFree().
|
---|
3018 | */
|
---|
3019 | #define RTUtf16ToUtf8(pwszString, ppszString) RTUtf16ToUtf8Tag((pwszString), (ppszString), RTSTR_TAG)
|
---|
3020 |
|
---|
3021 | /**
|
---|
3022 | * Translate a UTF-16 string into a UTF-8 allocating the result buffer.
|
---|
3023 | *
|
---|
3024 | * @returns iprt status code.
|
---|
3025 | * @param pwszString UTF-16 string to convert.
|
---|
3026 | * @param ppszString Receives pointer of allocated UTF-8 string on
|
---|
3027 | * success, and is always set to NULL on failure.
|
---|
3028 | * The returned pointer must be freed using RTStrFree().
|
---|
3029 | * @param pszTag Allocation tag used for statistics and such.
|
---|
3030 | */
|
---|
3031 | RTDECL(int) RTUtf16ToUtf8Tag(PCRTUTF16 pwszString, char **ppszString, const char *pszTag);
|
---|
3032 |
|
---|
3033 | /**
|
---|
3034 | * Translates UTF-16 to UTF-8 using buffer provided by the caller or a fittingly
|
---|
3035 | * sized buffer allocated by the function (default tag).
|
---|
3036 | *
|
---|
3037 | * @returns iprt status code.
|
---|
3038 | * @param pwszString The UTF-16 string to convert.
|
---|
3039 | * @param cwcString The number of RTUTF16 items to translate from pwszString.
|
---|
3040 | * The translation will stop when reaching cwcString or the terminator ('\\0').
|
---|
3041 | * Use RTSTR_MAX to translate the entire string.
|
---|
3042 | * @param ppsz If cch is non-zero, this must either be pointing to a pointer to
|
---|
3043 | * a buffer of the specified size, or pointer to a NULL pointer.
|
---|
3044 | * If *ppsz is NULL or cch is zero a buffer of at least cch chars
|
---|
3045 | * will be allocated to hold the translated string.
|
---|
3046 | * If a buffer was requested it must be freed using RTStrFree().
|
---|
3047 | * @param cch The buffer size in chars (the type). This includes the terminator.
|
---|
3048 | * @param pcch Where to store the length of the translated string,
|
---|
3049 | * excluding the terminator. (Optional)
|
---|
3050 | *
|
---|
3051 | * This may be set under some error conditions,
|
---|
3052 | * however, only for VERR_BUFFER_OVERFLOW and
|
---|
3053 | * VERR_NO_STR_MEMORY will it contain a valid string
|
---|
3054 | * length that can be used to resize the buffer.
|
---|
3055 | */
|
---|
3056 | #define RTUtf16ToUtf8Ex(pwszString, cwcString, ppsz, cch, pcch) \
|
---|
3057 | RTUtf16ToUtf8ExTag((pwszString), (cwcString), (ppsz), (cch), (pcch), RTSTR_TAG)
|
---|
3058 |
|
---|
3059 | /**
|
---|
3060 | * Translates UTF-16 to UTF-8 using buffer provided by the caller or a fittingly
|
---|
3061 | * sized buffer allocated by the function (custom tag).
|
---|
3062 | *
|
---|
3063 | * @returns iprt status code.
|
---|
3064 | * @param pwszString The UTF-16 string to convert.
|
---|
3065 | * @param cwcString The number of RTUTF16 items to translate from pwszString.
|
---|
3066 | * The translation will stop when reaching cwcString or the terminator ('\\0').
|
---|
3067 | * Use RTSTR_MAX to translate the entire string.
|
---|
3068 | * @param ppsz If cch is non-zero, this must either be pointing to a pointer to
|
---|
3069 | * a buffer of the specified size, or pointer to a NULL pointer.
|
---|
3070 | * If *ppsz is NULL or cch is zero a buffer of at least cch chars
|
---|
3071 | * will be allocated to hold the translated string.
|
---|
3072 | * If a buffer was requested it must be freed using RTStrFree().
|
---|
3073 | * @param cch The buffer size in chars (the type). This includes the terminator.
|
---|
3074 | * @param pcch Where to store the length of the translated string,
|
---|
3075 | * excluding the terminator. (Optional)
|
---|
3076 | *
|
---|
3077 | * This may be set under some error conditions,
|
---|
3078 | * however, only for VERR_BUFFER_OVERFLOW and
|
---|
3079 | * VERR_NO_STR_MEMORY will it contain a valid string
|
---|
3080 | * length that can be used to resize the buffer.
|
---|
3081 | * @param pszTag Allocation tag used for statistics and such.
|
---|
3082 | */
|
---|
3083 | RTDECL(int) RTUtf16ToUtf8ExTag(PCRTUTF16 pwszString, size_t cwcString, char **ppsz, size_t cch, size_t *pcch, const char *pszTag);
|
---|
3084 |
|
---|
3085 | /**
|
---|
3086 | * Calculates the length of the UTF-16 string in UTF-8 chars (bytes).
|
---|
3087 | *
|
---|
3088 | * This function will validate the string, and incorrectly encoded UTF-16
|
---|
3089 | * strings will be rejected. The primary purpose of this function is to
|
---|
3090 | * help allocate buffers for RTUtf16ToUtf8() of the correct size. For most
|
---|
3091 | * other purposes RTUtf16ToUtf8Ex() should be used.
|
---|
3092 | *
|
---|
3093 | * @returns Number of char (bytes).
|
---|
3094 | * @returns 0 if the string was incorrectly encoded.
|
---|
3095 | * @param pwsz The UTF-16 string.
|
---|
3096 | */
|
---|
3097 | RTDECL(size_t) RTUtf16CalcUtf8Len(PCRTUTF16 pwsz);
|
---|
3098 |
|
---|
3099 | /**
|
---|
3100 | * Calculates the length of the UTF-16 string in UTF-8 chars (bytes).
|
---|
3101 | *
|
---|
3102 | * This function will validate the string, and incorrectly encoded UTF-16
|
---|
3103 | * strings will be rejected.
|
---|
3104 | *
|
---|
3105 | * @returns iprt status code.
|
---|
3106 | * @param pwsz The string.
|
---|
3107 | * @param cwc The max string length. Use RTSTR_MAX to process the entire string.
|
---|
3108 | * @param pcch Where to store the string length (in bytes). Optional.
|
---|
3109 | * This is undefined on failure.
|
---|
3110 | */
|
---|
3111 | RTDECL(int) RTUtf16CalcUtf8LenEx(PCRTUTF16 pwsz, size_t cwc, size_t *pcch);
|
---|
3112 |
|
---|
3113 | /**
|
---|
3114 | * Translate a UTF-16 string into a Latin-1 (ISO-8859-1) allocating the result
|
---|
3115 | * buffer (default tag).
|
---|
3116 | *
|
---|
3117 | * @returns iprt status code.
|
---|
3118 | * @param pwszString UTF-16 string to convert.
|
---|
3119 | * @param ppszString Receives pointer of allocated Latin1 string on
|
---|
3120 | * success, and is always set to NULL on failure.
|
---|
3121 | * The returned pointer must be freed using RTStrFree().
|
---|
3122 | */
|
---|
3123 | #define RTUtf16ToLatin1(pwszString, ppszString) RTUtf16ToLatin1Tag((pwszString), (ppszString), RTSTR_TAG)
|
---|
3124 |
|
---|
3125 | /**
|
---|
3126 | * Translate a UTF-16 string into a Latin-1 (ISO-8859-1) allocating the result
|
---|
3127 | * buffer (custom tag).
|
---|
3128 | *
|
---|
3129 | * @returns iprt status code.
|
---|
3130 | * @param pwszString UTF-16 string to convert.
|
---|
3131 | * @param ppszString Receives pointer of allocated Latin1 string on
|
---|
3132 | * success, and is always set to NULL on failure.
|
---|
3133 | * The returned pointer must be freed using RTStrFree().
|
---|
3134 | * @param pszTag Allocation tag used for statistics and such.
|
---|
3135 | */
|
---|
3136 | RTDECL(int) RTUtf16ToLatin1Tag(PCRTUTF16 pwszString, char **ppszString, const char *pszTag);
|
---|
3137 |
|
---|
3138 | /**
|
---|
3139 | * Translates UTF-16 to Latin-1 (ISO-8859-1) using buffer provided by the caller
|
---|
3140 | * or a fittingly sized buffer allocated by the function (default tag).
|
---|
3141 | *
|
---|
3142 | * @returns iprt status code.
|
---|
3143 | * @param pwszString The UTF-16 string to convert.
|
---|
3144 | * @param cwcString The number of RTUTF16 items to translate from
|
---|
3145 | * pwszString. The translation will stop when reaching
|
---|
3146 | * cwcString or the terminator ('\\0'). Use RTSTR_MAX
|
---|
3147 | * to translate the entire string.
|
---|
3148 | * @param ppsz Pointer to the pointer to the Latin-1 string. The
|
---|
3149 | * buffer can optionally be preallocated by the caller.
|
---|
3150 | *
|
---|
3151 | * If cch is zero, *ppsz is undefined.
|
---|
3152 | *
|
---|
3153 | * If cch is non-zero and *ppsz is not NULL, then this
|
---|
3154 | * will be used as the output buffer.
|
---|
3155 | * VERR_BUFFER_OVERFLOW will be returned if this is
|
---|
3156 | * insufficient.
|
---|
3157 | *
|
---|
3158 | * If cch is zero or *ppsz is NULL, then a buffer of
|
---|
3159 | * sufficent size is allocated. cch can be used to
|
---|
3160 | * specify a minimum size of this buffer. Use
|
---|
3161 | * RTUtf16Free() to free the result.
|
---|
3162 | *
|
---|
3163 | * @param cch The buffer size in chars (the type). This includes
|
---|
3164 | * the terminator.
|
---|
3165 | * @param pcch Where to store the length of the translated string,
|
---|
3166 | * excluding the terminator. (Optional)
|
---|
3167 | *
|
---|
3168 | * This may be set under some error conditions,
|
---|
3169 | * however, only for VERR_BUFFER_OVERFLOW and
|
---|
3170 | * VERR_NO_STR_MEMORY will it contain a valid string
|
---|
3171 | * length that can be used to resize the buffer.
|
---|
3172 | */
|
---|
3173 | #define RTUtf16ToLatin1Ex(pwszString, cwcString, ppsz, cch, pcch) \
|
---|
3174 | RTUtf16ToLatin1ExTag((pwszString), (cwcString), (ppsz), (cch), (pcch), RTSTR_TAG)
|
---|
3175 |
|
---|
3176 | /**
|
---|
3177 | * Translates UTF-16 to Latin-1 (ISO-8859-1) using buffer provided by the caller
|
---|
3178 | * or a fittingly sized buffer allocated by the function (custom tag).
|
---|
3179 | *
|
---|
3180 | * @returns iprt status code.
|
---|
3181 | * @param pwszString The UTF-16 string to convert.
|
---|
3182 | * @param cwcString The number of RTUTF16 items to translate from
|
---|
3183 | * pwszString. The translation will stop when reaching
|
---|
3184 | * cwcString or the terminator ('\\0'). Use RTSTR_MAX
|
---|
3185 | * to translate the entire string.
|
---|
3186 | * @param ppsz Pointer to the pointer to the Latin-1 string. The
|
---|
3187 | * buffer can optionally be preallocated by the caller.
|
---|
3188 | *
|
---|
3189 | * If cch is zero, *ppsz is undefined.
|
---|
3190 | *
|
---|
3191 | * If cch is non-zero and *ppsz is not NULL, then this
|
---|
3192 | * will be used as the output buffer.
|
---|
3193 | * VERR_BUFFER_OVERFLOW will be returned if this is
|
---|
3194 | * insufficient.
|
---|
3195 | *
|
---|
3196 | * If cch is zero or *ppsz is NULL, then a buffer of
|
---|
3197 | * sufficent size is allocated. cch can be used to
|
---|
3198 | * specify a minimum size of this buffer. Use
|
---|
3199 | * RTUtf16Free() to free the result.
|
---|
3200 | *
|
---|
3201 | * @param cch The buffer size in chars (the type). This includes
|
---|
3202 | * the terminator.
|
---|
3203 | * @param pcch Where to store the length of the translated string,
|
---|
3204 | * excluding the terminator. (Optional)
|
---|
3205 | *
|
---|
3206 | * This may be set under some error conditions,
|
---|
3207 | * however, only for VERR_BUFFER_OVERFLOW and
|
---|
3208 | * VERR_NO_STR_MEMORY will it contain a valid string
|
---|
3209 | * length that can be used to resize the buffer.
|
---|
3210 | * @param pszTag Allocation tag used for statistics and such.
|
---|
3211 | */
|
---|
3212 | RTDECL(int) RTUtf16ToLatin1ExTag(PCRTUTF16 pwszString, size_t cwcString, char **ppsz, size_t cch, size_t *pcch, const char *pszTag);
|
---|
3213 |
|
---|
3214 | /**
|
---|
3215 | * Calculates the length of the UTF-16 string in Latin-1 (ISO-8859-1) chars.
|
---|
3216 | *
|
---|
3217 | * This function will validate the string, and incorrectly encoded UTF-16
|
---|
3218 | * strings will be rejected. The primary purpose of this function is to
|
---|
3219 | * help allocate buffers for RTUtf16ToLatin1() of the correct size. For most
|
---|
3220 | * other purposes RTUtf16ToLatin1Ex() should be used.
|
---|
3221 | *
|
---|
3222 | * @returns Number of char (bytes).
|
---|
3223 | * @returns 0 if the string was incorrectly encoded.
|
---|
3224 | * @param pwsz The UTF-16 string.
|
---|
3225 | */
|
---|
3226 | RTDECL(size_t) RTUtf16CalcLatin1Len(PCRTUTF16 pwsz);
|
---|
3227 |
|
---|
3228 | /**
|
---|
3229 | * Calculates the length of the UTF-16 string in Latin-1 (ISO-8859-1) chars.
|
---|
3230 | *
|
---|
3231 | * This function will validate the string, and incorrectly encoded UTF-16
|
---|
3232 | * strings will be rejected.
|
---|
3233 | *
|
---|
3234 | * @returns iprt status code.
|
---|
3235 | * @param pwsz The string.
|
---|
3236 | * @param cwc The max string length. Use RTSTR_MAX to process the
|
---|
3237 | * entire string.
|
---|
3238 | * @param pcch Where to store the string length (in bytes). Optional.
|
---|
3239 | * This is undefined on failure.
|
---|
3240 | */
|
---|
3241 | RTDECL(int) RTUtf16CalcLatin1LenEx(PCRTUTF16 pwsz, size_t cwc, size_t *pcch);
|
---|
3242 |
|
---|
3243 | /**
|
---|
3244 | * Get the unicode code point at the given string position.
|
---|
3245 | *
|
---|
3246 | * @returns unicode code point.
|
---|
3247 | * @returns RTUNICP_INVALID if the encoding is invalid.
|
---|
3248 | * @param pwsz The string.
|
---|
3249 | *
|
---|
3250 | * @remark This is an internal worker for RTUtf16GetCp().
|
---|
3251 | */
|
---|
3252 | RTDECL(RTUNICP) RTUtf16GetCpInternal(PCRTUTF16 pwsz);
|
---|
3253 |
|
---|
3254 | /**
|
---|
3255 | * Get the unicode code point at the given string position.
|
---|
3256 | *
|
---|
3257 | * @returns iprt status code.
|
---|
3258 | * @param ppwsz Pointer to the string pointer. This will be updated to
|
---|
3259 | * point to the char following the current code point.
|
---|
3260 | * @param pCp Where to store the code point.
|
---|
3261 | * RTUNICP_INVALID is stored here on failure.
|
---|
3262 | *
|
---|
3263 | * @remark This is an internal worker for RTUtf16GetCpEx().
|
---|
3264 | */
|
---|
3265 | RTDECL(int) RTUtf16GetCpExInternal(PCRTUTF16 *ppwsz, PRTUNICP pCp);
|
---|
3266 |
|
---|
3267 | /**
|
---|
3268 | * Put the unicode code point at the given string position
|
---|
3269 | * and return the pointer to the char following it.
|
---|
3270 | *
|
---|
3271 | * This function will not consider anything at or following the
|
---|
3272 | * buffer area pointed to by pwsz. It is therefore not suitable for
|
---|
3273 | * inserting code points into a string, only appending/overwriting.
|
---|
3274 | *
|
---|
3275 | * @returns pointer to the char following the written code point.
|
---|
3276 | * @param pwsz The string.
|
---|
3277 | * @param CodePoint The code point to write.
|
---|
3278 | * This should not be RTUNICP_INVALID or any other
|
---|
3279 | * character out of the UTF-16 range.
|
---|
3280 | *
|
---|
3281 | * @remark This is an internal worker for RTUtf16GetCpEx().
|
---|
3282 | */
|
---|
3283 | RTDECL(PRTUTF16) RTUtf16PutCpInternal(PRTUTF16 pwsz, RTUNICP CodePoint);
|
---|
3284 |
|
---|
3285 | /**
|
---|
3286 | * Get the unicode code point at the given string position.
|
---|
3287 | *
|
---|
3288 | * @returns unicode code point.
|
---|
3289 | * @returns RTUNICP_INVALID if the encoding is invalid.
|
---|
3290 | * @param pwsz The string.
|
---|
3291 | *
|
---|
3292 | * @remark We optimize this operation by using an inline function for
|
---|
3293 | * everything which isn't a surrogate pair or an endian indicator.
|
---|
3294 | */
|
---|
3295 | DECLINLINE(RTUNICP) RTUtf16GetCp(PCRTUTF16 pwsz)
|
---|
3296 | {
|
---|
3297 | const RTUTF16 wc = *pwsz;
|
---|
3298 | if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
|
---|
3299 | return wc;
|
---|
3300 | return RTUtf16GetCpInternal(pwsz);
|
---|
3301 | }
|
---|
3302 |
|
---|
3303 | /**
|
---|
3304 | * Get the unicode code point at the given string position.
|
---|
3305 | *
|
---|
3306 | * @returns iprt status code.
|
---|
3307 | * @param ppwsz Pointer to the string pointer. This will be updated to
|
---|
3308 | * point to the char following the current code point.
|
---|
3309 | * @param pCp Where to store the code point.
|
---|
3310 | * RTUNICP_INVALID is stored here on failure.
|
---|
3311 | *
|
---|
3312 | * @remark We optimize this operation by using an inline function for
|
---|
3313 | * everything which isn't a surrogate pair or and endian indicator.
|
---|
3314 | */
|
---|
3315 | DECLINLINE(int) RTUtf16GetCpEx(PCRTUTF16 *ppwsz, PRTUNICP pCp)
|
---|
3316 | {
|
---|
3317 | const RTUTF16 wc = **ppwsz;
|
---|
3318 | if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
|
---|
3319 | {
|
---|
3320 | (*ppwsz)++;
|
---|
3321 | *pCp = wc;
|
---|
3322 | return VINF_SUCCESS;
|
---|
3323 | }
|
---|
3324 | return RTUtf16GetCpExInternal(ppwsz, pCp);
|
---|
3325 | }
|
---|
3326 |
|
---|
3327 | /**
|
---|
3328 | * Put the unicode code point at the given string position
|
---|
3329 | * and return the pointer to the char following it.
|
---|
3330 | *
|
---|
3331 | * This function will not consider anything at or following the
|
---|
3332 | * buffer area pointed to by pwsz. It is therefore not suitable for
|
---|
3333 | * inserting code points into a string, only appending/overwriting.
|
---|
3334 | *
|
---|
3335 | * @returns pointer to the char following the written code point.
|
---|
3336 | * @param pwsz The string.
|
---|
3337 | * @param CodePoint The code point to write.
|
---|
3338 | * This should not be RTUNICP_INVALID or any other
|
---|
3339 | * character out of the UTF-16 range.
|
---|
3340 | *
|
---|
3341 | * @remark We optimize this operation by using an inline function for
|
---|
3342 | * everything which isn't a surrogate pair or and endian indicator.
|
---|
3343 | */
|
---|
3344 | DECLINLINE(PRTUTF16) RTUtf16PutCp(PRTUTF16 pwsz, RTUNICP CodePoint)
|
---|
3345 | {
|
---|
3346 | if (CodePoint < 0xd800 || (CodePoint > 0xd800 && CodePoint < 0xfffe))
|
---|
3347 | {
|
---|
3348 | *pwsz++ = (RTUTF16)CodePoint;
|
---|
3349 | return pwsz;
|
---|
3350 | }
|
---|
3351 | return RTUtf16PutCpInternal(pwsz, CodePoint);
|
---|
3352 | }
|
---|
3353 |
|
---|
3354 | /**
|
---|
3355 | * Skips ahead, past the current code point.
|
---|
3356 | *
|
---|
3357 | * @returns Pointer to the char after the current code point.
|
---|
3358 | * @param pwsz Pointer to the current code point.
|
---|
3359 | * @remark This will not move the next valid code point, only past the current one.
|
---|
3360 | */
|
---|
3361 | DECLINLINE(PRTUTF16) RTUtf16NextCp(PCRTUTF16 pwsz)
|
---|
3362 | {
|
---|
3363 | RTUNICP Cp;
|
---|
3364 | RTUtf16GetCpEx(&pwsz, &Cp);
|
---|
3365 | return (PRTUTF16)pwsz;
|
---|
3366 | }
|
---|
3367 |
|
---|
3368 | /**
|
---|
3369 | * Skips backwards, to the previous code point.
|
---|
3370 | *
|
---|
3371 | * @returns Pointer to the char after the current code point.
|
---|
3372 | * @param pwszStart Pointer to the start of the string.
|
---|
3373 | * @param pwsz Pointer to the current code point.
|
---|
3374 | */
|
---|
3375 | RTDECL(PRTUTF16) RTUtf16PrevCp(PCRTUTF16 pwszStart, PCRTUTF16 pwsz);
|
---|
3376 |
|
---|
3377 |
|
---|
3378 | /**
|
---|
3379 | * Checks if the UTF-16 char is the high surrogate char (i.e.
|
---|
3380 | * the 1st char in the pair).
|
---|
3381 | *
|
---|
3382 | * @returns true if it is.
|
---|
3383 | * @returns false if it isn't.
|
---|
3384 | * @param wc The character to investigate.
|
---|
3385 | */
|
---|
3386 | DECLINLINE(bool) RTUtf16IsHighSurrogate(RTUTF16 wc)
|
---|
3387 | {
|
---|
3388 | return wc >= 0xd800 && wc <= 0xdbff;
|
---|
3389 | }
|
---|
3390 |
|
---|
3391 | /**
|
---|
3392 | * Checks if the UTF-16 char is the low surrogate char (i.e.
|
---|
3393 | * the 2nd char in the pair).
|
---|
3394 | *
|
---|
3395 | * @returns true if it is.
|
---|
3396 | * @returns false if it isn't.
|
---|
3397 | * @param wc The character to investigate.
|
---|
3398 | */
|
---|
3399 | DECLINLINE(bool) RTUtf16IsLowSurrogate(RTUTF16 wc)
|
---|
3400 | {
|
---|
3401 | return wc >= 0xdc00 && wc <= 0xdfff;
|
---|
3402 | }
|
---|
3403 |
|
---|
3404 |
|
---|
3405 | /**
|
---|
3406 | * Checks if the two UTF-16 chars form a valid surrogate pair.
|
---|
3407 | *
|
---|
3408 | * @returns true if they do.
|
---|
3409 | * @returns false if they doesn't.
|
---|
3410 | * @param wcHigh The high (1st) character.
|
---|
3411 | * @param wcLow The low (2nd) character.
|
---|
3412 | */
|
---|
3413 | DECLINLINE(bool) RTUtf16IsSurrogatePair(RTUTF16 wcHigh, RTUTF16 wcLow)
|
---|
3414 | {
|
---|
3415 | return RTUtf16IsHighSurrogate(wcHigh)
|
---|
3416 | && RTUtf16IsLowSurrogate(wcLow);
|
---|
3417 | }
|
---|
3418 |
|
---|
3419 | /** @} */
|
---|
3420 |
|
---|
3421 |
|
---|
3422 | /** @defgroup rt_str_latin1 Latin-1 (ISO-8859-1) String Manipulation
|
---|
3423 | * @ingroup grp_rt_str
|
---|
3424 | * @{
|
---|
3425 | */
|
---|
3426 |
|
---|
3427 | /**
|
---|
3428 | * Calculates the length of the Latin-1 (ISO-8859-1) string in RTUTF16 items.
|
---|
3429 | *
|
---|
3430 | * @returns Number of RTUTF16 items.
|
---|
3431 | * @param psz The Latin-1 string.
|
---|
3432 | */
|
---|
3433 | RTDECL(size_t) RTLatin1CalcUtf16Len(const char *psz);
|
---|
3434 |
|
---|
3435 | /**
|
---|
3436 | * Calculates the length of the Latin-1 (ISO-8859-1) string in RTUTF16 items.
|
---|
3437 | *
|
---|
3438 | * @returns iprt status code.
|
---|
3439 | * @param psz The Latin-1 string.
|
---|
3440 | * @param cch The max string length. Use RTSTR_MAX to process the
|
---|
3441 | * entire string.
|
---|
3442 | * @param pcwc Where to store the string length. Optional.
|
---|
3443 | * This is undefined on failure.
|
---|
3444 | */
|
---|
3445 | RTDECL(int) RTLatin1CalcUtf16LenEx(const char *psz, size_t cch, size_t *pcwc);
|
---|
3446 |
|
---|
3447 | /**
|
---|
3448 | * Translate a Latin-1 (ISO-8859-1) string into a UTF-16 allocating the result
|
---|
3449 | * buffer (default tag).
|
---|
3450 | *
|
---|
3451 | * @returns iprt status code.
|
---|
3452 | * @param pszString The Latin-1 string to convert.
|
---|
3453 | * @param ppwszString Receives pointer to the allocated UTF-16 string. The
|
---|
3454 | * returned string must be freed using RTUtf16Free().
|
---|
3455 | */
|
---|
3456 | #define RTLatin1ToUtf16(pszString, ppwszString) RTLatin1ToUtf16Tag((pszString), (ppwszString), RTSTR_TAG)
|
---|
3457 |
|
---|
3458 | /**
|
---|
3459 | * Translate a Latin-1 (ISO-8859-1) string into a UTF-16 allocating the result
|
---|
3460 | * buffer (custom tag).
|
---|
3461 | *
|
---|
3462 | * @returns iprt status code.
|
---|
3463 | * @param pszString The Latin-1 string to convert.
|
---|
3464 | * @param ppwszString Receives pointer to the allocated UTF-16 string. The
|
---|
3465 | * returned string must be freed using RTUtf16Free().
|
---|
3466 | * @param pszTag Allocation tag used for statistics and such.
|
---|
3467 | */
|
---|
3468 | RTDECL(int) RTLatin1ToUtf16Tag(const char *pszString, PRTUTF16 *ppwszString, const char *pszTag);
|
---|
3469 |
|
---|
3470 | /**
|
---|
3471 | * Translates pszString from Latin-1 (ISO-8859-1) to UTF-16, allocating the
|
---|
3472 | * result buffer if requested (default tag).
|
---|
3473 | *
|
---|
3474 | * @returns iprt status code.
|
---|
3475 | * @param pszString The Latin-1 string to convert.
|
---|
3476 | * @param cchString The maximum size in chars (the type) to convert.
|
---|
3477 | * The conversion stops when it reaches cchString or
|
---|
3478 | * the string terminator ('\\0').
|
---|
3479 | * Use RTSTR_MAX to translate the entire string.
|
---|
3480 | * @param ppwsz If cwc is non-zero, this must either be pointing
|
---|
3481 | * to pointer to a buffer of the specified size, or
|
---|
3482 | * pointer to a NULL pointer.
|
---|
3483 | * If *ppwsz is NULL or cwc is zero a buffer of at
|
---|
3484 | * least cwc items will be allocated to hold the
|
---|
3485 | * translated string. If a buffer was requested it
|
---|
3486 | * must be freed using RTUtf16Free().
|
---|
3487 | * @param cwc The buffer size in RTUTF16s. This includes the
|
---|
3488 | * terminator.
|
---|
3489 | * @param pcwc Where to store the length of the translated string,
|
---|
3490 | * excluding the terminator. (Optional)
|
---|
3491 | *
|
---|
3492 | * This may be set under some error conditions,
|
---|
3493 | * however, only for VERR_BUFFER_OVERFLOW and
|
---|
3494 | * VERR_NO_STR_MEMORY will it contain a valid string
|
---|
3495 | * length that can be used to resize the buffer.
|
---|
3496 | */
|
---|
3497 | #define RTLatin1ToUtf16Ex(pszString, cchString, ppwsz, cwc, pcwc) \
|
---|
3498 | RTLatin1ToUtf16ExTag((pszString), (cchString), (ppwsz), (cwc), (pcwc), RTSTR_TAG)
|
---|
3499 |
|
---|
3500 | /**
|
---|
3501 | * Translates pszString from Latin-1 (ISO-8859-1) to UTF-16, allocating the
|
---|
3502 | * result buffer if requested.
|
---|
3503 | *
|
---|
3504 | * @returns iprt status code.
|
---|
3505 | * @param pszString The Latin-1 string to convert.
|
---|
3506 | * @param cchString The maximum size in chars (the type) to convert.
|
---|
3507 | * The conversion stops when it reaches cchString or
|
---|
3508 | * the string terminator ('\\0').
|
---|
3509 | * Use RTSTR_MAX to translate the entire string.
|
---|
3510 | * @param ppwsz If cwc is non-zero, this must either be pointing
|
---|
3511 | * to pointer to a buffer of the specified size, or
|
---|
3512 | * pointer to a NULL pointer.
|
---|
3513 | * If *ppwsz is NULL or cwc is zero a buffer of at
|
---|
3514 | * least cwc items will be allocated to hold the
|
---|
3515 | * translated string. If a buffer was requested it
|
---|
3516 | * must be freed using RTUtf16Free().
|
---|
3517 | * @param cwc The buffer size in RTUTF16s. This includes the
|
---|
3518 | * terminator.
|
---|
3519 | * @param pcwc Where to store the length of the translated string,
|
---|
3520 | * excluding the terminator. (Optional)
|
---|
3521 | *
|
---|
3522 | * This may be set under some error conditions,
|
---|
3523 | * however, only for VERR_BUFFER_OVERFLOW and
|
---|
3524 | * VERR_NO_STR_MEMORY will it contain a valid string
|
---|
3525 | * length that can be used to resize the buffer.
|
---|
3526 | * @param pszTag Allocation tag used for statistics and such.
|
---|
3527 | */
|
---|
3528 | RTDECL(int) RTLatin1ToUtf16ExTag(const char *pszString, size_t cchString,
|
---|
3529 | PRTUTF16 *ppwsz, size_t cwc, size_t *pcwc, const char *pszTag);
|
---|
3530 |
|
---|
3531 | /** @} */
|
---|
3532 |
|
---|
3533 |
|
---|
3534 | RT_C_DECLS_END
|
---|
3535 |
|
---|
3536 | /** @} */
|
---|
3537 |
|
---|
3538 | #endif
|
---|
3539 |
|
---|