1 /*===--- ConvertUTF.h - Universal Character Names conversions ---------------===
3 * The LLVM Compiler Infrastructure
5 * This file is distributed under the University of Illinois Open Source
6 * License. See LICENSE.TXT for details.
8 *==------------------------------------------------------------------------==*/
10 * Copyright 2001-2004 Unicode, Inc.
14 * This source code is provided as is by Unicode, Inc. No claims are
15 * made as to fitness for any particular purpose. No warranties of any
16 * kind are expressed or implied. The recipient agrees to determine
17 * applicability of information provided. If this file has been
18 * purchased on magnetic or optical media from Unicode, Inc., the
19 * sole remedy for any claim will be exchange of defective media
20 * within 90 days of receipt.
22 * Limitations on Rights to Redistribute This Code
24 * Unicode, Inc. hereby grants the right to freely use the information
25 * supplied in this file in the creation of products supporting the
26 * Unicode Standard, and to make copies of this file in any form
27 * for internal or external distribution as long as this notice
31 /* ---------------------------------------------------------------------
33 Conversions between UTF32, UTF-16, and UTF-8. Header file.
35 Several funtions are included here, forming a complete set of
36 conversions between the three formats. UTF-7 is not included
37 here, but is handled in a separate source file.
39 Each of these routines takes pointers to input buffers and output
40 buffers. The input buffers are const.
42 Each routine converts the text between *sourceStart and sourceEnd,
43 putting the result into the buffer between *targetStart and
44 targetEnd. Note: the end pointers are *after* the last item: e.g.
45 *(sourceEnd - 1) is the last item.
47 The return result indicates whether the conversion was successful,
48 and if not, whether the problem was in the source or target buffers.
49 (Only the first encountered problem is indicated.)
51 After the conversion, *sourceStart and *targetStart are both
52 updated to point to the end of last text successfully converted in
53 the respective buffers.
56 sourceStart - pointer to a pointer to the source buffer.
57 The contents of this are modified on return so that
58 it points at the next thing to be converted.
59 targetStart - similarly, pointer to pointer to the target buffer.
60 sourceEnd, targetEnd - respectively pointers to the ends of the
61 two buffers, for overflow checking only.
63 These conversion functions take a ConversionFlags argument. When this
64 flag is set to strict, both irregular sequences and isolated surrogates
65 will cause an error. When the flag is set to lenient, both irregular
66 sequences and isolated surrogates are converted.
68 Whether the flag is strict or lenient, all illegal sequences will cause
69 an error return. This includes sequences such as: <F4 90 80 80>, <C0 80>,
70 or <A0> in UTF-8, and values above 0x10FFFF in UTF-32. Conformant code
71 must check for illegal sequences.
73 When the flag is set to lenient, characters over 0x10FFFF are converted
74 to the replacement character; otherwise (when the flag is set to strict)
75 they constitute an error.
78 The value "sourceIllegal" is returned from some routines if the input
79 sequence is malformed. When "sourceIllegal" is returned, the source
80 value will point to the illegal value that caused the problem. E.g.,
81 in UTF-8 when a sequence is malformed, it points to the start of the
84 Author: Mark E. Davis, 1994.
85 Rev History: Rick McGowan, fixes & updates May 2001.
86 Fixes & updates, Sept 2001.
88 ------------------------------------------------------------------------ */
90 #ifndef CLANG_BASIC_CONVERTUTF_H
91 #define CLANG_BASIC_CONVERTUTF_H
93 /* ---------------------------------------------------------------------
94 The following 4 definitions are compiler-specific.
95 The C standard does not guarantee that wchar_t has at least
96 16 bits, so wchar_t is no less portable than unsigned short!
97 All should be unsigned values to avoid sign extension during
98 bit mask & shift operations.
99 ------------------------------------------------------------------------ */
101 typedef unsigned int UTF32; /* at least 32 bits */
102 typedef unsigned short UTF16; /* at least 16 bits */
103 typedef unsigned char UTF8; /* typically 8 bits */
104 typedef unsigned char Boolean; /* 0 or 1 */
106 /* Some fundamental constants */
107 #define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD
108 #define UNI_MAX_BMP (UTF32)0x0000FFFF
109 #define UNI_MAX_UTF16 (UTF32)0x0010FFFF
110 #define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF
111 #define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF
113 #define UNI_MAX_UTF8_BYTES_PER_CODE_POINT 4
116 conversionOK, /* conversion successful */
117 sourceExhausted, /* partial character in source, but hit end */
118 targetExhausted, /* insuff. room in target for conversion */
119 sourceIllegal /* source sequence is illegal/malformed */
123 strictConversion = 0,
127 /* This is for C++ and does no harm in C */
132 ConversionResult ConvertUTF8toUTF16 (
133 const UTF8** sourceStart, const UTF8* sourceEnd,
134 UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags);
136 ConversionResult ConvertUTF8toUTF32 (
137 const UTF8** sourceStart, const UTF8* sourceEnd,
138 UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags);
140 ConversionResult ConvertUTF16toUTF8 (
141 const UTF16** sourceStart, const UTF16* sourceEnd,
142 UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);
144 ConversionResult ConvertUTF32toUTF8 (
145 const UTF32** sourceStart, const UTF32* sourceEnd,
146 UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);
148 ConversionResult ConvertUTF16toUTF32 (
149 const UTF16** sourceStart, const UTF16* sourceEnd,
150 UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags);
152 ConversionResult ConvertUTF32toUTF16 (
153 const UTF32** sourceStart, const UTF32* sourceEnd,
154 UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags);
156 Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd);
158 Boolean isLegalUTF8String(const UTF8 **source, const UTF8 *sourceEnd);
160 unsigned getNumBytesForUTF8(UTF8 firstByte);
165 /*************************************************************************/
166 /* Below are LLVM-specific wrappers of the functions above. */
168 #include "llvm/ADT/StringRef.h"
173 * Convert an UTF8 StringRef to UTF8, UTF16, or UTF32 depending on
174 * WideCharWidth. The converted data is written to ResultPtr, which needs to
175 * point to at least WideCharWidth * (Source.Size() + 1) bytes. On success,
176 * ResultPtr will point one after the end of the copied string. On failure,
177 * ResultPtr will not be changed, and ErrorPtr will be set to the location of
178 * the first character which could not be converted.
179 * \return true on success.
181 bool ConvertUTF8toWide(unsigned WideCharWidth, llvm::StringRef Source,
182 char *&ResultPtr, const UTF8 *&ErrorPtr);
185 * Convert an Unicode code point to UTF8 sequence.
187 * \param Source a Unicode code point.
188 * \param [in,out] ResultPtr pointer to the output buffer, needs to be at least
189 * \c UNI_MAX_UTF8_BYTES_PER_CODE_POINT bytes. On success \c ResultPtr is
190 * updated one past end of the converted sequence.
192 * \returns true on success.
194 bool ConvertCodePointToUTF8(unsigned Source, char *&ResultPtr);
197 * Convert the first UTF8 sequence in the given source buffer to a UTF32
200 * \param [in,out] source A pointer to the source buffer. If the conversion
201 * succeeds, this pointer will be updated to point to the byte just past the
202 * end of the converted sequence.
203 * \param sourceEnd A pointer just past the end of the source buffer.
204 * \param [out] target The converted code
205 * \param flags Whether the conversion is strict or lenient.
207 * \returns conversionOK on success
209 * \sa ConvertUTF8toUTF32
211 static inline ConversionResult convertUTF8Sequence(const UTF8 **source,
212 const UTF8 *sourceEnd,
214 ConversionFlags flags) {
215 if (*source == sourceEnd)
216 return sourceExhausted;
217 unsigned size = getNumBytesForUTF8(**source);
218 if ((ptrdiff_t)size > sourceEnd - *source)
219 return sourceExhausted;
220 return ConvertUTF8toUTF32(source, *source + size, &target, target + 1, flags);
222 } // end namespace llvm
226 /* --------------------------------------------------------------------- */