Line data Source code
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/Config/llvm-config.h"
13 : #include "llvm/Support/Debug.h"
14 : #include "llvm/Support/FormatVariadic.h"
15 : #include "llvm/Support/raw_ostream.h"
16 : using namespace llvm;
17 :
18 12227133 : std::string Twine::str() const {
19 : // If we're storing only a std::string, just return it.
20 12227133 : if (LHSKind == StdStringKind && RHSKind == EmptyKind)
21 1633414 : return *LHS.stdString;
22 :
23 : // If we're storing a formatv_object, we can avoid an extra copy by formatting
24 : // it immediately and returning the result.
25 10593719 : if (LHSKind == FormatvObjectKind && RHSKind == EmptyKind)
26 17 : return LHS.formatvObject->str();
27 :
28 : // Otherwise, flatten and copy the contents first.
29 : SmallString<256> Vec;
30 21187374 : return toStringRef(Vec).str();
31 : }
32 :
33 16616477 : void Twine::toVector(SmallVectorImpl<char> &Out) const {
34 : raw_svector_ostream OS(Out);
35 16616564 : print(OS);
36 16616627 : }
37 :
38 4471784 : StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
39 : if (isUnary()) {
40 4251928 : switch (getLHSKind()) {
41 619953 : case CStringKind:
42 : // Already null terminated, yay!
43 1239906 : return StringRef(LHS.cString);
44 1398609 : case StdStringKind: {
45 1398609 : const std::string *str = LHS.stdString;
46 2797218 : return StringRef(str->c_str(), str->size());
47 : }
48 : default:
49 : break;
50 : }
51 : }
52 2453222 : toVector(Out);
53 2453219 : Out.push_back(0);
54 : Out.pop_back();
55 2453219 : return StringRef(Out.data(), Out.size());
56 : }
57 :
58 168911433 : void Twine::printOneChild(raw_ostream &OS, Child Ptr,
59 : NodeKind Kind) const {
60 168911433 : switch (Kind) {
61 : case Twine::NullKind: break;
62 : case Twine::EmptyKind: break;
63 32487011 : case Twine::TwineKind:
64 32487011 : Ptr.twine->print(OS);
65 32487006 : break;
66 56646855 : case Twine::CStringKind:
67 56646855 : OS << Ptr.cString;
68 56646949 : break;
69 8160539 : case Twine::StdStringKind:
70 8160539 : OS << *Ptr.stdString;
71 : break;
72 25024092 : case Twine::StringRefKind:
73 25024092 : OS << *Ptr.stringRef;
74 25024263 : break;
75 334767 : case Twine::SmallStringKind:
76 334767 : OS << *Ptr.smallString;
77 : break;
78 38253 : case Twine::FormatvObjectKind:
79 38253 : OS << *Ptr.formatvObject;
80 38253 : break;
81 8217380 : case Twine::CharKind:
82 8217380 : OS << Ptr.character;
83 : break;
84 7261685 : case Twine::DecUIKind:
85 7261685 : OS << Ptr.decUI;
86 : break;
87 6610006 : case Twine::DecIKind:
88 6610006 : OS << Ptr.decI;
89 : break;
90 531675 : case Twine::DecULKind:
91 531675 : OS << *Ptr.decUL;
92 531675 : break;
93 8744 : case Twine::DecLKind:
94 8744 : OS << *Ptr.decL;
95 8744 : break;
96 0 : case Twine::DecULLKind:
97 0 : OS << *Ptr.decULL;
98 0 : break;
99 0 : case Twine::DecLLKind:
100 0 : OS << *Ptr.decLL;
101 0 : break;
102 63615 : case Twine::UHexKind:
103 63615 : OS.write_hex(*Ptr.uHex);
104 63615 : break;
105 : }
106 168911728 : }
107 :
108 30 : void Twine::printOneChildRepr(raw_ostream &OS, Child Ptr,
109 : NodeKind Kind) const {
110 30 : switch (Kind) {
111 2 : case Twine::NullKind:
112 2 : OS << "null"; break;
113 7 : case Twine::EmptyKind:
114 7 : OS << "empty"; break;
115 3 : case Twine::TwineKind:
116 3 : OS << "rope:";
117 3 : Ptr.twine->printRepr(OS);
118 3 : break;
119 13 : case Twine::CStringKind:
120 13 : OS << "cstring:\""
121 13 : << Ptr.cString << "\"";
122 13 : break;
123 0 : case Twine::StdStringKind:
124 0 : OS << "std::string:\""
125 0 : << Ptr.stdString << "\"";
126 0 : break;
127 0 : case Twine::StringRefKind:
128 0 : OS << "stringref:\""
129 0 : << Ptr.stringRef << "\"";
130 0 : break;
131 3 : case Twine::SmallStringKind:
132 3 : OS << "smallstring:\"" << *Ptr.smallString << "\"";
133 3 : break;
134 2 : case Twine::FormatvObjectKind:
135 2 : OS << "formatv:\"" << *Ptr.formatvObject << "\"";
136 2 : break;
137 0 : case Twine::CharKind:
138 0 : OS << "char:\"" << Ptr.character << "\"";
139 0 : break;
140 0 : case Twine::DecUIKind:
141 0 : OS << "decUI:\"" << Ptr.decUI << "\"";
142 0 : break;
143 0 : case Twine::DecIKind:
144 0 : OS << "decI:\"" << Ptr.decI << "\"";
145 0 : break;
146 0 : case Twine::DecULKind:
147 0 : OS << "decUL:\"" << *Ptr.decUL << "\"";
148 0 : break;
149 0 : case Twine::DecLKind:
150 0 : OS << "decL:\"" << *Ptr.decL << "\"";
151 0 : break;
152 0 : case Twine::DecULLKind:
153 0 : OS << "decULL:\"" << *Ptr.decULL << "\"";
154 0 : break;
155 0 : case Twine::DecLLKind:
156 0 : OS << "decLL:\"" << *Ptr.decLL << "\"";
157 0 : break;
158 0 : case Twine::UHexKind:
159 0 : OS << "uhex:\"" << Ptr.uHex << "\"";
160 0 : break;
161 : }
162 30 : }
163 :
164 84454557 : void Twine::print(raw_ostream &OS) const {
165 84454557 : printOneChild(OS, LHS, getLHSKind());
166 84454509 : printOneChild(OS, RHS, getRHSKind());
167 84454738 : }
168 :
169 15 : void Twine::printRepr(raw_ostream &OS) const {
170 15 : OS << "(Twine ";
171 15 : printOneChildRepr(OS, LHS, getLHSKind());
172 15 : OS << " ";
173 15 : printOneChildRepr(OS, RHS, getRHSKind());
174 15 : OS << ")";
175 15 : }
176 :
177 : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
178 : LLVM_DUMP_METHOD void Twine::dump() const {
179 : print(dbgs());
180 : }
181 :
182 : LLVM_DUMP_METHOD void Twine::dumpRepr() const {
183 : printRepr(dbgs());
184 : }
185 : #endif
|