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/Support/raw_ostream.h"
14 std::string Twine::str() const {
16 raw_string_ostream OS(Res);
21 void Twine::toVector(SmallVectorImpl<char> &Out) const {
22 // FIXME: This is very inefficient, since we are creating a large raw_ostream
23 // buffer -- hitting malloc, which we were supposed to avoid -- all when we
24 // have this pretty little small vector available.
26 // The best way to fix this is to make raw_svector_ostream do the right thing
27 // and be efficient, by augmenting the base raw_ostream with the ability to
28 // have the buffer managed by a concrete implementation.
29 raw_svector_ostream OS(Out);
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::UDec32Kind:
51 OS << *static_cast<const uint32_t*>(Ptr);
53 case Twine::SDec32Kind:
54 OS << *static_cast<const int32_t*>(Ptr);
56 case Twine::UDec64Kind:
57 OS << *static_cast<const uint64_t*>(Ptr);
59 case Twine::SDec64Kind:
60 OS << *static_cast<const int64_t*>(Ptr);
63 // FIXME: Add raw_ostream functionality for this.
64 OS << ::utohexstr(*static_cast<const uint64_t*>(Ptr));
69 void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr,
70 NodeKind Kind) const {
74 case Twine::EmptyKind:
76 case Twine::TwineKind:
78 static_cast<const Twine*>(Ptr)->printRepr(OS);
80 case Twine::CStringKind:
82 << static_cast<const char*>(Ptr) << "\"";
84 case Twine::StdStringKind:
85 OS << "std::string:\""
86 << static_cast<const std::string*>(Ptr) << "\"";
88 case Twine::StringRefKind:
90 << static_cast<const StringRef*>(Ptr) << "\"";
92 case Twine::UDec32Kind:
93 OS << "udec32:" << static_cast<const uint64_t*>(Ptr) << "\"";
95 case Twine::SDec32Kind:
96 OS << "sdec32:" << static_cast<const int64_t*>(Ptr) << "\"";
98 case Twine::UDec64Kind:
99 OS << "udec64:" << static_cast<const uint64_t*>(Ptr) << "\"";
101 case Twine::SDec64Kind:
102 OS << "sdec64:" << static_cast<const int64_t*>(Ptr) << "\"";
104 case Twine::UHexKind:
105 OS << "uhex:" << static_cast<const uint64_t*>(Ptr) << "\"";
110 void Twine::print(raw_ostream &OS) const {
111 printOneChild(OS, LHS, getLHSKind());
112 printOneChild(OS, RHS, getRHSKind());
115 void Twine::printRepr(raw_ostream &OS) const {
117 printOneChildRepr(OS, LHS, getLHSKind());
119 printOneChildRepr(OS, RHS, getRHSKind());
123 void Twine::dump() const {
127 void Twine::dumpRepr() const {
128 printRepr(llvm::errs());