1 //===- StringToOffsetTable.h - Emit a big concatenated string ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_TABLEGEN_STRINGTOOFFSETTABLE_H
11 #define LLVM_TABLEGEN_STRINGTOOFFSETTABLE_H
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/StringExtras.h"
15 #include "llvm/ADT/StringMap.h"
16 #include "llvm/Support/raw_ostream.h"
21 /// StringToOffsetTable - This class uniques a bunch of nul-terminated strings
22 /// and keeps track of their offset in a massive contiguous string allocation.
23 /// It can then output this string blob and use indexes into the string to
24 /// reference each piece.
25 class StringToOffsetTable {
26 StringMap<unsigned> StringOffset;
27 std::string AggregateString;
30 bool Empty() const { return StringOffset.empty(); }
32 unsigned GetOrAddStringOffset(StringRef Str, bool appendZero = true) {
34 StringOffset.insert(std::make_pair(Str, AggregateString.size()));
35 if (IterBool.second) {
36 // Add the string to the aggregate if this is the first time found.
37 AggregateString.append(Str.begin(), Str.end());
39 AggregateString += '\0';
42 return IterBool.first->second;
45 void EmitString(raw_ostream &O) {
48 raw_svector_ostream(Str).write_escaped(AggregateString);
49 AggregateString = Str.str();
52 unsigned CharsPrinted = 0;
53 for (unsigned i = 0, e = AggregateString.size(); i != e; ++i) {
54 if (CharsPrinted > 70) {
58 O << AggregateString[i];
61 // Print escape sequences all together.
62 if (AggregateString[i] != '\\')
65 assert(i + 1 < AggregateString.size() && "Incomplete escape sequence!");
66 if (isdigit(AggregateString[i + 1])) {
67 assert(isdigit(AggregateString[i + 2]) &&
68 isdigit(AggregateString[i + 3]) &&
69 "Expected 3 digit octal escape!");
70 O << AggregateString[++i];
71 O << AggregateString[++i];
72 O << AggregateString[++i];
75 O << AggregateString[++i];
82 /// Emit the string using character literals. MSVC has a limitation that
83 /// string literals cannot be longer than 64K.
84 void EmitCharArray(raw_ostream &O) {
85 assert(AggregateString.find(')') == std::string::npos &&
86 "can't emit raw string with closing parens");
89 for (char C : AggregateString) {
91 O.write_escaped(StringRef(&C, 1));
103 } // end namespace llvm