1 //===--- Encoding.h - Format C++ code -------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Contains functions for text encoding manipulation. Supports UTF-8,
12 /// 8-bit encodings and escape sequences in C++ string literals.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_CLANG_LIB_FORMAT_ENCODING_H
17 #define LLVM_CLANG_LIB_FORMAT_ENCODING_H
19 #include "clang/Basic/LLVM.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/ConvertUTF.h"
22 #include "llvm/Support/Unicode.h"
30 Encoding_Unknown // We treat all other encodings as 8-bit encodings.
33 /// \brief Detects encoding of the Text. If the Text can be decoded using UTF-8,
34 /// it is considered UTF8, otherwise we treat it as some 8-bit encoding.
35 inline Encoding detectEncoding(StringRef Text) {
36 const llvm::UTF8 *Ptr = reinterpret_cast<const llvm::UTF8 *>(Text.begin());
37 const llvm::UTF8 *BufEnd = reinterpret_cast<const llvm::UTF8 *>(Text.end());
38 if (llvm::isLegalUTF8String(&Ptr, BufEnd))
40 return Encoding_Unknown;
43 /// \brief Returns the number of columns required to display the \p Text on a
44 /// generic Unicode-capable terminal. Text is assumed to use the specified
46 inline unsigned columnWidth(StringRef Text, Encoding Encoding) {
47 if (Encoding == Encoding_UTF8) {
48 int ContentWidth = llvm::sys::unicode::columnWidthUTF8(Text);
49 // FIXME: Figure out the correct way to handle this in the presence of both
50 // printable and unprintable multi-byte UTF-8 characters. Falling back to
51 // returning the number of bytes may cause problems, as columnWidth suddenly
52 // becomes non-additive.
53 if (ContentWidth >= 0)
59 /// \brief Returns the number of columns required to display the \p Text,
60 /// starting from the \p StartColumn on a terminal with the \p TabWidth. The
61 /// text is assumed to use the specified \p Encoding.
62 inline unsigned columnWidthWithTabs(StringRef Text, unsigned StartColumn,
63 unsigned TabWidth, Encoding Encoding) {
64 unsigned TotalWidth = 0;
65 StringRef Tail = Text;
67 StringRef::size_type TabPos = Tail.find('\t');
68 if (TabPos == StringRef::npos)
69 return TotalWidth + columnWidth(Tail, Encoding);
70 TotalWidth += columnWidth(Tail.substr(0, TabPos), Encoding);
71 TotalWidth += TabWidth - (TotalWidth + StartColumn) % TabWidth;
72 Tail = Tail.substr(TabPos + 1);
76 /// \brief Gets the number of bytes in a sequence representing a single
77 /// codepoint and starting with FirstChar in the specified Encoding.
78 inline unsigned getCodePointNumBytes(char FirstChar, Encoding Encoding) {
81 return llvm::getNumBytesForUTF8(FirstChar);
87 inline bool isOctDigit(char c) { return '0' <= c && c <= '7'; }
89 inline bool isHexDigit(char c) {
90 return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') ||
91 ('A' <= c && c <= 'F');
94 /// \brief Gets the length of an escape sequence inside a C++ string literal.
95 /// Text should span from the beginning of the escape sequence (starting with a
96 /// backslash) to the end of the string literal.
97 inline unsigned getEscapeSequenceLength(StringRef Text) {
98 assert(Text[0] == '\\');
108 unsigned I = 2; // Point after '\x'.
109 while (I < Text.size() && isHexDigit(Text[I]))
114 if (isOctDigit(Text[1])) {
116 while (I < Text.size() && I < 4 && isOctDigit(Text[I]))
120 return 1 + llvm::getNumBytesForUTF8(Text[1]);
124 } // namespace encoding
125 } // namespace format