Encoding.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //===--- Encoding.h - Format C++ code -------------------------------------===//
  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. /// \file
  11. /// \brief Contains functions for text encoding manipulation. Supports UTF-8,
  12. /// 8-bit encodings and escape sequences in C++ string literals.
  13. ///
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_CLANG_LIB_FORMAT_ENCODING_H
  16. #define LLVM_CLANG_LIB_FORMAT_ENCODING_H
  17. #include "clang/Basic/LLVM.h"
  18. #include "llvm/Support/ConvertUTF.h"
  19. #include "llvm/Support/Unicode.h"
  20. namespace clang {
  21. namespace format {
  22. namespace encoding {
  23. enum Encoding {
  24. Encoding_UTF8,
  25. Encoding_Unknown // We treat all other encodings as 8-bit encodings.
  26. };
  27. /// \brief Detects encoding of the Text. If the Text can be decoded using UTF-8,
  28. /// it is considered UTF8, otherwise we treat it as some 8-bit encoding.
  29. inline Encoding detectEncoding(StringRef Text) {
  30. const UTF8 *Ptr = reinterpret_cast<const UTF8 *>(Text.begin());
  31. const UTF8 *BufEnd = reinterpret_cast<const UTF8 *>(Text.end());
  32. if (::isLegalUTF8String(&Ptr, BufEnd))
  33. return Encoding_UTF8;
  34. return Encoding_Unknown;
  35. }
  36. inline unsigned getCodePointCountUTF8(StringRef Text) {
  37. unsigned CodePoints = 0;
  38. for (size_t i = 0, e = Text.size(); i < e; i += getNumBytesForUTF8(Text[i])) {
  39. ++CodePoints;
  40. }
  41. return CodePoints;
  42. }
  43. /// \brief Gets the number of code points in the Text using the specified
  44. /// Encoding.
  45. inline unsigned getCodePointCount(StringRef Text, Encoding Encoding) {
  46. switch (Encoding) {
  47. case Encoding_UTF8:
  48. return getCodePointCountUTF8(Text);
  49. default:
  50. return Text.size();
  51. }
  52. }
  53. /// \brief Returns the number of columns required to display the \p Text on a
  54. /// generic Unicode-capable terminal. Text is assumed to use the specified
  55. /// \p Encoding.
  56. inline unsigned columnWidth(StringRef Text, Encoding Encoding) {
  57. if (Encoding == Encoding_UTF8) {
  58. int ContentWidth = llvm::sys::unicode::columnWidthUTF8(Text);
  59. // FIXME: Figure out the correct way to handle this in the presence of both
  60. // printable and unprintable multi-byte UTF-8 characters. Falling back to
  61. // returning the number of bytes may cause problems, as columnWidth suddenly
  62. // becomes non-additive.
  63. if (ContentWidth >= 0)
  64. return ContentWidth;
  65. }
  66. return Text.size();
  67. }
  68. /// \brief Returns the number of columns required to display the \p Text,
  69. /// starting from the \p StartColumn on a terminal with the \p TabWidth. The
  70. /// text is assumed to use the specified \p Encoding.
  71. inline unsigned columnWidthWithTabs(StringRef Text, unsigned StartColumn,
  72. unsigned TabWidth, Encoding Encoding) {
  73. unsigned TotalWidth = 0;
  74. StringRef Tail = Text;
  75. for (;;) {
  76. StringRef::size_type TabPos = Tail.find('\t');
  77. if (TabPos == StringRef::npos)
  78. return TotalWidth + columnWidth(Tail, Encoding);
  79. TotalWidth += columnWidth(Tail.substr(0, TabPos), Encoding);
  80. TotalWidth += TabWidth - (TotalWidth + StartColumn) % TabWidth;
  81. Tail = Tail.substr(TabPos + 1);
  82. }
  83. }
  84. /// \brief Gets the number of bytes in a sequence representing a single
  85. /// codepoint and starting with FirstChar in the specified Encoding.
  86. inline unsigned getCodePointNumBytes(char FirstChar, Encoding Encoding) {
  87. switch (Encoding) {
  88. case Encoding_UTF8:
  89. return getNumBytesForUTF8(FirstChar);
  90. default:
  91. return 1;
  92. }
  93. }
  94. inline bool isOctDigit(char c) { return '0' <= c && c <= '7'; }
  95. inline bool isHexDigit(char c) {
  96. return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') ||
  97. ('A' <= c && c <= 'F');
  98. }
  99. /// \brief Gets the length of an escape sequence inside a C++ string literal.
  100. /// Text should span from the beginning of the escape sequence (starting with a
  101. /// backslash) to the end of the string literal.
  102. inline unsigned getEscapeSequenceLength(StringRef Text) {
  103. assert(Text[0] == '\\');
  104. if (Text.size() < 2)
  105. return 1;
  106. switch (Text[1]) {
  107. case 'u':
  108. return 6;
  109. case 'U':
  110. return 10;
  111. case 'x': {
  112. unsigned I = 2; // Point after '\x'.
  113. while (I < Text.size() && isHexDigit(Text[I]))
  114. ++I;
  115. return I;
  116. }
  117. default:
  118. if (isOctDigit(Text[1])) {
  119. unsigned I = 1;
  120. while (I < Text.size() && I < 4 && isOctDigit(Text[I]))
  121. ++I;
  122. return I;
  123. }
  124. return 2;
  125. }
  126. }
  127. } // namespace encoding
  128. } // namespace format
  129. } // namespace clang
  130. #endif