Coverage Report

Created: 2023-02-22 06:51

/src/hermes/external/llvh/lib/Support/Twine.cpp
Line
Count
Source (jump to first uncovered line)
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 "llvh/ADT/Twine.h"
11
#include "llvh/ADT/SmallString.h"
12
#include "llvh/Config/llvm-config.h"
13
#include "llvh/Support/Debug.h"
14
#include "llvh/Support/FormatVariadic.h"
15
#include "llvh/Support/raw_ostream.h"
16
using namespace llvh;
17
18
6.28k
std::string Twine::str() const {
19
  // If we're storing only a std::string, just return it.
20
6.28k
  if (LHSKind == StdStringKind && RHSKind == EmptyKind)
21
18
    return *LHS.stdString;
22
23
  // If we're storing a formatv_object, we can avoid an extra copy by formatting
24
  // it immediately and returning the result.
25
6.26k
  if (LHSKind == FormatvObjectKind && RHSKind == EmptyKind)
26
0
    return LHS.formatvObject->str();
27
28
  // Otherwise, flatten and copy the contents first.
29
6.26k
  SmallString<256> Vec;
30
6.26k
  return toStringRef(Vec).str();
31
6.26k
}
32
33
6.02k
void Twine::toVector(SmallVectorImpl<char> &Out) const {
34
6.02k
  raw_svector_ostream OS(Out);
35
6.02k
  print(OS);
36
6.02k
}
37
38
0
StringRef Twine::toNullTerminatedStringRef(SmallVectorImpl<char> &Out) const {
39
0
  if (isUnary()) {
40
0
    switch (getLHSKind()) {
41
0
    case CStringKind:
42
      // Already null terminated, yay!
43
0
      return StringRef(LHS.cString);
44
0
    case StdStringKind: {
45
0
      const std::string *str = LHS.stdString;
46
0
      return StringRef(str->c_str(), str->size());
47
0
    }
48
0
    default:
49
0
      break;
50
0
    }
51
0
  }
52
0
  toVector(Out);
53
0
  Out.push_back(0);
54
0
  Out.pop_back();
55
0
  return StringRef(Out.data(), Out.size());
56
0
}
57
58
void Twine::printOneChild(raw_ostream &OS, Child Ptr,
59
12.3k
                          NodeKind Kind) const {
60
12.3k
  switch (Kind) {
61
0
  case Twine::NullKind: break;
62
0
  case Twine::EmptyKind: break;
63
129
  case Twine::TwineKind:
64
129
    Ptr.twine->print(OS);
65
129
    break;
66
93
  case Twine::CStringKind:
67
93
    OS << Ptr.cString;
68
93
    break;
69
0
  case Twine::StdStringKind:
70
0
    OS << *Ptr.stdString;
71
0
    break;
72
11.9k
  case Twine::StringRefKind:
73
11.9k
    OS << *Ptr.stringRef;
74
11.9k
    break;
75
0
  case Twine::SmallStringKind:
76
0
    OS << *Ptr.smallString;
77
0
    break;
78
0
  case Twine::FormatvObjectKind:
79
0
    OS << *Ptr.formatvObject;
80
0
    break;
81
0
  case Twine::CharKind:
82
0
    OS << Ptr.character;
83
0
    break;
84
0
  case Twine::DecUIKind:
85
0
    OS << Ptr.decUI;
86
0
    break;
87
86
  case Twine::DecIKind:
88
86
    OS << Ptr.decI;
89
86
    break;
90
0
  case Twine::DecULKind:
91
0
    OS << *Ptr.decUL;
92
0
    break;
93
0
  case Twine::DecLKind:
94
0
    OS << *Ptr.decL;
95
0
    break;
96
0
  case Twine::DecULLKind:
97
0
    OS << *Ptr.decULL;
98
0
    break;
99
0
  case Twine::DecLLKind:
100
0
    OS << *Ptr.decLL;
101
0
    break;
102
7
  case Twine::UHexKind:
103
7
    OS.write_hex(*Ptr.uHex);
104
7
    break;
105
12.3k
  }
106
12.3k
}
107
108
void Twine::printOneChildRepr(raw_ostream &OS, Child Ptr,
109
0
                              NodeKind Kind) const {
110
0
  switch (Kind) {
111
0
  case Twine::NullKind:
112
0
    OS << "null"; break;
113
0
  case Twine::EmptyKind:
114
0
    OS << "empty"; break;
115
0
  case Twine::TwineKind:
116
0
    OS << "rope:";
117
0
    Ptr.twine->printRepr(OS);
118
0
    break;
119
0
  case Twine::CStringKind:
120
0
    OS << "cstring:\""
121
0
       << Ptr.cString << "\"";
122
0
    break;
123
0
  case Twine::StdStringKind:
124
0
    OS << "std::string:\""
125
0
       << Ptr.stdString << "\"";
126
0
    break;
127
0
  case Twine::StringRefKind:
128
0
    OS << "stringref:\""
129
0
       << Ptr.stringRef << "\"";
130
0
    break;
131
0
  case Twine::SmallStringKind:
132
0
    OS << "smallstring:\"" << *Ptr.smallString << "\"";
133
0
    break;
134
0
  case Twine::FormatvObjectKind:
135
0
    OS << "formatv:\"" << *Ptr.formatvObject << "\"";
136
0
    break;
137
0
  case Twine::CharKind:
138
0
    OS << "char:\"" << Ptr.character << "\"";
139
0
    break;
140
0
  case Twine::DecUIKind:
141
0
    OS << "decUI:\"" << Ptr.decUI << "\"";
142
0
    break;
143
0
  case Twine::DecIKind:
144
0
    OS << "decI:\"" << Ptr.decI << "\"";
145
0
    break;
146
0
  case Twine::DecULKind:
147
0
    OS << "decUL:\"" << *Ptr.decUL << "\"";
148
0
    break;
149
0
  case Twine::DecLKind:
150
0
    OS << "decL:\"" << *Ptr.decL << "\"";
151
0
    break;
152
0
  case Twine::DecULLKind:
153
0
    OS << "decULL:\"" << *Ptr.decULL << "\"";
154
0
    break;
155
0
  case Twine::DecLLKind:
156
0
    OS << "decLL:\"" << *Ptr.decLL << "\"";
157
0
    break;
158
0
  case Twine::UHexKind:
159
0
    OS << "uhex:\"" << Ptr.uHex << "\"";
160
0
    break;
161
0
  }
162
0
}
163
164
6.15k
void Twine::print(raw_ostream &OS) const {
165
6.15k
  printOneChild(OS, LHS, getLHSKind());
166
6.15k
  printOneChild(OS, RHS, getRHSKind());
167
6.15k
}
168
169
0
void Twine::printRepr(raw_ostream &OS) const {
170
0
  OS << "(Twine ";
171
0
  printOneChildRepr(OS, LHS, getLHSKind());
172
0
  OS << " ";
173
0
  printOneChildRepr(OS, RHS, getRHSKind());
174
0
  OS << ")";
175
0
}
176
177
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
178
0
LLVM_DUMP_METHOD void Twine::dump() const {
179
0
  print(dbgs());
180
0
}
181
182
0
LLVM_DUMP_METHOD void Twine::dumpRepr() const {
183
0
  printRepr(dbgs());
184
0
}
185
#endif