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/Support/Debug.h"
13 #include "llvm/Support/raw_ostream.h"
16 std::string Twine::str() const {
17 // If we're storing only a std::string, just return it.
18 if (LHSKind == StdStringKind && RHSKind == EmptyKind)
19 return *LHS.stdString;
21 // Otherwise, flatten and copy the contents first.
23 return toStringRef(Vec).str();
26 void Twine::toVector(SmallVectorImpl<char> &Out) const {
27 raw_svector_ostream OS(Out);
31 StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
33 switch (getLHSKind()) {
35 // Already null terminated, yay!
36 return StringRef(LHS.cString);
38 const std::string *str = LHS.stdString;
39 return StringRef(str->c_str(), str->size());
48 return StringRef(Out.data(), Out.size());
51 void Twine::printOneChild(raw_ostream &OS, Child Ptr,
52 NodeKind Kind) const {
54 case Twine::NullKind: break;
55 case Twine::EmptyKind: break;
56 case Twine::TwineKind:
59 case Twine::CStringKind:
62 case Twine::StdStringKind:
65 case Twine::StringRefKind:
68 case Twine::SmallStringKind:
69 OS << *Ptr.smallString;
74 case Twine::DecUIKind:
80 case Twine::DecULKind:
86 case Twine::DecULLKind:
89 case Twine::DecLLKind:
93 OS.write_hex(*Ptr.uHex);
98 void Twine::printOneChildRepr(raw_ostream &OS, Child Ptr,
99 NodeKind Kind) const {
101 case Twine::NullKind:
103 case Twine::EmptyKind:
104 OS << "empty"; break;
105 case Twine::TwineKind:
107 Ptr.twine->printRepr(OS);
109 case Twine::CStringKind:
111 << Ptr.cString << "\"";
113 case Twine::StdStringKind:
114 OS << "std::string:\""
115 << Ptr.stdString << "\"";
117 case Twine::StringRefKind:
119 << Ptr.stringRef << "\"";
121 case Twine::SmallStringKind:
\r
122 OS << "smallstring:\""
\r
123 << *Ptr.smallString << "\"";
\r
125 case Twine::CharKind:
126 OS << "char:\"" << Ptr.character << "\"";
128 case Twine::DecUIKind:
129 OS << "decUI:\"" << Ptr.decUI << "\"";
131 case Twine::DecIKind:
132 OS << "decI:\"" << Ptr.decI << "\"";
134 case Twine::DecULKind:
135 OS << "decUL:\"" << *Ptr.decUL << "\"";
137 case Twine::DecLKind:
138 OS << "decL:\"" << *Ptr.decL << "\"";
140 case Twine::DecULLKind:
141 OS << "decULL:\"" << *Ptr.decULL << "\"";
143 case Twine::DecLLKind:
144 OS << "decLL:\"" << *Ptr.decLL << "\"";
146 case Twine::UHexKind:
147 OS << "uhex:\"" << Ptr.uHex << "\"";
152 void Twine::print(raw_ostream &OS) const {
153 printOneChild(OS, LHS, getLHSKind());
154 printOneChild(OS, RHS, getRHSKind());
157 void Twine::printRepr(raw_ostream &OS) const {
159 printOneChildRepr(OS, LHS, getLHSKind());
161 printOneChildRepr(OS, RHS, getRHSKind());
165 void Twine::dump() const {
169 void Twine::dumpRepr() const {