Encoding.h revision 259701
1259701Sdim//===--- Encoding.h - Format C++ code -------------------------------------===// 2259701Sdim// 3259701Sdim// The LLVM Compiler Infrastructure 4259701Sdim// 5259701Sdim// This file is distributed under the University of Illinois Open Source 6259701Sdim// License. See LICENSE.TXT for details. 7259701Sdim// 8259701Sdim//===----------------------------------------------------------------------===// 9259701Sdim/// 10259701Sdim/// \file 11259701Sdim/// \brief Contains functions for text encoding manipulation. Supports UTF-8, 12259701Sdim/// 8-bit encodings and escape sequences in C++ string literals. 13259701Sdim/// 14259701Sdim//===----------------------------------------------------------------------===// 15259701Sdim 16259701Sdim#ifndef LLVM_CLANG_FORMAT_ENCODING_H 17259701Sdim#define LLVM_CLANG_FORMAT_ENCODING_H 18259701Sdim 19259701Sdim#include "clang/Basic/LLVM.h" 20259701Sdim#include "llvm/Support/ConvertUTF.h" 21259701Sdim#include "llvm/Support/Unicode.h" 22259701Sdim 23259701Sdimnamespace clang { 24259701Sdimnamespace format { 25259701Sdimnamespace encoding { 26259701Sdim 27259701Sdimenum Encoding { 28259701Sdim Encoding_UTF8, 29259701Sdim Encoding_Unknown // We treat all other encodings as 8-bit encodings. 30259701Sdim}; 31259701Sdim 32259701Sdim/// \brief Detects encoding of the Text. If the Text can be decoded using UTF-8, 33259701Sdim/// it is considered UTF8, otherwise we treat it as some 8-bit encoding. 34259701Sdiminline Encoding detectEncoding(StringRef Text) { 35259701Sdim const UTF8 *Ptr = reinterpret_cast<const UTF8 *>(Text.begin()); 36259701Sdim const UTF8 *BufEnd = reinterpret_cast<const UTF8 *>(Text.end()); 37259701Sdim if (::isLegalUTF8String(&Ptr, BufEnd)) 38259701Sdim return Encoding_UTF8; 39259701Sdim return Encoding_Unknown; 40259701Sdim} 41259701Sdim 42259701Sdiminline unsigned getCodePointCountUTF8(StringRef Text) { 43259701Sdim unsigned CodePoints = 0; 44259701Sdim for (size_t i = 0, e = Text.size(); i < e; i += getNumBytesForUTF8(Text[i])) { 45259701Sdim ++CodePoints; 46259701Sdim } 47259701Sdim return CodePoints; 48259701Sdim} 49259701Sdim 50259701Sdim/// \brief Gets the number of code points in the Text using the specified 51259701Sdim/// Encoding. 52259701Sdiminline unsigned getCodePointCount(StringRef Text, Encoding Encoding) { 53259701Sdim switch (Encoding) { 54259701Sdim case Encoding_UTF8: 55259701Sdim return getCodePointCountUTF8(Text); 56259701Sdim default: 57259701Sdim return Text.size(); 58259701Sdim } 59259701Sdim} 60259701Sdim 61259701Sdim/// \brief Returns the number of columns required to display the \p Text on a 62259701Sdim/// generic Unicode-capable terminal. Text is assumed to use the specified 63259701Sdim/// \p Encoding. 64259701Sdiminline unsigned columnWidth(StringRef Text, Encoding Encoding) { 65259701Sdim if (Encoding == Encoding_UTF8) { 66259701Sdim int ContentWidth = llvm::sys::unicode::columnWidthUTF8(Text); 67259701Sdim if (ContentWidth >= 0) 68259701Sdim return ContentWidth; 69259701Sdim } 70259701Sdim return Text.size(); 71259701Sdim} 72259701Sdim 73259701Sdim/// \brief Returns the number of columns required to display the \p Text, 74259701Sdim/// starting from the \p StartColumn on a terminal with the \p TabWidth. The 75259701Sdim/// text is assumed to use the specified \p Encoding. 76259701Sdiminline unsigned columnWidthWithTabs(StringRef Text, unsigned StartColumn, 77259701Sdim unsigned TabWidth, Encoding Encoding) { 78259701Sdim unsigned TotalWidth = 0; 79259701Sdim StringRef Tail = Text; 80259701Sdim for (;;) { 81259701Sdim StringRef::size_type TabPos = Tail.find('\t'); 82259701Sdim if (TabPos == StringRef::npos) 83259701Sdim return TotalWidth + columnWidth(Tail, Encoding); 84259701Sdim int Width = columnWidth(Tail.substr(0, TabPos), Encoding); 85259701Sdim assert(Width >= 0); 86259701Sdim TotalWidth += Width; 87259701Sdim TotalWidth += TabWidth - (TotalWidth + StartColumn) % TabWidth; 88259701Sdim Tail = Tail.substr(TabPos + 1); 89259701Sdim } 90259701Sdim} 91259701Sdim 92259701Sdim/// \brief Gets the number of bytes in a sequence representing a single 93259701Sdim/// codepoint and starting with FirstChar in the specified Encoding. 94259701Sdiminline unsigned getCodePointNumBytes(char FirstChar, Encoding Encoding) { 95259701Sdim switch (Encoding) { 96259701Sdim case Encoding_UTF8: 97259701Sdim return getNumBytesForUTF8(FirstChar); 98259701Sdim default: 99259701Sdim return 1; 100259701Sdim } 101259701Sdim} 102259701Sdim 103259701Sdiminline bool isOctDigit(char c) { return '0' <= c && c <= '7'; } 104259701Sdim 105259701Sdiminline bool isHexDigit(char c) { 106259701Sdim return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || 107259701Sdim ('A' <= c && c <= 'F'); 108259701Sdim} 109259701Sdim 110259701Sdim/// \brief Gets the length of an escape sequence inside a C++ string literal. 111259701Sdim/// Text should span from the beginning of the escape sequence (starting with a 112259701Sdim/// backslash) to the end of the string literal. 113259701Sdiminline unsigned getEscapeSequenceLength(StringRef Text) { 114259701Sdim assert(Text[0] == '\\'); 115259701Sdim if (Text.size() < 2) 116259701Sdim return 1; 117259701Sdim 118259701Sdim switch (Text[1]) { 119259701Sdim case 'u': 120259701Sdim return 6; 121259701Sdim case 'U': 122259701Sdim return 10; 123259701Sdim case 'x': { 124259701Sdim unsigned I = 2; // Point after '\x'. 125259701Sdim while (I < Text.size() && isHexDigit(Text[I])) 126259701Sdim ++I; 127259701Sdim return I; 128259701Sdim } 129259701Sdim default: 130259701Sdim if (isOctDigit(Text[1])) { 131259701Sdim unsigned I = 1; 132259701Sdim while (I < Text.size() && I < 4 && isOctDigit(Text[I])) 133259701Sdim ++I; 134259701Sdim return I; 135259701Sdim } 136259701Sdim return 2; 137259701Sdim } 138259701Sdim} 139259701Sdim 140259701Sdim} // namespace encoding 141259701Sdim} // namespace format 142259701Sdim} // namespace clang 143259701Sdim 144259701Sdim#endif // LLVM_CLANG_FORMAT_ENCODING_H 145