1341825Sdim//===--- Encoding.h - Format C++ code ---------------------------*- C++ -*-===//
2259701Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6259701Sdim//
7259701Sdim//===----------------------------------------------------------------------===//
8259701Sdim///
9259701Sdim/// \file
10341825Sdim/// Contains functions for text encoding manipulation. Supports UTF-8,
11259701Sdim/// 8-bit encodings and escape sequences in C++ string literals.
12259701Sdim///
13259701Sdim//===----------------------------------------------------------------------===//
14259701Sdim
15280031Sdim#ifndef LLVM_CLANG_LIB_FORMAT_ENCODING_H
16280031Sdim#define LLVM_CLANG_LIB_FORMAT_ENCODING_H
17259701Sdim
18259701Sdim#include "clang/Basic/LLVM.h"
19309124Sdim#include "llvm/ADT/StringRef.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
32341825Sdim/// 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) {
35314564Sdim  const llvm::UTF8 *Ptr = reinterpret_cast<const llvm::UTF8 *>(Text.begin());
36314564Sdim  const llvm::UTF8 *BufEnd = reinterpret_cast<const llvm::UTF8 *>(Text.end());
37314564Sdim  if (llvm::isLegalUTF8String(&Ptr, BufEnd))
38259701Sdim    return Encoding_UTF8;
39259701Sdim  return Encoding_Unknown;
40259701Sdim}
41259701Sdim
42341825Sdim/// Returns the number of columns required to display the \p Text on a
43259701Sdim/// generic Unicode-capable terminal. Text is assumed to use the specified
44259701Sdim/// \p Encoding.
45259701Sdiminline unsigned columnWidth(StringRef Text, Encoding Encoding) {
46259701Sdim  if (Encoding == Encoding_UTF8) {
47259701Sdim    int ContentWidth = llvm::sys::unicode::columnWidthUTF8(Text);
48276479Sdim    // FIXME: Figure out the correct way to handle this in the presence of both
49276479Sdim    // printable and unprintable multi-byte UTF-8 characters. Falling back to
50276479Sdim    // returning the number of bytes may cause problems, as columnWidth suddenly
51276479Sdim    // becomes non-additive.
52259701Sdim    if (ContentWidth >= 0)
53259701Sdim      return ContentWidth;
54259701Sdim  }
55259701Sdim  return Text.size();
56259701Sdim}
57259701Sdim
58341825Sdim/// Returns the number of columns required to display the \p Text,
59259701Sdim/// starting from the \p StartColumn on a terminal with the \p TabWidth. The
60259701Sdim/// text is assumed to use the specified \p Encoding.
61259701Sdiminline unsigned columnWidthWithTabs(StringRef Text, unsigned StartColumn,
62259701Sdim                                    unsigned TabWidth, Encoding Encoding) {
63259701Sdim  unsigned TotalWidth = 0;
64259701Sdim  StringRef Tail = Text;
65259701Sdim  for (;;) {
66259701Sdim    StringRef::size_type TabPos = Tail.find('\t');
67259701Sdim    if (TabPos == StringRef::npos)
68259701Sdim      return TotalWidth + columnWidth(Tail, Encoding);
69276479Sdim    TotalWidth += columnWidth(Tail.substr(0, TabPos), Encoding);
70360784Sdim    if (TabWidth)
71360784Sdim      TotalWidth += TabWidth - (TotalWidth + StartColumn) % TabWidth;
72259701Sdim    Tail = Tail.substr(TabPos + 1);
73259701Sdim  }
74259701Sdim}
75259701Sdim
76341825Sdim/// Gets the number of bytes in a sequence representing a single
77259701Sdim/// codepoint and starting with FirstChar in the specified Encoding.
78259701Sdiminline unsigned getCodePointNumBytes(char FirstChar, Encoding Encoding) {
79259701Sdim  switch (Encoding) {
80259701Sdim  case Encoding_UTF8:
81314564Sdim    return llvm::getNumBytesForUTF8(FirstChar);
82259701Sdim  default:
83259701Sdim    return 1;
84259701Sdim  }
85259701Sdim}
86259701Sdim
87259701Sdiminline bool isOctDigit(char c) { return '0' <= c && c <= '7'; }
88259701Sdim
89259701Sdiminline bool isHexDigit(char c) {
90259701Sdim  return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') ||
91259701Sdim         ('A' <= c && c <= 'F');
92259701Sdim}
93259701Sdim
94341825Sdim/// Gets the length of an escape sequence inside a C++ string literal.
95259701Sdim/// Text should span from the beginning of the escape sequence (starting with a
96259701Sdim/// backslash) to the end of the string literal.
97259701Sdiminline unsigned getEscapeSequenceLength(StringRef Text) {
98259701Sdim  assert(Text[0] == '\\');
99259701Sdim  if (Text.size() < 2)
100259701Sdim    return 1;
101259701Sdim
102259701Sdim  switch (Text[1]) {
103259701Sdim  case 'u':
104259701Sdim    return 6;
105259701Sdim  case 'U':
106259701Sdim    return 10;
107259701Sdim  case 'x': {
108259701Sdim    unsigned I = 2; // Point after '\x'.
109259701Sdim    while (I < Text.size() && isHexDigit(Text[I]))
110259701Sdim      ++I;
111259701Sdim    return I;
112259701Sdim  }
113259701Sdim  default:
114259701Sdim    if (isOctDigit(Text[1])) {
115259701Sdim      unsigned I = 1;
116259701Sdim      while (I < Text.size() && I < 4 && isOctDigit(Text[I]))
117259701Sdim        ++I;
118259701Sdim      return I;
119259701Sdim    }
120314564Sdim    return 1 + llvm::getNumBytesForUTF8(Text[1]);
121259701Sdim  }
122259701Sdim}
123259701Sdim
124259701Sdim} // namespace encoding
125259701Sdim} // namespace format
126259701Sdim} // namespace clang
127259701Sdim
128280031Sdim#endif
129