antlr3convertutf.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2001-2004 Unicode, Inc.
  3. *
  4. * Disclaimer
  5. *
  6. * This source code is provided as is by Unicode, Inc. No claims are
  7. * made as to fitness for any particular purpose. No warranties of any
  8. * kind are expressed or implied. The recipient agrees to determine
  9. * applicability of information provided. If this file has been
  10. * purchased on magnetic or optical media from Unicode, Inc., the
  11. * sole remedy for any claim will be exchange of defective media
  12. * within 90 days of receipt.
  13. *
  14. * Limitations on Rights to Redistribute This Code
  15. *
  16. * Unicode, Inc. hereby grants the right to freely use the information
  17. * supplied in this file in the creation of products supporting the
  18. * Unicode Standard, and to make copies of this file in any form
  19. * for internal or external distribution as long as this notice
  20. * remains attached.
  21. */
  22. /* ---------------------------------------------------------------------
  23. Conversions between UTF32, UTF-16, and UTF-8. Header file.
  24. Several functions are included here, forming a complete set of
  25. conversions between the three formats. UTF-7 is not included
  26. here, but is handled in a separate source file.
  27. Each of these routines takes pointers to input buffers and output
  28. buffers. The input buffers are const.
  29. Each routine converts the text between *sourceStart and sourceEnd,
  30. putting the result into the buffer between *targetStart and
  31. targetEnd. Note: the end pointers are *after* the last item: e.g.
  32. *(sourceEnd - 1) is the last item.
  33. The return result indicates whether the conversion was successful,
  34. and if not, whether the problem was in the source or target buffers.
  35. (Only the first encountered problem is indicated.)
  36. After the conversion, *sourceStart and *targetStart are both
  37. updated to point to the end of last text successfully converted in
  38. the respective buffers.
  39. Input parameters:
  40. sourceStart - pointer to a pointer to the source buffer.
  41. The contents of this are modified on return so that
  42. it points at the next thing to be converted.
  43. targetStart - similarly, pointer to pointer to the target buffer.
  44. sourceEnd, targetEnd - respectively pointers to the ends of the
  45. two buffers, for overflow checking only.
  46. These conversion functions take a ConversionFlags argument. When this
  47. flag is set to strict, both irregular sequences and isolated surrogates
  48. will cause an error. When the flag is set to lenient, both irregular
  49. sequences and isolated surrogates are converted.
  50. Whether the flag is strict or lenient, all illegal sequences will cause
  51. an error return. This includes sequences such as: <F4 90 80 80>, <C0 80>,
  52. or <A0> in UTF-8, and values above 0x10FFFF in UTF-32. Conformant code
  53. must check for illegal sequences.
  54. When the flag is set to lenient, characters over 0x10FFFF are converted
  55. to the replacement character; otherwise (when the flag is set to strict)
  56. they constitute an error.
  57. Output parameters:
  58. The value "sourceIllegal" is returned from some routines if the input
  59. sequence is malformed. When "sourceIllegal" is returned, the source
  60. value will point to the illegal value that caused the problem. E.g.,
  61. in UTF-8 when a sequence is malformed, it points to the start of the
  62. malformed sequence.
  63. Author: Mark E. Davis, 1994.
  64. Rev History: Rick McGowan, fixes & updates May 2001.
  65. Fixes & updates, Sept 2001.
  66. ------------------------------------------------------------------------ */
  67. /* ---------------------------------------------------------------------
  68. The following 4 definitions are compiler-specific.
  69. The C standard does not guarantee that wchar_t has at least
  70. 16 bits, so wchar_t is no less portable than unsigned short!
  71. All should be unsigned values to avoid sign extension during
  72. bit mask & shift operations.
  73. ------------------------------------------------------------------------ */
  74. // Changes for ANTLR3 - Jim Idle, January 2008.
  75. // builtin types defined for Unicode types changed to
  76. // aliases for the types that are system determined by
  77. // ANTLR at compile time.
  78. //
  79. // typedef unsigned long UTF32; /* at least 32 bits */
  80. // typedef unsigned short UTF16; /* at least 16 bits */
  81. // typedef unsigned char UTF8; /* typically 8 bits */
  82. // typedef unsigned char Boolean; /* 0 or 1 */
  83. #ifndef _ANTLR3_CONVERTUTF_H
  84. #define _ANTLR3_CONVERTUTF_H
  85. namespace antlr3 {
  86. typedef ANTLR_UINT32 UTF32; /* at least 32 bits */
  87. typedef ANTLR_UINT16 UTF16; /* at least 16 bits */
  88. typedef ANTLR_UINT8 UTF8; /* typically 8 bits */
  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_SUR_HIGH_START (UTF32)0xD800
  96. #define UNI_SUR_HIGH_END (UTF32)0xDBFF
  97. #define UNI_SUR_LOW_START (UTF32)0xDC00
  98. #define UNI_SUR_LOW_END (UTF32)0xDFFF
  99. #define halfShift ((UTF32)10)
  100. #define halfBase ((UTF32)0x0010000UL)
  101. #define halfMask ((UTF32)0x3FFUL)
  102. enum ConversionResult {
  103. conversionOK, /* conversion successful */
  104. sourceExhausted, /* partial character in source, but hit end */
  105. targetExhausted, /* insuff. room in target for conversion */
  106. sourceIllegal /* source sequence is illegal/malformed */
  107. };
  108. enum ConversionFlags {
  109. strictConversion = 0,
  110. lenientConversion
  111. } ;
  112. }
  113. #endif
  114. /* --------------------------------------------------------------------- */