LLVM  mainline
Twine.cpp
Go to the documentation of this file.
00001 //===-- Twine.cpp - Fast Temporary String Concatenation -------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "llvm/ADT/Twine.h"
00011 #include "llvm/ADT/SmallString.h"
00012 #include "llvm/Support/Debug.h"
00013 #include "llvm/Support/raw_ostream.h"
00014 using namespace llvm;
00015 
00016 std::string Twine::str() const {
00017   // If we're storing only a std::string, just return it.
00018   if (LHSKind == StdStringKind && RHSKind == EmptyKind)
00019     return *LHS.stdString;
00020 
00021   // Otherwise, flatten and copy the contents first.
00022   SmallString<256> Vec;
00023   return toStringRef(Vec).str();
00024 }
00025 
00026 void Twine::toVector(SmallVectorImpl<char> &Out) const {
00027   raw_svector_ostream OS(Out);
00028   print(OS);
00029 }
00030 
00031 StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
00032   if (isUnary()) {
00033     switch (getLHSKind()) {
00034     case CStringKind:
00035       // Already null terminated, yay!
00036       return StringRef(LHS.cString);
00037     case StdStringKind: {
00038       const std::string *str = LHS.stdString;
00039       return StringRef(str->c_str(), str->size());
00040     }
00041     default:
00042       break;
00043     }
00044   }
00045   toVector(Out);
00046   Out.push_back(0);
00047   Out.pop_back();
00048   return StringRef(Out.data(), Out.size());
00049 }
00050 
00051 void Twine::printOneChild(raw_ostream &OS, Child Ptr,
00052                           NodeKind Kind) const {
00053   switch (Kind) {
00054   case Twine::NullKind: break;
00055   case Twine::EmptyKind: break;
00056   case Twine::TwineKind:
00057     Ptr.twine->print(OS);
00058     break;
00059   case Twine::CStringKind:
00060     OS << Ptr.cString;
00061     break;
00062   case Twine::StdStringKind:
00063     OS << *Ptr.stdString;
00064     break;
00065   case Twine::StringRefKind:
00066     OS << *Ptr.stringRef;
00067     break;
00068   case Twine::SmallStringKind:
00069     OS << *Ptr.smallString;
00070     break;
00071   case Twine::CharKind:
00072     OS << Ptr.character;
00073     break;
00074   case Twine::DecUIKind:
00075     OS << Ptr.decUI;
00076     break;
00077   case Twine::DecIKind:
00078     OS << Ptr.decI;
00079     break;
00080   case Twine::DecULKind:
00081     OS << *Ptr.decUL;
00082     break;
00083   case Twine::DecLKind:
00084     OS << *Ptr.decL;
00085     break;
00086   case Twine::DecULLKind:
00087     OS << *Ptr.decULL;
00088     break;
00089   case Twine::DecLLKind:
00090     OS << *Ptr.decLL;
00091     break;
00092   case Twine::UHexKind:
00093     OS.write_hex(*Ptr.uHex);
00094     break;
00095   }
00096 }
00097 
00098 void Twine::printOneChildRepr(raw_ostream &OS, Child Ptr,
00099                               NodeKind Kind) const {
00100   switch (Kind) {
00101   case Twine::NullKind:
00102     OS << "null"; break;
00103   case Twine::EmptyKind:
00104     OS << "empty"; break;
00105   case Twine::TwineKind:
00106     OS << "rope:";
00107     Ptr.twine->printRepr(OS);
00108     break;
00109   case Twine::CStringKind:
00110     OS << "cstring:\""
00111        << Ptr.cString << "\"";
00112     break;
00113   case Twine::StdStringKind:
00114     OS << "std::string:\""
00115        << Ptr.stdString << "\"";
00116     break;
00117   case Twine::StringRefKind:
00118     OS << "stringref:\""
00119        << Ptr.stringRef << "\"";
00120     break;
00121   case Twine::SmallStringKind:
00122     OS << "smallstring:\""
00123        << *Ptr.smallString << "\"";
00124     break;
00125   case Twine::CharKind:
00126     OS << "char:\"" << Ptr.character << "\"";
00127     break;
00128   case Twine::DecUIKind:
00129     OS << "decUI:\"" << Ptr.decUI << "\"";
00130     break;
00131   case Twine::DecIKind:
00132     OS << "decI:\"" << Ptr.decI << "\"";
00133     break;
00134   case Twine::DecULKind:
00135     OS << "decUL:\"" << *Ptr.decUL << "\"";
00136     break;
00137   case Twine::DecLKind:
00138     OS << "decL:\"" << *Ptr.decL << "\"";
00139     break;
00140   case Twine::DecULLKind:
00141     OS << "decULL:\"" << *Ptr.decULL << "\"";
00142     break;
00143   case Twine::DecLLKind:
00144     OS << "decLL:\"" << *Ptr.decLL << "\"";
00145     break;
00146   case Twine::UHexKind:
00147     OS << "uhex:\"" << Ptr.uHex << "\"";
00148     break;
00149   }
00150 }
00151 
00152 void Twine::print(raw_ostream &OS) const {
00153   printOneChild(OS, LHS, getLHSKind());
00154   printOneChild(OS, RHS, getRHSKind());
00155 }
00156 
00157 void Twine::printRepr(raw_ostream &OS) const {
00158   OS << "(Twine ";
00159   printOneChildRepr(OS, LHS, getLHSKind());
00160   OS << " ";
00161   printOneChildRepr(OS, RHS, getRHSKind());
00162   OS << ")";
00163 }
00164 
00165 void Twine::dump() const {
00166   print(dbgs());
00167 }
00168 
00169 void Twine::dumpRepr() const {
00170   printRepr(dbgs());
00171 }