freebsd-dev/contrib/llvm/lib/Support/Twine.cpp

172 lines
4.1 KiB
C++
Raw Normal View History

2009-10-14 17:57:32 +00:00
//===-- Twine.cpp - Fast Temporary String Concatenation -------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/Twine.h"
#include "llvm/ADT/SmallString.h"
2010-01-15 15:37:28 +00:00
#include "llvm/Support/Debug.h"
2009-10-14 17:57:32 +00:00
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
std::string Twine::str() const {
// 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.
2009-10-14 17:57:32 +00:00
SmallString<256> Vec;
2010-01-15 15:37:28 +00:00
return toStringRef(Vec).str();
2009-10-14 17:57:32 +00:00
}
void Twine::toVector(SmallVectorImpl<char> &Out) const {
raw_svector_ostream OS(Out);
print(OS);
}
2010-01-15 15:37:28 +00:00
StringRef Twine::toStringRef(SmallVectorImpl<char> &Out) const {
if (isSingleStringRef())
return getSingleStringRef();
toVector(Out);
return StringRef(Out.data(), Out.size());
}
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,
2009-10-14 17:57:32 +00:00
NodeKind Kind) const {
switch (Kind) {
case Twine::NullKind: break;
case Twine::EmptyKind: break;
case Twine::TwineKind:
Ptr.twine->print(OS);
2009-10-14 17:57:32 +00:00
break;
case Twine::CStringKind:
OS << Ptr.cString;
2009-10-14 17:57:32 +00:00
break;
case Twine::StdStringKind:
OS << *Ptr.stdString;
2009-10-14 17:57:32 +00:00
break;
case Twine::StringRefKind:
OS << *Ptr.stringRef;
break;
case Twine::CharKind:
OS << Ptr.character;
2009-10-14 17:57:32 +00:00
break;
case Twine::DecUIKind:
OS << Ptr.decUI;
2009-10-14 17:57:32 +00:00
break;
case Twine::DecIKind:
OS << Ptr.decI;
2009-10-14 17:57:32 +00:00
break;
case Twine::DecULKind:
OS << *Ptr.decUL;
2009-10-14 17:57:32 +00:00
break;
case Twine::DecLKind:
OS << *Ptr.decL;
2009-10-14 17:57:32 +00:00
break;
case Twine::DecULLKind:
OS << *Ptr.decULL;
2009-10-14 17:57:32 +00:00
break;
case Twine::DecLLKind:
OS << *Ptr.decLL;
2009-10-14 17:57:32 +00:00
break;
case Twine::UHexKind:
OS.write_hex(*Ptr.uHex);
2009-10-14 17:57:32 +00:00
break;
}
}
void Twine::printOneChildRepr(raw_ostream &OS, Child Ptr,
2009-10-14 17:57:32 +00:00
NodeKind Kind) const {
switch (Kind) {
case Twine::NullKind:
OS << "null"; break;
case Twine::EmptyKind:
OS << "empty"; break;
case Twine::TwineKind:
OS << "rope:";
Ptr.twine->printRepr(OS);
2009-10-14 17:57:32 +00:00
break;
case Twine::CStringKind:
OS << "cstring:\""
<< Ptr.cString << "\"";
2009-10-14 17:57:32 +00:00
break;
case Twine::StdStringKind:
OS << "std::string:\""
<< Ptr.stdString << "\"";
2009-10-14 17:57:32 +00:00
break;
case Twine::StringRefKind:
OS << "stringref:\""
<< Ptr.stringRef << "\"";
break;
case Twine::CharKind:
OS << "char:\"" << Ptr.character << "\"";
2009-10-14 17:57:32 +00:00
break;
case Twine::DecUIKind:
OS << "decUI:\"" << Ptr.decUI << "\"";
2009-10-14 17:57:32 +00:00
break;
case Twine::DecIKind:
OS << "decI:\"" << Ptr.decI << "\"";
2009-10-14 17:57:32 +00:00
break;
case Twine::DecULKind:
OS << "decUL:\"" << *Ptr.decUL << "\"";
2009-10-14 17:57:32 +00:00
break;
case Twine::DecLKind:
OS << "decL:\"" << *Ptr.decL << "\"";
2009-10-14 17:57:32 +00:00
break;
case Twine::DecULLKind:
OS << "decULL:\"" << *Ptr.decULL << "\"";
2009-10-14 17:57:32 +00:00
break;
case Twine::DecLLKind:
OS << "decLL:\"" << *Ptr.decLL << "\"";
2009-10-14 17:57:32 +00:00
break;
case Twine::UHexKind:
OS << "uhex:\"" << Ptr.uHex << "\"";
2009-10-14 17:57:32 +00:00
break;
}
}
void Twine::print(raw_ostream &OS) const {
printOneChild(OS, LHS, getLHSKind());
printOneChild(OS, RHS, getRHSKind());
}
void Twine::printRepr(raw_ostream &OS) const {
OS << "(Twine ";
printOneChildRepr(OS, LHS, getLHSKind());
OS << " ";
printOneChildRepr(OS, RHS, getRHSKind());
OS << ")";
}
void Twine::dump() const {
2010-01-15 15:37:28 +00:00
print(llvm::dbgs());
2009-10-14 17:57:32 +00:00
}
void Twine::dumpRepr() const {
2010-01-15 15:37:28 +00:00
printRepr(llvm::dbgs());
2009-10-14 17:57:32 +00:00
}