1 //===-- Twine.cpp - Fast Temporary String Concatenation -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
18 std::string
Twine::str() const {
19 // If we're storing only a std::string, just return it.
20 if (LHSKind
== StdStringKind
&& RHSKind
== EmptyKind
)
21 return *LHS
.stdString
;
23 // If we're storing a formatv_object, we can avoid an extra copy by formatting
24 // it immediately and returning the result.
25 if (LHSKind
== FormatvObjectKind
&& RHSKind
== EmptyKind
)
26 return LHS
.formatvObject
->str();
28 // Otherwise, flatten and copy the contents first.
30 return toStringRef(Vec
).str();
33 void Twine::toVector(SmallVectorImpl
<char> &Out
) const {
34 raw_svector_ostream
OS(Out
);
38 StringRef
Twine::toNullTerminatedStringRef(SmallVectorImpl
<char> &Out
) const {
40 switch (getLHSKind()) {
42 // Already null terminated, yay!
43 return StringRef(LHS
.cString
);
45 const std::string
*str
= LHS
.stdString
;
46 return StringRef(str
->c_str(), str
->size());
55 return StringRef(Out
.data(), Out
.size());
58 void Twine::printOneChild(raw_ostream
&OS
, Child Ptr
,
59 NodeKind Kind
) const {
61 case Twine::NullKind
: break;
62 case Twine::EmptyKind
: break;
63 case Twine::TwineKind
:
66 case Twine::CStringKind
:
69 case Twine::StdStringKind
:
72 case Twine::StringRefKind
:
75 case Twine::SmallStringKind
:
76 OS
<< *Ptr
.smallString
;
78 case Twine::FormatvObjectKind
:
79 OS
<< *Ptr
.formatvObject
;
84 case Twine::DecUIKind
:
90 case Twine::DecULKind
:
96 case Twine::DecULLKind
:
99 case Twine::DecLLKind
:
102 case Twine::UHexKind
:
103 OS
.write_hex(*Ptr
.uHex
);
108 void Twine::printOneChildRepr(raw_ostream
&OS
, Child Ptr
,
109 NodeKind Kind
) const {
111 case Twine::NullKind
:
113 case Twine::EmptyKind
:
114 OS
<< "empty"; break;
115 case Twine::TwineKind
:
117 Ptr
.twine
->printRepr(OS
);
119 case Twine::CStringKind
:
121 << Ptr
.cString
<< "\"";
123 case Twine::StdStringKind
:
124 OS
<< "std::string:\""
125 << Ptr
.stdString
<< "\"";
127 case Twine::StringRefKind
:
129 << Ptr
.stringRef
<< "\"";
131 case Twine::SmallStringKind
:
132 OS
<< "smallstring:\"" << *Ptr
.smallString
<< "\"";
134 case Twine::FormatvObjectKind
:
135 OS
<< "formatv:\"" << *Ptr
.formatvObject
<< "\"";
137 case Twine::CharKind
:
138 OS
<< "char:\"" << Ptr
.character
<< "\"";
140 case Twine::DecUIKind
:
141 OS
<< "decUI:\"" << Ptr
.decUI
<< "\"";
143 case Twine::DecIKind
:
144 OS
<< "decI:\"" << Ptr
.decI
<< "\"";
146 case Twine::DecULKind
:
147 OS
<< "decUL:\"" << *Ptr
.decUL
<< "\"";
149 case Twine::DecLKind
:
150 OS
<< "decL:\"" << *Ptr
.decL
<< "\"";
152 case Twine::DecULLKind
:
153 OS
<< "decULL:\"" << *Ptr
.decULL
<< "\"";
155 case Twine::DecLLKind
:
156 OS
<< "decLL:\"" << *Ptr
.decLL
<< "\"";
158 case Twine::UHexKind
:
159 OS
<< "uhex:\"" << Ptr
.uHex
<< "\"";
164 void Twine::print(raw_ostream
&OS
) const {
165 printOneChild(OS
, LHS
, getLHSKind());
166 printOneChild(OS
, RHS
, getRHSKind());
169 void Twine::printRepr(raw_ostream
&OS
) const {
171 printOneChildRepr(OS
, LHS
, getLHSKind());
173 printOneChildRepr(OS
, RHS
, getRHSKind());
177 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
178 LLVM_DUMP_METHOD
void Twine::dump() const {
182 LLVM_DUMP_METHOD
void Twine::dumpRepr() const {