Add raw_ostream::write_escaped, for writing escaped strings.
[oota-llvm.git] / lib / Support / Twine.cpp
1 //===-- 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/ADT/SmallString.h"
12 #include "llvm/Support/raw_ostream.h"
13 using namespace llvm;
14
15 std::string Twine::str() const {
16   SmallString<256> Vec;
17   toVector(Vec);
18   return std::string(Vec.begin(), Vec.end());
19 }
20
21 void Twine::toVector(SmallVectorImpl<char> &Out) const {
22   raw_svector_ostream OS(Out);
23   print(OS);
24 }
25
26 void Twine::printOneChild(raw_ostream &OS, const void *Ptr, 
27                           NodeKind Kind) const {
28   switch (Kind) {
29   case Twine::NullKind: break;
30   case Twine::EmptyKind: break;
31   case Twine::TwineKind:
32     static_cast<const Twine*>(Ptr)->print(OS); 
33     break;
34   case Twine::CStringKind: 
35     OS << static_cast<const char*>(Ptr); 
36     break;
37   case Twine::StdStringKind:
38     OS << *static_cast<const std::string*>(Ptr); 
39     break;
40   case Twine::StringRefKind:
41     OS << *static_cast<const StringRef*>(Ptr); 
42     break;
43   case Twine::DecUIKind:
44     OS << *static_cast<const unsigned int*>(Ptr);
45     break;
46   case Twine::DecIKind:
47     OS << *static_cast<const int*>(Ptr);
48     break;
49   case Twine::DecULKind:
50     OS << *static_cast<const unsigned long*>(Ptr);
51     break;
52   case Twine::DecLKind:
53     OS << *static_cast<const long*>(Ptr);
54     break;
55   case Twine::DecULLKind:
56     OS << *static_cast<const unsigned long long*>(Ptr);
57     break;
58   case Twine::DecLLKind:
59     OS << *static_cast<const long long*>(Ptr);
60     break;
61   case Twine::UHexKind:
62     OS.write_hex(*static_cast<const uint64_t*>(Ptr));
63     break;
64   }
65 }
66
67 void Twine::printOneChildRepr(raw_ostream &OS, const void *Ptr, 
68                               NodeKind Kind) const {
69   switch (Kind) {
70   case Twine::NullKind:
71     OS << "null"; break;
72   case Twine::EmptyKind:
73     OS << "empty"; break;
74   case Twine::TwineKind:
75     OS << "rope:";
76     static_cast<const Twine*>(Ptr)->printRepr(OS);
77     break;
78   case Twine::CStringKind:
79     OS << "cstring:\""
80        << static_cast<const char*>(Ptr) << "\"";
81     break;
82   case Twine::StdStringKind:
83     OS << "std::string:\""
84        << static_cast<const std::string*>(Ptr) << "\"";
85     break;
86   case Twine::StringRefKind:
87     OS << "stringref:\""
88        << static_cast<const StringRef*>(Ptr) << "\"";
89     break;
90   case Twine::DecUIKind:
91     OS << "decUI:\"" << *static_cast<const unsigned int*>(Ptr) << "\"";
92     break;
93   case Twine::DecIKind:
94     OS << "decI:\"" << *static_cast<const int*>(Ptr) << "\"";
95     break;
96   case Twine::DecULKind:
97     OS << "decUL:\"" << *static_cast<const unsigned long*>(Ptr) << "\"";
98     break;
99   case Twine::DecLKind:
100     OS << "decL:\"" << *static_cast<const long*>(Ptr) << "\"";
101     break;
102   case Twine::DecULLKind:
103     OS << "decULL:\"" << *static_cast<const unsigned long long*>(Ptr) << "\"";
104     break;
105   case Twine::DecLLKind:
106     OS << "decLL:\"" << *static_cast<const long long*>(Ptr) << "\"";
107     break;
108   case Twine::UHexKind:
109     OS << "uhex:\"" << static_cast<const uint64_t*>(Ptr) << "\"";
110     break;
111   }
112 }
113
114 void Twine::print(raw_ostream &OS) const {
115   printOneChild(OS, LHS, getLHSKind());
116   printOneChild(OS, RHS, getRHSKind());
117 }
118
119 void Twine::printRepr(raw_ostream &OS) const {
120   OS << "(Twine ";
121   printOneChildRepr(OS, LHS, getLHSKind());
122   OS << " ";
123   printOneChildRepr(OS, RHS, getRHSKind());
124   OS << ")";
125 }
126
127 void Twine::dump() const {
128   print(llvm::errs());
129 }
130
131 void Twine::dumpRepr() const {
132   printRepr(llvm::errs());
133 }