LLVM API Documentation
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::toStringRef(SmallVectorImpl<char> &Out) const { 00032 if (isSingleStringRef()) 00033 return getSingleStringRef(); 00034 toVector(Out); 00035 return StringRef(Out.data(), Out.size()); 00036 } 00037 00038 StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const { 00039 if (isUnary()) { 00040 switch (getLHSKind()) { 00041 case CStringKind: 00042 // Already null terminated, yay! 00043 return StringRef(LHS.cString); 00044 case StdStringKind: { 00045 const std::string *str = LHS.stdString; 00046 return StringRef(str->c_str(), str->size()); 00047 } 00048 default: 00049 break; 00050 } 00051 } 00052 toVector(Out); 00053 Out.push_back(0); 00054 Out.pop_back(); 00055 return StringRef(Out.data(), Out.size()); 00056 } 00057 00058 void Twine::printOneChild(raw_ostream &OS, Child Ptr, 00059 NodeKind Kind) const { 00060 switch (Kind) { 00061 case Twine::NullKind: break; 00062 case Twine::EmptyKind: break; 00063 case Twine::TwineKind: 00064 Ptr.twine->print(OS); 00065 break; 00066 case Twine::CStringKind: 00067 OS << Ptr.cString; 00068 break; 00069 case Twine::StdStringKind: 00070 OS << *Ptr.stdString; 00071 break; 00072 case Twine::StringRefKind: 00073 OS << *Ptr.stringRef; 00074 break; 00075 case Twine::CharKind: 00076 OS << Ptr.character; 00077 break; 00078 case Twine::DecUIKind: 00079 OS << Ptr.decUI; 00080 break; 00081 case Twine::DecIKind: 00082 OS << Ptr.decI; 00083 break; 00084 case Twine::DecULKind: 00085 OS << *Ptr.decUL; 00086 break; 00087 case Twine::DecLKind: 00088 OS << *Ptr.decL; 00089 break; 00090 case Twine::DecULLKind: 00091 OS << *Ptr.decULL; 00092 break; 00093 case Twine::DecLLKind: 00094 OS << *Ptr.decLL; 00095 break; 00096 case Twine::UHexKind: 00097 OS.write_hex(*Ptr.uHex); 00098 break; 00099 } 00100 } 00101 00102 void Twine::printOneChildRepr(raw_ostream &OS, Child Ptr, 00103 NodeKind Kind) const { 00104 switch (Kind) { 00105 case Twine::NullKind: 00106 OS << "null"; break; 00107 case Twine::EmptyKind: 00108 OS << "empty"; break; 00109 case Twine::TwineKind: 00110 OS << "rope:"; 00111 Ptr.twine->printRepr(OS); 00112 break; 00113 case Twine::CStringKind: 00114 OS << "cstring:\"" 00115 << Ptr.cString << "\""; 00116 break; 00117 case Twine::StdStringKind: 00118 OS << "std::string:\"" 00119 << Ptr.stdString << "\""; 00120 break; 00121 case Twine::StringRefKind: 00122 OS << "stringref:\"" 00123 << Ptr.stringRef << "\""; 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 }