blob: c9e5f2401eca40b36c71d3f4ec89c9e84f161342 [file] [log] [blame]
Daniel Dunbar2538f7a2009-07-24 07:04:27 +00001//===-- 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//===----------------------------------------------------------------------===//
9
10#include "llvm/ADT/Twine.h"
11#include "llvm/Support/raw_ostream.h"
12using namespace llvm;
13
14std::string Twine::str() const {
15 std::string Res;
16 raw_string_ostream OS(Res);
17 print(OS);
18 return Res;
19}
20
21void Twine::toVector(SmallVectorImpl<char> &Out) const {
Daniel Dunbar763457e2009-07-29 07:08:44 +000022 // 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.
25 //
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.
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000029 raw_svector_ostream OS(Out);
30 print(OS);
31}
32
33void Twine::printOneChild(raw_ostream &OS, const void *Ptr,
34 NodeKind Kind) const {
35 switch (Kind) {
36 case Twine::NullKind: break;
37 case Twine::EmptyKind: break;
Daniel Dunbar763457e2009-07-29 07:08:44 +000038 case Twine::TwineKind:
39 static_cast<const Twine*>(Ptr)->print(OS);
40 break;
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000041 case Twine::CStringKind:
42 OS << static_cast<const char*>(Ptr);
43 break;
44 case Twine::StdStringKind:
45 OS << *static_cast<const std::string*>(Ptr);
46 break;
47 case Twine::StringRefKind:
48 OS << *static_cast<const StringRef*>(Ptr);
49 break;
Daniel Dunbar763457e2009-07-29 07:08:44 +000050 case Twine::UDecKind:
51 OS << *static_cast<const uint64_t*>(Ptr);
52 break;
53 case Twine::SDecKind:
54 OS << *static_cast<const int64_t*>(Ptr);
55 break;
56 case Twine::UHexKind:
57 // FIXME: Add raw_ostream functionality for this.
58 OS << ::utohexstr(*static_cast<const uint64_t*>(Ptr));
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000059 break;
60 }
61}
62
63void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr,
64 NodeKind Kind) const {
65 switch (Kind) {
66 case Twine::NullKind:
67 OS << "null"; break;
68 case Twine::EmptyKind:
69 OS << "empty"; break;
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000070 case Twine::TwineKind:
71 OS << "rope:";
72 static_cast<const Twine*>(Ptr)->printRepr(OS);
73 break;
Daniel Dunbar763457e2009-07-29 07:08:44 +000074 case Twine::CStringKind:
75 OS << "cstring:\""
76 << static_cast<const char*>(Ptr) << "\"";
77 break;
78 case Twine::StdStringKind:
79 OS << "std::string:\""
80 << static_cast<const std::string*>(Ptr) << "\"";
81 break;
82 case Twine::StringRefKind:
83 OS << "stringref:\""
84 << static_cast<const StringRef*>(Ptr) << "\"";
85 break;
86 case Twine::UDecKind:
87 OS << "udec:" << static_cast<const uint64_t*>(Ptr) << "\"";
88 break;
89 case Twine::SDecKind:
90 OS << "sdec:" << static_cast<const int64_t*>(Ptr) << "\"";
91 break;
92 case Twine::UHexKind:
93 OS << "uhex:" << static_cast<const uint64_t*>(Ptr) << "\"";
94 break;
Daniel Dunbar2538f7a2009-07-24 07:04:27 +000095 }
96}
97
98void Twine::print(raw_ostream &OS) const {
99 printOneChild(OS, LHS, getLHSKind());
100 printOneChild(OS, RHS, getRHSKind());
101}
102
103void Twine::printRepr(raw_ostream &OS) const {
104 OS << "(Twine ";
105 printOneChildRepr(OS, LHS, getLHSKind());
106 OS << " ";
107 printOneChildRepr(OS, RHS, getRHSKind());
108 OS << ")";
109}
110
111void Twine::dump() const {
112 print(llvm::errs());
113}
114
115void Twine::dumpRepr() const {
116 printRepr(llvm::errs());
117}