Twine.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //===-- Twine.cpp - Fast Temporary String Concatenation -------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/ADT/Twine.h"
  9. #include "llvm/ADT/SmallString.h"
  10. #include "llvm/Config/llvm-config.h"
  11. #include "llvm/Support/Debug.h"
  12. #include "llvm/Support/FormatVariadic.h"
  13. #include "llvm/Support/raw_ostream.h"
  14. using namespace llvm;
  15. std::string Twine::str() const {
  16. // If we're storing only a std::string, just return it.
  17. if (LHSKind == StdStringKind && RHSKind == EmptyKind)
  18. return *LHS.stdString;
  19. // If we're storing a formatv_object, we can avoid an extra copy by formatting
  20. // it immediately and returning the result.
  21. if (LHSKind == FormatvObjectKind && RHSKind == EmptyKind)
  22. return LHS.formatvObject->str();
  23. // Otherwise, flatten and copy the contents first.
  24. SmallString<256> Vec;
  25. return toStringRef(Vec).str();
  26. }
  27. void Twine::toVector(SmallVectorImpl<char> &Out) const {
  28. raw_svector_ostream OS(Out);
  29. print(OS);
  30. }
  31. StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
  32. if (isUnary()) {
  33. switch (getLHSKind()) {
  34. case CStringKind:
  35. // Already null terminated, yay!
  36. return StringRef(LHS.cString);
  37. case StdStringKind: {
  38. const std::string *str = LHS.stdString;
  39. return StringRef(str->c_str(), str->size());
  40. }
  41. default:
  42. break;
  43. }
  44. }
  45. toVector(Out);
  46. Out.push_back(0);
  47. Out.pop_back();
  48. return StringRef(Out.data(), Out.size());
  49. }
  50. void Twine::printOneChild(raw_ostream &OS, Child Ptr,
  51. NodeKind Kind) const {
  52. switch (Kind) {
  53. case Twine::NullKind: break;
  54. case Twine::EmptyKind: break;
  55. case Twine::TwineKind:
  56. Ptr.twine->print(OS);
  57. break;
  58. case Twine::CStringKind:
  59. OS << Ptr.cString;
  60. break;
  61. case Twine::StdStringKind:
  62. OS << *Ptr.stdString;
  63. break;
  64. case Twine::PtrAndLengthKind:
  65. OS << StringRef(Ptr.ptrAndLength.ptr, Ptr.ptrAndLength.length);
  66. break;
  67. case Twine::FormatvObjectKind:
  68. OS << *Ptr.formatvObject;
  69. break;
  70. case Twine::CharKind:
  71. OS << Ptr.character;
  72. break;
  73. case Twine::DecUIKind:
  74. OS << Ptr.decUI;
  75. break;
  76. case Twine::DecIKind:
  77. OS << Ptr.decI;
  78. break;
  79. case Twine::DecULKind:
  80. OS << *Ptr.decUL;
  81. break;
  82. case Twine::DecLKind:
  83. OS << *Ptr.decL;
  84. break;
  85. case Twine::DecULLKind:
  86. OS << *Ptr.decULL;
  87. break;
  88. case Twine::DecLLKind:
  89. OS << *Ptr.decLL;
  90. break;
  91. case Twine::UHexKind:
  92. OS.write_hex(*Ptr.uHex);
  93. break;
  94. }
  95. }
  96. void Twine::printOneChildRepr(raw_ostream &OS, Child Ptr,
  97. NodeKind Kind) const {
  98. switch (Kind) {
  99. case Twine::NullKind:
  100. OS << "null"; break;
  101. case Twine::EmptyKind:
  102. OS << "empty"; break;
  103. case Twine::TwineKind:
  104. OS << "rope:";
  105. Ptr.twine->printRepr(OS);
  106. break;
  107. case Twine::CStringKind:
  108. OS << "cstring:\""
  109. << Ptr.cString << "\"";
  110. break;
  111. case Twine::StdStringKind:
  112. OS << "std::string:\""
  113. << Ptr.stdString << "\"";
  114. break;
  115. case Twine::PtrAndLengthKind:
  116. OS << "ptrAndLength:\""
  117. << StringRef(Ptr.ptrAndLength.ptr, Ptr.ptrAndLength.length) << "\"";
  118. break;
  119. case Twine::FormatvObjectKind:
  120. OS << "formatv:\"" << *Ptr.formatvObject << "\"";
  121. break;
  122. case Twine::CharKind:
  123. OS << "char:\"" << Ptr.character << "\"";
  124. break;
  125. case Twine::DecUIKind:
  126. OS << "decUI:\"" << Ptr.decUI << "\"";
  127. break;
  128. case Twine::DecIKind:
  129. OS << "decI:\"" << Ptr.decI << "\"";
  130. break;
  131. case Twine::DecULKind:
  132. OS << "decUL:\"" << *Ptr.decUL << "\"";
  133. break;
  134. case Twine::DecLKind:
  135. OS << "decL:\"" << *Ptr.decL << "\"";
  136. break;
  137. case Twine::DecULLKind:
  138. OS << "decULL:\"" << *Ptr.decULL << "\"";
  139. break;
  140. case Twine::DecLLKind:
  141. OS << "decLL:\"" << *Ptr.decLL << "\"";
  142. break;
  143. case Twine::UHexKind:
  144. OS << "uhex:\"" << Ptr.uHex << "\"";
  145. break;
  146. }
  147. }
  148. void Twine::print(raw_ostream &OS) const {
  149. printOneChild(OS, LHS, getLHSKind());
  150. printOneChild(OS, RHS, getRHSKind());
  151. }
  152. void Twine::printRepr(raw_ostream &OS) const {
  153. OS << "(Twine ";
  154. printOneChildRepr(OS, LHS, getLHSKind());
  155. OS << " ";
  156. printOneChildRepr(OS, RHS, getRHSKind());
  157. OS << ")";
  158. }
  159. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  160. LLVM_DUMP_METHOD void Twine::dump() const {
  161. print(dbgs());
  162. }
  163. LLVM_DUMP_METHOD void Twine::dumpRepr() const {
  164. printRepr(dbgs());
  165. }
  166. #endif