//===----------------------------------------------------------------------===//
#include "llvm/ADT/Twine.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
std::string Twine::str() const {
- std::string Res;
- raw_string_ostream OS(Res);
- print(OS);
- return Res;
+ // If we're storing only a std::string, just return it.
+ if (LHSKind == StdStringKind && RHSKind == EmptyKind)
+ return *LHS.stdString;
+
+ // Otherwise, flatten and copy the contents first.
+ SmallString<256> Vec;
+ return toStringRef(Vec).str();
}
void Twine::toVector(SmallVectorImpl<char> &Out) const {
- // FIXME: This is very inefficient, since we are creating a large raw_ostream
- // buffer -- hitting malloc, which we were supposed to avoid -- all when we
- // have this pretty little small vector available.
- //
- // The best way to fix this is to make raw_svector_ostream do the right thing
- // and be efficient, by augmenting the base raw_ostream with the ability to
- // have the buffer managed by a concrete implementation.
raw_svector_ostream OS(Out);
print(OS);
}
-void Twine::printOneChild(raw_ostream &OS, const void *Ptr,
+StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
+ if (isUnary()) {
+ switch (getLHSKind()) {
+ case CStringKind:
+ // Already null terminated, yay!
+ return StringRef(LHS.cString);
+ case StdStringKind: {
+ const std::string *str = LHS.stdString;
+ return StringRef(str->c_str(), str->size());
+ }
+ default:
+ break;
+ }
+ }
+ toVector(Out);
+ Out.push_back(0);
+ Out.pop_back();
+ return StringRef(Out.data(), Out.size());
+}
+
+void Twine::printOneChild(raw_ostream &OS, Child Ptr,
NodeKind Kind) const {
switch (Kind) {
case Twine::NullKind: break;
case Twine::EmptyKind: break;
case Twine::TwineKind:
- static_cast<const Twine*>(Ptr)->print(OS);
+ Ptr.twine->print(OS);
break;
- case Twine::CStringKind:
- OS << static_cast<const char*>(Ptr);
+ case Twine::CStringKind:
+ OS << Ptr.cString;
break;
case Twine::StdStringKind:
- OS << *static_cast<const std::string*>(Ptr);
+ OS << *Ptr.stdString;
break;
case Twine::StringRefKind:
- OS << *static_cast<const StringRef*>(Ptr);
+ OS << *Ptr.stringRef;
+ break;
+ case Twine::SmallStringKind:
+ OS << *Ptr.smallString;
break;
- case Twine::UDec32Kind:
- OS << *static_cast<const uint32_t*>(Ptr);
+ case Twine::CharKind:
+ OS << Ptr.character;
break;
- case Twine::SDec32Kind:
- OS << *static_cast<const int32_t*>(Ptr);
+ case Twine::DecUIKind:
+ OS << Ptr.decUI;
break;
- case Twine::UDec64Kind:
- OS << *static_cast<const uint64_t*>(Ptr);
+ case Twine::DecIKind:
+ OS << Ptr.decI;
break;
- case Twine::SDec64Kind:
- OS << *static_cast<const int64_t*>(Ptr);
+ case Twine::DecULKind:
+ OS << *Ptr.decUL;
+ break;
+ case Twine::DecLKind:
+ OS << *Ptr.decL;
+ break;
+ case Twine::DecULLKind:
+ OS << *Ptr.decULL;
+ break;
+ case Twine::DecLLKind:
+ OS << *Ptr.decLL;
break;
case Twine::UHexKind:
- // FIXME: Add raw_ostream functionality for this.
- OS << ::utohexstr(*static_cast<const uint64_t*>(Ptr));
+ OS.write_hex(*Ptr.uHex);
break;
}
}
-void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr,
+void Twine::printOneChildRepr(raw_ostream &OS, Child Ptr,
NodeKind Kind) const {
switch (Kind) {
case Twine::NullKind:
OS << "empty"; break;
case Twine::TwineKind:
OS << "rope:";
- static_cast<const Twine*>(Ptr)->printRepr(OS);
+ Ptr.twine->printRepr(OS);
break;
case Twine::CStringKind:
OS << "cstring:\""
- << static_cast<const char*>(Ptr) << "\"";
+ << Ptr.cString << "\"";
break;
case Twine::StdStringKind:
OS << "std::string:\""
- << static_cast<const std::string*>(Ptr) << "\"";
+ << Ptr.stdString << "\"";
break;
case Twine::StringRefKind:
OS << "stringref:\""
- << static_cast<const StringRef*>(Ptr) << "\"";
+ << Ptr.stringRef << "\"";
+ break;
+ case Twine::SmallStringKind:
+ OS << "smallstring:\"" << *Ptr.smallString << "\"";
+ break;
+ case Twine::CharKind:
+ OS << "char:\"" << Ptr.character << "\"";
+ break;
+ case Twine::DecUIKind:
+ OS << "decUI:\"" << Ptr.decUI << "\"";
+ break;
+ case Twine::DecIKind:
+ OS << "decI:\"" << Ptr.decI << "\"";
break;
- case Twine::UDec32Kind:
- OS << "udec32:" << static_cast<const uint64_t*>(Ptr) << "\"";
+ case Twine::DecULKind:
+ OS << "decUL:\"" << *Ptr.decUL << "\"";
break;
- case Twine::SDec32Kind:
- OS << "sdec32:" << static_cast<const int64_t*>(Ptr) << "\"";
+ case Twine::DecLKind:
+ OS << "decL:\"" << *Ptr.decL << "\"";
break;
- case Twine::UDec64Kind:
- OS << "udec64:" << static_cast<const uint64_t*>(Ptr) << "\"";
+ case Twine::DecULLKind:
+ OS << "decULL:\"" << *Ptr.decULL << "\"";
break;
- case Twine::SDec64Kind:
- OS << "sdec64:" << static_cast<const int64_t*>(Ptr) << "\"";
+ case Twine::DecLLKind:
+ OS << "decLL:\"" << *Ptr.decLL << "\"";
break;
case Twine::UHexKind:
- OS << "uhex:" << static_cast<const uint64_t*>(Ptr) << "\"";
+ OS << "uhex:\"" << Ptr.uHex << "\"";
break;
}
}
}
void Twine::dump() const {
- print(llvm::errs());
+ print(dbgs());
}
void Twine::dumpRepr() const {
- printRepr(llvm::errs());
+ printRepr(dbgs());
}