WPILibC++ 2023.4.3
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 2001-2004 Unicode, Inc.
10 *
11 * Disclaimer
12 *
13 * This source code is provided as is by Unicode, Inc. No claims are
14 * made as to fitness for any particular purpose. No warranties of any
15 * kind are expressed or implied. The recipient agrees to determine
16 * applicability of information provided. If this file has been
17 * purchased on magnetic or optical media from Unicode, Inc., the
18 * sole remedy for any claim will be exchange of defective media
19 * within 90 days of receipt.
20 *
21 * Limitations on Rights to Redistribute This Code
22 *
23 * Unicode, Inc. hereby grants the right to freely use the information
24 * supplied in this file in the creation of products supporting the
25 * Unicode Standard, and to make copies of this file in any form
26 * for internal or external distribution as long as this notice
27 * remains attached.
28 */
29
30/* ---------------------------------------------------------------------
31
32 Conversions between UTF32, UTF-16, and UTF-8. Header file.
33
34 Several funtions are included here, forming a complete set of
35 conversions between the three formats. UTF-7 is not included
36 here, but is handled in a separate source file.
37
38 Each of these routines takes pointers to input buffers and output
39 buffers. The input buffers are const.
40
41 Each routine converts the text between *sourceStart and sourceEnd,
42 putting the result into the buffer between *targetStart and
43 targetEnd. Note: the end pointers are *after* the last item: e.g.
44 *(sourceEnd - 1) is the last item.
45
46 The return result indicates whether the conversion was successful,
47 and if not, whether the problem was in the source or target buffers.
48 (Only the first encountered problem is indicated.)
49
50 After the conversion, *sourceStart and *targetStart are both
51 updated to point to the end of last text successfully converted in
52 the respective buffers.
53
54 Input parameters:
55 sourceStart - pointer to a pointer to the source buffer.
56 The contents of this are modified on return so that
57 it points at the next thing to be converted.
58 targetStart - similarly, pointer to pointer to the target buffer.
59 sourceEnd, targetEnd - respectively pointers to the ends of the
60 two buffers, for overflow checking only.
61
62 These conversion functions take a ConversionFlags argument. When this
63 flag is set to strict, both irregular sequences and isolated surrogates
64 will cause an error. When the flag is set to lenient, both irregular
65 sequences and isolated surrogates are converted.
66
67 Whether the flag is strict or lenient, all illegal sequences will cause
68 an error return. This includes sequences such as: <F4 90 80 80>, <C0 80>,
69 or <A0> in UTF-8, and values above 0x10FFFF in UTF-32. Conformant code
70 must check for illegal sequences.
71
72 When the flag is set to lenient, characters over 0x10FFFF are converted
73 to the replacement character; otherwise (when the flag is set to strict)
74 they constitute an error.
75
76 Output parameters:
77 The value "sourceIllegal" is returned from some routines if the input
78 sequence is malformed. When "sourceIllegal" is returned, the source
79 value will point to the illegal value that caused the problem. E.g.,
80 in UTF-8 when a sequence is malformed, it points to the start of the
81 malformed sequence.
82
83 Author: Mark E. Davis, 1994.
84 Rev History: Rick McGowan, fixes & updates May 2001.
85 Fixes & updates, Sept 2001.
86
87------------------------------------------------------------------------ */
88
89#ifndef WPIUTIL_WPI_CONVERTUTF_H
90#define WPIUTIL_WPI_CONVERTUTF_H
91
92#include <span>
93
94#include <cstddef>
95#include <string>
96#include <string_view>
97#include <system_error>
98
99// Wrap everything in namespace wpi so that programs can link with llvm and
100// their own version of the unicode libraries.
101
102namespace wpi {
103
104/* ---------------------------------------------------------------------
105 The following 4 definitions are compiler-specific.
106 The C standard does not guarantee that wchar_t has at least
107 16 bits, so wchar_t is no less portable than unsigned short!
108 All should be unsigned values to avoid sign extension during
109 bit mask & shift operations.
110------------------------------------------------------------------------ */
111
112typedef unsigned int UTF32; /* at least 32 bits */
113typedef unsigned short UTF16; /* at least 16 bits */
114typedef unsigned char UTF8; /* typically 8 bits */
115typedef bool Boolean; /* 0 or 1 */
116
117/* Some fundamental constants */
118#define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD
119#define UNI_MAX_BMP (UTF32)0x0000FFFF
120#define UNI_MAX_UTF16 (UTF32)0x0010FFFF
121#define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF
122#define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF
123
124#define UNI_MAX_UTF8_BYTES_PER_CODE_POINT 4
125
126#define UNI_UTF16_BYTE_ORDER_MARK_NATIVE 0xFEFF
127#define UNI_UTF16_BYTE_ORDER_MARK_SWAPPED 0xFFFE
128
129typedef enum {
130 conversionOK, /* conversion successful */
131 sourceExhausted, /* partial character in source, but hit end */
132 targetExhausted, /* insuff. room in target for conversion */
133 sourceIllegal /* source sequence is illegal/malformed */
135
136typedef enum {
140
142 const UTF8** sourceStart, const UTF8* sourceEnd,
143 UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags);
144
145/**
146 * Convert a partial UTF8 sequence to UTF32. If the sequence ends in an
147 * incomplete code unit sequence, returns \c sourceExhausted.
148 */
150 const UTF8** sourceStart, const UTF8* sourceEnd,
151 UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags);
152
153/**
154 * Convert a partial UTF8 sequence to UTF32. If the sequence ends in an
155 * incomplete code unit sequence, returns \c sourceIllegal.
156 */
158 const UTF8** sourceStart, const UTF8* sourceEnd,
159 UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags);
160
162 const UTF16** sourceStart, const UTF16* sourceEnd,
163 UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);
164
166 const UTF32** sourceStart, const UTF32* sourceEnd,
167 UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);
168
170 const UTF16** sourceStart, const UTF16* sourceEnd,
171 UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags);
172
174 const UTF32** sourceStart, const UTF32* sourceEnd,
175 UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags);
176
177Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd);
178
179Boolean isLegalUTF8String(const UTF8 **source, const UTF8 *sourceEnd);
180
181unsigned getNumBytesForUTF8(UTF8 firstByte);
182
183/*************************************************************************/
184/* Below are LLVM-specific wrappers of the functions above. */
185
186template <typename T> class SmallVectorImpl;
187
188/**
189 * Convert an UTF8 string_view to UTF8, UTF16, or UTF32 depending on
190 * WideCharWidth. The converted data is written to ResultPtr, which needs to
191 * point to at least WideCharWidth * (Source.Size() + 1) bytes. On success,
192 * ResultPtr will point one after the end of the copied string. On failure,
193 * ResultPtr will not be changed, and ErrorPtr will be set to the location of
194 * the first character which could not be converted.
195 * \return true on success.
196 */
197bool ConvertUTF8toWide(unsigned WideCharWidth, std::string_view Source,
198 char *&ResultPtr, const UTF8 *&ErrorPtr);
199
200/**
201* Converts a UTF-8 string_view to a std::wstring.
202* \return true on success.
203*/
204bool ConvertUTF8toWide(std::string_view Source, std::wstring &Result);
205
206/**
207* Converts a UTF-8 C-string to a std::wstring.
208* \return true on success.
209*/
210bool ConvertUTF8toWide(const char *Source, std::wstring &Result);
211
212/**
213* Converts a std::wstring to a UTF-8 encoded std::string.
214* \return true on success.
215*/
216bool convertWideToUTF8(const std::wstring &Source, SmallVectorImpl<char> &Result);
217
218
219/**
220 * Convert an Unicode code point to UTF8 sequence.
221 *
222 * \param Source a Unicode code point.
223 * \param [in,out] ResultPtr pointer to the output buffer, needs to be at least
224 * \c UNI_MAX_UTF8_BYTES_PER_CODE_POINT bytes. On success \c ResultPtr is
225 * updated one past end of the converted sequence.
226 *
227 * \returns true on success.
228 */
229bool ConvertCodePointToUTF8(unsigned Source, char *&ResultPtr);
230
231/**
232 * Convert the first UTF8 sequence in the given source buffer to a UTF32
233 * code point.
234 *
235 * \param [in,out] source A pointer to the source buffer. If the conversion
236 * succeeds, this pointer will be updated to point to the byte just past the
237 * end of the converted sequence.
238 * \param sourceEnd A pointer just past the end of the source buffer.
239 * \param [out] target The converted code
240 * \param flags Whether the conversion is strict or lenient.
241 *
242 * \returns conversionOK on success
243 *
244 * \sa ConvertUTF8toUTF32
245 */
247 const UTF8 *sourceEnd,
248 UTF32 *target,
250 if (*source == sourceEnd)
251 return sourceExhausted;
252 unsigned size = getNumBytesForUTF8(**source);
253 if ((ptrdiff_t)size > sourceEnd - *source)
254 return sourceExhausted;
255 return ConvertUTF8toUTF32(source, *source + size, &target, target + 1, flags);
256}
257
258/**
259 * Returns true if a blob of text starts with a UTF-16 big or little endian byte
260 * order mark.
261 */
262bool hasUTF16ByteOrderMark(std::span<const char> SrcBytes);
263
264/**
265 * Converts a stream of raw bytes assumed to be UTF16 into a UTF8 std::string.
266 *
267 * \param [in] SrcBytes A buffer of what is assumed to be UTF-16 encoded text.
268 * \param [out] Out Converted UTF-8 is stored here on success.
269 * \returns true on success
270 */
271bool convertUTF16ToUTF8String(std::span<const char> SrcBytes, SmallVectorImpl<char> &Out);
272
273/**
274* Converts a UTF16 string into a UTF8 std::string.
275*
276* \param [in] Src A buffer of UTF-16 encoded text.
277* \param [out] Out Converted UTF-8 is stored here on success.
278* \returns true on success
279*/
280bool convertUTF16ToUTF8String(std::span<const UTF16> Src, SmallVectorImpl<char> &Out);
281
282/**
283 * Converts a UTF-8 string into a UTF-16 string with native endianness.
284 *
285 * \returns true on success
286 */
288 SmallVectorImpl<UTF16> &DstUTF16);
289
290#if defined(_WIN32)
291namespace sys {
292namespace windows {
293std::error_code UTF8ToUTF16(std::string_view utf8, SmallVectorImpl<wchar_t> &utf16);
294/// Convert to UTF16 from the current code page used in the system
295std::error_code CurCPToUTF16(std::string_view utf8, SmallVectorImpl<wchar_t> &utf16);
296std::error_code UTF16ToUTF8(const wchar_t *utf16, size_t utf16_len,
298/// Convert from UTF16 to the current code page used in the system
299std::error_code UTF16ToCurCP(const wchar_t *utf16, size_t utf16_len,
301} // namespace windows
302} // namespace sys
303#endif
304
305} /* end namespace wpi */
306
307#endif
and restrictions which apply to each piece of software is included later in this file and or inside of the individual applicable source files The disclaimer of warranty in the WPILib license above applies to all code in and nothing in any of the other licenses gives permission to use the names of FIRST nor the names of the WPILib contributors to endorse or promote products derived from this software The following pieces of software have additional or alternate and or Google Inc All rights reserved Redistribution and use in source and binary with or without are permitted provided that the following conditions are this list of conditions and the following disclaimer *Redistributions in binary form must reproduce the above copyright this list of conditions and the following disclaimer in the documentation and or other materials provided with the distribution *Neither the name of Google Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation source
Definition: ThirdPartyNotices.txt:114
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:557
basic_string_view< char > string_view
Definition: core.h:520
EIGEN_CONSTEXPR Index size(const T &x)
Definition: Meta.h:479
/file This file defines the SmallVector class.
Definition: AprilTagFieldLayout.h:18
bool convertUTF8ToUTF16String(std::string_view SrcUTF8, SmallVectorImpl< UTF16 > &DstUTF16)
Converts a UTF-8 string into a UTF-16 string with native endianness.
bool ConvertCodePointToUTF8(unsigned Source, char *&ResultPtr)
Convert an Unicode code point to UTF8 sequence.
Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd)
ConversionResult ConvertUTF32toUTF16(const UTF32 **sourceStart, const UTF32 *sourceEnd, UTF16 **targetStart, UTF16 *targetEnd, ConversionFlags flags)
bool hasUTF16ByteOrderMark(std::span< const char > SrcBytes)
Returns true if a blob of text starts with a UTF-16 big or little endian byte order mark.
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:246
unsigned char UTF8
Definition: ConvertUTF.h:114
unsigned short UTF16
Definition: ConvertUTF.h:113
ConversionResult ConvertUTF8toUTF16(const UTF8 **sourceStart, const UTF8 *sourceEnd, UTF16 **targetStart, UTF16 *targetEnd, ConversionFlags flags)
bool convertWideToUTF8(const std::wstring &Source, SmallVectorImpl< char > &Result)
Converts a std::wstring to a UTF-8 encoded std::string.
bool ConvertUTF8toWide(unsigned WideCharWidth, std::string_view Source, char *&ResultPtr, const UTF8 *&ErrorPtr)
Convert an UTF8 string_view to UTF8, UTF16, or UTF32 depending on WideCharWidth.
unsigned getNumBytesForUTF8(UTF8 firstByte)
flags
Definition: http_parser.h:206
ConversionResult ConvertUTF32toUTF8(const UTF32 **sourceStart, const UTF32 *sourceEnd, UTF8 **targetStart, UTF8 *targetEnd, ConversionFlags flags)
ConversionResult ConvertUTF16toUTF32(const UTF16 **sourceStart, const UTF16 *sourceEnd, UTF32 **targetStart, UTF32 *targetEnd, ConversionFlags flags)
ConversionResult ConvertUTF8toUTF32Partial(const UTF8 **sourceStart, const UTF8 *sourceEnd, UTF32 **targetStart, UTF32 *targetEnd, ConversionFlags flags)
Convert a partial UTF8 sequence to UTF32.
ConversionFlags
Definition: ConvertUTF.h:136
@ lenientConversion
Definition: ConvertUTF.h:138
@ strictConversion
Definition: ConvertUTF.h:137
unsigned int UTF32
Definition: ConvertUTF.h:112
ConversionResult ConvertUTF8toUTF32(const UTF8 **sourceStart, const UTF8 *sourceEnd, UTF32 **targetStart, UTF32 *targetEnd, ConversionFlags flags)
Convert a partial UTF8 sequence to UTF32.
ConversionResult ConvertUTF16toUTF8(const UTF16 **sourceStart, const UTF16 *sourceEnd, UTF8 **targetStart, UTF8 *targetEnd, ConversionFlags flags)
ConversionResult
Definition: ConvertUTF.h:129
@ sourceIllegal
Definition: ConvertUTF.h:133
@ sourceExhausted
Definition: ConvertUTF.h:131
@ conversionOK
Definition: ConvertUTF.h:130
@ targetExhausted
Definition: ConvertUTF.h:132
bool Boolean
Definition: ConvertUTF.h:115
Boolean isLegalUTF8String(const UTF8 **source, const UTF8 *sourceEnd)
bool convertUTF16ToUTF8String(std::span< const char > SrcBytes, SmallVectorImpl< char > &Out)
Converts a stream of raw bytes assumed to be UTF16 into a UTF8 std::string.