Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
8.7 KiB

  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 CLANG_BASIC_CONVERTUTF_H
  76. #define CLANG_BASIC_CONVERTUTF_H
  77. /* ---------------------------------------------------------------------
  78. The following 4 definitions are compiler-specific.
  79. The C standard does not guarantee that wchar_t has at least
  80. 16 bits, so wchar_t is no less portable than unsigned short!
  81. All should be unsigned values to avoid sign extension during
  82. bit mask & shift operations.
  83. ------------------------------------------------------------------------ */
  84. typedef unsigned int UTF32; /* at least 32 bits */
  85. typedef unsigned short UTF16; /* at least 16 bits */
  86. typedef unsigned char UTF8; /* typically 8 bits */
  87. typedef unsigned char Boolean; /* 0 or 1 */
  88. /* Some fundamental constants */
  89. #define UNI_REPLACEMENT_CHAR (UTF32)0x0000FFFD
  90. #define UNI_MAX_BMP (UTF32)0x0000FFFF
  91. #define UNI_MAX_UTF16 (UTF32)0x0010FFFF
  92. #define UNI_MAX_UTF32 (UTF32)0x7FFFFFFF
  93. #define UNI_MAX_LEGAL_UTF32 (UTF32)0x0010FFFF
  94. #define UNI_MAX_UTF8_BYTES_PER_CODE_POINT 4
  95. typedef enum {
  96. conversionOK, /* conversion successful */
  97. sourceExhausted, /* partial character in source, but hit end */
  98. targetExhausted, /* insuff. room in target for conversion */
  99. sourceIllegal /* source sequence is illegal/malformed */
  100. } ConversionResult;
  101. typedef enum {
  102. strictConversion = 0,
  103. lenientConversion
  104. } ConversionFlags;
  105. /* This is for C++ and does no harm in C */
  106. #ifdef __cplusplus
  107. extern "C" {
  108. #endif
  109. ConversionResult ConvertUTF8toUTF16 (
  110. const UTF8** sourceStart, const UTF8* sourceEnd,
  111. UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags);
  112. ConversionResult ConvertUTF8toUTF32 (
  113. const UTF8** sourceStart, const UTF8* sourceEnd,
  114. UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags);
  115. ConversionResult ConvertUTF16toUTF8 (
  116. const UTF16** sourceStart, const UTF16* sourceEnd,
  117. UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);
  118. ConversionResult ConvertUTF32toUTF8 (
  119. const UTF32** sourceStart, const UTF32* sourceEnd,
  120. UTF8** targetStart, UTF8* targetEnd, ConversionFlags flags);
  121. ConversionResult ConvertUTF16toUTF32 (
  122. const UTF16** sourceStart, const UTF16* sourceEnd,
  123. UTF32** targetStart, UTF32* targetEnd, ConversionFlags flags);
  124. ConversionResult ConvertUTF32toUTF16 (
  125. const UTF32** sourceStart, const UTF32* sourceEnd,
  126. UTF16** targetStart, UTF16* targetEnd, ConversionFlags flags);
  127. Boolean isLegalUTF8Sequence(const UTF8 *source, const UTF8 *sourceEnd);
  128. Boolean isLegalUTF8String(const UTF8 **source, const UTF8 *sourceEnd);
  129. unsigned getNumBytesForUTF8(UTF8 firstByte);
  130. #ifdef __cplusplus
  131. }
  132. /*************************************************************************/
  133. /* Below are LLVM-specific wrappers of the functions above. */
  134. #include "llvm/ADT/StringRef.h"
  135. namespace llvm {
  136. /**
  137. * Convert an UTF8 StringRef to UTF8, UTF16, or UTF32 depending on
  138. * WideCharWidth. The converted data is written to ResultPtr, which needs to
  139. * point to at least WideCharWidth * (Source.Size() + 1) bytes. On success,
  140. * ResultPtr will point one after the end of the copied string. On failure,
  141. * ResultPtr will not be changed, and ErrorPtr will be set to the location of
  142. * the first character which could not be converted.
  143. * \return true on success.
  144. */
  145. bool ConvertUTF8toWide(unsigned WideCharWidth, llvm::StringRef Source,
  146. char *&ResultPtr, const UTF8 *&ErrorPtr);
  147. /**
  148. * Convert an Unicode code point to UTF8 sequence.
  149. *
  150. * \param Source a Unicode code point.
  151. * \param [in,out] ResultPtr pointer to the output buffer, needs to be at least
  152. * \c UNI_MAX_UTF8_BYTES_PER_CODE_POINT bytes. On success \c ResultPtr is
  153. * updated one past end of the converted sequence.
  154. *
  155. * \returns true on success.
  156. */
  157. bool ConvertCodePointToUTF8(unsigned Source, char *&ResultPtr);
  158. /**
  159. * Convert the first UTF8 sequence in the given source buffer to a UTF32
  160. * code point.
  161. *
  162. * \param [in,out] source A pointer to the source buffer. If the conversion
  163. * succeeds, this pointer will be updated to point to the byte just past the
  164. * end of the converted sequence.
  165. * \param sourceEnd A pointer just past the end of the source buffer.
  166. * \param [out] target The converted code
  167. * \param flags Whether the conversion is strict or lenient.
  168. *
  169. * \returns conversionOK on success
  170. *
  171. * \sa ConvertUTF8toUTF32
  172. */
  173. static inline ConversionResult convertUTF8Sequence(const UTF8 **source,
  174. const UTF8 *sourceEnd,
  175. UTF32 *target,
  176. ConversionFlags flags) {
  177. if (*source == sourceEnd)
  178. return sourceExhausted;
  179. unsigned size = getNumBytesForUTF8(**source);
  180. if ((ptrdiff_t)size > sourceEnd - *source)
  181. return sourceExhausted;
  182. return ConvertUTF8toUTF32(source, *source + size, &target, target + 1, flags);
  183. }
  184. } /* end namespace llvm */
  185. #endif
  186. /* --------------------------------------------------------------------- */
  187. #endif