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); |
Zachary Turner | 91dcab4 | 2017-07-05 21:59:20 +0000 | [diff] [blame] | 24 | uint32_t FinalLen = std::min<size_t>(S.size(), MaxLen - 3); |
Zachary Turner | 8120ebf | 2017-07-05 21:54:58 +0000 | [diff] [blame] | 25 | S = S.take_front(FinalLen); |
| 26 | return std::string(S) + std::string("..."); |
| 27 | } |
| 28 | |
Zachary Turner | f3b4b2d | 2017-07-07 18:45:37 +0000 | [diff] [blame^] | 29 | std::string llvm::pdb::truncateStringMiddle(StringRef S, uint32_t MaxLen) { |
| 30 | if (MaxLen == 0 || S.size() <= MaxLen || S.size() <= 3) |
| 31 | return S; |
| 32 | |
| 33 | assert(MaxLen >= 3); |
| 34 | uint32_t FinalLen = std::min<size_t>(S.size(), MaxLen - 3); |
| 35 | StringRef Front = S.take_front(FinalLen / 2); |
| 36 | StringRef Back = S.take_back(Front.size()); |
| 37 | return std::string(Front) + std::string("...") + std::string(Back); |
| 38 | } |
| 39 | |
Zachary Turner | 8120ebf | 2017-07-05 21:54:58 +0000 | [diff] [blame] | 40 | std::string llvm::pdb::truncateStringFront(StringRef S, uint32_t MaxLen) { |
| 41 | if (MaxLen == 0 || S.size() <= MaxLen || S.size() <= 3) |
| 42 | return S; |
| 43 | |
| 44 | assert(MaxLen >= 3); |
| 45 | S = S.take_back(MaxLen - 3); |
| 46 | return std::string("...") + std::string(S); |
| 47 | } |
| 48 | |
| 49 | std::string llvm::pdb::truncateQuotedNameFront(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 = truncateStringFront(Name, MaxLen - RequiredExtraChars); |
| 57 | return formatv("{0} \"{1}\"", Label, TN).str(); |
| 58 | } |
| 59 | |
| 60 | std::string llvm::pdb::truncateQuotedNameBack(StringRef Label, StringRef Name, |
| 61 | uint32_t MaxLen) { |
| 62 | uint32_t RequiredExtraChars = Label.size() + 1 + 2; |
| 63 | if (MaxLen == 0 || RequiredExtraChars + Name.size() <= MaxLen) |
| 64 | return formatv("{0} \"{1}\"", Label, Name).str(); |
| 65 | |
| 66 | assert(MaxLen >= RequiredExtraChars); |
| 67 | std::string TN = truncateStringBack(Name, MaxLen - RequiredExtraChars); |
| 68 | return formatv("{0} \"{1}\"", Label, TN).str(); |
| 69 | } |
| 70 | |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 71 | std::string llvm::pdb::typesetItemList(ArrayRef<std::string> Opts, |
Zachary Turner | 47d9a56 | 2017-06-16 00:04:24 +0000 | [diff] [blame] | 72 | uint32_t IndentLevel, uint32_t GroupSize, |
Zachary Turner | 6305545 | 2017-06-15 22:24:24 +0000 | [diff] [blame] | 73 | StringRef Sep) { |
| 74 | std::string Result; |
| 75 | while (!Opts.empty()) { |
| 76 | ArrayRef<std::string> ThisGroup; |
| 77 | ThisGroup = Opts.take_front(GroupSize); |
| 78 | Opts = Opts.drop_front(ThisGroup.size()); |
| 79 | Result += join(ThisGroup, Sep); |
| 80 | if (!Opts.empty()) { |
| 81 | Result += Sep; |
| 82 | Result += "\n"; |
| 83 | Result += formatv("{0}", fmt_repeat(' ', IndentLevel)); |
| 84 | } |
| 85 | } |
| 86 | return Result; |
| 87 | } |
| 88 | |
| 89 | std::string llvm::pdb::typesetStringList(uint32_t IndentLevel, |
| 90 | ArrayRef<StringRef> Strings) { |
| 91 | std::string Result = "["; |
| 92 | for (const auto &S : Strings) { |
| 93 | Result += formatv("\n{0}{1}", fmt_repeat(' ', IndentLevel), S); |
| 94 | } |
| 95 | Result += "]"; |
| 96 | return Result; |
| 97 | } |
| 98 | |
| 99 | std::string llvm::pdb::formatSegmentOffset(uint16_t Segment, uint32_t Offset) { |
| 100 | return formatv("{0:4}:{1:4}", Segment, Offset); |
| 101 | } |