[llvm-commits] [llvm] r64005 - in /llvm/trunk: include/llvm/ADT/FoldingSet.h lib/Support/FoldingSet.cpp

Nick Lewycky nicholas at mxc.ca
Fri Feb 6 20:57:09 PST 2009


Author: nicholas
Date: Fri Feb  6 22:57:08 2009
New Revision: 64005

URL: http://llvm.org/viewvc/llvm-project?rev=64005&view=rev
Log:
Add an API for strings with possible NULLs in the middle. Refactor the other
two AddString methods to use it.

Modified:
    llvm/trunk/include/llvm/ADT/FoldingSet.h
    llvm/trunk/lib/Support/FoldingSet.cpp

Modified: llvm/trunk/include/llvm/ADT/FoldingSet.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/FoldingSet.h?rev=64005&r1=64004&r2=64005&view=diff

==============================================================================
--- llvm/trunk/include/llvm/ADT/FoldingSet.h (original)
+++ llvm/trunk/include/llvm/ADT/FoldingSet.h Fri Feb  6 22:57:08 2009
@@ -225,6 +225,7 @@
   void AddInteger(unsigned long I);
   void AddInteger(long long I);
   void AddInteger(unsigned long long I);
+  void AddString(const char* String, const char* End);
   void AddString(const std::string &String);
   void AddString(const char* String);
 

Modified: llvm/trunk/lib/Support/FoldingSet.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/FoldingSet.cpp?rev=64005&r1=64004&r2=64005&view=diff

==============================================================================
--- llvm/trunk/lib/Support/FoldingSet.cpp (original)
+++ llvm/trunk/lib/Support/FoldingSet.cpp Fri Feb  6 22:57:08 2009
@@ -62,8 +62,8 @@
     Bits.push_back(unsigned(I >> 32));
 }
 
-void FoldingSetNodeID::AddString(const char *String) {
-  unsigned Size = static_cast<unsigned>(strlen(String));
+void FoldingSetNodeID::AddString(const char *String, const char *End) {
+  unsigned Size =  static_cast<unsigned>(End - String);
   Bits.push_back(Size);
   if (!Size) return;
 
@@ -77,7 +77,7 @@
     Pos = (Units + 1) * 4;
   } else {
     // Otherwise do it the hard way.
-    for ( Pos += 4; Pos <= Size; Pos += 4) {
+    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) |
@@ -99,41 +99,12 @@
   Bits.push_back(V);
 }
 
-void FoldingSetNodeID::AddString(const std::string &String) {
-  unsigned Size = static_cast<unsigned>(String.size());
-  Bits.push_back(Size);
-  if (!Size) return;
-
-  unsigned Units = Size / 4;
-  unsigned Pos = 0;
-  const unsigned *Base = (const unsigned *)String.data();
-  
-  // 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.
-  }
+void FoldingSetNodeID::AddString(const char *String) {
+  AddString(String, String + strlen(String));
+}
 
-  Bits.push_back(V);
+void FoldingSetNodeID::AddString(const std::string &String) {
+  AddString(&*String.begin(), &*String.end());
 }
 
 /// ComputeHash - Compute a strong hash value for this FoldingSetNodeID, used to 





More information about the llvm-commits mailing list