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:\"" << *Ptr.smallString << "\"";
123 break;
124 case Twine::CharKind:
125 OS << "char:\"" << Ptr.character << "\"";
126 break;
127 case Twine::DecUIKind:
128 OS << "decUI:\"" << Ptr.decUI << "\"";
129 break;
130 case Twine::DecIKind:
131 OS << "decI:\"" << Ptr.decI << "\"";
132 break;
133 case Twine::DecULKind:
134 OS << "decUL:\"" << *Ptr.decUL << "\"";
135 break;
136 case Twine::DecLKind:
137 OS << "decL:\"" << *Ptr.decL << "\"";
138 break;
139 case Twine::DecULLKind:
140 OS << "decULL:\"" << *Ptr.decULL << "\"";
141 break;
142 case Twine::DecLLKind:
143 OS << "decLL:\"" << *Ptr.decLL << "\"";
144 break;
145 case Twine::UHexKind:
146 OS << "uhex:\"" << Ptr.uHex << "\"";
147 break;
148 }
149 }
150
print(raw_ostream & OS) const151 void Twine::print(raw_ostream &OS) const {
152 printOneChild(OS, LHS, getLHSKind());
153 printOneChild(OS, RHS, getRHSKind());
154 }
155
printRepr(raw_ostream & OS) const156 void Twine::printRepr(raw_ostream &OS) const {
157 OS << "(Twine ";
158 printOneChildRepr(OS, LHS, getLHSKind());
159 OS << " ";
160 printOneChildRepr(OS, RHS, getRHSKind());
161 OS << ")";
162 }
163
dump() const164 LLVM_DUMP_METHOD void Twine::dump() const {
165 print(dbgs());
166 }
167
dumpRepr() const168 void Twine::dumpRepr() const {
169 printRepr(dbgs());
170 }
171