VirtualBox

source: vbox/trunk/include/iprt/utf16.h@ 76327

Last change on this file since 76327 was 76096, checked in by vboxsync, 6 years ago

IPRT: Added RTUtf16Chr.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 59.1 KB
Line 
1/** @file
2 * IPRT - String Manipulation, UTF-16 encoding.
3 */
4
5/*
6 * Copyright (C) 2006-2017 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___iprt_utf16_h
27#define ___iprt_utf16_h
28
29#include <iprt/string.h>
30
31RT_C_DECLS_BEGIN
32
33
34/** @defgroup rt_str_utf16 UTF-16 String Manipulation
35 * @ingroup grp_rt_str
36 * @{
37 */
38
39/**
40 * Allocates memory for UTF-16 string storage (default tag).
41 *
42 * You should normally not use this function, except if there is some very
43 * custom string handling you need doing that isn't covered by any of the other
44 * APIs.
45 *
46 * @returns Pointer to the allocated UTF-16 string. The first wide char is
47 * always set to the string terminator char, the contents of the
48 * remainder of the memory is undefined. The string must be freed by
49 * calling RTUtf16Free.
50 *
51 * NULL is returned if the allocation failed. Please translate this to
52 * VERR_NO_UTF16_MEMORY and not VERR_NO_MEMORY. Also consider
53 * RTUtf16AllocEx if an IPRT status code is required.
54 *
55 * @param cb How many bytes to allocate, will be rounded up
56 * to a multiple of two. If this is zero, we will
57 * allocate a terminator wide char anyway.
58 */
59#define RTUtf16Alloc(cb) RTUtf16AllocTag((cb), RTSTR_TAG)
60
61/**
62 * Allocates memory for UTF-16 string storage (custom tag).
63 *
64 * You should normally not use this function, except if there is some very
65 * custom string handling you need doing that isn't covered by any of the other
66 * APIs.
67 *
68 * @returns Pointer to the allocated UTF-16 string. The first wide char is
69 * always set to the string terminator char, the contents of the
70 * remainder of the memory is undefined. The string must be freed by
71 * calling RTUtf16Free.
72 *
73 * NULL is returned if the allocation failed. Please translate this to
74 * VERR_NO_UTF16_MEMORY and not VERR_NO_MEMORY. Also consider
75 * RTUtf16AllocExTag if an IPRT status code is required.
76 *
77 * @param cb How many bytes to allocate, will be rounded up
78 * to a multiple of two. If this is zero, we will
79 * allocate a terminator wide char anyway.
80 * @param pszTag Allocation tag used for statistics and such.
81 */
82RTDECL(PRTUTF16) RTUtf16AllocTag(size_t cb, const char *pszTag);
83
84/**
85 * Reallocates the specified UTF-16 string (default tag).
86 *
87 * You should normally not use this function, except if there is some very
88 * custom string handling you need doing that isn't covered by any of the other
89 * APIs.
90 *
91 * @returns VINF_SUCCESS.
92 * @retval VERR_NO_UTF16_MEMORY if we failed to reallocate the string, @a
93 * *ppwsz remains unchanged.
94 *
95 * @param ppwsz Pointer to the string variable containing the
96 * input and output string.
97 *
98 * When not freeing the string, the result will
99 * always have the last RTUTF16 set to the
100 * terminator character so that when used for
101 * string truncation the result will be a valid
102 * C-style string (your job to keep it a valid
103 * UTF-16 string).
104 *
105 * When the input string is NULL and we're supposed
106 * to reallocate, the returned string will also
107 * have the first RTUTF16 set to the terminator
108 * char so it will be a valid C-style string.
109 *
110 * @param cbNew When @a cbNew is zero, we'll behave like
111 * RTUtf16Free and @a *ppwsz will be set to NULL.
112 *
113 * When not zero, this will be rounded up to a
114 * multiple of two, and used as the new size of the
115 * memory backing the string, i.e. it includes the
116 * terminator (RTUTF16) char.
117 */
118#define RTUtf16Realloc(ppwsz, cbNew) RTUtf16ReallocTag((ppwsz), (cbNew), RTSTR_TAG)
119
120/**
121 * Reallocates the specified UTF-16 string (custom tag).
122 *
123 * You should normally not use this function, except if there is some very
124 * custom string handling you need doing that isn't covered by any of the other
125 * APIs.
126 *
127 * @returns VINF_SUCCESS.
128 * @retval VERR_NO_UTF16_MEMORY if we failed to reallocate the string, @a
129 * *ppwsz remains unchanged.
130 *
131 * @param ppwsz Pointer to the string variable containing the
132 * input and output string.
133 *
134 * When not freeing the string, the result will
135 * always have the last RTUTF16 set to the
136 * terminator character so that when used for
137 * string truncation the result will be a valid
138 * C-style string (your job to keep it a valid
139 * UTF-16 string).
140 *
141 * When the input string is NULL and we're supposed
142 * to reallocate, the returned string will also
143 * have the first RTUTF16 set to the terminator
144 * char so it will be a valid C-style string.
145 *
146 * @param cbNew When @a cbNew is zero, we'll behave like
147 * RTUtf16Free and @a *ppwsz will be set to NULL.
148 *
149 * When not zero, this will be rounded up to a
150 * multiple of two, and used as the new size of the
151 * memory backing the string, i.e. it includes the
152 * terminator (RTUTF16) char.
153 * @param pszTag Allocation tag used for statistics and such.
154 */
155RTDECL(int) RTUtf16ReallocTag(PRTUTF16 *ppwsz, size_t cbNew, const char *pszTag);
156
157/**
158 * Free a UTF-16 string allocated by RTStrToUtf16(), RTStrToUtf16Ex(),
159 * RTLatin1ToUtf16(), RTLatin1ToUtf16Ex(), RTUtf16Dup() or RTUtf16DupEx().
160 *
161 * @returns iprt status code.
162 * @param pwszString The UTF-16 string to free. NULL is accepted.
163 */
164RTDECL(void) RTUtf16Free(PRTUTF16 pwszString);
165
166/**
167 * Allocates a new copy of the specified UTF-16 string (default tag).
168 *
169 * @returns Pointer to the allocated string copy. Use RTUtf16Free() to free it.
170 * @returns NULL when out of memory.
171 * @param pwszString UTF-16 string to duplicate.
172 * @remark This function will not make any attempt to validate the encoding.
173 */
174#define RTUtf16Dup(pwszString) RTUtf16DupTag((pwszString), RTSTR_TAG)
175
176/**
177 * Allocates a new copy of the specified UTF-16 string (custom tag).
178 *
179 * @returns Pointer to the allocated string copy. Use RTUtf16Free() to free it.
180 * @returns NULL when out of memory.
181 * @param pwszString UTF-16 string to duplicate.
182 * @param pszTag Allocation tag used for statistics and such.
183 * @remark This function will not make any attempt to validate the encoding.
184 */
185RTDECL(PRTUTF16) RTUtf16DupTag(PCRTUTF16 pwszString, const char *pszTag);
186
187/**
188 * Allocates a new copy of the specified UTF-16 string (default tag).
189 *
190 * @returns iprt status code.
191 * @param ppwszString Receives pointer of the allocated UTF-16 string.
192 * The returned pointer must be freed using RTUtf16Free().
193 * @param pwszString UTF-16 string to duplicate.
194 * @param cwcExtra Number of extra RTUTF16 items to allocate.
195 * @remark This function will not make any attempt to validate the encoding.
196 */
197#define RTUtf16DupEx(ppwszString, pwszString, cwcExtra) \
198 RTUtf16DupExTag((ppwszString), (pwszString), (cwcExtra), RTSTR_TAG)
199
200/**
201 * Allocates a new copy of the specified UTF-16 string (custom tag).
202 *
203 * @returns iprt status code.
204 * @param ppwszString Receives pointer of the allocated UTF-16 string.
205 * The returned pointer must be freed using RTUtf16Free().
206 * @param pwszString UTF-16 string to duplicate.
207 * @param cwcExtra Number of extra RTUTF16 items to allocate.
208 * @param pszTag Allocation tag used for statistics and such.
209 * @remark This function will not make any attempt to validate the encoding.
210 */
211RTDECL(int) RTUtf16DupExTag(PRTUTF16 *ppwszString, PCRTUTF16 pwszString, size_t cwcExtra, const char *pszTag);
212
213/**
214 * Returns the length of a UTF-16 string in UTF-16 characters
215 * without trailing '\\0'.
216 *
217 * Surrogate pairs counts as two UTF-16 characters here. Use RTUtf16CpCnt()
218 * to get the exact number of code points in the string.
219 *
220 * @returns The number of RTUTF16 items in the string.
221 * @param pwszString Pointer the UTF-16 string.
222 * @remark This function will not make any attempt to validate the encoding.
223 */
224RTDECL(size_t) RTUtf16Len(PCRTUTF16 pwszString);
225
226/**
227 * Find the length of a zero-terminated byte string, given a max string length.
228 *
229 * @returns The string length or cbMax. The returned length does not include
230 * the zero terminator if it was found.
231 *
232 * @param pwszString The string.
233 * @param cwcMax The max string length in RTUTF16s.
234 * @sa RTUtf16NLenEx, RTStrNLen.
235 */
236RTDECL(size_t) RTUtf16NLen(PCRTUTF16 pwszString, size_t cwcMax);
237
238/**
239 * Find the length of a zero-terminated byte string, given
240 * a max string length.
241 *
242 * @returns IPRT status code.
243 * @retval VINF_SUCCESS if the string has a length less than cchMax.
244 * @retval VERR_BUFFER_OVERFLOW if the end of the string wasn't found
245 * before cwcMax was reached.
246 *
247 * @param pwszString The string.
248 * @param cwcMax The max string length in RTUTF16s.
249 * @param pcwc Where to store the string length excluding the
250 * terminator. This is set to cwcMax if the terminator
251 * isn't found.
252 * @sa RTUtf16NLen, RTStrNLenEx.
253 */
254RTDECL(int) RTUtf16NLenEx(PCRTUTF16 pwszString, size_t cwcMax, size_t *pcwc);
255
256/**
257 * Find the zero terminator in a string with a limited length.
258 *
259 * @returns Pointer to the zero terminator.
260 * @returns NULL if the zero terminator was not found.
261 *
262 * @param pwszString The string.
263 * @param cwcMax The max string length. RTSTR_MAX is fine.
264 */
265RTDECL(PCRTUTF16) RTUtf16End(PCRTUTF16 pwszString, size_t cwcMax);
266
267/**
268 * Finds a give UTF-16 character in a UTF-16 string.
269 *
270 * @returns Pointer to the first occurence of @a wc.
271 * @returns NULL if @a wc was not found.
272 *
273 * @param pwszString The string to search.
274 * @param wc The UTF-16 character to search for.
275 */
276RTDECL(PRTUTF16) RTUtf16Chr(PCRTUTF16 pwszString, RTUTF16 wc);
277
278/**
279 * Strips blankspaces from both ends of the string.
280 *
281 * @returns Pointer to first non-blank char in the string.
282 * @param pwsz The string to strip.
283 */
284RTDECL(PRTUTF16) RTUtf16Strip(PRTUTF16 pwsz);
285
286/**
287 * Strips blankspaces from the start of the string.
288 *
289 * @returns Pointer to first non-blank char in the string.
290 * @param pwsz The string to strip.
291 */
292RTDECL(PRTUTF16) RTUtf16StripL(PCRTUTF16 pwsz);
293
294/**
295 * Strips blankspaces from the end of the string.
296 *
297 * @returns pwsz.
298 * @param pwsz The string to strip.
299 */
300RTDECL(PRTUTF16) RTUtf16StripR(PRTUTF16 pwsz);
301
302/**
303 * String copy with overflow handling.
304 *
305 * @retval VINF_SUCCESS on success.
306 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
307 * buffer will contain as much of the string as it can hold, fully
308 * terminated.
309 *
310 * @param pwszDst The destination buffer.
311 * @param cwcDst The size of the destination buffer in RTUTF16s.
312 * @param pwszSrc The source string. NULL is not OK.
313 */
314RTDECL(int) RTUtf16Copy(PRTUTF16 pwszDst, size_t cwcDst, PCRTUTF16 pwszSrc);
315
316/**
317 * String copy with overflow handling, ASCII source.
318 *
319 * @retval VINF_SUCCESS on success.
320 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
321 * buffer will contain as much of the string as it can hold, fully
322 * terminated.
323 *
324 * @param pwszDst The destination buffer.
325 * @param cwcDst The size of the destination buffer in RTUTF16s.
326 * @param pszSrc The source string, pure ASCII. NULL is not OK.
327 */
328RTDECL(int) RTUtf16CopyAscii(PRTUTF16 pwszDst, size_t cwcDst, const char *pszSrc);
329
330/**
331 * String copy with overflow handling.
332 *
333 * @retval VINF_SUCCESS on success.
334 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
335 * buffer will contain as much of the string as it can hold, fully
336 * terminated.
337 *
338 * @param pwszDst The destination buffer.
339 * @param cwcDst The size of the destination buffer in RTUTF16s.
340 * @param pwszSrc The source string. NULL is not OK.
341 * @param cwcSrcMax The maximum number of chars (not code points) to
342 * copy from the source string, not counting the
343 * terminator as usual.
344 */
345RTDECL(int) RTUtf16CopyEx(PRTUTF16 pwszDst, size_t cwcDst, PCRTUTF16 pwszSrc, size_t cwcSrcMax);
346
347/**
348 * String concatenation with overflow handling.
349 *
350 * @retval VINF_SUCCESS on success.
351 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
352 * buffer will contain as much of the string as it can hold, fully
353 * terminated.
354 *
355 * @param pwszDst The destination buffer.
356 * @param cwcDst The size of the destination buffer in RTUTF16s.
357 * @param pwszSrc The source string. NULL is not OK.
358 */
359RTDECL(int) RTUtf16Cat(PRTUTF16 pwszDst, size_t cwcDst, PCRTUTF16 pwszSrc);
360
361/**
362 * String concatenation with overflow handling, ASCII source.
363 *
364 * @retval VINF_SUCCESS on success.
365 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
366 * buffer will contain as much of the string as it can hold, fully
367 * terminated.
368 *
369 * @param pwszDst The destination buffer.
370 * @param cwcDst The size of the destination buffer in RTUTF16s.
371 * @param pszSrc The source string, pure ASCII. NULL is not OK.
372 */
373RTDECL(int) RTUtf16CatAscii(PRTUTF16 pwszDst, size_t cwcDst, const char *pszSrc);
374
375/**
376 * String concatenation with overflow handling.
377 *
378 * @retval VINF_SUCCESS on success.
379 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
380 * buffer will contain as much of the string as it can hold, fully
381 * terminated.
382 *
383 * @param pwszDst The destination buffer.
384 * @param cwcDst The size of the destination buffer in RTUTF16s.
385 * @param pwszSrc The source string. NULL is not OK.
386 * @param cwcSrcMax The maximum number of UTF-16 chars (not code
387 * points) to copy from the source string, not
388 * counting the terminator as usual.
389 */
390RTDECL(int) RTUtf16CatEx(PRTUTF16 pwszDst, size_t cwcDst, PCRTUTF16 pwszSrc, size_t cwcSrcMax);
391
392/**
393 * Performs a case sensitive string compare between two UTF-16 strings.
394 *
395 * @returns < 0 if the first string less than the second string.
396 * @returns 0 if the first string identical to the second string.
397 * @returns > 0 if the first string greater than the second string.
398 * @param pwsz1 First UTF-16 string. Null is allowed.
399 * @param pwsz2 Second UTF-16 string. Null is allowed.
400 * @remark This function will not make any attempt to validate the encoding.
401 */
402RTDECL(int) RTUtf16Cmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
403
404/**
405 * Performs a case sensitive string compare between an UTF-16 string and a pure
406 * ASCII string.
407 *
408 * @returns < 0 if the first string less than the second string.
409 * @returns 0 if the first string identical to the second string.
410 * @returns > 0 if the first string greater than the second string.
411 * @param pwsz1 First UTF-16 string. Null is allowed.
412 * @param psz2 Second string, pure ASCII. Null is allowed.
413 * @remark This function will not make any attempt to validate the encoding.
414 */
415RTDECL(int) RTUtf16CmpAscii(PCRTUTF16 pwsz1, const char *psz2);
416
417/**
418 * Performs a case sensitive string compare between an UTF-16 string and a UTF-8
419 * string.
420 *
421 * @returns < 0 if the first string less than the second string.
422 * @returns 0 if the first string identical to the second string.
423 * @returns > 0 if the first string greater than the second string.
424 * @param pwsz1 First UTF-16 string. Null is allowed.
425 * @param psz2 Second string, UTF-8. Null is allowed.
426 * @remarks NULL and empty strings are treated equally.
427 */
428RTDECL(int) RTUtf16CmpUtf8(PCRTUTF16 pwsz1, const char *psz2);
429
430
431/**
432 * Performs a case sensitive and length limited string compare between two UTF-16 strings.
433 *
434 * @returns < 0 if the first string less than the second string.
435 * @returns 0 if the first string identical to the second string.
436 * @returns > 0 if the first string greater than the second string.
437 * @param pwsz1 First UTF-16 string. Null is allowed.
438 * @param pwsz2 Second UTF-16 string. Null is allowed.
439 * @param cwcMax Maximum number of characters (RTUTF16) from the first
440 * @remark This function will not make any attempt to validate the encoding.
441 */
442RTDECL(int) RTUtf16NCmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2, size_t cwcMax);
443
444/**
445 * Performs a case sensitive and length limited string compare between an UTF-16
446 * string and a pure ASCII string.
447 *
448 * @returns < 0 if the first string less than the second string.
449 * @returns 0 if the first string identical to the second string.
450 * @returns > 0 if the first string greater than the second string.
451 * @param pwsz1 First UTF-16 string. Null is allowed.
452 * @param psz2 Second string, pure ASCII. Null is allowed.
453 * @param cwcMax Maximum number of characters (RTUTF16) to compare.
454 * @remark This function will not make any attempt to validate the encoding.
455 */
456RTDECL(int) RTUtf16NCmpAscii(PCRTUTF16 pwsz1, const char *psz2, size_t cwcMax);
457
458/**
459 * Performs a case sensitive and length limited string compare between an UTF-16
460 * string and a UTF-8 string.
461 *
462 * @returns < 0 if the first string less than the second string.
463 * @returns 0 if the first string identical to the second string.
464 * @returns > 0 if the first string greater than the second string.
465 * @param pwsz1 First UTF-16 string. Null is allowed.
466 * @param psz2 Second string, UTF-8. Null is allowed.
467 * @param cwcMax1 Maximum number of UTF-16 characters (RTUTF16) from the
468 * first string to compare.
469 * @param cchMax2 Maximum number of UTF-8 characters (char) from the
470 * second string to compare.
471 * @remarks NULL and empty strings are treated equally.
472 */
473RTDECL(int) RTUtf16NCmpUtf8(PCRTUTF16 pwsz1, const char *psz2, size_t cwcMax1, size_t cchMax2);
474
475
476/**
477 * Performs a case insensitive string compare between two UTF-16 strings.
478 *
479 * This is a simplified compare, as only the simplified lower/upper case folding
480 * specified by the unicode specs are used. It does not consider character pairs
481 * as they are used in some languages, just simple upper & lower case compares.
482 *
483 * @returns < 0 if the first string less than the second string.
484 * @returns 0 if the first string identical to the second string.
485 * @returns > 0 if the first string greater than the second string.
486 * @param pwsz1 First UTF-16 string. Null is allowed.
487 * @param pwsz2 Second UTF-16 string. Null is allowed.
488 */
489RTDECL(int) RTUtf16ICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
490
491/**
492 * Performs a case insensitive string compare between two big endian UTF-16
493 * strings.
494 *
495 * This is a simplified compare, as only the simplified lower/upper case folding
496 * specified by the unicode specs are used. It does not consider character pairs
497 * as they are used in some languages, just simple upper & lower case compares.
498 *
499 * @returns < 0 if the first string less than the second string.
500 * @returns 0 if the first string identical to the second string.
501 * @returns > 0 if the first string greater than the second string.
502 * @param pwsz1 First big endian UTF-16 string. Null is allowed.
503 * @param pwsz2 Second big endian UTF-16 string. Null is allowed.
504 */
505RTDECL(int) RTUtf16BigICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
506
507/**
508 * Performs a case insensitive string compare between an UTF-16 string and a
509 * UTF-8 string.
510 *
511 * @returns < 0 if the first string less than the second string.s
512 * @returns 0 if the first string identical to the second string.
513 * @returns > 0 if the first string greater than the second string.
514 * @param pwsz1 First UTF-16 string. Null is allowed.
515 * @param psz2 Second string, UTF-8. Null is allowed.
516 * @remarks NULL and empty strings are treated equally.
517 */
518RTDECL(int) RTUtf16ICmpUtf8(PCRTUTF16 pwsz1, const char *psz2);
519
520/**
521 * Performs a case insensitive string compare between an UTF-16 string and a
522 * pure ASCII string.
523 *
524 * Since this compare only takes cares about the first 128 codepoints in
525 * unicode, no tables are needed and there aren't any real complications.
526 *
527 * @returns < 0 if the first string less than the second string.
528 * @returns 0 if the first string identical to the second string.
529 * @returns > 0 if the first string greater than the second string.
530 * @param pwsz1 First UTF-16 string. Null is allowed.
531 * @param psz2 Second string, pure ASCII. Null is allowed.
532 */
533RTDECL(int) RTUtf16ICmpAscii(PCRTUTF16 pwsz1, const char *psz2);
534
535/**
536 * Performs a case insensitive string compare between two UTF-16 strings
537 * using the current locale of the process (if applicable).
538 *
539 * This differs from RTUtf16ICmp() in that it will try, if a locale with the
540 * required data is available, to do a correct case-insensitive compare. It
541 * follows that it is more complex and thereby likely to be more expensive.
542 *
543 * @returns < 0 if the first string less than the second string.
544 * @returns 0 if the first string identical to the second string.
545 * @returns > 0 if the first string greater than the second string.
546 * @param pwsz1 First UTF-16 string. Null is allowed.
547 * @param pwsz2 Second UTF-16 string. Null is allowed.
548 */
549RTDECL(int) RTUtf16LocaleICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
550
551/**
552 * Performs a case insensitive string compare between two UTF-16 strings,
553 * stopping after N characters.
554 *
555 * This is a simplified compare, as only the simplified lower/upper case folding
556 * specified by the unicode specs are used. It does not consider character pairs
557 * as they are used in some languages, just simple upper & lower case compares.
558 *
559 * @returns < 0 if the first string less than the second string.
560 * @returns 0 if the first string identical to the second string.
561 * @returns > 0 if the first string greater than the second string.
562 * @param pwsz1 First UTF-16 string. Null is allowed.
563 * @param pwsz2 Second UTF-16 string. Null is allowed.
564 * @param cwcMax Maximum number of characters to compare.
565 */
566RTDECL(int) RTUtf16NICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2, size_t cwcMax);
567
568/**
569 * Performs a case insensitive string compare between two big endian UTF-16
570 * strings, stopping after N characters.
571 *
572 * This is a simplified compare, as only the simplified lower/upper case folding
573 * specified by the unicode specs are used. It does not consider character pairs
574 * as they are used in some languages, just simple upper & lower case compares.
575 *
576 * @returns < 0 if the first string less than the second string.
577 * @returns 0 if the first string identical to the second string.
578 * @returns > 0 if the first string greater than the second string.
579 * @param pwsz1 First big endian UTF-16 string. Null is allowed.
580 * @param pwsz2 Second big endian UTF-16 string. Null is allowed.
581 * @param cwcMax Maximum number of characters to compare.
582 */
583RTDECL(int) RTUtf16BigNICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2, size_t cwcMax);
584
585/**
586 * Performs a case insensitive string compare between a UTF-16 string and a pure
587 * ASCII string, stopping after N characters.
588 *
589 * Since this compare only takes cares about the first 128 codepoints in
590 * unicode, no tables are needed and there aren't any real complications.
591 *
592 * @returns < 0 if the first string less than the second string.
593 * @returns 0 if the first string identical to the second string.
594 * @returns > 0 if the first string greater than the second string.
595 * @param pwsz1 The UTF-16 first string. Null is allowed.
596 * @param psz2 The pure ASCII second string. Null is allowed.
597 * @param cwcMax Maximum number of UTF-16 characters to compare.
598 */
599RTDECL(int) RTUtf16NICmpAscii(PCRTUTF16 pwsz1, const char *psz2, size_t cwcMax);
600
601
602/**
603 * Folds a UTF-16 string to lowercase.
604 *
605 * This is a very simple folding; is uses the simple lowercase
606 * code point, it is not related to any locale just the most common
607 * lowercase codepoint setup by the unicode specs, and it will not
608 * create new surrogate pairs or remove existing ones.
609 *
610 * @returns Pointer to the passed in string.
611 * @param pwsz The string to fold.
612 */
613RTDECL(PRTUTF16) RTUtf16ToLower(PRTUTF16 pwsz);
614
615/**
616 * Folds a UTF-16 string to uppercase.
617 *
618 * This is a very simple folding; is uses the simple uppercase
619 * code point, it is not related to any locale just the most common
620 * uppercase codepoint setup by the unicode specs, and it will not
621 * create new surrogate pairs or remove existing ones.
622 *
623 * @returns Pointer to the passed in string.
624 * @param pwsz The string to fold.
625 */
626RTDECL(PRTUTF16) RTUtf16ToUpper(PRTUTF16 pwsz);
627
628/**
629 * Validates the UTF-16 encoding of the string.
630 *
631 * @returns iprt status code.
632 * @param pwsz The string.
633 */
634RTDECL(int) RTUtf16ValidateEncoding(PCRTUTF16 pwsz);
635
636/**
637 * Validates the UTF-16 encoding of the string.
638 *
639 * @returns iprt status code.
640 * @param pwsz The string.
641 * @param cwc The max string length (/ size) in UTF-16 units. Use
642 * RTSTR_MAX to process the entire string.
643 * @param fFlags Combination of RTSTR_VALIDATE_ENCODING_XXX flags.
644 */
645RTDECL(int) RTUtf16ValidateEncodingEx(PCRTUTF16 pwsz, size_t cwc, uint32_t fFlags);
646
647/**
648 * Checks if the UTF-16 encoding is valid.
649 *
650 * @returns true / false.
651 * @param pwsz The string.
652 */
653RTDECL(bool) RTUtf16IsValidEncoding(PCRTUTF16 pwsz);
654
655/**
656 * Sanitise a (valid) UTF-16 string by replacing all characters outside a white
657 * list in-place by an ASCII replacement character.
658 *
659 * Surrogate paris will be replaced by two chars.
660 *
661 * @returns The number of code points replaced. In the case of an incorrectly
662 * encoded string -1 will be returned, and the string is not completely
663 * processed. In the case of puszValidPairs having an odd number of
664 * code points, -1 will be also return but without any modification to
665 * the string.
666 * @param pwsz The string to sanitise.
667 * @param puszValidPairs A zero-terminated array of pairs of Unicode points.
668 * Each pair is the start and end point of a range,
669 * and the union of these ranges forms the white list.
670 * @param chReplacement The ASCII replacement character.
671 * @sa RTStrPurgeComplementSet
672 */
673RTDECL(ssize_t) RTUtf16PurgeComplementSet(PRTUTF16 pwsz, PCRTUNICP puszValidPairs, char chReplacement);
674
675
676/**
677 * Translate a UTF-16 string into a UTF-8 allocating the result buffer (default
678 * tag).
679 *
680 * @returns iprt status code.
681 * @param pwszString UTF-16 string to convert.
682 * @param ppszString Receives pointer of allocated UTF-8 string on
683 * success, and is always set to NULL on failure.
684 * The returned pointer must be freed using RTStrFree().
685 */
686#define RTUtf16ToUtf8(pwszString, ppszString) RTUtf16ToUtf8Tag((pwszString), (ppszString), RTSTR_TAG)
687
688/**
689 * Translate a UTF-16 string into a UTF-8 allocating the result buffer.
690 *
691 * @returns iprt status code.
692 * @param pwszString UTF-16 string to convert.
693 * @param ppszString Receives pointer of allocated UTF-8 string on
694 * success, and is always set to NULL on failure.
695 * The returned pointer must be freed using RTStrFree().
696 * @param pszTag Allocation tag used for statistics and such.
697 */
698RTDECL(int) RTUtf16ToUtf8Tag(PCRTUTF16 pwszString, char **ppszString, const char *pszTag);
699
700/**
701 * Translate a UTF-16BE string into a UTF-8 allocating the result buffer
702 * (default tag).
703 *
704 * This differs from RTUtf16ToUtf8 in that the input is always a
705 * big-endian string.
706 *
707 * @returns iprt status code.
708 * @param pwszString UTF-16BE string to convert.
709 * @param ppszString Receives pointer of allocated UTF-8 string on
710 * success, and is always set to NULL on failure.
711 * The returned pointer must be freed using RTStrFree().
712 */
713#define RTUtf16BigToUtf8(pwszString, ppszString) RTUtf16BigToUtf8Tag((pwszString), (ppszString), RTSTR_TAG)
714
715/**
716 * Translate a UTF-16BE string into a UTF-8 allocating the result buffer.
717 *
718 * This differs from RTUtf16ToUtf8Tag in that the input is always a
719 * big-endian string.
720 *
721 * @returns iprt status code.
722 * @param pwszString UTF-16BE string to convert.
723 * @param ppszString Receives pointer of allocated UTF-8 string on
724 * success, and is always set to NULL on failure.
725 * The returned pointer must be freed using RTStrFree().
726 * @param pszTag Allocation tag used for statistics and such.
727 */
728RTDECL(int) RTUtf16BigToUtf8Tag(PCRTUTF16 pwszString, char **ppszString, const char *pszTag);
729
730/**
731 * Translate a UTF-16LE string into a UTF-8 allocating the result buffer
732 * (default tag).
733 *
734 * This differs from RTUtf16ToUtf8 in that the input is always a
735 * little-endian string.
736 *
737 * @returns iprt status code.
738 * @param pwszString UTF-16LE string to convert.
739 * @param ppszString Receives pointer of allocated UTF-8 string on
740 * success, and is always set to NULL on failure.
741 * The returned pointer must be freed using RTStrFree().
742 */
743#define RTUtf16LittleToUtf8(pwszString, ppszString) RTUtf16LittleToUtf8Tag((pwszString), (ppszString), RTSTR_TAG)
744
745/**
746 * Translate a UTF-16LE string into a UTF-8 allocating the result buffer.
747 *
748 * This differs from RTUtf16ToUtf8Tag in that the input is always a
749 * little-endian string.
750 *
751 * @returns iprt status code.
752 * @param pwszString UTF-16LE string to convert.
753 * @param ppszString Receives pointer of allocated UTF-8 string on
754 * success, and is always set to NULL on failure.
755 * The returned pointer must be freed using RTStrFree().
756 * @param pszTag Allocation tag used for statistics and such.
757 */
758RTDECL(int) RTUtf16LittleToUtf8Tag(PCRTUTF16 pwszString, char **ppszString, const char *pszTag);
759
760
761/**
762 * Translates UTF-16 to UTF-8 using buffer provided by the caller or a fittingly
763 * sized buffer allocated by the function (default tag).
764 *
765 * @returns iprt status code.
766 * @param pwszString The UTF-16 string to convert.
767 * @param cwcString The number of RTUTF16 items to translate from pwszString.
768 * The translation will stop when reaching cwcString or the terminator ('\\0').
769 * Use RTSTR_MAX to translate the entire string.
770 * @param ppsz If cch is non-zero, this must either be pointing to a pointer to
771 * a buffer of the specified size, or pointer to a NULL pointer.
772 * If *ppsz is NULL or cch is zero a buffer of at least cch chars
773 * will be allocated to hold the translated string.
774 * If a buffer was requested it must be freed using RTStrFree().
775 * @param cch The buffer size in chars (the type). This includes the terminator.
776 * @param pcch Where to store the length of the translated string,
777 * excluding the terminator. (Optional)
778 *
779 * This may be set under some error conditions,
780 * however, only for VERR_BUFFER_OVERFLOW and
781 * VERR_NO_STR_MEMORY will it contain a valid string
782 * length that can be used to resize the buffer.
783 */
784#define RTUtf16ToUtf8Ex(pwszString, cwcString, ppsz, cch, pcch) \
785 RTUtf16ToUtf8ExTag((pwszString), (cwcString), (ppsz), (cch), (pcch), RTSTR_TAG)
786
787/**
788 * Translates UTF-16 to UTF-8 using buffer provided by the caller or a fittingly
789 * sized buffer allocated by the function (custom tag).
790 *
791 * @returns iprt status code.
792 * @param pwszString The UTF-16 string to convert.
793 * @param cwcString The number of RTUTF16 items to translate from pwszString.
794 * The translation will stop when reaching cwcString or the terminator ('\\0').
795 * Use RTSTR_MAX to translate the entire string.
796 * @param ppsz If cch is non-zero, this must either be pointing to a pointer to
797 * a buffer of the specified size, or pointer to a NULL pointer.
798 * If *ppsz is NULL or cch is zero a buffer of at least cch chars
799 * will be allocated to hold the translated string.
800 * If a buffer was requested it must be freed using RTStrFree().
801 * @param cch The buffer size in chars (the type). This includes the terminator.
802 * @param pcch Where to store the length of the translated string,
803 * excluding the terminator. (Optional)
804 *
805 * This may be set under some error conditions,
806 * however, only for VERR_BUFFER_OVERFLOW and
807 * VERR_NO_STR_MEMORY will it contain a valid string
808 * length that can be used to resize the buffer.
809 * @param pszTag Allocation tag used for statistics and such.
810 */
811RTDECL(int) RTUtf16ToUtf8ExTag(PCRTUTF16 pwszString, size_t cwcString, char **ppsz, size_t cch, size_t *pcch, const char *pszTag);
812
813/**
814 * Translates UTF-16BE to UTF-8 using buffer provided by the caller or a
815 * fittingly sized buffer allocated by the function (default tag).
816 *
817 * This differs from RTUtf16ToUtf8Ex in that the input is always a
818 * big-endian string.
819 *
820 * @returns iprt status code.
821 * @param pwszString The UTF-16BE string to convert.
822 * @param cwcString The number of RTUTF16 items to translate from pwszString.
823 * The translation will stop when reaching cwcString or the terminator ('\\0').
824 * Use RTSTR_MAX to translate the entire string.
825 * @param ppsz If cch is non-zero, this must either be pointing to a pointer to
826 * a buffer of the specified size, or pointer to a NULL pointer.
827 * If *ppsz is NULL or cch is zero a buffer of at least cch chars
828 * will be allocated to hold the translated string.
829 * If a buffer was requested it must be freed using RTStrFree().
830 * @param cch The buffer size in chars (the type). This includes the terminator.
831 * @param pcch Where to store the length of the translated string,
832 * excluding the terminator. (Optional)
833 *
834 * This may be set under some error conditions,
835 * however, only for VERR_BUFFER_OVERFLOW and
836 * VERR_NO_STR_MEMORY will it contain a valid string
837 * length that can be used to resize the buffer.
838 */
839#define RTUtf16BigToUtf8Ex(pwszString, cwcString, ppsz, cch, pcch) \
840 RTUtf16BigToUtf8ExTag((pwszString), (cwcString), (ppsz), (cch), (pcch), RTSTR_TAG)
841
842/**
843 * Translates UTF-16BE to UTF-8 using buffer provided by the caller or a
844 * fittingly sized buffer allocated by the function (custom tag).
845 *
846 * This differs from RTUtf16ToUtf8ExTag in that the input is always a
847 * big-endian string.
848 *
849 * @returns iprt status code.
850 * @param pwszString The UTF-16BE string to convert.
851 * @param cwcString The number of RTUTF16 items to translate from pwszString.
852 * The translation will stop when reaching cwcString or the terminator ('\\0').
853 * Use RTSTR_MAX to translate the entire string.
854 * @param ppsz If cch is non-zero, this must either be pointing to a pointer to
855 * a buffer of the specified size, or pointer to a NULL pointer.
856 * If *ppsz is NULL or cch is zero a buffer of at least cch chars
857 * will be allocated to hold the translated string.
858 * If a buffer was requested it must be freed using RTStrFree().
859 * @param cch The buffer size in chars (the type). This includes the terminator.
860 * @param pcch Where to store the length of the translated string,
861 * excluding the terminator. (Optional)
862 *
863 * This may be set under some error conditions,
864 * however, only for VERR_BUFFER_OVERFLOW and
865 * VERR_NO_STR_MEMORY will it contain a valid string
866 * length that can be used to resize the buffer.
867 * @param pszTag Allocation tag used for statistics and such.
868 */
869RTDECL(int) RTUtf16BigToUtf8ExTag(PCRTUTF16 pwszString, size_t cwcString, char **ppsz, size_t cch, size_t *pcch, const char *pszTag);
870
871/**
872 * Translates UTF-16LE to UTF-8 using buffer provided by the caller or a
873 * fittingly sized buffer allocated by the function (default tag).
874 *
875 * This differs from RTUtf16ToUtf8Ex in that the input is always a
876 * little-endian string.
877 *
878 * @returns iprt status code.
879 * @param pwszString The UTF-16LE string to convert.
880 * @param cwcString The number of RTUTF16 items to translate from pwszString.
881 * The translation will stop when reaching cwcString or the terminator ('\\0').
882 * Use RTSTR_MAX to translate the entire string.
883 * @param ppsz If cch is non-zero, this must either be pointing to a pointer to
884 * a buffer of the specified size, or pointer to a NULL pointer.
885 * If *ppsz is NULL or cch is zero a buffer of at least cch chars
886 * will be allocated to hold the translated string.
887 * If a buffer was requested it must be freed using RTStrFree().
888 * @param cch The buffer size in chars (the type). This includes the terminator.
889 * @param pcch Where to store the length of the translated string,
890 * excluding the terminator. (Optional)
891 *
892 * This may be set under some error conditions,
893 * however, only for VERR_BUFFER_OVERFLOW and
894 * VERR_NO_STR_MEMORY will it contain a valid string
895 * length that can be used to resize the buffer.
896 */
897#define RTUtf16LittleToUtf8Ex(pwszString, cwcString, ppsz, cch, pcch) \
898 RTUtf16LittleToUtf8ExTag((pwszString), (cwcString), (ppsz), (cch), (pcch), RTSTR_TAG)
899
900/**
901 * Translates UTF-16LE to UTF-8 using buffer provided by the caller or a
902 * fittingly sized buffer allocated by the function (custom tag).
903 *
904 * This differs from RTUtf16ToUtf8ExTag in that the input is always a
905 * little-endian string.
906 *
907 * @returns iprt status code.
908 * @param pwszString The UTF-16LE string to convert.
909 * @param cwcString The number of RTUTF16 items to translate from pwszString.
910 * The translation will stop when reaching cwcString or the terminator ('\\0').
911 * Use RTSTR_MAX to translate the entire string.
912 * @param ppsz If cch is non-zero, this must either be pointing to a pointer to
913 * a buffer of the specified size, or pointer to a NULL pointer.
914 * If *ppsz is NULL or cch is zero a buffer of at least cch chars
915 * will be allocated to hold the translated string.
916 * If a buffer was requested it must be freed using RTStrFree().
917 * @param cch The buffer size in chars (the type). This includes the terminator.
918 * @param pcch Where to store the length of the translated string,
919 * excluding the terminator. (Optional)
920 *
921 * This may be set under some error conditions,
922 * however, only for VERR_BUFFER_OVERFLOW and
923 * VERR_NO_STR_MEMORY will it contain a valid string
924 * length that can be used to resize the buffer.
925 * @param pszTag Allocation tag used for statistics and such.
926 */
927RTDECL(int) RTUtf16LittleToUtf8ExTag(PCRTUTF16 pwszString, size_t cwcString, char **ppsz, size_t cch, size_t *pcch,
928 const char *pszTag);
929
930/**
931 * Calculates the length of the UTF-16 string in UTF-8 chars (bytes).
932 *
933 * This function will validate the string, and incorrectly encoded UTF-16
934 * strings will be rejected. The primary purpose of this function is to
935 * help allocate buffers for RTUtf16ToUtf8() of the correct size. For most
936 * other purposes RTUtf16ToUtf8Ex() should be used.
937 *
938 * @returns Number of char (bytes).
939 * @returns 0 if the string was incorrectly encoded.
940 * @param pwsz The UTF-16 string.
941 */
942RTDECL(size_t) RTUtf16CalcUtf8Len(PCRTUTF16 pwsz);
943
944/**
945 * Calculates the length of the UTF-16BE string in UTF-8 chars (bytes).
946 *
947 * This function will validate the string, and incorrectly encoded UTF-16BE
948 * strings will be rejected. The primary purpose of this function is to
949 * help allocate buffers for RTUtf16BigToUtf8() of the correct size. For most
950 * other purposes RTUtf16BigToUtf8Ex() should be used.
951 *
952 * @returns Number of char (bytes).
953 * @returns 0 if the string was incorrectly encoded.
954 * @param pwsz The UTF-16BE string.
955 */
956RTDECL(size_t) RTUtf16BigCalcUtf8Len(PCRTUTF16 pwsz);
957
958/**
959 * Calculates the length of the UTF-16LE string in UTF-8 chars (bytes).
960 *
961 * This function will validate the string, and incorrectly encoded UTF-16LE
962 * strings will be rejected. The primary purpose of this function is to
963 * help allocate buffers for RTUtf16LittleToUtf8() of the correct size. For
964 * most other purposes RTUtf16LittleToUtf8Ex() should be used.
965 *
966 * @returns Number of char (bytes).
967 * @returns 0 if the string was incorrectly encoded.
968 * @param pwsz The UTF-16LE string.
969 */
970RTDECL(size_t) RTUtf16LittleCalcUtf8Len(PCRTUTF16 pwsz);
971
972/**
973 * Calculates the length of the UTF-16 string in UTF-8 chars (bytes).
974 *
975 * This function will validate the string, and incorrectly encoded UTF-16
976 * strings will be rejected.
977 *
978 * @returns iprt status code.
979 * @param pwsz The string.
980 * @param cwc The max string length. Use RTSTR_MAX to process the entire string.
981 * @param pcch Where to store the string length (in bytes). Optional.
982 * This is undefined on failure.
983 */
984RTDECL(int) RTUtf16CalcUtf8LenEx(PCRTUTF16 pwsz, size_t cwc, size_t *pcch);
985
986/**
987 * Calculates the length of the UTF-16BE string in UTF-8 chars (bytes).
988 *
989 * This function will validate the string, and incorrectly encoded UTF-16BE
990 * strings will be rejected.
991 *
992 * @returns iprt status code.
993 * @param pwsz The string.
994 * @param cwc The max string length. Use RTSTR_MAX to process the entire string.
995 * @param pcch Where to store the string length (in bytes). Optional.
996 * This is undefined on failure.
997 */
998RTDECL(int) RTUtf16BigCalcUtf8LenEx(PCRTUTF16 pwsz, size_t cwc, size_t *pcch);
999
1000/**
1001 * Calculates the length of the UTF-16LE string in UTF-8 chars (bytes).
1002 *
1003 * This function will validate the string, and incorrectly encoded UTF-16LE
1004 * strings will be rejected.
1005 *
1006 * @returns iprt status code.
1007 * @param pwsz The string.
1008 * @param cwc The max string length. Use RTSTR_MAX to process the entire string.
1009 * @param pcch Where to store the string length (in bytes). Optional.
1010 * This is undefined on failure.
1011 */
1012RTDECL(int) RTUtf16LittleCalcUtf8LenEx(PCRTUTF16 pwsz, size_t cwc, size_t *pcch);
1013
1014/**
1015 * Translate a UTF-16 string into a Latin-1 (ISO-8859-1) allocating the result
1016 * buffer (default tag).
1017 *
1018 * @returns iprt status code.
1019 * @param pwszString UTF-16 string to convert.
1020 * @param ppszString Receives pointer of allocated Latin1 string on
1021 * success, and is always set to NULL on failure.
1022 * The returned pointer must be freed using RTStrFree().
1023 */
1024#define RTUtf16ToLatin1(pwszString, ppszString) RTUtf16ToLatin1Tag((pwszString), (ppszString), RTSTR_TAG)
1025
1026/**
1027 * Translate a UTF-16 string into a Latin-1 (ISO-8859-1) allocating the result
1028 * buffer (custom tag).
1029 *
1030 * @returns iprt status code.
1031 * @param pwszString UTF-16 string to convert.
1032 * @param ppszString Receives pointer of allocated Latin1 string on
1033 * success, and is always set to NULL on failure.
1034 * The returned pointer must be freed using RTStrFree().
1035 * @param pszTag Allocation tag used for statistics and such.
1036 */
1037RTDECL(int) RTUtf16ToLatin1Tag(PCRTUTF16 pwszString, char **ppszString, const char *pszTag);
1038
1039/**
1040 * Translates UTF-16 to Latin-1 (ISO-8859-1) using buffer provided by the caller
1041 * or a fittingly sized buffer allocated by the function (default tag).
1042 *
1043 * @returns iprt status code.
1044 * @param pwszString The UTF-16 string to convert.
1045 * @param cwcString The number of RTUTF16 items to translate from
1046 * pwszString. The translation will stop when reaching
1047 * cwcString or the terminator ('\\0'). Use RTSTR_MAX
1048 * to translate the entire string.
1049 * @param ppsz Pointer to the pointer to the Latin-1 string. The
1050 * buffer can optionally be preallocated by the caller.
1051 *
1052 * If cch is zero, *ppsz is undefined.
1053 *
1054 * If cch is non-zero and *ppsz is not NULL, then this
1055 * will be used as the output buffer.
1056 * VERR_BUFFER_OVERFLOW will be returned if this is
1057 * insufficient.
1058 *
1059 * If cch is zero or *ppsz is NULL, then a buffer of
1060 * sufficient size is allocated. cch can be used to
1061 * specify a minimum size of this buffer. Use
1062 * RTUtf16Free() to free the result.
1063 *
1064 * @param cch The buffer size in chars (the type). This includes
1065 * the terminator.
1066 * @param pcch Where to store the length of the translated string,
1067 * excluding the terminator. (Optional)
1068 *
1069 * This may be set under some error conditions,
1070 * however, only for VERR_BUFFER_OVERFLOW and
1071 * VERR_NO_STR_MEMORY will it contain a valid string
1072 * length that can be used to resize the buffer.
1073 */
1074#define RTUtf16ToLatin1Ex(pwszString, cwcString, ppsz, cch, pcch) \
1075 RTUtf16ToLatin1ExTag((pwszString), (cwcString), (ppsz), (cch), (pcch), RTSTR_TAG)
1076
1077/**
1078 * Translates UTF-16 to Latin-1 (ISO-8859-1) using buffer provided by the caller
1079 * or a fittingly sized buffer allocated by the function (custom tag).
1080 *
1081 * @returns iprt status code.
1082 * @param pwszString The UTF-16 string to convert.
1083 * @param cwcString The number of RTUTF16 items to translate from
1084 * pwszString. The translation will stop when reaching
1085 * cwcString or the terminator ('\\0'). Use RTSTR_MAX
1086 * to translate the entire string.
1087 * @param ppsz Pointer to the pointer to the Latin-1 string. The
1088 * buffer can optionally be preallocated by the caller.
1089 *
1090 * If cch is zero, *ppsz is undefined.
1091 *
1092 * If cch is non-zero and *ppsz is not NULL, then this
1093 * will be used as the output buffer.
1094 * VERR_BUFFER_OVERFLOW will be returned if this is
1095 * insufficient.
1096 *
1097 * If cch is zero or *ppsz is NULL, then a buffer of
1098 * sufficient size is allocated. cch can be used to
1099 * specify a minimum size of this buffer. Use
1100 * RTUtf16Free() to free the result.
1101 *
1102 * @param cch The buffer size in chars (the type). This includes
1103 * the terminator.
1104 * @param pcch Where to store the length of the translated string,
1105 * excluding the terminator. (Optional)
1106 *
1107 * This may be set under some error conditions,
1108 * however, only for VERR_BUFFER_OVERFLOW and
1109 * VERR_NO_STR_MEMORY will it contain a valid string
1110 * length that can be used to resize the buffer.
1111 * @param pszTag Allocation tag used for statistics and such.
1112 */
1113RTDECL(int) RTUtf16ToLatin1ExTag(PCRTUTF16 pwszString, size_t cwcString, char **ppsz, size_t cch, size_t *pcch, const char *pszTag);
1114
1115/**
1116 * Calculates the length of the UTF-16 string in Latin-1 (ISO-8859-1) chars.
1117 *
1118 * This function will validate the string, and incorrectly encoded UTF-16
1119 * strings will be rejected. The primary purpose of this function is to
1120 * help allocate buffers for RTUtf16ToLatin1() of the correct size. For most
1121 * other purposes RTUtf16ToLatin1Ex() should be used.
1122 *
1123 * @returns Number of char (bytes).
1124 * @returns 0 if the string was incorrectly encoded.
1125 * @param pwsz The UTF-16 string.
1126 */
1127RTDECL(size_t) RTUtf16CalcLatin1Len(PCRTUTF16 pwsz);
1128
1129/**
1130 * Calculates the length of the UTF-16 string in Latin-1 (ISO-8859-1) chars.
1131 *
1132 * This function will validate the string, and incorrectly encoded UTF-16
1133 * strings will be rejected.
1134 *
1135 * @returns iprt status code.
1136 * @param pwsz The string.
1137 * @param cwc The max string length. Use RTSTR_MAX to process the
1138 * entire string.
1139 * @param pcch Where to store the string length (in bytes). Optional.
1140 * This is undefined on failure.
1141 */
1142RTDECL(int) RTUtf16CalcLatin1LenEx(PCRTUTF16 pwsz, size_t cwc, size_t *pcch);
1143
1144/**
1145 * Get the unicode code point at the given string position.
1146 *
1147 * @returns unicode code point.
1148 * @returns RTUNICP_INVALID if the encoding is invalid.
1149 * @param pwsz The string.
1150 *
1151 * @remark This is an internal worker for RTUtf16GetCp().
1152 */
1153RTDECL(RTUNICP) RTUtf16GetCpInternal(PCRTUTF16 pwsz);
1154
1155/**
1156 * Get the unicode code point at the given string position.
1157 *
1158 * @returns iprt status code.
1159 * @param ppwsz Pointer to the string pointer. This will be updated to
1160 * point to the char following the current code point.
1161 * @param pCp Where to store the code point.
1162 * RTUNICP_INVALID is stored here on failure.
1163 *
1164 * @remark This is an internal worker for RTUtf16GetCpEx().
1165 */
1166RTDECL(int) RTUtf16GetCpExInternal(PCRTUTF16 *ppwsz, PRTUNICP pCp);
1167
1168/**
1169 * Get the unicode code point at the given string position, big endian.
1170 *
1171 * @returns iprt status code.
1172 * @param ppwsz Pointer to the string pointer. This will be updated to
1173 * point to the char following the current code point.
1174 * @param pCp Where to store the code point.
1175 * RTUNICP_INVALID is stored here on failure.
1176 *
1177 * @remark This is an internal worker for RTUtf16BigGetCpEx().
1178 */
1179RTDECL(int) RTUtf16BigGetCpExInternal(PCRTUTF16 *ppwsz, PRTUNICP pCp);
1180
1181/**
1182 * Put the unicode code point at the given string position
1183 * and return the pointer to the char following it.
1184 *
1185 * This function will not consider anything at or following the
1186 * buffer area pointed to by pwsz. It is therefore not suitable for
1187 * inserting code points into a string, only appending/overwriting.
1188 *
1189 * @returns pointer to the char following the written code point.
1190 * @param pwsz The string.
1191 * @param CodePoint The code point to write.
1192 * This should not be RTUNICP_INVALID or any other
1193 * character out of the UTF-16 range.
1194 *
1195 * @remark This is an internal worker for RTUtf16GetCpEx().
1196 */
1197RTDECL(PRTUTF16) RTUtf16PutCpInternal(PRTUTF16 pwsz, RTUNICP CodePoint);
1198
1199/**
1200 * Get the unicode code point at the given string position.
1201 *
1202 * @returns unicode code point.
1203 * @returns RTUNICP_INVALID if the encoding is invalid.
1204 * @param pwsz The string.
1205 *
1206 * @remark We optimize this operation by using an inline function for
1207 * everything which isn't a surrogate pair or an endian indicator.
1208 */
1209DECLINLINE(RTUNICP) RTUtf16GetCp(PCRTUTF16 pwsz)
1210{
1211 const RTUTF16 wc = *pwsz;
1212 if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
1213 return wc;
1214 return RTUtf16GetCpInternal(pwsz);
1215}
1216
1217/**
1218 * Get the unicode code point at the given string position.
1219 *
1220 * @returns iprt status code.
1221 * @param ppwsz Pointer to the string pointer. This will be updated to
1222 * point to the char following the current code point.
1223 * @param pCp Where to store the code point.
1224 * RTUNICP_INVALID is stored here on failure.
1225 *
1226 * @remark We optimize this operation by using an inline function for
1227 * everything which isn't a surrogate pair or and endian indicator.
1228 */
1229DECLINLINE(int) RTUtf16GetCpEx(PCRTUTF16 *ppwsz, PRTUNICP pCp)
1230{
1231 const RTUTF16 wc = **ppwsz;
1232 if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
1233 {
1234 (*ppwsz)++;
1235 *pCp = wc;
1236 return VINF_SUCCESS;
1237 }
1238 return RTUtf16GetCpExInternal(ppwsz, pCp);
1239}
1240
1241/**
1242 * Get the unicode code point at the given string position, big endian version.
1243 *
1244 * @returns iprt status code.
1245 * @param ppwsz Pointer to the string pointer. This will be updated to
1246 * point to the char following the current code point.
1247 * @param pCp Where to store the code point.
1248 * RTUNICP_INVALID is stored here on failure.
1249 *
1250 * @remark We optimize this operation by using an inline function for
1251 * everything which isn't a surrogate pair or and endian indicator.
1252 */
1253DECLINLINE(int) RTUtf16BigGetCpEx(PCRTUTF16 *ppwsz, PRTUNICP pCp)
1254{
1255#ifdef RT_BIG_ENDIAN
1256 return RTUtf16GetCpEx(ppwsz, pCp);
1257#else
1258# ifdef ___iprt_asm_h
1259 const RTUTF16 wc = RT_BE2H_U16(**ppwsz);
1260 if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
1261 {
1262 (*ppwsz)++;
1263 *pCp = wc;
1264 return VINF_SUCCESS;
1265 }
1266# endif
1267 return RTUtf16BigGetCpExInternal(ppwsz, pCp);
1268#endif
1269}
1270
1271/**
1272 * Put the unicode code point at the given string position
1273 * and return the pointer to the char following it.
1274 *
1275 * This function will not consider anything at or following the
1276 * buffer area pointed to by pwsz. It is therefore not suitable for
1277 * inserting code points into a string, only appending/overwriting.
1278 *
1279 * @returns pointer to the char following the written code point.
1280 * @param pwsz The string.
1281 * @param CodePoint The code point to write.
1282 * This should not be RTUNICP_INVALID or any other
1283 * character out of the UTF-16 range.
1284 *
1285 * @remark We optimize this operation by using an inline function for
1286 * everything which isn't a surrogate pair or and endian indicator.
1287 */
1288DECLINLINE(PRTUTF16) RTUtf16PutCp(PRTUTF16 pwsz, RTUNICP CodePoint)
1289{
1290 if (CodePoint < 0xd800 || (CodePoint > 0xd800 && CodePoint < 0xfffe))
1291 {
1292 *pwsz++ = (RTUTF16)CodePoint;
1293 return pwsz;
1294 }
1295 return RTUtf16PutCpInternal(pwsz, CodePoint);
1296}
1297
1298/**
1299 * Skips ahead, past the current code point.
1300 *
1301 * @returns Pointer to the char after the current code point.
1302 * @param pwsz Pointer to the current code point.
1303 * @remark This will not move the next valid code point, only past the current one.
1304 */
1305DECLINLINE(PRTUTF16) RTUtf16NextCp(PCRTUTF16 pwsz)
1306{
1307 RTUNICP Cp;
1308 RTUtf16GetCpEx(&pwsz, &Cp);
1309 return (PRTUTF16)pwsz;
1310}
1311
1312/**
1313 * Skips backwards, to the previous code point.
1314 *
1315 * @returns Pointer to the char after the current code point.
1316 * @param pwszStart Pointer to the start of the string.
1317 * @param pwsz Pointer to the current code point.
1318 */
1319RTDECL(PRTUTF16) RTUtf16PrevCp(PCRTUTF16 pwszStart, PCRTUTF16 pwsz);
1320
1321
1322/**
1323 * Checks if the UTF-16 char is the high surrogate char (i.e.
1324 * the 1st char in the pair).
1325 *
1326 * @returns true if it is.
1327 * @returns false if it isn't.
1328 * @param wc The character to investigate.
1329 */
1330DECLINLINE(bool) RTUtf16IsHighSurrogate(RTUTF16 wc)
1331{
1332 return wc >= 0xd800 && wc <= 0xdbff;
1333}
1334
1335/**
1336 * Checks if the UTF-16 char is the low surrogate char (i.e.
1337 * the 2nd char in the pair).
1338 *
1339 * @returns true if it is.
1340 * @returns false if it isn't.
1341 * @param wc The character to investigate.
1342 */
1343DECLINLINE(bool) RTUtf16IsLowSurrogate(RTUTF16 wc)
1344{
1345 return wc >= 0xdc00 && wc <= 0xdfff;
1346}
1347
1348
1349/**
1350 * Checks if the two UTF-16 chars form a valid surrogate pair.
1351 *
1352 * @returns true if they do.
1353 * @returns false if they doesn't.
1354 * @param wcHigh The high (1st) character.
1355 * @param wcLow The low (2nd) character.
1356 */
1357DECLINLINE(bool) RTUtf16IsSurrogatePair(RTUTF16 wcHigh, RTUTF16 wcLow)
1358{
1359 return RTUtf16IsHighSurrogate(wcHigh)
1360 && RTUtf16IsLowSurrogate(wcLow);
1361}
1362
1363/**
1364 * Formats a buffer stream as hex bytes.
1365 *
1366 * The default is no separating spaces or line breaks or anything.
1367 *
1368 * @returns IPRT status code.
1369 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
1370 * @retval VERR_BUFFER_OVERFLOW if the buffer is insufficent to hold the bytes.
1371 *
1372 * @param pwszBuf Output string buffer.
1373 * @param cwcBuf The size of the output buffer in RTUTF16 units.
1374 * @param pv Pointer to the bytes to stringify.
1375 * @param cb The number of bytes to stringify.
1376 * @param fFlags Combination of RTSTRPRINTHEXBYTES_F_XXX values.
1377 * @sa RTStrPrintHexBytes.
1378 */
1379RTDECL(int) RTUtf16PrintHexBytes(PRTUTF16 pwszBuf, size_t cwcBuf, void const *pv, size_t cb, uint32_t fFlags);
1380
1381/** @} */
1382
1383
1384RT_C_DECLS_END
1385
1386/** @} */
1387
1388#endif
1389
Note: See TracBrowser for help on using the repository browser.

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