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
toStringRef(SmallVectorImpl<char> & Out) const31 StringRef Twine::toStringRef(SmallVectorImpl<char> &Out) const {
32 if (isSingleStringRef())
33 return getSingleStringRef();
34 toVector(Out);
35 return StringRef(Out.data(), Out.size());
36 }
37
toNullTerminatedStringRef(SmallVectorImpl<char> & Out) const38 StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
39 if (isUnary()) {
40 switch (getLHSKind()) {
41 case CStringKind:
42 // Already null terminated, yay!
43 return StringRef(LHS.cString);
44 case StdStringKind: {
45 const std::string *str = LHS.stdString;
46 return StringRef(str->c_str(), str->size());
47 }
48 default:
49 break;
50 }
51 }
52 toVector(Out);
53 Out.push_back(0);
54 Out.pop_back();
55 return StringRef(Out.data(), Out.size());
56 }
57
printOneChild(raw_ostream & OS,Child Ptr,NodeKind Kind) const58 void Twine::printOneChild(raw_ostream &OS, Child Ptr,
59 NodeKind Kind) const {
60 switch (Kind) {
61 case Twine::NullKind: break;
62 case Twine::EmptyKind: break;
63 case Twine::TwineKind:
64 Ptr.twine->print(OS);
65 break;
66 case Twine::CStringKind:
67 OS << Ptr.cString;
68 break;
69 case Twine::StdStringKind:
70 OS << *Ptr.stdString;
71 break;
72 case Twine::StringRefKind:
73 OS << *Ptr.stringRef;
74 break;
75 case Twine::CharKind:
76 OS << Ptr.character;
77 break;
78 case Twine::DecUIKind:
79 OS << Ptr.decUI;
80 break;
81 case Twine::DecIKind:
82 OS << Ptr.decI;
83 break;
84 case Twine::DecULKind:
85 OS << *Ptr.decUL;
86 break;
87 case Twine::DecLKind:
88 OS << *Ptr.decL;
89 break;
90 case Twine::DecULLKind:
91 OS << *Ptr.decULL;
92 break;
93 case Twine::DecLLKind:
94 OS << *Ptr.decLL;
95 break;
96 case Twine::UHexKind:
97 OS.write_hex(*Ptr.uHex);
98 break;
99 }
100 }
101
printOneChildRepr(raw_ostream & OS,Child Ptr,NodeKind Kind) const102 void Twine::printOneChildRepr(raw_ostream &OS, Child Ptr,
103 NodeKind Kind) const {
104 switch (Kind) {
105 case Twine::NullKind:
106 OS << "null"; break;
107 case Twine::EmptyKind:
108 OS << "empty"; break;
109 case Twine::TwineKind:
110 OS << "rope:";
111 Ptr.twine->printRepr(OS);
112 break;
113 case Twine::CStringKind:
114 OS << "cstring:\""
115 << Ptr.cString << "\"";
116 break;
117 case Twine::StdStringKind:
118 OS << "std::string:\""
119 << Ptr.stdString << "\"";
120 break;
121 case Twine::StringRefKind:
122 OS << "stringref:\""
123 << Ptr.stringRef << "\"";
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