diff options
author | Alexander Kornienko <alexfh@google.com> | 2013-09-05 14:08:34 +0000 |
---|---|---|
committer | Alexander Kornienko <alexfh@google.com> | 2013-09-05 14:08:34 +0000 |
commit | ebb43caae250d5d5f3f853392460456a0a58abb1 (patch) | |
tree | 29874d67e67d34dd9d144d1ad8a29c2de1906a8b /clang/lib/Format/Encoding.h | |
parent | 15832288f4feac7abfbccdbb567a7cd456913ae5 (diff) | |
download | bcm5719-llvm-ebb43caae250d5d5f3f853392460456a0a58abb1.tar.gz bcm5719-llvm-ebb43caae250d5d5f3f853392460456a0a58abb1.zip |
Handle zero-width and double-width characters in string literals and comments.
Summary:
Count column width instead of the number of code points. This also
includes correct handling of tabs inside string literals and comments (with an
exception of multiline string literals/comments, where tabs are present before
the first escaped newline).
Reviewers: djasper, klimek
Reviewed By: klimek
CC: cfe-commits, klimek
Differential Revision: http://llvm-reviews.chandlerc.com/D1601
llvm-svn: 190052
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) { |