VirtualBox

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

Last change on this file since 98103 was 98103, checked in by vboxsync, 20 months ago

Copyright year updates by scm.

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