[llvm-exegesis][NFC] Fix typo
[llvm-complete.git] / lib / Support / Twine.cpp
blob4726c8ab74940ca5b6d6317debabf2e6992f56ff
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 //===----------------------------------------------------------------------===//
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;
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.
29 SmallString<256> Vec;
30 return toStringRef(Vec).str();
33 void Twine::toVector(SmallVectorImpl<char> &Out) const {
34 raw_svector_ostream OS(Out);
35 print(OS);
38 StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
39 if (isUnary()) {
40 switch (getLHSKind()) {
41 case CStringKind:
42 // Already null terminated, yay!
43 return StringRef(LHS.cString);
44 case StdStringKind: {
45 const std::string *str = LHS.stdString;
46 return StringRef(str->c_str(), str->size());
48 default:
49 break;
52 toVector(Out);
53 Out.push_back(0);
54 Out.pop_back();
55 return StringRef(Out.data(), Out.size());
58 void Twine::printOneChild(raw_ostream &OS, Child Ptr,
59 NodeKind Kind) const {
60 switch (Kind) {
61 case Twine::NullKind: break;
62 case Twine::EmptyKind: break;
63 case Twine::TwineKind:
64 Ptr.twine->print(OS);
65 break;
66 case Twine::CStringKind:
67 OS << Ptr.cString;
68 break;
69 case Twine::StdStringKind:
70 OS << *Ptr.stdString;
71 break;
72 case Twine::StringRefKind:
73 OS << *Ptr.stringRef;
74 break;
75 case Twine::SmallStringKind:
76 OS << *Ptr.smallString;
77 break;
78 case Twine::FormatvObjectKind:
79 OS << *Ptr.formatvObject;
80 break;
81 case Twine::CharKind:
82 OS << Ptr.character;
83 break;
84 case Twine::DecUIKind:
85 OS << Ptr.decUI;
86 break;
87 case Twine::DecIKind:
88 OS << Ptr.decI;
89 break;
90 case Twine::DecULKind:
91 OS << *Ptr.decUL;
92 break;
93 case Twine::DecLKind:
94 OS << *Ptr.decL;
95 break;
96 case Twine::DecULLKind:
97 OS << *Ptr.decULL;
98 break;
99 case Twine::DecLLKind:
100 OS << *Ptr.decLL;
101 break;
102 case Twine::UHexKind:
103 OS.write_hex(*Ptr.uHex);
104 break;
108 void Twine::printOneChildRepr(raw_ostream &OS, Child Ptr,
109 NodeKind Kind) const {
110 switch (Kind) {
111 case Twine::NullKind:
112 OS << "null"; break;
113 case Twine::EmptyKind:
114 OS << "empty"; break;
115 case Twine::TwineKind:
116 OS << "rope:";
117 Ptr.twine->printRepr(OS);
118 break;
119 case Twine::CStringKind:
120 OS << "cstring:\""
121 << Ptr.cString << "\"";
122 break;
123 case Twine::StdStringKind:
124 OS << "std::string:\""
125 << Ptr.stdString << "\"";
126 break;
127 case Twine::StringRefKind:
128 OS << "stringref:\""
129 << Ptr.stringRef << "\"";
130 break;
131 case Twine::SmallStringKind:
132 OS << "smallstring:\"" << *Ptr.smallString << "\"";
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;
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 {
170 OS << "(Twine ";
171 printOneChildRepr(OS, LHS, getLHSKind());
172 OS << " ";
173 printOneChildRepr(OS, RHS, getRHSKind());
174 OS << ")";
177 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
178 LLVM_DUMP_METHOD void Twine::dump() const {
179 print(dbgs());
182 LLVM_DUMP_METHOD void Twine::dumpRepr() const {
183 printRepr(dbgs());
185 #endif