1 //===-- Twine.cpp - Fast Temporary String Concatenation -------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/ADT/Twine.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/Support/Debug.h"
13 #include "llvm/Support/raw_ostream.h"
14 using namespace llvm;
15 
str() const16 std::string Twine::str() const {
17   // If we're storing only a std::string, just return it.
18   if (LHSKind == StdStringKind && RHSKind == EmptyKind)
19     return *LHS.stdString;
20 
21   // Otherwise, flatten and copy the contents first.
22   SmallString<256> Vec;
23   return toStringRef(Vec).str();
24 }
25 
toVector(SmallVectorImpl<char> & Out) const26 void Twine::toVector(SmallVectorImpl<char> &Out) const {
27   raw_svector_ostream OS(Out);
28   print(OS);
29 }
30 
toNullTerminatedStringRef(SmallVectorImpl<char> & Out) const31 StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
32   if (isUnary()) {
33     switch (getLHSKind()) {
34     case CStringKind:
35       // Already null terminated, yay!
36       return StringRef(LHS.cString);
37     case StdStringKind: {
38       const std::string *str = LHS.stdString;
39       return StringRef(str->c_str(), str->size());
40     }
41     default:
42       break;
43     }
44   }
45   toVector(Out);
46   Out.push_back(0);
47   Out.pop_back();
48   return StringRef(Out.data(), Out.size());
49 }
50 
printOneChild(raw_ostream & OS,Child Ptr,NodeKind Kind) const51 void Twine::printOneChild(raw_ostream &OS, Child Ptr,
52                           NodeKind Kind) const {
53   switch (Kind) {
54   case Twine::NullKind: break;
55   case Twine::EmptyKind: break;
56   case Twine::TwineKind:
57     Ptr.twine->print(OS);
58     break;
59   case Twine::CStringKind:
60     OS << Ptr.cString;
61     break;
62   case Twine::StdStringKind:
63     OS << *Ptr.stdString;
64     break;
65   case Twine::StringRefKind:
66     OS << *Ptr.stringRef;
67     break;
68   case Twine::SmallStringKind:
69     OS << *Ptr.smallString;
70     break;
71   case Twine::CharKind:
72     OS << Ptr.character;
73     break;
74   case Twine::DecUIKind:
75     OS << Ptr.decUI;
76     break;
77   case Twine::DecIKind:
78     OS << Ptr.decI;
79     break;
80   case Twine::DecULKind:
81     OS << *Ptr.decUL;
82     break;
83   case Twine::DecLKind:
84     OS << *Ptr.decL;
85     break;
86   case Twine::DecULLKind:
87     OS << *Ptr.decULL;
88     break;
89   case Twine::DecLLKind:
90     OS << *Ptr.decLL;
91     break;
92   case Twine::UHexKind:
93     OS.write_hex(*Ptr.uHex);
94     break;
95   }
96 }
97 
printOneChildRepr(raw_ostream & OS,Child Ptr,NodeKind Kind) const98 void Twine::printOneChildRepr(raw_ostream &OS, Child Ptr,
99                               NodeKind Kind) const {
100   switch (Kind) {
101   case Twine::NullKind:
102     OS << "null"; break;
103   case Twine::EmptyKind:
104     OS << "empty"; break;
105   case Twine::TwineKind:
106     OS << "rope:";
107     Ptr.twine->printRepr(OS);
108     break;
109   case Twine::CStringKind:
110     OS << "cstring:\""
111        << Ptr.cString << "\"";
112     break;
113   case Twine::StdStringKind:
114     OS << "std::string:\""
115        << Ptr.stdString << "\"";
116     break;
117   case Twine::StringRefKind:
118     OS << "stringref:\""
119        << Ptr.stringRef << "\"";
120     break;
121   case Twine::SmallStringKind:
122     OS << "smallstring:\""
123        << *Ptr.smallString << "\"";
124     break;
125   case Twine::CharKind:
126     OS << "char:\"" << Ptr.character << "\"";
127     break;
128   case Twine::DecUIKind:
129     OS << "decUI:\"" << Ptr.decUI << "\"";
130     break;
131   case Twine::DecIKind:
132     OS << "decI:\"" << Ptr.decI << "\"";
133     break;
134   case Twine::DecULKind:
135     OS << "decUL:\"" << *Ptr.decUL << "\"";
136     break;
137   case Twine::DecLKind:
138     OS << "decL:\"" << *Ptr.decL << "\"";
139     break;
140   case Twine::DecULLKind:
141     OS << "decULL:\"" << *Ptr.decULL << "\"";
142     break;
143   case Twine::DecLLKind:
144     OS << "decLL:\"" << *Ptr.decLL << "\"";
145     break;
146   case Twine::UHexKind:
147     OS << "uhex:\"" << Ptr.uHex << "\"";
148     break;
149   }
150 }
151 
print(raw_ostream & OS) const152 void Twine::print(raw_ostream &OS) const {
153   printOneChild(OS, LHS, getLHSKind());
154   printOneChild(OS, RHS, getRHSKind());
155 }
156 
printRepr(raw_ostream & OS) const157 void Twine::printRepr(raw_ostream &OS) const {
158   OS << "(Twine ";
159   printOneChildRepr(OS, LHS, getLHSKind());
160   OS << " ";
161   printOneChildRepr(OS, RHS, getRHSKind());
162   OS << ")";
163 }
164 
dump() const165 void Twine::dump() const {
166   print(dbgs());
167 }
168 
dumpRepr() const169 void Twine::dumpRepr() const {
170   printRepr(dbgs());
171 }
172