1//===-- Twine.cpp - Fast Temporary String Concatenation -------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
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"
15using namespace llvm;
16
17std::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;
21
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();
26
27 // Otherwise, flatten and copy the contents first.
28 SmallString<256> Vec;
29 return toStringRef(Out&: Vec).str();
30}
31
32void Twine::toVector(SmallVectorImpl<char> &Out) const {
33 raw_svector_ostream OS(Out);
34 print(OS);
35}
36
37StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
38 if (isUnary()) {
39 switch (getLHSKind()) {
40 case CStringKind:
41 // Already null terminated, yay!
42 return StringRef(LHS.cString);
43 case StdStringKind: {
44 const std::string *str = LHS.stdString;
45 return StringRef(str->c_str(), str->size());
46 }
47 case StringLiteralKind:
48 return StringRef(LHS.ptrAndLength.ptr, LHS.ptrAndLength.length);
49 default:
50 break;
51 }
52 }
53 toVector(Out);
54 Out.push_back(Elt: 0);
55 Out.pop_back();
56 return StringRef(Out.data(), Out.size());
57}
58
59void Twine::printOneChild(raw_ostream &OS, Child Ptr,
60 NodeKind Kind) const {
61 switch (Kind) {
62 case Twine::NullKind: break;
63 case Twine::EmptyKind: break;
64 case Twine::TwineKind:
65 Ptr.twine->print(OS);
66 break;
67 case Twine::CStringKind:
68 OS << Ptr.cString;
69 break;
70 case Twine::StdStringKind:
71 OS << *Ptr.stdString;
72 break;
73 case Twine::PtrAndLengthKind:
74 case Twine::StringLiteralKind:
75 OS << StringRef(Ptr.ptrAndLength.ptr, Ptr.ptrAndLength.length);
76 break;
77 case Twine::FormatvObjectKind:
78 OS << *Ptr.formatvObject;
79 break;
80 case Twine::CharKind:
81 OS << Ptr.character;
82 break;
83 case Twine::DecUIKind:
84 OS << Ptr.decUI;
85 break;
86 case Twine::DecIKind:
87 OS << Ptr.decI;
88 break;
89 case Twine::DecULKind:
90 OS << *Ptr.decUL;
91 break;
92 case Twine::DecLKind:
93 OS << *Ptr.decL;
94 break;
95 case Twine::DecULLKind:
96 OS << *Ptr.decULL;
97 break;
98 case Twine::DecLLKind:
99 OS << *Ptr.decLL;
100 break;
101 case Twine::UHexKind:
102 OS.write_hex(N: *Ptr.uHex);
103 break;
104 }
105}
106
107void Twine::printOneChildRepr(raw_ostream &OS, Child Ptr,
108 NodeKind Kind) const {
109 switch (Kind) {
110 case Twine::NullKind:
111 OS << "null"; break;
112 case Twine::EmptyKind:
113 OS << "empty"; break;
114 case Twine::TwineKind:
115 OS << "rope:";
116 Ptr.twine->printRepr(OS);
117 break;
118 case Twine::CStringKind:
119 OS << "cstring:\""
120 << Ptr.cString << "\"";
121 break;
122 case Twine::StdStringKind:
123 OS << "std::string:\""
124 << Ptr.stdString << "\"";
125 break;
126 case Twine::PtrAndLengthKind:
127 OS << "ptrAndLength:\""
128 << StringRef(Ptr.ptrAndLength.ptr, Ptr.ptrAndLength.length) << "\"";
129 break;
130 case Twine::StringLiteralKind:
131 OS << "constexprPtrAndLength:\""
132 << StringRef(Ptr.ptrAndLength.ptr, Ptr.ptrAndLength.length) << "\"";
133 break;
134 case Twine::FormatvObjectKind:
135 OS << "formatv:\"" << *Ptr.formatvObject << "\"";
136 break;
137 case Twine::CharKind:
138 OS << "char:\"" << Ptr.character << "\"";
139 break;
140 case Twine::DecUIKind:
141 OS << "decUI:\"" << Ptr.decUI << "\"";
142 break;
143 case Twine::DecIKind:
144 OS << "decI:\"" << Ptr.decI << "\"";
145 break;
146 case Twine::DecULKind:
147 OS << "decUL:\"" << *Ptr.decUL << "\"";
148 break;
149 case Twine::DecLKind:
150 OS << "decL:\"" << *Ptr.decL << "\"";
151 break;
152 case Twine::DecULLKind:
153 OS << "decULL:\"" << *Ptr.decULL << "\"";
154 break;
155 case Twine::DecLLKind:
156 OS << "decLL:\"" << *Ptr.decLL << "\"";
157 break;
158 case Twine::UHexKind:
159 OS << "uhex:\"" << Ptr.uHex << "\"";
160 break;
161 }
162}
163
164void Twine::print(raw_ostream &OS) const {
165 printOneChild(OS, Ptr: LHS, Kind: getLHSKind());
166 printOneChild(OS, Ptr: RHS, Kind: getRHSKind());
167}
168
169void Twine::printRepr(raw_ostream &OS) const {
170 OS << "(Twine ";
171 printOneChildRepr(OS, Ptr: LHS, Kind: getLHSKind());
172 OS << " ";
173 printOneChildRepr(OS, Ptr: RHS, Kind: getRHSKind());
174 OS << ")";
175}
176
177#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
178LLVM_DUMP_METHOD void Twine::dump() const {
179 print(OS&: dbgs());
180}
181
182LLVM_DUMP_METHOD void Twine::dumpRepr() const {
183 printRepr(OS&: dbgs());
184}
185#endif
186

source code of llvm/lib/Support/Twine.cpp