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
129using UTF32 = unsigned int; /* at least 32 bits */
130using UTF16 = unsigned short; /* at least 16 bits */
131using UTF8 = unsigned char; /* typically 8 bits */
132using Boolean = unsigned char; /* 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
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 */
154};
155
157
159 const UTF8 *sourceEnd,
160 UTF16 **targetStart,
161 UTF16 *targetEnd,
162 ConversionFlags flags);
163
164/**
165 * Convert a partial UTF8 sequence to UTF32. If the sequence ends in an
166 * incomplete code unit sequence, returns \c sourceExhausted.
167 */
169 const UTF8 *sourceEnd,
170 UTF32 **targetStart,
171 UTF32 *targetEnd,
172 ConversionFlags flags);
173
174/**
175 * Convert a partial UTF8 sequence to UTF32. If the sequence ends in an
176 * incomplete code unit sequence, returns \c sourceIllegal.
177 */
179 const UTF8 *sourceEnd,
180 UTF32 **targetStart,
181 UTF32 *targetEnd,
182 ConversionFlags flags);
183
185 const UTF16 *sourceEnd,
186 UTF8 **targetStart,
187 UTF8 *targetEnd,
188 ConversionFlags flags);
189
191 const UTF32 *sourceEnd,
192 UTF8 **targetStart,
193 UTF8 *targetEnd,
194 ConversionFlags flags);
195
197 const UTF16 *sourceEnd,
198 UTF32 **targetStart,
199 UTF32 *targetEnd,
200 ConversionFlags flags);
201
203 const UTF32 *sourceEnd,
204 UTF16 **targetStart,
205 UTF16 *targetEnd,
206 ConversionFlags flags);
207
208LLVM_ABI Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd);
209
210LLVM_ABI Boolean isLegalUTF8String(const UTF8 **source, const UTF8 *sourceEnd);
211
212LLVM_ABI unsigned getUTF8SequenceSize(const UTF8 *source,
213 const UTF8 *sourceEnd);
214
215LLVM_ABI unsigned getNumBytesForUTF8(UTF8 firstByte);
216
217/*************************************************************************/
218/* Below are LLVM-specific wrappers of the functions above. */
219
220template <typename T> class ArrayRef;
221template <typename T> class SmallVectorImpl;
222class StringRef;
223
224/**
225 * Convert an UTF8 StringRef to UTF8, UTF16, or UTF32 depending on
226 * WideCharWidth. The converted data is written to ResultPtr, which needs to
227 * point to at least WideCharWidth * (Source.Size() + 1) bytes. On success,
228 * ResultPtr will point one after the end of the copied string. On failure,
229 * ResultPtr will not be changed, and ErrorPtr will be set to the location of
230 * the first character which could not be converted.
231 * \return true on success.
232 */
233LLVM_ABI bool ConvertUTF8toWide(unsigned WideCharWidth, llvm::StringRef Source,
234 char *&ResultPtr, const UTF8 *&ErrorPtr);
235
236/**
237* Converts a UTF-8 StringRef to a std::wstring.
238* \return true on success.
239*/
240LLVM_ABI bool ConvertUTF8toWide(llvm::StringRef Source, std::wstring &Result);
241
242/**
243* Converts a UTF-8 C-string to a std::wstring.
244* \return true on success.
245*/
246LLVM_ABI bool ConvertUTF8toWide(const char *Source, std::wstring &Result);
247
248/**
249* Converts a std::wstring to a UTF-8 encoded std::string.
250* \return true on success.
251*/
252LLVM_ABI bool convertWideToUTF8(const std::wstring &Source,
253 std::string &Result);
254
255/**
256 * Convert an Unicode code point to UTF8 sequence.
257 *
258 * \param Source a Unicode code point.
259 * \param [in,out] ResultPtr pointer to the output buffer, needs to be at least
260 * \c UNI_MAX_UTF8_BYTES_PER_CODE_POINT bytes. On success \c ResultPtr is
261 * updated one past end of the converted sequence.
262 *
263 * \returns true on success.
264 */
265LLVM_ABI bool ConvertCodePointToUTF8(unsigned Source, char *&ResultPtr);
266
267/**
268 * Convert the first UTF8 sequence in the given source buffer to a UTF32
269 * code point.
270 *
271 * \param [in,out] source A pointer to the source buffer. If the conversion
272 * succeeds, this pointer will be updated to point to the byte just past the
273 * end of the converted sequence.
274 * \param sourceEnd A pointer just past the end of the source buffer.
275 * \param [out] target The converted code
276 * \param flags Whether the conversion is strict or lenient.
277 *
278 * \returns conversionOK on success
279 *
280 * \sa ConvertUTF8toUTF32
281 */
283 const UTF8 *sourceEnd,
284 UTF32 *target,
285 ConversionFlags flags) {
286 if (*source == sourceEnd)
287 return sourceExhausted;
288 unsigned size = getNumBytesForUTF8(**source);
289 if ((ptrdiff_t)size > sourceEnd - *source)
290 return sourceExhausted;
291 return ConvertUTF8toUTF32(source, *source + size, &target, target + 1, flags);
292}
293
294/**
295 * Returns true if a blob of text starts with a UTF-16 big or little endian byte
296 * order mark.
297 */
298LLVM_ABI bool hasUTF16ByteOrderMark(ArrayRef<char> SrcBytes);
299
300/**
301 * Converts a stream of raw bytes assumed to be UTF16 into a UTF8 std::string.
302 *
303 * \param [in] SrcBytes A buffer of what is assumed to be UTF-16 encoded text.
304 * \param [out] Out Converted UTF-8 is stored here on success.
305 * \returns true on success
306 */
307LLVM_ABI bool convertUTF16ToUTF8String(ArrayRef<char> SrcBytes,
308 std::string &Out);
309
310/**
311* Converts a UTF16 string into a UTF8 std::string.
312*
313* \param [in] Src A buffer of UTF-16 encoded text.
314* \param [out] Out Converted UTF-8 is stored here on success.
315* \returns true on success
316*/
317LLVM_ABI bool convertUTF16ToUTF8String(ArrayRef<UTF16> Src, std::string &Out);
318
319/**
320 * Converts a stream of raw bytes assumed to be UTF32 into a UTF8 std::string.
321 *
322 * \param [in] SrcBytes A buffer of what is assumed to be UTF-32 encoded text.
323 * \param [out] Out Converted UTF-8 is stored here on success.
324 * \returns true on success
325 */
326LLVM_ABI bool convertUTF32ToUTF8String(ArrayRef<char> SrcBytes,
327 std::string &Out);
328
329/**
330 * Converts a UTF32 string into a UTF8 std::string.
331 *
332 * \param [in] Src A buffer of UTF-32 encoded text.
333 * \param [out] Out Converted UTF-8 is stored here on success.
334 * \returns true on success
335 */
336LLVM_ABI bool convertUTF32ToUTF8String(ArrayRef<UTF32> Src, std::string &Out);
337
338/**
339 * Converts a UTF-8 string into a UTF-16 string with native endianness.
340 *
341 * \returns true on success
342 */
343LLVM_ABI bool convertUTF8ToUTF16String(StringRef SrcUTF8,
344 SmallVectorImpl<UTF16> &DstUTF16);
345
349
350#if defined(_WIN32)
351namespace sys {
352namespace windows {
353LLVM_ABI std::error_code UTF8ToUTF16(StringRef utf8,
354 SmallVectorImpl<wchar_t> &utf16);
355/// Convert to UTF16 from the current code page used in the system
356LLVM_ABI std::error_code CurCPToUTF16(StringRef utf8,
357 SmallVectorImpl<wchar_t> &utf16);
358LLVM_ABI std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
359 SmallVectorImpl<char> &utf8);
360/// Convert from UTF16 to the current code page used in the system
361LLVM_ABI std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len,
362 SmallVectorImpl<char> &utf8);
363} // namespace windows
364} // namespace sys
365#endif
366
367} /* end namespace llvm */
368
369#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.
unsigned char UTF8
Definition ConvertUTF.h:131
unsigned char Boolean
Definition ConvertUTF.h:132
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:1655
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.
LLVM_ABI bool IsSingleCodeUnitUTF16Codepoint(unsigned)
LLVM_ABI bool IsSingleCodeUnitUTF32Codepoint(unsigned)
LLVM_ABI unsigned getNumBytesForUTF8(UTF8 firstByte)
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.
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:282
unsigned int UTF32
Definition ConvertUTF.h:129
ConversionFlags
Definition ConvertUTF.h:156
@ lenientConversion
Definition ConvertUTF.h:156
@ strictConversion
Definition ConvertUTF.h:156
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)
LLVM_ABI bool IsSingleCodeUnitUTF8Codepoint(unsigned)
LLVM_ABI Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd)
LLVM_ABI ConversionResult ConvertUTF16toUTF8(const UTF16 **sourceStart, const UTF16 *sourceEnd, UTF8 **targetStart, UTF8 *targetEnd, ConversionFlags flags)
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)
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI Boolean isLegalUTF8String(const UTF8 **source, const UTF8 *sourceEnd)
LLVM_ABI unsigned getUTF8SequenceSize(const UTF8 *source, const UTF8 *sourceEnd)
LLVM_ABI ConversionResult ConvertUTF16toUTF32(const UTF16 **sourceStart, const UTF16 *sourceEnd, UTF32 **targetStart, UTF32 *targetEnd, ConversionFlags flags)
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.
LLVM_ABI ConversionResult ConvertUTF8toUTF16(const UTF8 **sourceStart, const UTF8 *sourceEnd, UTF16 **targetStart, UTF16 *targetEnd, ConversionFlags flags)