ConvertUTF.h 14 KB

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