Encoding.h revision 303975
176633Stshiozak//===--- Encoding.h - Format C++ code -------------------------------------===//
276633Stshiozak//
376633Stshiozak//                     The LLVM Compiler Infrastructure
476633Stshiozak//
576633Stshiozak// This file is distributed under the University of Illinois Open Source
676633Stshiozak// License. See LICENSE.TXT for details.
776633Stshiozak//
876633Stshiozak//===----------------------------------------------------------------------===//
976633Stshiozak///
1076633Stshiozak/// \file
1176633Stshiozak/// \brief Contains functions for text encoding manipulation. Supports UTF-8,
1276633Stshiozak/// 8-bit encodings and escape sequences in C++ string literals.
1376633Stshiozak///
1476633Stshiozak//===----------------------------------------------------------------------===//
1576633Stshiozak
1676633Stshiozak#ifndef LLVM_CLANG_LIB_FORMAT_ENCODING_H
1776633Stshiozak#define LLVM_CLANG_LIB_FORMAT_ENCODING_H
1876633Stshiozak
1976633Stshiozak#include "clang/Basic/LLVM.h"
2076633Stshiozak#include "llvm/Support/ConvertUTF.h"
2176633Stshiozak#include "llvm/Support/Unicode.h"
2276633Stshiozak
2376633Stshiozaknamespace clang {
2476633Stshiozaknamespace format {
2576633Stshiozaknamespace encoding {
2676633Stshiozak
2779555Sobrienenum Encoding {
2876633Stshiozak  Encoding_UTF8,
2976633Stshiozak  Encoding_Unknown // We treat all other encodings as 8-bit encodings.
3076633Stshiozak};
3176633Stshiozak
3276633Stshiozak/// \brief Detects encoding of the Text. If the Text can be decoded using UTF-8,
3376633Stshiozak/// it is considered UTF8, otherwise we treat it as some 8-bit encoding.
3476633Stshiozakinline Encoding detectEncoding(StringRef Text) {
3576633Stshiozak  const UTF8 *Ptr = reinterpret_cast<const UTF8 *>(Text.begin());
3676633Stshiozak  const UTF8 *BufEnd = reinterpret_cast<const UTF8 *>(Text.end());
37101253Stjr  if (::isLegalUTF8String(&Ptr, BufEnd))
38101253Stjr    return Encoding_UTF8;
39101313Stjr  return Encoding_Unknown;
40101409Stjr}
41101409Stjr
42101313Stjrinline unsigned getCodePointCountUTF8(StringRef Text) {
43101313Stjr  unsigned CodePoints = 0;
44101253Stjr  for (size_t i = 0, e = Text.size(); i < e; i += getNumBytesForUTF8(Text[i])) {
45101409Stjr    ++CodePoints;
46101409Stjr  }
47101253Stjr  return CodePoints;
48101253Stjr}
49101409Stjr
50101409Stjr/// \brief Gets the number of code points in the Text using the specified
51101409Stjr/// Encoding.
52101409Stjrinline unsigned getCodePointCount(StringRef Text, Encoding Encoding) {
53101409Stjr  switch (Encoding) {
5476633Stshiozak  case Encoding_UTF8:
5576633Stshiozak    return getCodePointCountUTF8(Text);
5676633Stshiozak  default:
5776633Stshiozak    return Text.size();
5876633Stshiozak  }
5993032Simp}
6093032Simp
6193032Simp/// \brief Returns the number of columns required to display the \p Text on a
6293032Simp/// generic Unicode-capable terminal. Text is assumed to use the specified
63101369Stjr/// \p Encoding.
6493032Simpinline unsigned columnWidth(StringRef Text, Encoding Encoding) {
6593032Simp  if (Encoding == Encoding_UTF8) {
6693032Simp    int ContentWidth = llvm::sys::unicode::columnWidthUTF8(Text);
6793032Simp    // FIXME: Figure out the correct way to handle this in the presence of both
6893032Simp    // printable and unprintable multi-byte UTF-8 characters. Falling back to
6993032Simp    // returning the number of bytes may cause problems, as columnWidth suddenly
7093032Simp    // becomes non-additive.
7193032Simp    if (ContentWidth >= 0)
72101369Stjr      return ContentWidth;
7393032Simp  }
7493032Simp  return Text.size();
75101409Stjr}
76101409Stjr
77101409Stjr/// \brief Returns the number of columns required to display the \p Text,
78101409Stjr/// starting from the \p StartColumn on a terminal with the \p TabWidth. The
79101369Stjr/// text is assumed to use the specified \p Encoding.
80101369Stjrinline unsigned columnWidthWithTabs(StringRef Text, unsigned StartColumn,
81101369Stjr                                    unsigned TabWidth, Encoding Encoding) {
82101369Stjr  unsigned TotalWidth = 0;
83101369Stjr  StringRef Tail = Text;
84101369Stjr  for (;;) {
85101369Stjr    StringRef::size_type TabPos = Tail.find('\t');
86101369Stjr    if (TabPos == StringRef::npos)
87101369Stjr      return TotalWidth + columnWidth(Tail, Encoding);
88101253Stjr    TotalWidth += columnWidth(Tail.substr(0, TabPos), Encoding);
8976633Stshiozak    TotalWidth += TabWidth - (TotalWidth + StartColumn) % TabWidth;
9076633Stshiozak    Tail = Tail.substr(TabPos + 1);
91101409Stjr  }
92101409Stjr}
93101409Stjr
94101409Stjr/// \brief Gets the number of bytes in a sequence representing a single
95101409Stjr/// codepoint and starting with FirstChar in the specified Encoding.
96101409Stjrinline unsigned getCodePointNumBytes(char FirstChar, Encoding Encoding) {
97101409Stjr  switch (Encoding) {
98101409Stjr  case Encoding_UTF8:
99101409Stjr    return getNumBytesForUTF8(FirstChar);
100101409Stjr  default:
101101409Stjr    return 1;
102101409Stjr  }
103101409Stjr}
104101409Stjr
105101409Stjrinline bool isOctDigit(char c) { return '0' <= c && c <= '7'; }
106101253Stjr
107101369Stjrinline bool isHexDigit(char c) {
108101409Stjr  return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') ||
109101409Stjr         ('A' <= c && c <= 'F');
110101409Stjr}
111101409Stjr
112101409Stjr/// \brief Gets the length of an escape sequence inside a C++ string literal.
113101409Stjr/// Text should span from the beginning of the escape sequence (starting with a
114101409Stjr/// backslash) to the end of the string literal.
115101369Stjrinline unsigned getEscapeSequenceLength(StringRef Text) {
116101369Stjr  assert(Text[0] == '\\');
11776633Stshiozak  if (Text.size() < 2)
118    return 1;
119
120  switch (Text[1]) {
121  case 'u':
122    return 6;
123  case 'U':
124    return 10;
125  case 'x': {
126    unsigned I = 2; // Point after '\x'.
127    while (I < Text.size() && isHexDigit(Text[I]))
128      ++I;
129    return I;
130  }
131  default:
132    if (isOctDigit(Text[1])) {
133      unsigned I = 1;
134      while (I < Text.size() && I < 4 && isOctDigit(Text[I]))
135        ++I;
136      return I;
137    }
138    return 1 + getNumBytesForUTF8(Text[1]);
139  }
140}
141
142} // namespace encoding
143} // namespace format
144} // namespace clang
145
146#endif
147