Add a version of AddString that takes a const char* so we can avoid extraneous
authorOwen Anderson <resistor@mac.com>
Tue, 1 Jul 2008 23:49:59 +0000 (23:49 +0000)
committerOwen Anderson <resistor@mac.com>
Tue, 1 Jul 2008 23:49:59 +0000 (23:49 +0000)
conversions to std::string.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@52995 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/ADT/FoldingSet.h
lib/Support/FoldingSet.cpp

index 5f541aeaa4f0812f6007ad8b2473f60088f8036f..cce1eec1848f85028413040fc841f1f10aa63e07 100644 (file)
@@ -220,6 +220,7 @@ public:
   void AddFloat(float F);
   void AddDouble(double D);
   void AddString(const std::string &String);
+  void AddString(const char* String);
   
   template <typename T>
   inline void Add(const T& x) { FoldingSetTrait<T>::Profile(x, *this); }
index 80140438714d99284d247802e6d39d7653b4f4ca..5f1de4a657fa4530da76857c7de902f00cab2011 100644 (file)
@@ -57,6 +57,44 @@ void FoldingSetNodeID::AddFloat(float F) {
 void FoldingSetNodeID::AddDouble(double D) {
  AddInteger(DoubleToBits(D));
 }
+
+void FoldingSetNodeID::AddString(const char *String) {
+  unsigned Size = static_cast<unsigned>(strlen(String));
+  Bits.push_back(Size);
+  if (!Size) return;
+
+  unsigned Units = Size / 4;
+  unsigned Pos = 0;
+  const unsigned *Base = (const unsigned *)String;
+  
+  // If the string is aligned do a bulk transfer.
+  if (!((intptr_t)Base & 3)) {
+    Bits.append(Base, Base + Units);
+    Pos = (Units + 1) * 4;
+  } else {
+    // Otherwise do it the hard way.
+    for ( Pos += 4; Pos <= Size; Pos += 4) {
+      unsigned V = ((unsigned char)String[Pos - 4] << 24) |
+                   ((unsigned char)String[Pos - 3] << 16) |
+                   ((unsigned char)String[Pos - 2] << 8) |
+                    (unsigned char)String[Pos - 1];
+      Bits.push_back(V);
+    }
+  }
+  
+  // With the leftover bits.
+  unsigned V = 0;
+  // Pos will have overshot size by 4 - #bytes left over. 
+  switch (Pos - Size) {
+  case 1: V = (V << 8) | (unsigned char)String[Size - 3]; // Fall thru.
+  case 2: V = (V << 8) | (unsigned char)String[Size - 2]; // Fall thru.
+  case 3: V = (V << 8) | (unsigned char)String[Size - 1]; break;
+  default: return; // Nothing left.
+  }
+
+  Bits.push_back(V);
+}
+
 void FoldingSetNodeID::AddString(const std::string &String) {
   unsigned Size = static_cast<unsigned>(String.size());
   Bits.push_back(Size);