VirtualBox

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

Last change on this file since 58624 was 57941, checked in by vboxsync, 9 years ago

iprt/string.h: split out the UTF-16 and Latin-1 parts.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 35.5 KB
Line 
1/** @file
2 * IPRT - String Manipulation, UTF-16 encoding.
3 */
4
5/*
6 * Copyright (C) 2006-2015 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/**
86 * Free a UTF-16 string allocated by RTStrToUtf16(), RTStrToUtf16Ex(),
87 * RTLatin1ToUtf16(), RTLatin1ToUtf16Ex(), RTUtf16Dup() or RTUtf16DupEx().
88 *
89 * @returns iprt status code.
90 * @param pwszString The UTF-16 string to free. NULL is accepted.
91 */
92RTDECL(void) RTUtf16Free(PRTUTF16 pwszString);
93
94/**
95 * Allocates a new copy of the specified UTF-16 string (default tag).
96 *
97 * @returns Pointer to the allocated string copy. Use RTUtf16Free() to free it.
98 * @returns NULL when out of memory.
99 * @param pwszString UTF-16 string to duplicate.
100 * @remark This function will not make any attempt to validate the encoding.
101 */
102#define RTUtf16Dup(pwszString) RTUtf16DupTag((pwszString), RTSTR_TAG)
103
104/**
105 * Allocates a new copy of the specified UTF-16 string (custom tag).
106 *
107 * @returns Pointer to the allocated string copy. Use RTUtf16Free() to free it.
108 * @returns NULL when out of memory.
109 * @param pwszString UTF-16 string to duplicate.
110 * @param pszTag Allocation tag used for statistics and such.
111 * @remark This function will not make any attempt to validate the encoding.
112 */
113RTDECL(PRTUTF16) RTUtf16DupTag(PCRTUTF16 pwszString, const char *pszTag);
114
115/**
116 * Allocates a new copy of the specified UTF-16 string (default tag).
117 *
118 * @returns iprt status code.
119 * @param ppwszString Receives pointer of the allocated UTF-16 string.
120 * The returned pointer must be freed using RTUtf16Free().
121 * @param pwszString UTF-16 string to duplicate.
122 * @param cwcExtra Number of extra RTUTF16 items to allocate.
123 * @remark This function will not make any attempt to validate the encoding.
124 */
125#define RTUtf16DupEx(ppwszString, pwszString, cwcExtra) \
126 RTUtf16DupExTag((ppwszString), (pwszString), (cwcExtra), RTSTR_TAG)
127
128/**
129 * Allocates a new copy of the specified UTF-16 string (custom tag).
130 *
131 * @returns iprt status code.
132 * @param ppwszString Receives pointer of the allocated UTF-16 string.
133 * The returned pointer must be freed using RTUtf16Free().
134 * @param pwszString UTF-16 string to duplicate.
135 * @param cwcExtra Number of extra RTUTF16 items to allocate.
136 * @param pszTag Allocation tag used for statistics and such.
137 * @remark This function will not make any attempt to validate the encoding.
138 */
139RTDECL(int) RTUtf16DupExTag(PRTUTF16 *ppwszString, PCRTUTF16 pwszString, size_t cwcExtra, const char *pszTag);
140
141/**
142 * Returns the length of a UTF-16 string in UTF-16 characters
143 * without trailing '\\0'.
144 *
145 * Surrogate pairs counts as two UTF-16 characters here. Use RTUtf16CpCnt()
146 * to get the exact number of code points in the string.
147 *
148 * @returns The number of RTUTF16 items in the string.
149 * @param pwszString Pointer the UTF-16 string.
150 * @remark This function will not make any attempt to validate the encoding.
151 */
152RTDECL(size_t) RTUtf16Len(PCRTUTF16 pwszString);
153
154/**
155 * Find the length of a zero-terminated byte string, given a max string length.
156 *
157 * @returns The string length or cbMax. The returned length does not include
158 * the zero terminator if it was found.
159 *
160 * @param pwszString The string.
161 * @param cwcMax The max string length in RTUTF16s.
162 * @sa RTUtf16NLenEx, RTStrNLen.
163 */
164RTDECL(size_t) RTUtf16NLen(PCRTUTF16 pwszString, size_t cwcMax);
165
166/**
167 * Find the length of a zero-terminated byte string, given
168 * a max string length.
169 *
170 * @returns IPRT status code.
171 * @retval VINF_SUCCESS if the string has a length less than cchMax.
172 * @retval VERR_BUFFER_OVERFLOW if the end of the string wasn't found
173 * before cwcMax was reached.
174 *
175 * @param pwszString The string.
176 * @param cwcMax The max string length in RTUTF16s.
177 * @param pcwc Where to store the string length excluding the
178 * terminator. This is set to cwcMax if the terminator
179 * isn't found.
180 * @sa RTUtf16NLen, RTStrNLenEx.
181 */
182RTDECL(int) RTUtf16NLenEx(PCRTUTF16 pwszString, size_t cwcMax, size_t *pcwc);
183
184/**
185 * Find the zero terminator in a string with a limited length.
186 *
187 * @returns Pointer to the zero terminator.
188 * @returns NULL if the zero terminator was not found.
189 *
190 * @param pwszString The string.
191 * @param cwcMax The max string length. RTSTR_MAX is fine.
192 */
193RTDECL(PCRTUTF16) RTUtf16End(PCRTUTF16 pwszString, size_t cwcMax);
194
195/**
196 * Strips blankspaces from both ends of the string.
197 *
198 * @returns Pointer to first non-blank char in the string.
199 * @param pwsz The string to strip.
200 */
201RTDECL(PRTUTF16) RTUtf16Strip(PRTUTF16 pwsz);
202
203/**
204 * Strips blankspaces from the start of the string.
205 *
206 * @returns Pointer to first non-blank char in the string.
207 * @param pwsz The string to strip.
208 */
209RTDECL(PRTUTF16) RTUtf16StripL(PCRTUTF16 pwsz);
210
211/**
212 * Strips blankspaces from the end of the string.
213 *
214 * @returns pwsz.
215 * @param pwsz The string to strip.
216 */
217RTDECL(PRTUTF16) RTUtf16StripR(PRTUTF16 pwsz);
218
219/**
220 * String copy with overflow handling.
221 *
222 * @retval VINF_SUCCESS on success.
223 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
224 * buffer will contain as much of the string as it can hold, fully
225 * terminated.
226 *
227 * @param pwszDst The destination buffer.
228 * @param cwcDst The size of the destination buffer in RTUTF16s.
229 * @param pwszSrc The source string. NULL is not OK.
230 */
231RTDECL(int) RTUtf16Copy(PRTUTF16 pwszDst, size_t cwcDst, PCRTUTF16 pwszSrc);
232
233/**
234 * String copy with overflow handling, ASCII source.
235 *
236 * @retval VINF_SUCCESS on success.
237 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
238 * buffer will contain as much of the string as it can hold, fully
239 * terminated.
240 *
241 * @param pwszDst The destination buffer.
242 * @param cwcDst The size of the destination buffer in RTUTF16s.
243 * @param pszSrc The source string, pure ASCII. NULL is not OK.
244 */
245RTDECL(int) RTUtf16CopyAscii(PRTUTF16 pwszDst, size_t cwcDst, const char *pszSrc);
246
247/**
248 * String copy with overflow handling.
249 *
250 * @retval VINF_SUCCESS on success.
251 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
252 * buffer will contain as much of the string as it can hold, fully
253 * terminated.
254 *
255 * @param pwszDst The destination buffer.
256 * @param cwcDst The size of the destination buffer in RTUTF16s.
257 * @param pwszSrc The source string. NULL is not OK.
258 * @param cwcSrcMax The maximum number of chars (not code points) to
259 * copy from the source string, not counting the
260 * terminator as usual.
261 */
262RTDECL(int) RTUtf16CopyEx(PRTUTF16 pwszDst, size_t cwcDst, PCRTUTF16 pwszSrc, size_t cwcSrcMax);
263
264/**
265 * String concatenation with overflow handling.
266 *
267 * @retval VINF_SUCCESS on success.
268 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
269 * buffer will contain as much of the string as it can hold, fully
270 * terminated.
271 *
272 * @param pwszDst The destination buffer.
273 * @param cwcDst The size of the destination buffer in RTUTF16s.
274 * @param pwszSrc The source string. NULL is not OK.
275 */
276RTDECL(int) RTUtf16Cat(PRTUTF16 pwszDst, size_t cwcDst, PCRTUTF16 pwszSrc);
277
278/**
279 * String concatenation with overflow handling, ASCII source.
280 *
281 * @retval VINF_SUCCESS on success.
282 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
283 * buffer will contain as much of the string as it can hold, fully
284 * terminated.
285 *
286 * @param pwszDst The destination buffer.
287 * @param cwcDst The size of the destination buffer in RTUTF16s.
288 * @param pszSrc The source string, pure ASCII. NULL is not OK.
289 */
290RTDECL(int) RTUtf16CatAscii(PRTUTF16 pwszDst, size_t cwcDst, const char *pszSrc);
291
292/**
293 * String concatenation with overflow handling.
294 *
295 * @retval VINF_SUCCESS on success.
296 * @retval VERR_BUFFER_OVERFLOW if the destination buffer is too small. The
297 * buffer will contain as much of the string as it can hold, fully
298 * terminated.
299 *
300 * @param pwszDst The destination buffer.
301 * @param cwcDst The size of the destination buffer in RTUTF16s.
302 * @param pwszSrc The source string. NULL is not OK.
303 * @param cwcSrcMax The maximum number of UTF-16 chars (not code
304 * points) to copy from the source string, not
305 * counting the terminator as usual.
306 */
307RTDECL(int) RTUtf16CatEx(PRTUTF16 pwszDst, size_t cwcDst, PCRTUTF16 pwszSrc, size_t cwcSrcMax);
308
309/**
310 * Performs a case sensitive string compare between two UTF-16 strings.
311 *
312 * @returns < 0 if the first string less than the second string.s
313 * @returns 0 if the first string identical to the second string.
314 * @returns > 0 if the first string greater than the second string.
315 * @param pwsz1 First UTF-16 string. Null is allowed.
316 * @param pwsz2 Second UTF-16 string. Null is allowed.
317 * @remark This function will not make any attempt to validate the encoding.
318 */
319RTDECL(int) RTUtf16Cmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
320
321/**
322 * Performs a case sensitive string compare between an UTF-16 string and a pure
323 * ASCII string.
324 *
325 * @returns < 0 if the first string less than the second string.s
326 * @returns 0 if the first string identical to the second string.
327 * @returns > 0 if the first string greater than the second string.
328 * @param pwsz1 First UTF-16 string. Null is allowed.
329 * @param psz2 Second string, pure ASCII. Null is allowed.
330 * @remark This function will not make any attempt to validate the encoding.
331 */
332RTDECL(int) RTUtf16CmpAscii(PCRTUTF16 pwsz1, const char *psz2);
333
334/**
335 * Performs a case insensitive string compare between two UTF-16 strings.
336 *
337 * This is a simplified compare, as only the simplified lower/upper case folding
338 * specified by the unicode specs are used. It does not consider character pairs
339 * as they are used in some languages, just simple upper & lower case compares.
340 *
341 * @returns < 0 if the first string less than the second string.
342 * @returns 0 if the first string identical to the second string.
343 * @returns > 0 if the first string greater than the second string.
344 * @param pwsz1 First UTF-16 string. Null is allowed.
345 * @param pwsz2 Second UTF-16 string. Null is allowed.
346 */
347RTDECL(int) RTUtf16ICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
348
349/**
350 * Performs a case insensitive string compare between an UTF-16 string and an
351 * pure ASCII string.
352 *
353 * Since this compare only takes cares about the first 128 codepoints in
354 * unicode, no tables are needed and there aren't any real complications.
355 *
356 * @returns < 0 if the first string less than the second string.
357 * @returns 0 if the first string identical to the second string.
358 * @returns > 0 if the first string greater than the second string.
359 * @param pwsz1 First UTF-16 string. Null is allowed.
360 * @param psz2 Second string, pure ASCII. Null is allowed.
361 */
362RTDECL(int) RTUtf16ICmpAscii(PCRTUTF16 pwsz1, const char *psz2);
363
364/**
365 * Performs a case insensitive string compare between two UTF-16 strings
366 * using the current locale of the process (if applicable).
367 *
368 * This differs from RTUtf16ICmp() in that it will try, if a locale with the
369 * required data is available, to do a correct case-insensitive compare. It
370 * follows that it is more complex and thereby likely to be more expensive.
371 *
372 * @returns < 0 if the first string less than the second string.
373 * @returns 0 if the first string identical to the second string.
374 * @returns > 0 if the first string greater than the second string.
375 * @param pwsz1 First UTF-16 string. Null is allowed.
376 * @param pwsz2 Second UTF-16 string. Null is allowed.
377 */
378RTDECL(int) RTUtf16LocaleICmp(PCRTUTF16 pwsz1, PCRTUTF16 pwsz2);
379
380/**
381 * Folds a UTF-16 string to lowercase.
382 *
383 * This is a very simple folding; is uses the simple lowercase
384 * code point, it is not related to any locale just the most common
385 * lowercase codepoint setup by the unicode specs, and it will not
386 * create new surrogate pairs or remove existing ones.
387 *
388 * @returns Pointer to the passed in string.
389 * @param pwsz The string to fold.
390 */
391RTDECL(PRTUTF16) RTUtf16ToLower(PRTUTF16 pwsz);
392
393/**
394 * Folds a UTF-16 string to uppercase.
395 *
396 * This is a very simple folding; is uses the simple uppercase
397 * code point, it is not related to any locale just the most common
398 * uppercase codepoint setup by the unicode specs, and it will not
399 * create new surrogate pairs or remove existing ones.
400 *
401 * @returns Pointer to the passed in string.
402 * @param pwsz The string to fold.
403 */
404RTDECL(PRTUTF16) RTUtf16ToUpper(PRTUTF16 pwsz);
405
406/**
407 * Validates the UTF-16 encoding of the string.
408 *
409 * @returns iprt status code.
410 * @param pwsz The string.
411 */
412RTDECL(int) RTUtf16ValidateEncoding(PCRTUTF16 pwsz);
413
414/**
415 * Validates the UTF-16 encoding of the string.
416 *
417 * @returns iprt status code.
418 * @param pwsz The string.
419 * @param cwc The max string length (/ size) in UTF-16 units. Use
420 * RTSTR_MAX to process the entire string.
421 * @param fFlags Combination of RTSTR_VALIDATE_ENCODING_XXX flags.
422 */
423RTDECL(int) RTUtf16ValidateEncodingEx(PCRTUTF16 pwsz, size_t cwc, uint32_t fFlags);
424
425/**
426 * Checks if the UTF-16 encoding is valid.
427 *
428 * @returns true / false.
429 * @param pwsz The string.
430 */
431RTDECL(bool) RTUtf16IsValidEncoding(PCRTUTF16 pwsz);
432
433/**
434 * Sanitise a (valid) UTF-16 string by replacing all characters outside a white
435 * list in-place by an ASCII replacement character. Multi-byte characters will
436 * be replaced byte by byte.
437 *
438 * @returns The number of code points replaced, or a negative value if the
439 * string is not correctly encoded. In this last case the string
440 * may be partially processed.
441 * @param pwsz The string to sanitise.
442 * @param puszValidSet A zero-terminated array of pairs of Unicode points.
443 * Each pair is the start and end point of a range,
444 * and the union of these ranges forms the white list.
445 * @param chReplacement The ASCII replacement character.
446 */
447RTDECL(ssize_t) RTUtf16PurgeComplementSet(PRTUTF16 pwsz, PCRTUNICP puszValidSet, char chReplacement);
448
449/**
450 * Translate a UTF-16 string into a UTF-8 allocating the result buffer (default
451 * tag).
452 *
453 * @returns iprt status code.
454 * @param pwszString UTF-16 string to convert.
455 * @param ppszString Receives pointer of allocated UTF-8 string on
456 * success, and is always set to NULL on failure.
457 * The returned pointer must be freed using RTStrFree().
458 */
459#define RTUtf16ToUtf8(pwszString, ppszString) RTUtf16ToUtf8Tag((pwszString), (ppszString), RTSTR_TAG)
460
461/**
462 * Translate a UTF-16 string into a UTF-8 allocating the result buffer.
463 *
464 * @returns iprt status code.
465 * @param pwszString UTF-16 string to convert.
466 * @param ppszString Receives pointer of allocated UTF-8 string on
467 * success, and is always set to NULL on failure.
468 * The returned pointer must be freed using RTStrFree().
469 * @param pszTag Allocation tag used for statistics and such.
470 */
471RTDECL(int) RTUtf16ToUtf8Tag(PCRTUTF16 pwszString, char **ppszString, const char *pszTag);
472
473/**
474 * Translates UTF-16 to UTF-8 using buffer provided by the caller or a fittingly
475 * sized buffer allocated by the function (default tag).
476 *
477 * @returns iprt status code.
478 * @param pwszString The UTF-16 string to convert.
479 * @param cwcString The number of RTUTF16 items to translate from pwszString.
480 * The translation will stop when reaching cwcString or the terminator ('\\0').
481 * Use RTSTR_MAX to translate the entire string.
482 * @param ppsz If cch is non-zero, this must either be pointing to a pointer to
483 * a buffer of the specified size, or pointer to a NULL pointer.
484 * If *ppsz is NULL or cch is zero a buffer of at least cch chars
485 * will be allocated to hold the translated string.
486 * If a buffer was requested it must be freed using RTStrFree().
487 * @param cch The buffer size in chars (the type). This includes the terminator.
488 * @param pcch Where to store the length of the translated string,
489 * excluding the terminator. (Optional)
490 *
491 * This may be set under some error conditions,
492 * however, only for VERR_BUFFER_OVERFLOW and
493 * VERR_NO_STR_MEMORY will it contain a valid string
494 * length that can be used to resize the buffer.
495 */
496#define RTUtf16ToUtf8Ex(pwszString, cwcString, ppsz, cch, pcch) \
497 RTUtf16ToUtf8ExTag((pwszString), (cwcString), (ppsz), (cch), (pcch), RTSTR_TAG)
498
499/**
500 * Translates UTF-16 to UTF-8 using buffer provided by the caller or a fittingly
501 * sized buffer allocated by the function (custom tag).
502 *
503 * @returns iprt status code.
504 * @param pwszString The UTF-16 string to convert.
505 * @param cwcString The number of RTUTF16 items to translate from pwszString.
506 * The translation will stop when reaching cwcString or the terminator ('\\0').
507 * Use RTSTR_MAX to translate the entire string.
508 * @param ppsz If cch is non-zero, this must either be pointing to a pointer to
509 * a buffer of the specified size, or pointer to a NULL pointer.
510 * If *ppsz is NULL or cch is zero a buffer of at least cch chars
511 * will be allocated to hold the translated string.
512 * If a buffer was requested it must be freed using RTStrFree().
513 * @param cch The buffer size in chars (the type). This includes the terminator.
514 * @param pcch Where to store the length of the translated string,
515 * excluding the terminator. (Optional)
516 *
517 * This may be set under some error conditions,
518 * however, only for VERR_BUFFER_OVERFLOW and
519 * VERR_NO_STR_MEMORY will it contain a valid string
520 * length that can be used to resize the buffer.
521 * @param pszTag Allocation tag used for statistics and such.
522 */
523RTDECL(int) RTUtf16ToUtf8ExTag(PCRTUTF16 pwszString, size_t cwcString, char **ppsz, size_t cch, size_t *pcch, const char *pszTag);
524
525/**
526 * Calculates the length of the UTF-16 string in UTF-8 chars (bytes).
527 *
528 * This function will validate the string, and incorrectly encoded UTF-16
529 * strings will be rejected. The primary purpose of this function is to
530 * help allocate buffers for RTUtf16ToUtf8() of the correct size. For most
531 * other purposes RTUtf16ToUtf8Ex() should be used.
532 *
533 * @returns Number of char (bytes).
534 * @returns 0 if the string was incorrectly encoded.
535 * @param pwsz The UTF-16 string.
536 */
537RTDECL(size_t) RTUtf16CalcUtf8Len(PCRTUTF16 pwsz);
538
539/**
540 * Calculates the length of the UTF-16 string in UTF-8 chars (bytes).
541 *
542 * This function will validate the string, and incorrectly encoded UTF-16
543 * strings will be rejected.
544 *
545 * @returns iprt status code.
546 * @param pwsz The string.
547 * @param cwc The max string length. Use RTSTR_MAX to process the entire string.
548 * @param pcch Where to store the string length (in bytes). Optional.
549 * This is undefined on failure.
550 */
551RTDECL(int) RTUtf16CalcUtf8LenEx(PCRTUTF16 pwsz, size_t cwc, size_t *pcch);
552
553/**
554 * Translate a UTF-16 string into a Latin-1 (ISO-8859-1) allocating the result
555 * buffer (default tag).
556 *
557 * @returns iprt status code.
558 * @param pwszString UTF-16 string to convert.
559 * @param ppszString Receives pointer of allocated Latin1 string on
560 * success, and is always set to NULL on failure.
561 * The returned pointer must be freed using RTStrFree().
562 */
563#define RTUtf16ToLatin1(pwszString, ppszString) RTUtf16ToLatin1Tag((pwszString), (ppszString), RTSTR_TAG)
564
565/**
566 * Translate a UTF-16 string into a Latin-1 (ISO-8859-1) allocating the result
567 * buffer (custom tag).
568 *
569 * @returns iprt status code.
570 * @param pwszString UTF-16 string to convert.
571 * @param ppszString Receives pointer of allocated Latin1 string on
572 * success, and is always set to NULL on failure.
573 * The returned pointer must be freed using RTStrFree().
574 * @param pszTag Allocation tag used for statistics and such.
575 */
576RTDECL(int) RTUtf16ToLatin1Tag(PCRTUTF16 pwszString, char **ppszString, const char *pszTag);
577
578/**
579 * Translates UTF-16 to Latin-1 (ISO-8859-1) using buffer provided by the caller
580 * or a fittingly sized buffer allocated by the function (default tag).
581 *
582 * @returns iprt status code.
583 * @param pwszString The UTF-16 string to convert.
584 * @param cwcString The number of RTUTF16 items to translate from
585 * pwszString. The translation will stop when reaching
586 * cwcString or the terminator ('\\0'). Use RTSTR_MAX
587 * to translate the entire string.
588 * @param ppsz Pointer to the pointer to the Latin-1 string. The
589 * buffer can optionally be preallocated by the caller.
590 *
591 * If cch is zero, *ppsz is undefined.
592 *
593 * If cch is non-zero and *ppsz is not NULL, then this
594 * will be used as the output buffer.
595 * VERR_BUFFER_OVERFLOW will be returned if this is
596 * insufficient.
597 *
598 * If cch is zero or *ppsz is NULL, then a buffer of
599 * sufficient size is allocated. cch can be used to
600 * specify a minimum size of this buffer. Use
601 * RTUtf16Free() to free the result.
602 *
603 * @param cch The buffer size in chars (the type). This includes
604 * the terminator.
605 * @param pcch Where to store the length of the translated string,
606 * excluding the terminator. (Optional)
607 *
608 * This may be set under some error conditions,
609 * however, only for VERR_BUFFER_OVERFLOW and
610 * VERR_NO_STR_MEMORY will it contain a valid string
611 * length that can be used to resize the buffer.
612 */
613#define RTUtf16ToLatin1Ex(pwszString, cwcString, ppsz, cch, pcch) \
614 RTUtf16ToLatin1ExTag((pwszString), (cwcString), (ppsz), (cch), (pcch), RTSTR_TAG)
615
616/**
617 * Translates UTF-16 to Latin-1 (ISO-8859-1) using buffer provided by the caller
618 * or a fittingly sized buffer allocated by the function (custom tag).
619 *
620 * @returns iprt status code.
621 * @param pwszString The UTF-16 string to convert.
622 * @param cwcString The number of RTUTF16 items to translate from
623 * pwszString. The translation will stop when reaching
624 * cwcString or the terminator ('\\0'). Use RTSTR_MAX
625 * to translate the entire string.
626 * @param ppsz Pointer to the pointer to the Latin-1 string. The
627 * buffer can optionally be preallocated by the caller.
628 *
629 * If cch is zero, *ppsz is undefined.
630 *
631 * If cch is non-zero and *ppsz is not NULL, then this
632 * will be used as the output buffer.
633 * VERR_BUFFER_OVERFLOW will be returned if this is
634 * insufficient.
635 *
636 * If cch is zero or *ppsz is NULL, then a buffer of
637 * sufficient size is allocated. cch can be used to
638 * specify a minimum size of this buffer. Use
639 * RTUtf16Free() to free the result.
640 *
641 * @param cch The buffer size in chars (the type). This includes
642 * the terminator.
643 * @param pcch Where to store the length of the translated string,
644 * excluding the terminator. (Optional)
645 *
646 * This may be set under some error conditions,
647 * however, only for VERR_BUFFER_OVERFLOW and
648 * VERR_NO_STR_MEMORY will it contain a valid string
649 * length that can be used to resize the buffer.
650 * @param pszTag Allocation tag used for statistics and such.
651 */
652RTDECL(int) RTUtf16ToLatin1ExTag(PCRTUTF16 pwszString, size_t cwcString, char **ppsz, size_t cch, size_t *pcch, const char *pszTag);
653
654/**
655 * Calculates the length of the UTF-16 string in Latin-1 (ISO-8859-1) chars.
656 *
657 * This function will validate the string, and incorrectly encoded UTF-16
658 * strings will be rejected. The primary purpose of this function is to
659 * help allocate buffers for RTUtf16ToLatin1() of the correct size. For most
660 * other purposes RTUtf16ToLatin1Ex() should be used.
661 *
662 * @returns Number of char (bytes).
663 * @returns 0 if the string was incorrectly encoded.
664 * @param pwsz The UTF-16 string.
665 */
666RTDECL(size_t) RTUtf16CalcLatin1Len(PCRTUTF16 pwsz);
667
668/**
669 * Calculates the length of the UTF-16 string in Latin-1 (ISO-8859-1) chars.
670 *
671 * This function will validate the string, and incorrectly encoded UTF-16
672 * strings will be rejected.
673 *
674 * @returns iprt status code.
675 * @param pwsz The string.
676 * @param cwc The max string length. Use RTSTR_MAX to process the
677 * entire string.
678 * @param pcch Where to store the string length (in bytes). Optional.
679 * This is undefined on failure.
680 */
681RTDECL(int) RTUtf16CalcLatin1LenEx(PCRTUTF16 pwsz, size_t cwc, size_t *pcch);
682
683/**
684 * Get the unicode code point at the given string position.
685 *
686 * @returns unicode code point.
687 * @returns RTUNICP_INVALID if the encoding is invalid.
688 * @param pwsz The string.
689 *
690 * @remark This is an internal worker for RTUtf16GetCp().
691 */
692RTDECL(RTUNICP) RTUtf16GetCpInternal(PCRTUTF16 pwsz);
693
694/**
695 * Get the unicode code point at the given string position.
696 *
697 * @returns iprt status code.
698 * @param ppwsz Pointer to the string pointer. This will be updated to
699 * point to the char following the current code point.
700 * @param pCp Where to store the code point.
701 * RTUNICP_INVALID is stored here on failure.
702 *
703 * @remark This is an internal worker for RTUtf16GetCpEx().
704 */
705RTDECL(int) RTUtf16GetCpExInternal(PCRTUTF16 *ppwsz, PRTUNICP pCp);
706
707/**
708 * Put the unicode code point at the given string position
709 * and return the pointer to the char following it.
710 *
711 * This function will not consider anything at or following the
712 * buffer area pointed to by pwsz. It is therefore not suitable for
713 * inserting code points into a string, only appending/overwriting.
714 *
715 * @returns pointer to the char following the written code point.
716 * @param pwsz The string.
717 * @param CodePoint The code point to write.
718 * This should not be RTUNICP_INVALID or any other
719 * character out of the UTF-16 range.
720 *
721 * @remark This is an internal worker for RTUtf16GetCpEx().
722 */
723RTDECL(PRTUTF16) RTUtf16PutCpInternal(PRTUTF16 pwsz, RTUNICP CodePoint);
724
725/**
726 * Get the unicode code point at the given string position.
727 *
728 * @returns unicode code point.
729 * @returns RTUNICP_INVALID if the encoding is invalid.
730 * @param pwsz The string.
731 *
732 * @remark We optimize this operation by using an inline function for
733 * everything which isn't a surrogate pair or an endian indicator.
734 */
735DECLINLINE(RTUNICP) RTUtf16GetCp(PCRTUTF16 pwsz)
736{
737 const RTUTF16 wc = *pwsz;
738 if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
739 return wc;
740 return RTUtf16GetCpInternal(pwsz);
741}
742
743/**
744 * Get the unicode code point at the given string position.
745 *
746 * @returns iprt status code.
747 * @param ppwsz Pointer to the string pointer. This will be updated to
748 * point to the char following the current code point.
749 * @param pCp Where to store the code point.
750 * RTUNICP_INVALID is stored here on failure.
751 *
752 * @remark We optimize this operation by using an inline function for
753 * everything which isn't a surrogate pair or and endian indicator.
754 */
755DECLINLINE(int) RTUtf16GetCpEx(PCRTUTF16 *ppwsz, PRTUNICP pCp)
756{
757 const RTUTF16 wc = **ppwsz;
758 if (wc < 0xd800 || (wc > 0xdfff && wc < 0xfffe))
759 {
760 (*ppwsz)++;
761 *pCp = wc;
762 return VINF_SUCCESS;
763 }
764 return RTUtf16GetCpExInternal(ppwsz, pCp);
765}
766
767/**
768 * Put the unicode code point at the given string position
769 * and return the pointer to the char following it.
770 *
771 * This function will not consider anything at or following the
772 * buffer area pointed to by pwsz. It is therefore not suitable for
773 * inserting code points into a string, only appending/overwriting.
774 *
775 * @returns pointer to the char following the written code point.
776 * @param pwsz The string.
777 * @param CodePoint The code point to write.
778 * This should not be RTUNICP_INVALID or any other
779 * character out of the UTF-16 range.
780 *
781 * @remark We optimize this operation by using an inline function for
782 * everything which isn't a surrogate pair or and endian indicator.
783 */
784DECLINLINE(PRTUTF16) RTUtf16PutCp(PRTUTF16 pwsz, RTUNICP CodePoint)
785{
786 if (CodePoint < 0xd800 || (CodePoint > 0xd800 && CodePoint < 0xfffe))
787 {
788 *pwsz++ = (RTUTF16)CodePoint;
789 return pwsz;
790 }
791 return RTUtf16PutCpInternal(pwsz, CodePoint);
792}
793
794/**
795 * Skips ahead, past the current code point.
796 *
797 * @returns Pointer to the char after the current code point.
798 * @param pwsz Pointer to the current code point.
799 * @remark This will not move the next valid code point, only past the current one.
800 */
801DECLINLINE(PRTUTF16) RTUtf16NextCp(PCRTUTF16 pwsz)
802{
803 RTUNICP Cp;
804 RTUtf16GetCpEx(&pwsz, &Cp);
805 return (PRTUTF16)pwsz;
806}
807
808/**
809 * Skips backwards, to the previous code point.
810 *
811 * @returns Pointer to the char after the current code point.
812 * @param pwszStart Pointer to the start of the string.
813 * @param pwsz Pointer to the current code point.
814 */
815RTDECL(PRTUTF16) RTUtf16PrevCp(PCRTUTF16 pwszStart, PCRTUTF16 pwsz);
816
817
818/**
819 * Checks if the UTF-16 char is the high surrogate char (i.e.
820 * the 1st char in the pair).
821 *
822 * @returns true if it is.
823 * @returns false if it isn't.
824 * @param wc The character to investigate.
825 */
826DECLINLINE(bool) RTUtf16IsHighSurrogate(RTUTF16 wc)
827{
828 return wc >= 0xd800 && wc <= 0xdbff;
829}
830
831/**
832 * Checks if the UTF-16 char is the low surrogate char (i.e.
833 * the 2nd char in the pair).
834 *
835 * @returns true if it is.
836 * @returns false if it isn't.
837 * @param wc The character to investigate.
838 */
839DECLINLINE(bool) RTUtf16IsLowSurrogate(RTUTF16 wc)
840{
841 return wc >= 0xdc00 && wc <= 0xdfff;
842}
843
844
845/**
846 * Checks if the two UTF-16 chars form a valid surrogate pair.
847 *
848 * @returns true if they do.
849 * @returns false if they doesn't.
850 * @param wcHigh The high (1st) character.
851 * @param wcLow The low (2nd) character.
852 */
853DECLINLINE(bool) RTUtf16IsSurrogatePair(RTUTF16 wcHigh, RTUTF16 wcLow)
854{
855 return RTUtf16IsHighSurrogate(wcHigh)
856 && RTUtf16IsLowSurrogate(wcLow);
857}
858
859/**
860 * Formats a buffer stream as hex bytes.
861 *
862 * The default is no separating spaces or line breaks or anything.
863 *
864 * @returns IPRT status code.
865 * @retval VERR_INVALID_POINTER if any of the pointers are wrong.
866 * @retval VERR_BUFFER_OVERFLOW if the buffer is insufficent to hold the bytes.
867 *
868 * @param pwszBuf Output string buffer.
869 * @param cwcBuf The size of the output buffer in RTUTF16 units.
870 * @param pv Pointer to the bytes to stringify.
871 * @param cb The number of bytes to stringify.
872 * @param fFlags Combination of RTSTRPRINTHEXBYTES_F_XXX values.
873 * @sa RTStrPrintHexBytes.
874 */
875RTDECL(int) RTUtf16PrintHexBytes(PRTUTF16 pwszBuf, size_t cwcBuf, void const *pv, size_t cb, uint32_t fFlags);
876
877/** @} */
878
879
880RT_C_DECLS_END
881
882/** @} */
883
884#endif
885
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