/src/llvm-project/clang/lib/Format/Encoding.h
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- Encoding.h - Format C++ code ---------------------------*- C++ -*-===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | /// |
9 | | /// \file |
10 | | /// Contains functions for text encoding manipulation. Supports UTF-8, |
11 | | /// 8-bit encodings and escape sequences in C++ string literals. |
12 | | /// |
13 | | //===----------------------------------------------------------------------===// |
14 | | |
15 | | #ifndef LLVM_CLANG_LIB_FORMAT_ENCODING_H |
16 | | #define LLVM_CLANG_LIB_FORMAT_ENCODING_H |
17 | | |
18 | | #include "clang/Basic/LLVM.h" |
19 | | #include "llvm/ADT/StringRef.h" |
20 | | #include "llvm/Support/ConvertUTF.h" |
21 | | #include "llvm/Support/Unicode.h" |
22 | | |
23 | | namespace clang { |
24 | | namespace format { |
25 | | namespace encoding { |
26 | | |
27 | | enum Encoding { |
28 | | Encoding_UTF8, |
29 | | Encoding_Unknown // We treat all other encodings as 8-bit encodings. |
30 | | }; |
31 | | |
32 | | /// 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. |
34 | 1.01k | inline Encoding detectEncoding(StringRef Text) { |
35 | 1.01k | const llvm::UTF8 *Ptr = reinterpret_cast<const llvm::UTF8 *>(Text.begin()); |
36 | 1.01k | const llvm::UTF8 *BufEnd = reinterpret_cast<const llvm::UTF8 *>(Text.end()); |
37 | 1.01k | if (llvm::isLegalUTF8String(&Ptr, BufEnd)) |
38 | 219 | return Encoding_UTF8; |
39 | 792 | return Encoding_Unknown; |
40 | 1.01k | } |
41 | | |
42 | | /// Returns the number of columns required to display the \p Text on a |
43 | | /// generic Unicode-capable terminal. Text is assumed to use the specified |
44 | | /// \p Encoding. |
45 | 84.9M | inline unsigned columnWidth(StringRef Text, Encoding Encoding) { |
46 | 84.9M | if (Encoding == Encoding_UTF8) { |
47 | 7.31M | int ContentWidth = llvm::sys::unicode::columnWidthUTF8(Text); |
48 | | // FIXME: Figure out the correct way to handle this in the presence of both |
49 | | // printable and unprintable multi-byte UTF-8 characters. Falling back to |
50 | | // returning the number of bytes may cause problems, as columnWidth suddenly |
51 | | // becomes non-additive. |
52 | 7.31M | if (ContentWidth >= 0) |
53 | 6.65M | return ContentWidth; |
54 | 7.31M | } |
55 | 78.2M | return Text.size(); |
56 | 84.9M | } |
57 | | |
58 | | /// Returns the number of columns required to display the \p Text, |
59 | | /// starting from the \p StartColumn on a terminal with the \p TabWidth. The |
60 | | /// text is assumed to use the specified \p Encoding. |
61 | | inline unsigned columnWidthWithTabs(StringRef Text, unsigned StartColumn, |
62 | 82.9M | unsigned TabWidth, Encoding Encoding) { |
63 | 82.9M | unsigned TotalWidth = 0; |
64 | 82.9M | StringRef Tail = Text; |
65 | 84.8M | for (;;) { |
66 | 84.8M | StringRef::size_type TabPos = Tail.find('\t'); |
67 | 84.8M | if (TabPos == StringRef::npos) |
68 | 82.9M | return TotalWidth + columnWidth(Tail, Encoding); |
69 | 1.91M | TotalWidth += columnWidth(Tail.substr(0, TabPos), Encoding); |
70 | 1.91M | if (TabWidth) |
71 | 1.91M | TotalWidth += TabWidth - (TotalWidth + StartColumn) % TabWidth; |
72 | 1.91M | Tail = Tail.substr(TabPos + 1); |
73 | 1.91M | } |
74 | 82.9M | } |
75 | | |
76 | | /// Gets the number of bytes in a sequence representing a single |
77 | | /// codepoint and starting with FirstChar in the specified Encoding. |
78 | 29.2M | inline unsigned getCodePointNumBytes(char FirstChar, Encoding Encoding) { |
79 | 29.2M | switch (Encoding) { |
80 | 2.12M | case Encoding_UTF8: |
81 | 2.12M | return llvm::getNumBytesForUTF8(FirstChar); |
82 | 27.1M | default: |
83 | 27.1M | return 1; |
84 | 29.2M | } |
85 | 29.2M | } |
86 | | |
87 | 45.2k | inline bool isOctDigit(char c) { return '0' <= c && c <= '7'; } |
88 | | |
89 | 652 | inline bool isHexDigit(char c) { |
90 | 652 | return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') || |
91 | 652 | ('A' <= c && c <= 'F'); |
92 | 652 | } |
93 | | |
94 | | /// Gets the length of an escape sequence inside a C++ string literal. |
95 | | /// Text should span from the beginning of the escape sequence (starting with a |
96 | | /// backslash) to the end of the string literal. |
97 | 66.7k | inline unsigned getEscapeSequenceLength(StringRef Text) { |
98 | 66.7k | assert(Text[0] == '\\'); |
99 | 66.7k | if (Text.size() < 2) |
100 | 0 | return 1; |
101 | | |
102 | 66.7k | switch (Text[1]) { |
103 | 24.9k | case 'u': |
104 | 24.9k | return 6; |
105 | 0 | case 'U': |
106 | 0 | return 10; |
107 | 652 | case 'x': { |
108 | 652 | unsigned I = 2; // Point after '\x'. |
109 | 652 | while (I < Text.size() && isHexDigit(Text[I])) |
110 | 0 | ++I; |
111 | 652 | return I; |
112 | 0 | } |
113 | 41.1k | default: |
114 | 41.1k | if (isOctDigit(Text[1])) { |
115 | 1.36k | unsigned I = 1; |
116 | 5.45k | while (I < Text.size() && I < 4 && isOctDigit(Text[I])) |
117 | 4.09k | ++I; |
118 | 1.36k | return I; |
119 | 1.36k | } |
120 | 39.7k | return 1 + llvm::getNumBytesForUTF8(Text[1]); |
121 | 66.7k | } |
122 | 66.7k | } |
123 | | |
124 | | } // namespace encoding |
125 | | } // namespace format |
126 | | } // namespace clang |
127 | | |
128 | | #endif |