1 //===-- Twine.cpp - Fast Temporary String Concatenation -------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/Twine.h"
10 #include "llvm/ADT/SmallString.h"
11 #include "llvm/Config/llvm-config.h"
12 #include "llvm/Support/Debug.h"
13 #include "llvm/Support/FormatVariadic.h"
14 #include "llvm/Support/raw_ostream.h"
17 std::string
Twine::str() const {
18 // If we're storing only a std::string, just return it.
19 if (LHSKind
== StdStringKind
&& RHSKind
== EmptyKind
)
20 return *LHS
.stdString
;
22 // If we're storing a formatv_object, we can avoid an extra copy by formatting
23 // it immediately and returning the result.
24 if (LHSKind
== FormatvObjectKind
&& RHSKind
== EmptyKind
)
25 return LHS
.formatvObject
->str();
27 // Otherwise, flatten and copy the contents first.
29 return toStringRef(Vec
).str();
32 void Twine::toVector(SmallVectorImpl
<char> &Out
) const {
33 raw_svector_ostream
OS(Out
);
37 StringRef
Twine::toNullTerminatedStringRef(SmallVectorImpl
<char> &Out
) const {
39 switch (getLHSKind()) {
41 // Already null terminated, yay!
42 return StringRef(LHS
.cString
);
44 const std::string
*str
= LHS
.stdString
;
45 return StringRef(str
->c_str(), str
->size());
54 return StringRef(Out
.data(), Out
.size());
57 void Twine::printOneChild(raw_ostream
&OS
, Child Ptr
,
58 NodeKind Kind
) const {
60 case Twine::NullKind
: break;
61 case Twine::EmptyKind
: break;
62 case Twine::TwineKind
:
65 case Twine::CStringKind
:
68 case Twine::StdStringKind
:
71 case Twine::PtrAndLengthKind
:
72 OS
<< StringRef(Ptr
.ptrAndLength
.ptr
, Ptr
.ptrAndLength
.length
);
74 case Twine::FormatvObjectKind
:
75 OS
<< *Ptr
.formatvObject
;
80 case Twine::DecUIKind
:
86 case Twine::DecULKind
:
92 case Twine::DecULLKind
:
95 case Twine::DecLLKind
:
99 OS
.write_hex(*Ptr
.uHex
);
104 void Twine::printOneChildRepr(raw_ostream
&OS
, Child Ptr
,
105 NodeKind Kind
) const {
107 case Twine::NullKind
:
109 case Twine::EmptyKind
:
110 OS
<< "empty"; break;
111 case Twine::TwineKind
:
113 Ptr
.twine
->printRepr(OS
);
115 case Twine::CStringKind
:
117 << Ptr
.cString
<< "\"";
119 case Twine::StdStringKind
:
120 OS
<< "std::string:\""
121 << Ptr
.stdString
<< "\"";
123 case Twine::PtrAndLengthKind
:
124 OS
<< "ptrAndLength:\""
125 << StringRef(Ptr
.ptrAndLength
.ptr
, Ptr
.ptrAndLength
.length
) << "\"";
127 case Twine::FormatvObjectKind
:
128 OS
<< "formatv:\"" << *Ptr
.formatvObject
<< "\"";
130 case Twine::CharKind
:
131 OS
<< "char:\"" << Ptr
.character
<< "\"";
133 case Twine::DecUIKind
:
134 OS
<< "decUI:\"" << Ptr
.decUI
<< "\"";
136 case Twine::DecIKind
:
137 OS
<< "decI:\"" << Ptr
.decI
<< "\"";
139 case Twine::DecULKind
:
140 OS
<< "decUL:\"" << *Ptr
.decUL
<< "\"";
142 case Twine::DecLKind
:
143 OS
<< "decL:\"" << *Ptr
.decL
<< "\"";
145 case Twine::DecULLKind
:
146 OS
<< "decULL:\"" << *Ptr
.decULL
<< "\"";
148 case Twine::DecLLKind
:
149 OS
<< "decLL:\"" << *Ptr
.decLL
<< "\"";
151 case Twine::UHexKind
:
152 OS
<< "uhex:\"" << Ptr
.uHex
<< "\"";
157 void Twine::print(raw_ostream
&OS
) const {
158 printOneChild(OS
, LHS
, getLHSKind());
159 printOneChild(OS
, RHS
, getRHSKind());
162 void Twine::printRepr(raw_ostream
&OS
) const {
164 printOneChildRepr(OS
, LHS
, getLHSKind());
166 printOneChildRepr(OS
, RHS
, getRHSKind());
170 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
171 LLVM_DUMP_METHOD
void Twine::dump() const {
175 LLVM_DUMP_METHOD
void Twine::dumpRepr() const {