Revert r152202: "Use uint16_t to store InstrNameIndices in MCInstrInfo."
[oota-llvm.git] / utils / TableGen / StringToOffsetTable.h
index d9d7cf485efd09370231ae63423c798f491f68f1..803f5bd5cf014e1d5e7a18f60d11bc769a8e0745 100644 (file)
 #ifndef TBLGEN_STRING_TO_OFFSET_TABLE_H
 #define TBLGEN_STRING_TO_OFFSET_TABLE_H
 
+#include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringMap.h"
-#include "llvm/Support/raw_ostream.h"
 #include "llvm/ADT/StringExtras.h"
+#include "llvm/Support/raw_ostream.h"
 
 namespace llvm {
 
@@ -25,22 +26,27 @@ class StringToOffsetTable {
   std::string AggregateString;
 public:
   
-  unsigned GetOrAddStringOffset(StringRef Str) {
-    unsigned &Entry = StringOffset[Str];
-    if (Entry == 0) {
+  unsigned GetOrAddStringOffset(StringRef Str, bool appendZero = true) {
+    StringMapEntry<unsigned> &Entry = StringOffset.GetOrCreateValue(Str, -1U);
+    if (Entry.getValue() == -1U) {
       // Add the string to the aggregate if this is the first time found.
-      Entry = AggregateString.size();
+      Entry.setValue(AggregateString.size());
       AggregateString.append(Str.begin(), Str.end());
-      AggregateString += '\0';
+      if (appendZero)
+        AggregateString += '\0';
     }
     
-    return Entry;
+    return Entry.getValue();
   }
   
   void EmitString(raw_ostream &O) {
+    // Escape the string.
+    SmallString<256> Str;
+    raw_svector_ostream(Str).write_escaped(AggregateString);
+    AggregateString = Str.str();
+
     O << "    \"";
     unsigned CharsPrinted = 0;
-    EscapeString(AggregateString);
     for (unsigned i = 0, e = AggregateString.size(); i != e; ++i) {
       if (CharsPrinted > 70) {
         O << "\"\n    \"";