Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 1 | //===- FormatUtil.cpp ----------------------------------------- *- C++ --*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "FormatUtil.h" |
| 11 | #include "llvm/ADT/STLExtras.h" |
| 12 | #include "llvm/ADT/StringExtras.h" |
| 13 | #include "llvm/Support/FormatAdapters.h" |
| 14 | #include "llvm/Support/FormatVariadic.h" |
| 15 | |
| 16 | using namespace llvm; |
| 17 | using namespace llvm::pdb; |
| 18 | |
Zachary Turner | 8120ebf | 2017-07-05 21:54:58 +0000 | [diff] [blame^] | 19 | std::string llvm::pdb::truncateStringBack(StringRef S, uint32_t MaxLen) { |
| 20 | if (MaxLen == 0 || S.size() <= MaxLen || S.size() <= 3) |
| 21 | return S; |
| 22 | |
| 23 | assert(MaxLen >= 3); |
| 24 | uint32_t FinalLen = std::min(S.size(), MaxLen - 3); |
| 25 | S = S.take_front(FinalLen); |
| 26 | return std::string(S) + std::string("..."); |
| 27 | } |
| 28 | |
| 29 | std::string llvm::pdb::truncateStringFront(StringRef S, uint32_t MaxLen) { |
| 30 | if (MaxLen == 0 || S.size() <= MaxLen || S.size() <= 3) |
| 31 | return S; |
| 32 | |
| 33 | assert(MaxLen >= 3); |
| 34 | S = S.take_back(MaxLen - 3); |
| 35 | return std::string("...") + std::string(S); |
| 36 | } |
| 37 | |
| 38 | std::string llvm::pdb::truncateQuotedNameFront(StringRef Label, StringRef Name, |
| 39 | uint32_t MaxLen) { |
| 40 | uint32_t RequiredExtraChars = Label.size() + 1 + 2; |
| 41 | if (MaxLen == 0 || RequiredExtraChars + Name.size() <= MaxLen) |
| 42 | return formatv("{0} \"{1}\"", Label, Name).str(); |
| 43 | |
| 44 | assert(MaxLen >= RequiredExtraChars); |
| 45 | std::string TN = truncateStringFront(Name, MaxLen - RequiredExtraChars); |
| 46 | return formatv("{0} \"{1}\"", Label, TN).str(); |
| 47 | } |
| 48 | |
| 49 | std::string llvm::pdb::truncateQuotedNameBack(StringRef Label, StringRef Name, |
| 50 | uint32_t MaxLen) { |
| 51 | uint32_t RequiredExtraChars = Label.size() + 1 + 2; |
| 52 | if (MaxLen == 0 || RequiredExtraChars + Name.size() <= MaxLen) |
| 53 | return formatv("{0} \"{1}\"", Label, Name).str(); |
| 54 | |
| 55 | assert(MaxLen >= RequiredExtraChars); |
| 56 | std::string TN = truncateStringBack(Name, MaxLen - RequiredExtraChars); |
| 57 | return formatv("{0} \"{1}\"", Label, TN).str(); |
| 58 | } |
| 59 | |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 60 | std::string llvm::pdb::typesetItemList(ArrayRef<std::string> Opts, |
Zachary Turner | 47d9a56 | 2017-06-16 00:04:24 +0000 | [diff] [blame] | 61 | uint32_t IndentLevel, uint32_t GroupSize, |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 62 | StringRef Sep) { |
| 63 | std::string Result; |
| 64 | while (!Opts.empty()) { |
| 65 | ArrayRef<std::string> ThisGroup; |
| 66 | ThisGroup = Opts.take_front(GroupSize); |
| 67 | Opts = Opts.drop_front(ThisGroup.size()); |
| 68 | Result += join(ThisGroup, Sep); |
| 69 | if (!Opts.empty()) { |
| 70 | Result += Sep; |
| 71 | Result += "\n"; |
| 72 | Result += formatv("{0}", fmt_repeat(' ', IndentLevel)); |
| 73 | } |
| 74 | } |
| 75 | return Result; |
| 76 | } |
| 77 | |
| 78 | std::string llvm::pdb::typesetStringList(uint32_t IndentLevel, |
| 79 | ArrayRef<StringRef> Strings) { |
| 80 | std::string Result = "["; |
| 81 | for (const auto &S : Strings) { |
| 82 | Result += formatv("\n{0}{1}", fmt_repeat(' ', IndentLevel), S); |
| 83 | } |
| 84 | Result += "]"; |
| 85 | return Result; |
| 86 | } |
| 87 | |
| 88 | std::string llvm::pdb::formatSegmentOffset(uint16_t Segment, uint32_t Offset) { |
| 89 | return formatv("{0:4}:{1:4}", Segment, Offset); |
| 90 | } |