NIM 跨平台 C++ SDK
载入中...
搜索中...
未找到
convert_utf.h
浏览该文件的文档.
1/*
2 * Copyright 2001-2004 Unicode, Inc.
3 *
4 * Disclaimer
5 *
6 * This source code is provided as is by Unicode, Inc. No claims are
7 * made as to fitness for any particular purpose. No warranties of any
8 * kind are expressed or implied. The recipient agrees to determine
9 * applicability of information provided. If this file has been
10 * purchased on magnetic or optical media from Unicode, Inc., the
11 * sole remedy for any claim will be exchange of defective media
12 * within 90 days of receipt.
13 *
14 * Limitations on Rights to Redistribute This Code
15 *
16 * Unicode, Inc. hereby grants the right to freely use the information
17 * supplied in this file in the creation of products supporting the
18 * Unicode Standard, and to make copies of this file in any form
19 * for internal or external distribution as long as this notice
20 * remains attached.
21 */
22
23/* $Id: ConvertUTF.h 8178 2007-08-05 13:55:26Z leonardo $ */
24
25/* ---------------------------------------------------------------------
26
27 Conversions between UTF32, UTF-16, and UTF-8. Header file.
28
29 Several funtions are included here, forming a complete set of
30 conversions between the three formats. UTF-7 is not included
31 here, but is handled in a separate source file.
32
33 Each of these routines takes pointers to input buffers and output
34 buffers. The input buffers are const.
35
36 Each routine converts the text between *sourceStart and sourceEnd,
37 putting the result into the buffer between *targetStart and
38 targetEnd. Note: the end pointers are *after* the last item: e.g.
39 *(sourceEnd - 1) is the last item.
40
41 The return result indicates whether the conversion was successful,
42 and if not, whether the problem was in the source or target buffers.
43 (Only the first encountered problem is indicated.)
44
45 After the conversion, *sourceStart and *targetStart are both
46 updated to point to the end of last text successfully converted in
47 the respective buffers.
48
49 Input parameters:
50 sourceStart - pointer to a pointer to the source buffer.
51 The contents of this are modified on return so that
52 it points at the next thing to be converted.
53 targetStart - similarly, pointer to pointer to the target buffer.
54 sourceEnd, targetEnd - respectively pointers to the ends of the
55 two buffers, for overflow checking only.
56
57 These conversion functions take a ConversionFlags argument. When this
58 flag is set to strict, both irregular sequences and isolated surrogates
59 will cause an error. When the flag is set to lenient, both irregular
60 sequences and isolated surrogates are converted.
61
62 Whether the flag is strict or lenient, all illegal sequences will cause
63 an error return. This includes sequences such as: <F4 90 80 80>, <C0 80>,
64 or <A0> in UTF-8, and values above 0x10FFFF in UTF-32. Conformant code
65 must check for illegal sequences.
66
67 When the flag is set to lenient, characters over 0x10FFFF are converted
68 to the replacement character; otherwise (when the flag is set to strict)
69 they constitute an error.
70
71 Output parameters:
72 The value "sourceIllegal" is returned from some routines if the input
73 sequence is malformed. When "sourceIllegal" is returned, the source
74 value will point to the illegal value that caused the problem. E.g.,
75 in UTF-8 when a sequence is malformed, it points to the start of the
76 malformed sequence.
77
78 Author: Mark E. Davis, 1994.
79 Rev History: Rick McGowan, fixes & updates May 2001.
80 Fixes & updates, Sept 2001.
81
82------------------------------------------------------------------------ */
83
84/* ---------------------------------------------------------------------
85 The following 4 definitions are compiler-specific.
86 The C standard does not guarantee that wchar_t has at least
87 16 bits, so wchar_t is no less portable than unsigned short!
88 All should be unsigned values to avoid sign extension during
89 bit mask & shift operations.
90------------------------------------------------------------------------ */
91
92// typedef unsigned long UTF32; /* at least 32 bits */
93typedef unsigned int UTF32; /* at least 32 bits */
94typedef unsigned short UTF16; /* at least 16 bits */
95typedef unsigned char UTF8; /* typically 8 bits */
96typedef unsigned char Boolean; /* 0 or 1 */
97
98/* Some fundamental constants */
99#define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD
100#define UNI_MAX_BMP (UTF32)0x0000FFFF
101#define UNI_MAX_UTF16 (UTF32)0x0010FFFF
102#define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF
103#define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF
104
105typedef enum {
106 conversionOK, /* conversion successful */
107 sourceExhausted, /* partial character in source, but hit end */
108 targetExhausted, /* insuff. room in target for conversion */
109 sourceIllegal /* source sequence is illegal/malformed */
111
113
114/* This is for C++ and does no harm in C */
115#ifdef __cplusplus
116extern "C" {
117#endif
118
119ConversionResult ConvertUTF8toUTF16(const UTF8** sourceStart, const UTF8* sourceEnd, UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags);
120
121ConversionResult ConvertUTF16toUTF8(const UTF16** sourceStart, const UTF16* sourceEnd, UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);
122
123ConversionResult ConvertUTF8toUTF32(const UTF8** sourceStart, const UTF8* sourceEnd, UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags);
124
125ConversionResult ConvertUTF32toUTF8(const UTF32** sourceStart, const UTF32* sourceEnd, UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);
126
127ConversionResult ConvertUTF16toUTF32(const UTF16** sourceStart, const UTF16* sourceEnd, UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags);
128
129ConversionResult ConvertUTF32toUTF16(const UTF32** sourceStart, const UTF32* sourceEnd, UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags);
130
131Boolean isLegalUTF8Sequence(const UTF8* source, const UTF8* sourceEnd);
132
133#ifdef __cplusplus
134}
135#endif
136
137/* --------------------------------------------------------------------- */
ConversionResult ConvertUTF32toUTF16(const UTF32 **sourceStart, const UTF32 *sourceEnd, UTF16 **targetStart, UTF16 *targetEnd, ConversionFlags flags)
Definition: convert_utf.c:62
ConversionFlags
Definition: convert_utf.h:112
@ strictConversion
Definition: convert_utf.h:112
@ lenientConversion
Definition: convert_utf.h:112
ConversionResult ConvertUTF8toUTF32(const UTF8 **sourceStart, const UTF8 *sourceEnd, UTF32 **targetStart, UTF32 *targetEnd, ConversionFlags flags)
Definition: convert_utf.c:524
unsigned short UTF16
Definition: convert_utf.h:94
ConversionResult ConvertUTF8toUTF16(const UTF8 **sourceStart, const UTF8 *sourceEnd, UTF16 **targetStart, UTF16 *targetEnd, ConversionFlags flags)
Definition: convert_utf.c:374
unsigned int UTF32
Definition: convert_utf.h:93
ConversionResult ConvertUTF16toUTF8(const UTF16 **sourceStart, const UTF16 *sourceEnd, UTF8 **targetStart, UTF8 *targetEnd, ConversionFlags flags)
Definition: convert_utf.c:217
unsigned char UTF8
Definition: convert_utf.h:95
Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd)
Definition: convert_utf.c:364
ConversionResult ConvertUTF32toUTF8(const UTF32 **sourceStart, const UTF32 *sourceEnd, UTF8 **targetStart, UTF8 *targetEnd, ConversionFlags flags)
Definition: convert_utf.c:459
unsigned char Boolean
Definition: convert_utf.h:96
ConversionResult ConvertUTF16toUTF32(const UTF16 **sourceStart, const UTF16 *sourceEnd, UTF32 **targetStart, UTF32 *targetEnd, ConversionFlags flags)
Definition: convert_utf.c:115
ConversionResult
Definition: convert_utf.h:105
@ targetExhausted
Definition: convert_utf.h:108
@ sourceIllegal
Definition: convert_utf.h:109
@ conversionOK
Definition: convert_utf.h:106
@ sourceExhausted
Definition: convert_utf.h:107