Encoding.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //===--- Encoding.h - Format C++ code ---------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// \file
  10. /// Contains functions for text encoding manipulation. Supports UTF-8,
  11. /// 8-bit encodings and escape sequences in C++ string literals.
  12. ///
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_CLANG_LIB_FORMAT_ENCODING_H
  15. #define LLVM_CLANG_LIB_FORMAT_ENCODING_H
  16. #include "clang/Basic/LLVM.h"
  17. #include "llvm/ADT/StringRef.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. /// 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 llvm::UTF8 *Ptr = reinterpret_cast<const llvm::UTF8 *>(Text.begin());
  31. const llvm::UTF8 *BufEnd = reinterpret_cast<const llvm::UTF8 *>(Text.end());
  32. if (llvm::isLegalUTF8String(&Ptr, BufEnd))
  33. return Encoding_UTF8;
  34. return Encoding_Unknown;
  35. }
  36. /// Returns the number of columns required to display the \p Text on a
  37. /// generic Unicode-capable terminal. Text is assumed to use the specified
  38. /// \p Encoding.
  39. inline unsigned columnWidth(StringRef Text, Encoding Encoding) {
  40. if (Encoding == Encoding_UTF8) {
  41. int ContentWidth = llvm::sys::unicode::columnWidthUTF8(Text);
  42. // FIXME: Figure out the correct way to handle this in the presence of both
  43. // printable and unprintable multi-byte UTF-8 characters. Falling back to
  44. // returning the number of bytes may cause problems, as columnWidth suddenly
  45. // becomes non-additive.
  46. if (ContentWidth >= 0)
  47. return ContentWidth;
  48. }
  49. return Text.size();
  50. }
  51. /// Returns the number of columns required to display the \p Text,
  52. /// starting from the \p StartColumn on a terminal with the \p TabWidth. The
  53. /// text is assumed to use the specified \p Encoding.
  54. inline unsigned columnWidthWithTabs(StringRef Text, unsigned StartColumn,
  55. unsigned TabWidth, Encoding Encoding) {
  56. unsigned TotalWidth = 0;
  57. StringRef Tail = Text;
  58. for (;;) {
  59. StringRef::size_type TabPos = Tail.find('\t');
  60. if (TabPos == StringRef::npos)
  61. return TotalWidth + columnWidth(Tail, Encoding);
  62. TotalWidth += columnWidth(Tail.substr(0, TabPos), Encoding);
  63. if (TabWidth)
  64. TotalWidth += TabWidth - (TotalWidth + StartColumn) % TabWidth;
  65. Tail = Tail.substr(TabPos + 1);
  66. }
  67. }
  68. /// Gets the number of bytes in a sequence representing a single
  69. /// codepoint and starting with FirstChar in the specified Encoding.
  70. inline unsigned getCodePointNumBytes(char FirstChar, Encoding Encoding) {
  71. switch (Encoding) {
  72. case Encoding_UTF8:
  73. return llvm::getNumBytesForUTF8(FirstChar);
  74. default:
  75. return 1;
  76. }
  77. }
  78. inline bool isOctDigit(char c) { return '0' <= c && c <= '7'; }
  79. inline bool isHexDigit(char c) {
  80. return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') ||
  81. ('A' <= c && c <= 'F');
  82. }
  83. /// Gets the length of an escape sequence inside a C++ string literal.
  84. /// Text should span from the beginning of the escape sequence (starting with a
  85. /// backslash) to the end of the string literal.
  86. inline unsigned getEscapeSequenceLength(StringRef Text) {
  87. assert(Text[0] == '\\');
  88. if (Text.size() < 2)
  89. return 1;
  90. switch (Text[1]) {
  91. case 'u':
  92. return 6;
  93. case 'U':
  94. return 10;
  95. case 'x': {
  96. unsigned I = 2; // Point after '\x'.
  97. while (I < Text.size() && isHexDigit(Text[I]))
  98. ++I;
  99. return I;
  100. }
  101. default:
  102. if (isOctDigit(Text[1])) {
  103. unsigned I = 1;
  104. while (I < Text.size() && I < 4 && isOctDigit(Text[I]))
  105. ++I;
  106. return I;
  107. }
  108. return 1 + llvm::getNumBytesForUTF8(Text[1]);
  109. }
  110. }
  111. } // namespace encoding
  112. } // namespace format
  113. } // namespace clang
  114. #endif