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