LLVM 22.0.0git
ConvertUTF.h
Go to the documentation of this file.
1/*===--- ConvertUTF.h - Universal Character Names conversions ---------------===
2 *
3 * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 * See https://llvm.org/LICENSE.txt for license information.
5 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 *
7 *==------------------------------------------------------------------------==*/
8/*
9 * Copyright © 1991-2015 Unicode, Inc. All rights reserved.
10 * Distributed under the Terms of Use in
11 * http://www.unicode.org/copyright.html.
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of the Unicode data files and any associated documentation
15 * (the "Data Files") or Unicode software and any associated documentation
16 * (the "Software") to deal in the Data Files or Software
17 * without restriction, including without limitation the rights to use,
18 * copy, modify, merge, publish, distribute, and/or sell copies of
19 * the Data Files or Software, and to permit persons to whom the Data Files
20 * or Software are furnished to do so, provided that
21 * (a) this copyright and permission notice appear with all copies
22 * of the Data Files or Software,
23 * (b) this copyright and permission notice appear in associated
24 * documentation, and
25 * (c) there is clear notice in each modified Data File or in the Software
26 * as well as in the documentation associated with the Data File(s) or
27 * Software that the data or software has been modified.
28 *
29 * THE DATA FILES AND SOFTWARE ARE PROVIDED "AS IS", WITHOUT WARRANTY OF
30 * ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
31 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 * NONINFRINGEMENT OF THIRD PARTY RIGHTS.
33 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS
34 * NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL
35 * DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
36 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
37 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
38 * PERFORMANCE OF THE DATA FILES OR SOFTWARE.
39 *
40 * Except as contained in this notice, the name of a copyright holder
41 * shall not be used in advertising or otherwise to promote the sale,
42 * use or other dealings in these Data Files or Software without prior
43 * written authorization of the copyright holder.
44 */
45
46/* ---------------------------------------------------------------------
47
48 Conversions between UTF32, UTF-16, and UTF-8. Header file.
49
50 Several functions are included here, forming a complete set of
51 conversions between the three formats. UTF-7 is not included
52 here, but is handled in a separate source file.
53
54 Each of these routines takes pointers to input buffers and output
55 buffers. The input buffers are const.
56
57 Each routine converts the text between *sourceStart and sourceEnd,
58 putting the result into the buffer between *targetStart and
59 targetEnd. Note: the end pointers are *after* the last item: e.g.
60 *(sourceEnd - 1) is the last item.
61
62 The return result indicates whether the conversion was successful,
63 and if not, whether the problem was in the source or target buffers.
64 (Only the first encountered problem is indicated.)
65
66 After the conversion, *sourceStart and *targetStart are both
67 updated to point to the end of last text successfully converted in
68 the respective buffers.
69
70 Input parameters:
71 sourceStart - pointer to a pointer to the source buffer.
72 The contents of this are modified on return so that
73 it points at the next thing to be converted.
74 targetStart - similarly, pointer to pointer to the target buffer.
75 sourceEnd, targetEnd - respectively pointers to the ends of the
76 two buffers, for overflow checking only.
77
78 These conversion functions take a ConversionFlags argument. When this
79 flag is set to strict, both irregular sequences and isolated surrogates
80 will cause an error. When the flag is set to lenient, both irregular
81 sequences and isolated surrogates are converted.
82
83 Whether the flag is strict or lenient, all illegal sequences will cause
84 an error return. This includes sequences such as: <F4 90 80 80>, <C0 80>,
85 or <A0> in UTF-8, and values above 0x10FFFF in UTF-32. Conformant code
86 must check for illegal sequences.
87
88 When the flag is set to lenient, characters over 0x10FFFF are converted
89 to the replacement character; otherwise (when the flag is set to strict)
90 they constitute an error.
91
92 Output parameters:
93 The value "sourceIllegal" is returned from some routines if the input
94 sequence is malformed. When "sourceIllegal" is returned, the source
95 value will point to the illegal value that caused the problem. E.g.,
96 in UTF-8 when a sequence is malformed, it points to the start of the
97 malformed sequence.
98
99 Author: Mark E. Davis, 1994.
100 Rev History: Rick McGowan, fixes & updates May 2001.
101 Fixes & updates, Sept 2001.
102
103------------------------------------------------------------------------ */
104
105#ifndef LLVM_SUPPORT_CONVERTUTF_H
106#define LLVM_SUPPORT_CONVERTUTF_H
107
109#include <cstddef>
110#include <string>
111
112#if defined(_WIN32)
113#include <system_error>
114#endif
115
116// Wrap everything in namespace llvm so that programs can link with llvm and
117// their own version of the unicode libraries.
118
119namespace llvm {
120
121/* ---------------------------------------------------------------------
122 The following 4 definitions are compiler-specific.
123 The C standard does not guarantee that wchar_t has at least
124 16 bits, so wchar_t is no less portable than unsigned short!
125 All should be unsigned values to avoid sign extension during
126 bit mask & shift operations.
127------------------------------------------------------------------------ */
128
129typedef unsigned int UTF32; /* at least 32 bits */
130typedef unsigned short UTF16; /* at least 16 bits */
131typedef unsigned char UTF8; /* typically 8 bits */
132typedef unsigned char Boolean; /* 0 or 1 */
133
134/* Some fundamental constants */
135#define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD
136#define UNI_MAX_BMP (UTF32)0x0000FFFF
137#define UNI_MAX_UTF16 (UTF32)0x0010FFFF
138#define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF
139#define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF
140
141#define UNI_MAX_UTF8_BYTES_PER_CODE_POINT 4
142
143#define UNI_UTF16_BYTE_ORDER_MARK_NATIVE 0xFEFF
144#define UNI_UTF16_BYTE_ORDER_MARK_SWAPPED 0xFFFE
145
146#define UNI_UTF32_BYTE_ORDER_MARK_NATIVE 0x0000FEFF
147#define UNI_UTF32_BYTE_ORDER_MARK_SWAPPED 0xFFFE0000
148
149typedef enum {
150 conversionOK, /* conversion successful */
151 sourceExhausted, /* partial character in source, but hit end */
152 targetExhausted, /* insuff. room in target for conversion */
153 sourceIllegal /* source sequence is illegal/malformed */
155
156typedef enum {
160
162 const UTF8 *sourceEnd,
163 UTF16 **targetStart,
164 UTF16 *targetEnd,
165 ConversionFlags flags);
166
167/**
168 * Convert a partial UTF8 sequence to UTF32. If the sequence ends in an
169 * incomplete code unit sequence, returns \c sourceExhausted.
170 */
172 const UTF8 *sourceEnd,
173 UTF32 **targetStart,
174 UTF32 *targetEnd,
175 ConversionFlags flags);
176
177/**
178 * Convert a partial UTF8 sequence to UTF32. If the sequence ends in an
179 * incomplete code unit sequence, returns \c sourceIllegal.
180 */
182 const UTF8 *sourceEnd,
183 UTF32 **targetStart,
184 UTF32 *targetEnd,
185 ConversionFlags flags);
186
188 const UTF16 *sourceEnd,
189 UTF8 **targetStart,
190 UTF8 *targetEnd,
191 ConversionFlags flags);
192
194 const UTF32 *sourceEnd,
195 UTF8 **targetStart,
196 UTF8 *targetEnd,
197 ConversionFlags flags);
198
200 const UTF16 *sourceEnd,
201 UTF32 **targetStart,
202 UTF32 *targetEnd,
203 ConversionFlags flags);
204
206 const UTF32 *sourceEnd,
207 UTF16 **targetStart,
208 UTF16 *targetEnd,
209 ConversionFlags flags);
210
211LLVM_ABI Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd);
212
213LLVM_ABI Boolean isLegalUTF8String(const UTF8 **source, const UTF8 *sourceEnd);
214
215LLVM_ABI unsigned getUTF8SequenceSize(const UTF8 *source,
216 const UTF8 *sourceEnd);
217
218LLVM_ABI unsigned getNumBytesForUTF8(UTF8 firstByte);
219
220/*************************************************************************/
221/* Below are LLVM-specific wrappers of the functions above. */
222
223template <typename T> class ArrayRef;
224template <typename T> class SmallVectorImpl;
225class StringRef;
226
227/**
228 * Convert an UTF8 StringRef to UTF8, UTF16, or UTF32 depending on
229 * WideCharWidth. The converted data is written to ResultPtr, which needs to
230 * point to at least WideCharWidth * (Source.Size() + 1) bytes. On success,
231 * ResultPtr will point one after the end of the copied string. On failure,
232 * ResultPtr will not be changed, and ErrorPtr will be set to the location of
233 * the first character which could not be converted.
234 * \return true on success.
235 */
236LLVM_ABI bool ConvertUTF8toWide(unsigned WideCharWidth, llvm::StringRef Source,
237 char *&ResultPtr, const UTF8 *&ErrorPtr);
238
239/**
240* Converts a UTF-8 StringRef to a std::wstring.
241* \return true on success.
242*/
243LLVM_ABI bool ConvertUTF8toWide(llvm::StringRef Source, std::wstring &Result);
244
245/**
246* Converts a UTF-8 C-string to a std::wstring.
247* \return true on success.
248*/
249LLVM_ABI bool ConvertUTF8toWide(const char *Source, std::wstring &Result);
250
251/**
252* Converts a std::wstring to a UTF-8 encoded std::string.
253* \return true on success.
254*/
255LLVM_ABI bool convertWideToUTF8(const std::wstring &Source,
256 std::string &Result);
257
258/**
259 * Convert an Unicode code point to UTF8 sequence.
260 *
261 * \param Source a Unicode code point.
262 * \param [in,out] ResultPtr pointer to the output buffer, needs to be at least
263 * \c UNI_MAX_UTF8_BYTES_PER_CODE_POINT bytes. On success \c ResultPtr is
264 * updated one past end of the converted sequence.
265 *
266 * \returns true on success.
267 */
268LLVM_ABI bool ConvertCodePointToUTF8(unsigned Source, char *&ResultPtr);
269
270/**
271 * Convert the first UTF8 sequence in the given source buffer to a UTF32
272 * code point.
273 *
274 * \param [in,out] source A pointer to the source buffer. If the conversion
275 * succeeds, this pointer will be updated to point to the byte just past the
276 * end of the converted sequence.
277 * \param sourceEnd A pointer just past the end of the source buffer.
278 * \param [out] target The converted code
279 * \param flags Whether the conversion is strict or lenient.
280 *
281 * \returns conversionOK on success
282 *
283 * \sa ConvertUTF8toUTF32
284 */
286 const UTF8 *sourceEnd,
287 UTF32 *target,
288 ConversionFlags flags) {
289 if (*source == sourceEnd)
290 return sourceExhausted;
291 unsigned size = getNumBytesForUTF8(**source);
292 if ((ptrdiff_t)size > sourceEnd - *source)
293 return sourceExhausted;
294 return ConvertUTF8toUTF32(source, *source + size, &target, target + 1, flags);
295}
296
297/**
298 * Returns true if a blob of text starts with a UTF-16 big or little endian byte
299 * order mark.
300 */
301LLVM_ABI bool hasUTF16ByteOrderMark(ArrayRef<char> SrcBytes);
302
303/**
304 * Converts a stream of raw bytes assumed to be UTF16 into a UTF8 std::string.
305 *
306 * \param [in] SrcBytes A buffer of what is assumed to be UTF-16 encoded text.
307 * \param [out] Out Converted UTF-8 is stored here on success.
308 * \returns true on success
309 */
310LLVM_ABI bool convertUTF16ToUTF8String(ArrayRef<char> SrcBytes,
311 std::string &Out);
312
313/**
314* Converts a UTF16 string into a UTF8 std::string.
315*
316* \param [in] Src A buffer of UTF-16 encoded text.
317* \param [out] Out Converted UTF-8 is stored here on success.
318* \returns true on success
319*/
320LLVM_ABI bool convertUTF16ToUTF8String(ArrayRef<UTF16> Src, std::string &Out);
321
322/**
323 * Converts a stream of raw bytes assumed to be UTF32 into a UTF8 std::string.
324 *
325 * \param [in] SrcBytes A buffer of what is assumed to be UTF-32 encoded text.
326 * \param [out] Out Converted UTF-8 is stored here on success.
327 * \returns true on success
328 */
329LLVM_ABI bool convertUTF32ToUTF8String(ArrayRef<char> SrcBytes,
330 std::string &Out);
331
332/**
333 * Converts a UTF32 string into a UTF8 std::string.
334 *
335 * \param [in] Src A buffer of UTF-32 encoded text.
336 * \param [out] Out Converted UTF-8 is stored here on success.
337 * \returns true on success
338 */
339LLVM_ABI bool convertUTF32ToUTF8String(ArrayRef<UTF32> Src, std::string &Out);
340
341/**
342 * Converts a UTF-8 string into a UTF-16 string with native endianness.
343 *
344 * \returns true on success
345 */
346LLVM_ABI bool convertUTF8ToUTF16String(StringRef SrcUTF8,
347 SmallVectorImpl<UTF16> &DstUTF16);
348
352
353#if defined(_WIN32)
354namespace sys {
355namespace windows {
356LLVM_ABI std::error_code UTF8ToUTF16(StringRef utf8,
357 SmallVectorImpl<wchar_t> &utf16);
358/// Convert to UTF16 from the current code page used in the system
359LLVM_ABI std::error_code CurCPToUTF16(StringRef utf8,
360 SmallVectorImpl<wchar_t> &utf16);
361LLVM_ABI std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
362 SmallVectorImpl<char> &utf8);
363/// Convert from UTF16 to the current code page used in the system
364LLVM_ABI std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len,
365 SmallVectorImpl<char> &utf8);
366} // namespace windows
367} // namespace sys
368#endif
369
370} /* end namespace llvm */
371
372#endif
#define LLVM_ABI
Definition: Compiler.h:213
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1702
unsigned short UTF16
Definition: ConvertUTF.h:130
LLVM_ABI ConversionResult ConvertUTF8toUTF32(const UTF8 **sourceStart, const UTF8 *sourceEnd, UTF32 **targetStart, UTF32 *targetEnd, ConversionFlags flags)
Convert a partial UTF8 sequence to UTF32.
Definition: ConvertUTF.cpp:736
LLVM_ABI bool IsSingleCodeUnitUTF16Codepoint(unsigned)
LLVM_ABI bool IsSingleCodeUnitUTF32Codepoint(unsigned)
LLVM_ABI unsigned getNumBytesForUTF8(UTF8 firstByte)
Definition: ConvertUTF.cpp:545
LLVM_ABI bool hasUTF16ByteOrderMark(ArrayRef< char > SrcBytes)
Returns true if a blob of text starts with a UTF-16 big or little endian byte order mark.
LLVM_ABI ConversionResult ConvertUTF8toUTF32Partial(const UTF8 **sourceStart, const UTF8 *sourceEnd, UTF32 **targetStart, UTF32 *targetEnd, ConversionFlags flags)
Convert a partial UTF8 sequence to UTF32.
Definition: ConvertUTF.cpp:727
ConversionResult convertUTF8Sequence(const UTF8 **source, const UTF8 *sourceEnd, UTF32 *target, ConversionFlags flags)
Convert the first UTF8 sequence in the given source buffer to a UTF32 code point.
Definition: ConvertUTF.h:285
ConversionFlags
Definition: ConvertUTF.h:156
@ lenientConversion
Definition: ConvertUTF.h:158
@ strictConversion
Definition: ConvertUTF.h:157
ConversionResult
Definition: ConvertUTF.h:149
@ targetExhausted
Definition: ConvertUTF.h:152
@ sourceExhausted
Definition: ConvertUTF.h:151
@ conversionOK
Definition: ConvertUTF.h:150
@ sourceIllegal
Definition: ConvertUTF.h:153
LLVM_ABI bool convertWideToUTF8(const std::wstring &Source, std::string &Result)
Converts a std::wstring to a UTF-8 encoded std::string.
LLVM_ABI bool convertUTF16ToUTF8String(ArrayRef< char > SrcBytes, std::string &Out)
Converts a stream of raw bytes assumed to be UTF16 into a UTF8 std::string.
LLVM_ABI ConversionResult ConvertUTF32toUTF16(const UTF32 **sourceStart, const UTF32 *sourceEnd, UTF16 **targetStart, UTF16 *targetEnd, ConversionFlags flags)
Definition: ConvertUTF.cpp:160
LLVM_ABI bool IsSingleCodeUnitUTF8Codepoint(unsigned)
LLVM_ABI Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd)
Definition: ConvertUTF.cpp:428
unsigned char Boolean
Definition: ConvertUTF.h:132
LLVM_ABI ConversionResult ConvertUTF16toUTF8(const UTF16 **sourceStart, const UTF16 *sourceEnd, UTF8 **targetStart, UTF8 *targetEnd, ConversionFlags flags)
Definition: ConvertUTF.cpp:263
LLVM_ABI bool convertUTF32ToUTF8String(ArrayRef< char > SrcBytes, std::string &Out)
Converts a stream of raw bytes assumed to be UTF32 into a UTF8 std::string.
LLVM_ABI bool ConvertUTF8toWide(unsigned WideCharWidth, llvm::StringRef Source, char *&ResultPtr, const UTF8 *&ErrorPtr)
Convert an UTF8 StringRef to UTF8, UTF16, or UTF32 depending on WideCharWidth.
LLVM_ABI ConversionResult ConvertUTF32toUTF8(const UTF32 **sourceStart, const UTF32 *sourceEnd, UTF8 **targetStart, UTF8 *targetEnd, ConversionFlags flags)
Definition: ConvertUTF.cpp:333
LLVM_ABI Boolean isLegalUTF8String(const UTF8 **source, const UTF8 *sourceEnd)
Definition: ConvertUTF.cpp:555
LLVM_ABI unsigned getUTF8SequenceSize(const UTF8 *source, const UTF8 *sourceEnd)
Definition: ConvertUTF.cpp:440
LLVM_ABI ConversionResult ConvertUTF16toUTF32(const UTF16 **sourceStart, const UTF16 *sourceEnd, UTF32 **targetStart, UTF32 *targetEnd, ConversionFlags flags)
Definition: ConvertUTF.cpp:209
LLVM_ABI bool convertUTF8ToUTF16String(StringRef SrcUTF8, SmallVectorImpl< UTF16 > &DstUTF16)
Converts a UTF-8 string into a UTF-16 string with native endianness.
LLVM_ABI bool ConvertCodePointToUTF8(unsigned Source, char *&ResultPtr)
Convert an Unicode code point to UTF8 sequence.
unsigned char UTF8
Definition: ConvertUTF.h:131
LLVM_ABI ConversionResult ConvertUTF8toUTF16(const UTF8 **sourceStart, const UTF8 *sourceEnd, UTF16 **targetStart, UTF16 *targetEnd, ConversionFlags flags)
Definition: ConvertUTF.cpp:567
unsigned int UTF32
Definition: ConvertUTF.h:129