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