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::toStringRef(SmallVectorImpl<char> &Out) const {
32 if (isSingleStringRef())
33 return getSingleStringRef();
35 return StringRef(Out.data(), Out.size());
38 StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
40 switch (getLHSKind()) {
42 // Already null terminated, yay!
43 return StringRef(LHS.cString);
45 const std::string *str = LHS.stdString;
46 return StringRef(str->c_str(), str->size());
55 return StringRef(Out.data(), Out.size());
58 void Twine::printOneChild(raw_ostream &OS, Child Ptr,
59 NodeKind Kind) const {
61 case Twine::NullKind: break;
62 case Twine::EmptyKind: break;
63 case Twine::TwineKind:
66 case Twine::CStringKind:
69 case Twine::StdStringKind:
72 case Twine::StringRefKind:
78 case Twine::DecUIKind:
84 case Twine::DecULKind:
90 case Twine::DecULLKind:
93 case Twine::DecLLKind:
97 OS.write_hex(*Ptr.uHex);
102 void Twine::printOneChildRepr(raw_ostream &OS, Child Ptr,
103 NodeKind Kind) const {
105 case Twine::NullKind:
107 case Twine::EmptyKind:
108 OS << "empty"; break;
109 case Twine::TwineKind:
111 Ptr.twine->printRepr(OS);
113 case Twine::CStringKind:
115 << Ptr.cString << "\"";
117 case Twine::StdStringKind:
118 OS << "std::string:\""
119 << Ptr.stdString << "\"";
121 case Twine::StringRefKind:
123 << Ptr.stringRef << "\"";
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 {
170 printRepr(llvm::dbgs());