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 {
18 return toStringRef(Vec
).str();
21 void Twine::toVector(SmallVectorImpl
<char> &Out
) const {
22 raw_svector_ostream
OS(Out
);
26 StringRef
Twine::toStringRef(SmallVectorImpl
<char> &Out
) const {
27 if (isSingleStringRef())
28 return getSingleStringRef();
30 return StringRef(Out
.data(), Out
.size());
33 void Twine::printOneChild(raw_ostream
&OS
, const void *Ptr
,
34 NodeKind Kind
) const {
36 case Twine::NullKind
: break;
37 case Twine::EmptyKind
: break;
38 case Twine::TwineKind
:
39 static_cast<const Twine
*>(Ptr
)->print(OS
);
41 case Twine::CStringKind
:
42 OS
<< static_cast<const char*>(Ptr
);
44 case Twine::StdStringKind
:
45 OS
<< *static_cast<const std::string
*>(Ptr
);
47 case Twine::StringRefKind
:
48 OS
<< *static_cast<const StringRef
*>(Ptr
);
50 case Twine::DecUIKind
:
51 OS
<< (unsigned)(uintptr_t)Ptr
;
54 OS
<< (int)(intptr_t)Ptr
;
56 case Twine::DecULKind
:
57 OS
<< *static_cast<const unsigned long*>(Ptr
);
60 OS
<< *static_cast<const long*>(Ptr
);
62 case Twine::DecULLKind
:
63 OS
<< *static_cast<const unsigned long long*>(Ptr
);
65 case Twine::DecLLKind
:
66 OS
<< *static_cast<const long long*>(Ptr
);
69 OS
.write_hex(*static_cast<const uint64_t*>(Ptr
));
74 void Twine::printOneChildRepr(raw_ostream
&OS
, const void *Ptr
,
75 NodeKind Kind
) const {
79 case Twine::EmptyKind
:
81 case Twine::TwineKind
:
83 static_cast<const Twine
*>(Ptr
)->printRepr(OS
);
85 case Twine::CStringKind
:
87 << static_cast<const char*>(Ptr
) << "\"";
89 case Twine::StdStringKind
:
90 OS
<< "std::string:\""
91 << static_cast<const std::string
*>(Ptr
) << "\"";
93 case Twine::StringRefKind
:
95 << static_cast<const StringRef
*>(Ptr
) << "\"";
97 case Twine::DecUIKind
:
98 OS
<< "decUI:\"" << (unsigned)(uintptr_t)Ptr
<< "\"";
100 case Twine::DecIKind
:
101 OS
<< "decI:\"" << (int)(intptr_t)Ptr
<< "\"";
103 case Twine::DecULKind
:
104 OS
<< "decUL:\"" << *static_cast<const unsigned long*>(Ptr
) << "\"";
106 case Twine::DecLKind
:
107 OS
<< "decL:\"" << *static_cast<const long*>(Ptr
) << "\"";
109 case Twine::DecULLKind
:
110 OS
<< "decULL:\"" << *static_cast<const unsigned long long*>(Ptr
) << "\"";
112 case Twine::DecLLKind
:
113 OS
<< "decLL:\"" << *static_cast<const long long*>(Ptr
) << "\"";
115 case Twine::UHexKind
:
116 OS
<< "uhex:\"" << static_cast<const uint64_t*>(Ptr
) << "\"";
121 void Twine::print(raw_ostream
&OS
) const {
122 printOneChild(OS
, LHS
, getLHSKind());
123 printOneChild(OS
, RHS
, getRHSKind());
126 void Twine::printRepr(raw_ostream
&OS
) const {
128 printOneChildRepr(OS
, LHS
, getLHSKind());
130 printOneChildRepr(OS
, RHS
, getRHSKind());
134 void Twine::dump() const {
138 void Twine::dumpRepr() const {
139 printRepr(llvm::dbgs());