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
16280031Sdim#ifndef LLVM_CLANG_LIB_FORMAT_ENCODING_H
17280031Sdim#define LLVM_CLANG_LIB_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);
67276479Sdim    // FIXME: Figure out the correct way to handle this in the presence of both
68276479Sdim    // printable and unprintable multi-byte UTF-8 characters. Falling back to
69276479Sdim    // returning the number of bytes may cause problems, as columnWidth suddenly
70276479Sdim    // becomes non-additive.
71259701Sdim    if (ContentWidth >= 0)
72259701Sdim      return ContentWidth;
73259701Sdim  }
74259701Sdim  return Text.size();
75259701Sdim}
76259701Sdim
77259701Sdim/// \brief Returns the number of columns required to display the \p Text,
78259701Sdim/// starting from the \p StartColumn on a terminal with the \p TabWidth. The
79259701Sdim/// text is assumed to use the specified \p Encoding.
80259701Sdiminline unsigned columnWidthWithTabs(StringRef Text, unsigned StartColumn,
81259701Sdim                                    unsigned TabWidth, Encoding Encoding) {
82259701Sdim  unsigned TotalWidth = 0;
83259701Sdim  StringRef Tail = Text;
84259701Sdim  for (;;) {
85259701Sdim    StringRef::size_type TabPos = Tail.find('\t');
86259701Sdim    if (TabPos == StringRef::npos)
87259701Sdim      return TotalWidth + columnWidth(Tail, Encoding);
88276479Sdim    TotalWidth += columnWidth(Tail.substr(0, TabPos), Encoding);
89259701Sdim    TotalWidth += TabWidth - (TotalWidth + StartColumn) % TabWidth;
90259701Sdim    Tail = Tail.substr(TabPos + 1);
91259701Sdim  }
92259701Sdim}
93259701Sdim
94259701Sdim/// \brief Gets the number of bytes in a sequence representing a single
95259701Sdim/// codepoint and starting with FirstChar in the specified Encoding.
96259701Sdiminline unsigned getCodePointNumBytes(char FirstChar, Encoding Encoding) {
97259701Sdim  switch (Encoding) {
98259701Sdim  case Encoding_UTF8:
99259701Sdim    return getNumBytesForUTF8(FirstChar);
100259701Sdim  default:
101259701Sdim    return 1;
102259701Sdim  }
103259701Sdim}
104259701Sdim
105259701Sdiminline bool isOctDigit(char c) { return '0' <= c && c <= '7'; }
106259701Sdim
107259701Sdiminline bool isHexDigit(char c) {
108259701Sdim  return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') ||
109259701Sdim         ('A' <= c && c <= 'F');
110259701Sdim}
111259701Sdim
112259701Sdim/// \brief Gets the length of an escape sequence inside a C++ string literal.
113259701Sdim/// Text should span from the beginning of the escape sequence (starting with a
114259701Sdim/// backslash) to the end of the string literal.
115259701Sdiminline unsigned getEscapeSequenceLength(StringRef Text) {
116259701Sdim  assert(Text[0] == '\\');
117259701Sdim  if (Text.size() < 2)
118259701Sdim    return 1;
119259701Sdim
120259701Sdim  switch (Text[1]) {
121259701Sdim  case 'u':
122259701Sdim    return 6;
123259701Sdim  case 'U':
124259701Sdim    return 10;
125259701Sdim  case 'x': {
126259701Sdim    unsigned I = 2; // Point after '\x'.
127259701Sdim    while (I < Text.size() && isHexDigit(Text[I]))
128259701Sdim      ++I;
129259701Sdim    return I;
130259701Sdim  }
131259701Sdim  default:
132259701Sdim    if (isOctDigit(Text[1])) {
133259701Sdim      unsigned I = 1;
134259701Sdim      while (I < Text.size() && I < 4 && isOctDigit(Text[I]))
135259701Sdim        ++I;
136259701Sdim      return I;
137259701Sdim    }
138296417Sdim    return 1 + getNumBytesForUTF8(Text[1]);
139259701Sdim  }
140259701Sdim}
141259701Sdim
142259701Sdim} // namespace encoding
143259701Sdim} // namespace format
144259701Sdim} // namespace clang
145259701Sdim
146280031Sdim#endif
147