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