ConvertUTF.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*===--- ConvertUTF.h - Universal Character Names conversions ---------------===
  2. *
  3. * The LLVM Compiler Infrastructure
  4. *
  5. * This file is distributed under the University of Illinois Open Source
  6. * License. See LICENSE.TXT for details.
  7. *
  8. *==------------------------------------------------------------------------==*/
  9. /*
  10. * Copyright 2001-2004 Unicode, Inc.
  11. *
  12. * Disclaimer
  13. *
  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.
  21. *
  22. * Limitations on Rights to Redistribute This Code
  23. *
  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
  28. * remains attached.
  29. */
  30. /* ---------------------------------------------------------------------
  31. Conversions between UTF32, UTF-16, and UTF-8. Header file.
  32. Several funtions are included here, forming a complete set of
  33. conversions between the three formats. UTF-7 is not included
  34. here, but is handled in a separate source file.
  35. Each of these routines takes pointers to input buffers and output
  36. buffers. The input buffers are const.
  37. Each routine converts the text between *sourceStart and sourceEnd,
  38. putting the result into the buffer between *targetStart and
  39. targetEnd. Note: the end pointers are *after* the last item: e.g.
  40. *(sourceEnd - 1) is the last item.
  41. The return result indicates whether the conversion was successful,
  42. and if not, whether the problem was in the source or target buffers.
  43. (Only the first encountered problem is indicated.)
  44. After the conversion, *sourceStart and *targetStart are both
  45. updated to point to the end of last text successfully converted in
  46. the respective buffers.
  47. Input parameters:
  48. sourceStart - pointer to a pointer to the source buffer.
  49. The contents of this are modified on return so that
  50. it points at the next thing to be converted.
  51. targetStart - similarly, pointer to pointer to the target buffer.
  52. sourceEnd, targetEnd - respectively pointers to the ends of the
  53. two buffers, for overflow checking only.
  54. These conversion functions take a ConversionFlags argument. When this
  55. flag is set to strict, both irregular sequences and isolated surrogates
  56. will cause an error. When the flag is set to lenient, both irregular
  57. sequences and isolated surrogates are converted.
  58. Whether the flag is strict or lenient, all illegal sequences will cause
  59. an error return. This includes sequences such as: <F4 90 80 80>, <C0 80>,
  60. or <A0> in UTF-8, and values above 0x10FFFF in UTF-32. Conformant code
  61. must check for illegal sequences.
  62. When the flag is set to lenient, characters over 0x10FFFF are converted
  63. to the replacement character; otherwise (when the flag is set to strict)
  64. they constitute an error.
  65. Output parameters:
  66. The value "sourceIllegal" is returned from some routines if the input
  67. sequence is malformed. When "sourceIllegal" is returned, the source
  68. value will point to the illegal value that caused the problem. E.g.,
  69. in UTF-8 when a sequence is malformed, it points to the start of the
  70. malformed sequence.
  71. Author: Mark E. Davis, 1994.
  72. Rev History: Rick McGowan, fixes & updates May 2001.
  73. Fixes & updates, Sept 2001.
  74. ------------------------------------------------------------------------ */
  75. #ifndef LLVM_SUPPORT_CONVERTUTF_H
  76. #define LLVM_SUPPORT_CONVERTUTF_H
  77. #include <stddef.h> /* ptrdiff_t */
  78. /* ---------------------------------------------------------------------
  79. The following 4 definitions are compiler-specific.
  80. The C standard does not guarantee that wchar_t has at least
  81. 16 bits, so wchar_t is no less portable than unsigned short!
  82. All should be unsigned values to avoid sign extension during
  83. bit mask & shift operations.
  84. ------------------------------------------------------------------------ */
  85. typedef unsigned int UTF32; /* at least 32 bits */
  86. typedef unsigned short UTF16; /* at least 16 bits */
  87. typedef unsigned char UTF8; /* typically 8 bits */
  88. typedef unsigned char Boolean; /* 0 or 1 */
  89. /* Some fundamental constants */
  90. #define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD
  91. #define UNI_MAX_BMP (UTF32)0x0000FFFF
  92. #define UNI_MAX_UTF16 (UTF32)0x0010FFFF
  93. #define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF
  94. #define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF
  95. #define UNI_MAX_UTF8_BYTES_PER_CODE_POINT 4
  96. #define UNI_UTF16_BYTE_ORDER_MARK_NATIVE 0xFEFF
  97. #define UNI_UTF16_BYTE_ORDER_MARK_SWAPPED 0xFFFE
  98. typedef enum {
  99. conversionOK, /* conversion successful */
  100. sourceExhausted, /* partial character in source, but hit end */
  101. targetExhausted, /* insuff. room in target for conversion */
  102. sourceIllegal /* source sequence is illegal/malformed */
  103. } ConversionResult;
  104. typedef enum {
  105. strictConversion = 0,
  106. lenientConversion
  107. } ConversionFlags;
  108. /* This is for C++ and does no harm in C */
  109. #ifdef __cplusplus
  110. extern "C" {
  111. #endif
  112. ConversionResult ConvertUTF8toUTF16 (
  113. const UTF8** sourceStart, const UTF8* sourceEnd,
  114. UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags);
  115. ConversionResult ConvertUTF8toUTF32 (
  116. const UTF8** sourceStart, const UTF8* sourceEnd,
  117. UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags);
  118. ConversionResult ConvertUTF16toUTF8 (
  119. const UTF16** sourceStart, const UTF16* sourceEnd,
  120. UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);
  121. ConversionResult ConvertUTF32toUTF8 (
  122. const UTF32** sourceStart, const UTF32* sourceEnd,
  123. UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);
  124. ConversionResult ConvertUTF16toUTF32 (
  125. const UTF16** sourceStart, const UTF16* sourceEnd,
  126. UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags);
  127. ConversionResult ConvertUTF32toUTF16 (
  128. const UTF32** sourceStart, const UTF32* sourceEnd,
  129. UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags);
  130. Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd);
  131. Boolean isLegalUTF8String(const UTF8 **source, const UTF8 *sourceEnd);
  132. unsigned getNumBytesForUTF8(UTF8 firstByte);
  133. int getUTF8StringLength(const UTF8* utf8);
  134. #ifdef __cplusplus
  135. }
  136. /*************************************************************************/
  137. /* Below are LLVM-specific wrappers of the functions above. */
  138. //#include "llvm/ADT/ArrayRef.h"
  139. //#include "llvm/ADT/StringRef.h"
  140. #include <vector>
  141. #include <string>
  142. namespace llvm {
  143. /**
  144. * Convert an UTF8 StringRef to UTF8, UTF16, or UTF32 depending on
  145. * WideCharWidth. The converted data is written to ResultPtr, which needs to
  146. * point to at least WideCharWidth * (Source.Size() + 1) bytes. On success,
  147. * ResultPtr will point one after the end of the copied string. On failure,
  148. * ResultPtr will not be changed, and ErrorPtr will be set to the location of
  149. * the first character which could not be converted.
  150. * \return true on success.
  151. */
  152. bool ConvertUTF8toWide(unsigned WideCharWidth, const std::string& Source,
  153. char *&ResultPtr, const UTF8 *&ErrorPtr);
  154. /**
  155. * Convert an Unicode code point to UTF8 sequence.
  156. *
  157. * \param Source a Unicode code point.
  158. * \param [in,out] ResultPtr pointer to the output buffer, needs to be at least
  159. * \c UNI_MAX_UTF8_BYTES_PER_CODE_POINT bytes. On success \c ResultPtr is
  160. * updated one past end of the converted sequence.
  161. *
  162. * \returns true on success.
  163. */
  164. bool ConvertCodePointToUTF8(unsigned Source, char *&ResultPtr);
  165. /**
  166. * Convert the first UTF8 sequence in the given source buffer to a UTF32
  167. * code point.
  168. *
  169. * \param [in,out] source A pointer to the source buffer. If the conversion
  170. * succeeds, this pointer will be updated to point to the byte just past the
  171. * end of the converted sequence.
  172. * \param sourceEnd A pointer just past the end of the source buffer.
  173. * \param [out] target The converted code
  174. * \param flags Whether the conversion is strict or lenient.
  175. *
  176. * \returns conversionOK on success
  177. *
  178. * \sa ConvertUTF8toUTF32
  179. */
  180. static inline ConversionResult convertUTF8Sequence(const UTF8 **source,
  181. const UTF8 *sourceEnd,
  182. UTF32 *target,
  183. ConversionFlags flags) {
  184. if (*source == sourceEnd)
  185. return sourceExhausted;
  186. unsigned size = getNumBytesForUTF8(**source);
  187. if ((ptrdiff_t)size > sourceEnd - *source)
  188. return sourceExhausted;
  189. return ConvertUTF8toUTF32(source, *source + size, &target, target + 1, flags);
  190. }
  191. /**
  192. * Returns true if a blob of text starts with a UTF-16 big or little endian byte
  193. * order mark.
  194. */
  195. bool hasUTF16ByteOrderMark(const char* SrcBytes, size_t len);
  196. /**
  197. * Converts a stream of raw bytes assumed to be UTF16 into a UTF8 std::string.
  198. *
  199. * \param [in] SrcBytes A buffer of what is assumed to be UTF-16 encoded text.
  200. * \param [out] Out Converted UTF-8 is stored here on success.
  201. * \returns true on success
  202. */
  203. bool convertUTF16ToUTF8String(const std::u16string& utf16, std::string &Out);
  204. } /* end namespace llvm */
  205. #endif
  206. /* --------------------------------------------------------------------- */
  207. #endif