xref: /freebsd-src/contrib/llvm-project/clang/lib/Format/Encoding.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===--- Encoding.h - Format C++ code ---------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric ///
90b57cec5SDimitry Andric /// \file
100b57cec5SDimitry Andric /// Contains functions for text encoding manipulation. Supports UTF-8,
110b57cec5SDimitry Andric /// 8-bit encodings and escape sequences in C++ string literals.
120b57cec5SDimitry Andric ///
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #ifndef LLVM_CLANG_LIB_FORMAT_ENCODING_H
160b57cec5SDimitry Andric #define LLVM_CLANG_LIB_FORMAT_ENCODING_H
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric #include "clang/Basic/LLVM.h"
190b57cec5SDimitry Andric #include "llvm/Support/ConvertUTF.h"
200b57cec5SDimitry Andric #include "llvm/Support/Unicode.h"
210b57cec5SDimitry Andric 
220b57cec5SDimitry Andric namespace clang {
230b57cec5SDimitry Andric namespace format {
240b57cec5SDimitry Andric namespace encoding {
250b57cec5SDimitry Andric 
260b57cec5SDimitry Andric enum Encoding {
270b57cec5SDimitry Andric   Encoding_UTF8,
280b57cec5SDimitry Andric   Encoding_Unknown // We treat all other encodings as 8-bit encodings.
290b57cec5SDimitry Andric };
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric /// Detects encoding of the Text. If the Text can be decoded using UTF-8,
320b57cec5SDimitry Andric /// it is considered UTF8, otherwise we treat it as some 8-bit encoding.
330b57cec5SDimitry Andric inline Encoding detectEncoding(StringRef Text) {
340b57cec5SDimitry Andric   const llvm::UTF8 *Ptr = reinterpret_cast<const llvm::UTF8 *>(Text.begin());
350b57cec5SDimitry Andric   const llvm::UTF8 *BufEnd = reinterpret_cast<const llvm::UTF8 *>(Text.end());
360b57cec5SDimitry Andric   if (llvm::isLegalUTF8String(&Ptr, BufEnd))
370b57cec5SDimitry Andric     return Encoding_UTF8;
380b57cec5SDimitry Andric   return Encoding_Unknown;
390b57cec5SDimitry Andric }
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric /// Returns the number of columns required to display the \p Text on a
420b57cec5SDimitry Andric /// generic Unicode-capable terminal. Text is assumed to use the specified
430b57cec5SDimitry Andric /// \p Encoding.
440b57cec5SDimitry Andric inline unsigned columnWidth(StringRef Text, Encoding Encoding) {
450b57cec5SDimitry Andric   if (Encoding == Encoding_UTF8) {
460b57cec5SDimitry Andric     int ContentWidth = llvm::sys::unicode::columnWidthUTF8(Text);
470b57cec5SDimitry Andric     // FIXME: Figure out the correct way to handle this in the presence of both
480b57cec5SDimitry Andric     // printable and unprintable multi-byte UTF-8 characters. Falling back to
490b57cec5SDimitry Andric     // returning the number of bytes may cause problems, as columnWidth suddenly
500b57cec5SDimitry Andric     // becomes non-additive.
510b57cec5SDimitry Andric     if (ContentWidth >= 0)
520b57cec5SDimitry Andric       return ContentWidth;
530b57cec5SDimitry Andric   }
540b57cec5SDimitry Andric   return Text.size();
550b57cec5SDimitry Andric }
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric /// Returns the number of columns required to display the \p Text,
580b57cec5SDimitry Andric /// starting from the \p StartColumn on a terminal with the \p TabWidth. The
590b57cec5SDimitry Andric /// text is assumed to use the specified \p Encoding.
600b57cec5SDimitry Andric inline unsigned columnWidthWithTabs(StringRef Text, unsigned StartColumn,
610b57cec5SDimitry Andric                                     unsigned TabWidth, Encoding Encoding) {
620b57cec5SDimitry Andric   unsigned TotalWidth = 0;
630b57cec5SDimitry Andric   StringRef Tail = Text;
640b57cec5SDimitry Andric   for (;;) {
650b57cec5SDimitry Andric     StringRef::size_type TabPos = Tail.find('\t');
660b57cec5SDimitry Andric     if (TabPos == StringRef::npos)
670b57cec5SDimitry Andric       return TotalWidth + columnWidth(Tail, Encoding);
680b57cec5SDimitry Andric     TotalWidth += columnWidth(Tail.substr(0, TabPos), Encoding);
69*a7dea167SDimitry Andric     if (TabWidth)
700b57cec5SDimitry Andric       TotalWidth += TabWidth - (TotalWidth + StartColumn) % TabWidth;
710b57cec5SDimitry Andric     Tail = Tail.substr(TabPos + 1);
720b57cec5SDimitry Andric   }
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric 
750b57cec5SDimitry Andric /// Gets the number of bytes in a sequence representing a single
760b57cec5SDimitry Andric /// codepoint and starting with FirstChar in the specified Encoding.
770b57cec5SDimitry Andric inline unsigned getCodePointNumBytes(char FirstChar, Encoding Encoding) {
780b57cec5SDimitry Andric   switch (Encoding) {
790b57cec5SDimitry Andric   case Encoding_UTF8:
800b57cec5SDimitry Andric     return llvm::getNumBytesForUTF8(FirstChar);
810b57cec5SDimitry Andric   default:
820b57cec5SDimitry Andric     return 1;
830b57cec5SDimitry Andric   }
840b57cec5SDimitry Andric }
850b57cec5SDimitry Andric 
860b57cec5SDimitry Andric inline bool isOctDigit(char c) { return '0' <= c && c <= '7'; }
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric inline bool isHexDigit(char c) {
890b57cec5SDimitry Andric   return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') ||
900b57cec5SDimitry Andric          ('A' <= c && c <= 'F');
910b57cec5SDimitry Andric }
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric /// Gets the length of an escape sequence inside a C++ string literal.
940b57cec5SDimitry Andric /// Text should span from the beginning of the escape sequence (starting with a
950b57cec5SDimitry Andric /// backslash) to the end of the string literal.
960b57cec5SDimitry Andric inline unsigned getEscapeSequenceLength(StringRef Text) {
970b57cec5SDimitry Andric   assert(Text[0] == '\\');
980b57cec5SDimitry Andric   if (Text.size() < 2)
990b57cec5SDimitry Andric     return 1;
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric   switch (Text[1]) {
1020b57cec5SDimitry Andric   case 'u':
1030b57cec5SDimitry Andric     return 6;
1040b57cec5SDimitry Andric   case 'U':
1050b57cec5SDimitry Andric     return 10;
1060b57cec5SDimitry Andric   case 'x': {
1070b57cec5SDimitry Andric     unsigned I = 2; // Point after '\x'.
1080b57cec5SDimitry Andric     while (I < Text.size() && isHexDigit(Text[I]))
1090b57cec5SDimitry Andric       ++I;
1100b57cec5SDimitry Andric     return I;
1110b57cec5SDimitry Andric   }
1120b57cec5SDimitry Andric   default:
1130b57cec5SDimitry Andric     if (isOctDigit(Text[1])) {
1140b57cec5SDimitry Andric       unsigned I = 1;
1150b57cec5SDimitry Andric       while (I < Text.size() && I < 4 && isOctDigit(Text[I]))
1160b57cec5SDimitry Andric         ++I;
1170b57cec5SDimitry Andric       return I;
1180b57cec5SDimitry Andric     }
1190b57cec5SDimitry Andric     return 1 + llvm::getNumBytesForUTF8(Text[1]);
1200b57cec5SDimitry Andric   }
1210b57cec5SDimitry Andric }
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric } // namespace encoding
1240b57cec5SDimitry Andric } // namespace format
1250b57cec5SDimitry Andric } // namespace clang
1260b57cec5SDimitry Andric 
1270b57cec5SDimitry Andric #endif
128