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