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());
47 case StringLiteralKind
:
48 return StringRef(LHS
.ptrAndLength
.ptr
, LHS
.ptrAndLength
.length
);
56 return StringRef(Out
.data(), Out
.size());
59 void Twine::printOneChild(raw_ostream
&OS
, Child Ptr
,
60 NodeKind Kind
) const {
62 case Twine::NullKind
: break;
63 case Twine::EmptyKind
: break;
64 case Twine::TwineKind
:
67 case Twine::CStringKind
:
70 case Twine::StdStringKind
:
73 case Twine::PtrAndLengthKind
:
74 case Twine::StringLiteralKind
:
75 OS
<< StringRef(Ptr
.ptrAndLength
.ptr
, Ptr
.ptrAndLength
.length
);
77 case Twine::FormatvObjectKind
:
78 OS
<< *Ptr
.formatvObject
;
83 case Twine::DecUIKind
:
89 case Twine::DecULKind
:
95 case Twine::DecULLKind
:
98 case Twine::DecLLKind
:
101 case Twine::UHexKind
:
102 OS
.write_hex(*Ptr
.uHex
);
107 void Twine::printOneChildRepr(raw_ostream
&OS
, Child Ptr
,
108 NodeKind Kind
) const {
110 case Twine::NullKind
:
112 case Twine::EmptyKind
:
113 OS
<< "empty"; break;
114 case Twine::TwineKind
:
116 Ptr
.twine
->printRepr(OS
);
118 case Twine::CStringKind
:
120 << Ptr
.cString
<< "\"";
122 case Twine::StdStringKind
:
123 OS
<< "std::string:\""
124 << Ptr
.stdString
<< "\"";
126 case Twine::PtrAndLengthKind
:
127 OS
<< "ptrAndLength:\""
128 << StringRef(Ptr
.ptrAndLength
.ptr
, Ptr
.ptrAndLength
.length
) << "\"";
130 case Twine::StringLiteralKind
:
131 OS
<< "constexprPtrAndLength:\""
132 << StringRef(Ptr
.ptrAndLength
.ptr
, Ptr
.ptrAndLength
.length
) << "\"";
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 {