10 #ifndef WPIUTIL_WPI_TWINE_H
11 #define WPIUTIL_WPI_TWINE_H
13 #include "wpi/SmallVector.h"
14 #include "wpi/StringRef.h"
81 enum NodeKind :
unsigned char {
136 const std::string *stdString;
142 const unsigned long *decUL;
144 const unsigned long long *decULL;
145 const long long *decLL;
146 const uint64_t *uHex;
158 NodeKind LHSKind = EmptyKind;
161 NodeKind RHSKind = EmptyKind;
164 explicit Twine(NodeKind Kind) : LHSKind(Kind) {
165 assert(isNullary() &&
"Invalid kind!");
170 : LHSKind(TwineKind), RHSKind(TwineKind) {
171 this->LHS.twine = &LHS;
172 this->RHS.twine = &RHS;
173 assert(isValid() &&
"Invalid twine!");
177 explicit Twine(Child LHS, NodeKind LHSKind, Child RHS, NodeKind RHSKind)
178 : LHS(LHS), RHS(RHS), LHSKind(LHSKind), RHSKind(RHSKind) {
179 assert(isValid() &&
"Invalid twine!");
183 bool isEmpty()
const {
184 return getLHSKind() == EmptyKind;
188 bool isNullary()
const {
189 return isNull() || isEmpty();
193 bool isUnary()
const {
194 return getRHSKind() == EmptyKind && !isNullary();
198 bool isBinary()
const {
199 return getLHSKind() != NullKind && getRHSKind() != EmptyKind;
204 bool isValid()
const {
206 if (isNullary() && getRHSKind() != EmptyKind)
210 if (getRHSKind() == NullKind)
214 if (getRHSKind() != EmptyKind && getLHSKind() == EmptyKind)
218 if (getLHSKind() == TwineKind &&
219 !LHS.twine->isBinary())
221 if (getRHSKind() == TwineKind &&
222 !RHS.twine->isBinary())
229 NodeKind getLHSKind()
const {
return LHSKind; }
232 NodeKind getRHSKind()
const {
return RHSKind; }
235 void printOneChild(
raw_ostream &OS, Child Ptr, NodeKind Kind)
const;
239 NodeKind Kind)
const;
247 assert(isValid() &&
"Invalid twine!");
258 if (Str[0] !=
'\0') {
260 LHSKind = CStringKind;
264 assert(isValid() &&
"Invalid twine!");
268 Twine(
const std::string &Str) : LHSKind(StdStringKind) {
269 LHS.stdString = &Str;
270 assert(isValid() &&
"Invalid twine!");
275 LHS.stringRef = &Str;
276 assert(isValid() &&
"Invalid twine!");
281 : LHSKind(SmallStringKind) {
282 LHS.smallString = &Str;
283 assert(isValid() &&
"Invalid twine!");
287 explicit Twine(
char Val) : LHSKind(CharKind) {
292 explicit Twine(
signed char Val) : LHSKind(CharKind) {
293 LHS.character =
static_cast<char>(Val);
297 explicit Twine(
unsigned char Val) : LHSKind(CharKind) {
298 LHS.character =
static_cast<char>(Val);
302 explicit Twine(
unsigned Val) : LHSKind(DecUIKind) {
307 explicit Twine(
int Val) : LHSKind(DecIKind) {
312 explicit Twine(
const unsigned long &Val) : LHSKind(DecULKind) {
317 explicit Twine(
const long &Val) : LHSKind(DecLKind) {
322 explicit Twine(
const unsigned long long &Val) : LHSKind(DecULLKind) {
327 explicit Twine(
const long long &Val) : LHSKind(DecLLKind) {
338 : LHSKind(CStringKind), RHSKind(StringRefKind) {
339 this->LHS.cString = LHS;
340 this->RHS.stringRef = &RHS;
341 assert(isValid() &&
"Invalid twine!");
346 : LHSKind(StringRefKind), RHSKind(CStringKind) {
347 this->LHS.stringRef = &LHS;
348 this->RHS.cString = RHS;
349 assert(isValid() &&
"Invalid twine!");
359 return Twine(NullKind);
367 static Twine utohexstr(
const uint64_t &Val) {
371 return Twine(LHS, UHexKind, RHS, EmptyKind);
380 return getLHSKind() == NullKind;
392 if (getRHSKind() != EmptyKind)
return false;
394 switch (getLHSKind()) {
399 case SmallStringKind:
418 std::string
str()
const;
427 switch (getLHSKind()) {
432 case CStringKind:
return StringRef(LHS.cString);
433 case StdStringKind:
return StringRef(*LHS.stdString);
434 case StringRefKind:
return *LHS.stringRef;
435 case SmallStringKind:
436 return StringRef(LHS.smallString->data(), LHS.smallString->size());
437 case CharKind:
return StringRef(&LHS.character, 1);
477 inline Twine Twine::concat(
const Twine &Suffix)
const {
479 if (
isNull() || Suffix.isNull())
480 return Twine(NullKind);
485 if (Suffix.isEmpty())
490 Child NewLHS, NewRHS;
492 NewRHS.twine = &Suffix;
493 NodeKind NewLHSKind = TwineKind, NewRHSKind = TwineKind;
496 NewLHSKind = getLHSKind();
498 if (Suffix.isUnary()) {
500 NewRHSKind = Suffix.getLHSKind();
503 return Twine(NewLHS, NewLHSKind, NewRHS, NewRHSKind);
506 inline Twine operator+(
const Twine &LHS,
const Twine &RHS) {
507 return LHS.concat(RHS);
514 return Twine(LHS, RHS);
521 return Twine(LHS, RHS);
524 inline raw_ostream &operator<<(raw_ostream &OS,
const Twine &RHS) {
533 #endif // LLVM_ADT_TWINE_H
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef toNullTerminatedStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single null terminated StringRef if it can be represented as such...
bool isTriviallyEmpty() const
Check if this twine is trivially empty; a false return value does not necessarily mean the twine is e...
Definition: Twine.h:385
Twine(const unsigned long long &Val)
Construct a twine to print Val as an unsigned decimal integer.
Definition: Twine.h:322
Twine(const char *Str)
Construct from a C string.
Definition: Twine.h:257
Twine(unsigned char Val)
Construct from an unsigned char.
Definition: Twine.h:297
void dumpRepr() const
Dump the representation of this twine to stderr.
Twine()
Construct from an empty string.
Definition: Twine.h:246
static Twine createNull()
Create a 'null' string, which is an empty string that always concatenates to form another empty strin...
Definition: Twine.h:358
Twine(const unsigned long &Val)
Construct a twine to print Val as an unsigned decimal integer.
Definition: Twine.h:312
Twine(const long &Val)
Construct a twine to print Val as a signed decimal integer.
Definition: Twine.h:317
Twine(const StringRef &Str)
Construct from a StringRef.
Definition: Twine.h:274
namespace to hold default to_json function
Definition: SmallString.h:21
Twine(const char *LHS, const StringRef &RHS)
Construct as the concatenation of a C string and a StringRef.
Definition: Twine.h:337
bool isNull() const
Check for the null twine.
Definition: Twine.h:379
void print(raw_ostream &OS) const
Write the concatenated string represented by this twine to the stream OS.
Twine(char Val)
Construct from a char.
Definition: Twine.h:287
void printRepr(raw_ostream &OS) const
Write the representation of this twine to the stream OS.
std::string str() const
Return the twine contents as a std::string.
Twine(const SmallVectorImpl< char > &Str)
Construct from a SmallString.
Definition: Twine.h:280
Twine(const std::string &Str)
Construct from an std::string.
Definition: Twine.h:268
Twine(const StringRef &LHS, const char *RHS)
Construct as the concatenation of a StringRef and a C string.
Definition: Twine.h:345
void dump() const
Dump the concatenated string represented by this twine to stderr.
bool isSingleStringRef() const
Return true if this twine can be dynamically accessed as a single StringRef value with getSingleStrin...
Definition: Twine.h:391
void toVector(SmallVectorImpl< char > &Out) const
Append the concatenated string into the given SmallString or SmallVector.
Twine(int Val)
Construct a twine to print Val as a signed decimal integer.
Definition: Twine.h:307
Twine & operator=(const Twine &)=delete
Since the intended use of twines is as temporary objects, assignments when concatenating might cause ...
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Twine(signed char Val)
Construct from a signed char.
Definition: Twine.h:292
Twine(unsigned Val)
Construct a twine to print Val as an unsigned decimal integer.
Definition: Twine.h:302
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:151
StringRef getSingleStringRef() const
This returns the twine as a single StringRef.
Definition: Twine.h:425
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
Definition: Twine.h:444
Twine(const long long &Val)
Construct a twine to print Val as a signed decimal integer.
Definition: Twine.h:327