diff options
Diffstat (limited to 'clang/lib/Format/Encoding.h')
-rw-r--r-- | clang/lib/Format/Encoding.h | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/clang/lib/Format/Encoding.h b/clang/lib/Format/Encoding.h index e9e9ae71c79..356334d5376 100644 --- a/clang/lib/Format/Encoding.h +++ b/clang/lib/Format/Encoding.h @@ -18,6 +18,7 @@ #include "clang/Basic/LLVM.h" #include "llvm/Support/ConvertUTF.h" +#include "llvm/Support/Unicode.h" namespace clang { namespace format { @@ -57,6 +58,37 @@ inline unsigned getCodePointCount(StringRef Text, Encoding Encoding) { } } +/// \brief Returns the number of columns required to display the \p Text on a +/// generic Unicode-capable terminal. Text is assumed to use the specified +/// \p Encoding. +inline unsigned columnWidth(StringRef Text, Encoding Encoding) { + if (Encoding == Encoding_UTF8) { + int ContentWidth = llvm::sys::unicode::columnWidthUTF8(Text); + if (ContentWidth >= 0) + return ContentWidth; + } + return Text.size(); +} + +/// \brief Returns the number of columns required to display the \p Text, +/// starting from the \p StartColumn on a terminal with the \p TabWidth. The +/// text is assumed to use the specified \p Encoding. +inline unsigned columnWidthWithTabs(StringRef Text, unsigned StartColumn, + unsigned TabWidth, Encoding Encoding) { + unsigned TotalWidth = 0; + StringRef Tail = Text; + for (;;) { + StringRef::size_type TabPos = Tail.find('\t'); + if (TabPos == StringRef::npos) + return TotalWidth + columnWidth(Tail, Encoding); + int Width = columnWidth(Tail.substr(0, TabPos), Encoding); + assert(Width >= 0); + TotalWidth += Width; + TotalWidth += TabWidth - (TotalWidth + StartColumn) % TabWidth; + Tail = Tail.substr(TabPos + 1); + } +} + /// \brief Gets the number of bytes in a sequence representing a single /// codepoint and starting with FirstChar in the specified Encoding. inline unsigned getCodePointNumBytes(char FirstChar, Encoding Encoding) { |