diff options
author | Owen Anderson <resistor@mac.com> | 2008-07-01 23:49:59 +0000 |
---|---|---|
committer | Owen Anderson <resistor@mac.com> | 2008-07-01 23:49:59 +0000 |
commit | 31936d6ab639762ff488a94e1daad2b235b65607 (patch) | |
tree | 73fc3a79dbaf51241d6a7376c08b1229047d3552 /llvm/lib/Support/FoldingSet.cpp | |
parent | b7bd02be57122d33fddac15dae463fcd12a24cd1 (diff) | |
download | bcm5719-llvm-31936d6ab639762ff488a94e1daad2b235b65607.tar.gz bcm5719-llvm-31936d6ab639762ff488a94e1daad2b235b65607.zip |
Add a version of AddString that takes a const char* so we can avoid extraneous
conversions to std::string.
llvm-svn: 52995
Diffstat (limited to 'llvm/lib/Support/FoldingSet.cpp')
-rw-r--r-- | llvm/lib/Support/FoldingSet.cpp | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/llvm/lib/Support/FoldingSet.cpp b/llvm/lib/Support/FoldingSet.cpp index 80140438714..5f1de4a657f 100644 --- a/llvm/lib/Support/FoldingSet.cpp +++ b/llvm/lib/Support/FoldingSet.cpp @@ -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); |