Path: blob/main/contrib/llvm-project/clang/lib/Format/Encoding.h
35234 views
//===--- Encoding.h - Format C++ code ---------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7///8/// \file9/// Contains functions for text encoding manipulation. Supports UTF-8,10/// 8-bit encodings and escape sequences in C++ string literals.11///12//===----------------------------------------------------------------------===//1314#ifndef LLVM_CLANG_LIB_FORMAT_ENCODING_H15#define LLVM_CLANG_LIB_FORMAT_ENCODING_H1617#include "clang/Basic/LLVM.h"18#include "llvm/Support/ConvertUTF.h"19#include "llvm/Support/Unicode.h"2021namespace clang {22namespace format {23namespace encoding {2425enum Encoding {26Encoding_UTF8,27Encoding_Unknown // We treat all other encodings as 8-bit encodings.28};2930/// Detects encoding of the Text. If the Text can be decoded using UTF-8,31/// it is considered UTF8, otherwise we treat it as some 8-bit encoding.32inline Encoding detectEncoding(StringRef Text) {33const llvm::UTF8 *Ptr = reinterpret_cast<const llvm::UTF8 *>(Text.begin());34const llvm::UTF8 *BufEnd = reinterpret_cast<const llvm::UTF8 *>(Text.end());35if (llvm::isLegalUTF8String(&Ptr, BufEnd))36return Encoding_UTF8;37return Encoding_Unknown;38}3940/// Returns the number of columns required to display the \p Text on a41/// generic Unicode-capable terminal. Text is assumed to use the specified42/// \p Encoding.43inline unsigned columnWidth(StringRef Text, Encoding Encoding) {44if (Encoding == Encoding_UTF8) {45int ContentWidth = llvm::sys::unicode::columnWidthUTF8(Text);46// FIXME: Figure out the correct way to handle this in the presence of both47// printable and unprintable multi-byte UTF-8 characters. Falling back to48// returning the number of bytes may cause problems, as columnWidth suddenly49// becomes non-additive.50if (ContentWidth >= 0)51return ContentWidth;52}53return Text.size();54}5556/// Returns the number of columns required to display the \p Text,57/// starting from the \p StartColumn on a terminal with the \p TabWidth. The58/// text is assumed to use the specified \p Encoding.59inline unsigned columnWidthWithTabs(StringRef Text, unsigned StartColumn,60unsigned TabWidth, Encoding Encoding) {61unsigned TotalWidth = 0;62StringRef Tail = Text;63for (;;) {64StringRef::size_type TabPos = Tail.find('\t');65if (TabPos == StringRef::npos)66return TotalWidth + columnWidth(Tail, Encoding);67TotalWidth += columnWidth(Tail.substr(0, TabPos), Encoding);68if (TabWidth)69TotalWidth += TabWidth - (TotalWidth + StartColumn) % TabWidth;70Tail = Tail.substr(TabPos + 1);71}72}7374/// Gets the number of bytes in a sequence representing a single75/// codepoint and starting with FirstChar in the specified Encoding.76inline unsigned getCodePointNumBytes(char FirstChar, Encoding Encoding) {77switch (Encoding) {78case Encoding_UTF8:79return llvm::getNumBytesForUTF8(FirstChar);80default:81return 1;82}83}8485inline bool isOctDigit(char c) { return '0' <= c && c <= '7'; }8687inline bool isHexDigit(char c) {88return ('0' <= c && c <= '9') || ('a' <= c && c <= 'f') ||89('A' <= c && c <= 'F');90}9192/// Gets the length of an escape sequence inside a C++ string literal.93/// Text should span from the beginning of the escape sequence (starting with a94/// backslash) to the end of the string literal.95inline unsigned getEscapeSequenceLength(StringRef Text) {96assert(Text[0] == '\\');97if (Text.size() < 2)98return 1;99100switch (Text[1]) {101case 'u':102return 6;103case 'U':104return 10;105case 'x': {106unsigned I = 2; // Point after '\x'.107while (I < Text.size() && isHexDigit(Text[I]))108++I;109return I;110}111default:112if (isOctDigit(Text[1])) {113unsigned I = 1;114while (I < Text.size() && I < 4 && isOctDigit(Text[I]))115++I;116return I;117}118return 1 + llvm::getNumBytesForUTF8(Text[1]);119}120}121122} // namespace encoding123} // namespace format124} // namespace clang125126#endif127128129