10 #ifndef WPIUTIL_WPI_TWINE_H
11 #define WPIUTIL_WPI_TWINE_H
13 #include "wpi/SmallVector.h"
14 #include "wpi/StringRef.h"
15 #include "wpi/ErrorHandling.h"
22 #pragma warning(disable : 26495)
87 enum NodeKind :
unsigned char {
142 const std::string *stdString;
148 const unsigned long *decUL;
150 const unsigned long long *decULL;
151 const long long *decLL;
152 const uint64_t *uHex;
164 NodeKind LHSKind = EmptyKind;
167 NodeKind RHSKind = EmptyKind;
170 explicit Twine(NodeKind Kind) : LHSKind(Kind) {
171 assert(isNullary() &&
"Invalid kind!");
176 : LHSKind(TwineKind), RHSKind(TwineKind) {
177 this->LHS.twine = &LHS;
178 this->RHS.twine = &RHS;
179 assert(isValid() &&
"Invalid twine!");
183 explicit Twine(Child LHS, NodeKind LHSKind, Child RHS, NodeKind RHSKind)
184 : LHS(LHS), RHS(RHS), LHSKind(LHSKind), RHSKind(RHSKind) {
185 assert(isValid() &&
"Invalid twine!");
189 bool isEmpty()
const {
190 return getLHSKind() == EmptyKind;
194 bool isNullary()
const {
195 return isNull() || isEmpty();
199 bool isUnary()
const {
200 return getRHSKind() == EmptyKind && !isNullary();
204 bool isBinary()
const {
205 return getLHSKind() != NullKind && getRHSKind() != EmptyKind;
210 bool isValid()
const {
212 if (isNullary() && getRHSKind() != EmptyKind)
216 if (getRHSKind() == NullKind)
220 if (getRHSKind() != EmptyKind && getLHSKind() == EmptyKind)
222 #if 0 // causes spurious warnings
224 if (getLHSKind() == TwineKind &&
225 !LHS.twine->isBinary())
227 if (getRHSKind() == TwineKind &&
228 !RHS.twine->isBinary())
235 NodeKind getLHSKind()
const {
return LHSKind; }
238 NodeKind getRHSKind()
const {
return RHSKind; }
241 void printOneChild(
raw_ostream &OS, Child Ptr, NodeKind Kind)
const;
245 NodeKind Kind)
const;
253 assert(isValid() &&
"Invalid twine!");
264 if (Str[0] !=
'\0') {
266 LHSKind = CStringKind;
270 assert(isValid() &&
"Invalid twine!");
274 Twine(
const std::string &Str) : LHSKind(StdStringKind) {
275 LHS.stdString = &Str;
276 assert(isValid() &&
"Invalid twine!");
281 LHS.stringRef = &Str;
282 assert(isValid() &&
"Invalid twine!");
287 : LHSKind(SmallStringKind) {
288 LHS.smallString = &Str;
289 assert(isValid() &&
"Invalid twine!");
293 explicit Twine(
char Val) : LHSKind(CharKind) {
298 explicit Twine(
signed char Val) : LHSKind(CharKind) {
299 LHS.character = static_cast<char>(Val);
303 explicit Twine(
unsigned char Val) : LHSKind(CharKind) {
304 LHS.character = static_cast<char>(Val);
308 explicit Twine(
unsigned Val) : LHSKind(DecUIKind) {
313 explicit Twine(
int Val) : LHSKind(DecIKind) {
318 explicit Twine(
const unsigned long &Val) : LHSKind(DecULKind) {
323 explicit Twine(
const long &Val) : LHSKind(DecLKind) {
328 explicit Twine(
const unsigned long long &Val) : LHSKind(DecULLKind) {
333 explicit Twine(
const long long &Val) : LHSKind(DecLLKind) {
344 : LHSKind(CStringKind), RHSKind(StringRefKind) {
345 this->LHS.cString = LHS;
346 this->RHS.stringRef = &RHS;
347 assert(isValid() &&
"Invalid twine!");
352 : LHSKind(StringRefKind), RHSKind(CStringKind) {
353 this->LHS.stringRef = &LHS;
354 this->RHS.cString = RHS;
355 assert(isValid() &&
"Invalid twine!");
365 return Twine(NullKind);
373 static Twine utohexstr(
const uint64_t &Val) {
377 return Twine(LHS, UHexKind, RHS, EmptyKind);
386 return getLHSKind() == NullKind;
398 if (getRHSKind() != EmptyKind)
return false;
400 switch (getLHSKind()) {
405 case SmallStringKind:
424 std::string
str()
const;
433 switch (getLHSKind()) {
438 case CStringKind:
return StringRef(LHS.cString);
439 case StdStringKind:
return StringRef(*LHS.stdString);
440 case StringRefKind:
return *LHS.stringRef;
441 case SmallStringKind:
442 return StringRef(LHS.smallString->data(), LHS.smallString->size());
443 case CharKind:
return StringRef(&LHS.character, 1);
483 inline Twine Twine::concat(
const Twine &Suffix)
const {
485 if (
isNull() || Suffix.isNull())
486 return Twine(NullKind);
491 if (Suffix.isEmpty())
496 Child NewLHS, NewRHS;
498 NewRHS.twine = &Suffix;
499 NodeKind NewLHSKind = TwineKind, NewRHSKind = TwineKind;
502 NewLHSKind = getLHSKind();
504 if (Suffix.isUnary()) {
506 NewRHSKind = Suffix.getLHSKind();
509 return Twine(NewLHS, NewLHSKind, NewRHS, NewRHSKind);
512 inline Twine operator+(
const Twine &LHS,
const Twine &RHS) {
513 return LHS.concat(RHS);
520 return Twine(LHS, RHS);
527 return Twine(LHS, RHS);
530 inline raw_ostream &
operator<<(raw_ostream &OS,
const Twine &RHS) {
543 #endif // LLVM_ADT_TWINE_H
Twine(const SmallVectorImpl< char > &Str)
Construct from a SmallString.
Definition: Twine.h:286
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
Definition: Twine.h:450
void dump() const
Dump the concatenated string represented by this twine to stderr.
Twine(const char *LHS, const StringRef &RHS)
Construct as the concatenation of a C string and a StringRef.
Definition: Twine.h:343
Twine & operator=(const Twine &)=delete
Since the intended use of twines is as temporary objects, assignments when concatenating might cause ...
Twine(unsigned char Val)
Construct from an unsigned char.
Definition: Twine.h:303
Twine(signed char Val)
Construct from a signed char.
Definition: Twine.h:298
Twine()
Construct from an empty string.
Definition: Twine.h:252
bool isNull() const
Check for the null twine.
Definition: Twine.h:385
Twine(unsigned Val)
Construct a twine to print Val as an unsigned decimal integer.
Definition: Twine.h:308
static Twine createNull()
Create a 'null' string, which is an empty string that always concatenates to form another empty strin...
Definition: Twine.h:364
WPILib C++ utilities (wpiutil) namespace.
Definition: EventLoopRunner.h:17
bool isSingleStringRef() const
Return true if this twine can be dynamically accessed as a single StringRef value with getSingleStrin...
Definition: Twine.h:397
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:158
Twine(const char *Str)
Construct from a C string.
Definition: Twine.h:263
raw_ostream & operator<<(raw_ostream &Out, const VersionTuple &V)
Print a version number.
Twine(char Val)
Construct from a char.
Definition: Twine.h:293
StringRef toNullTerminatedStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single null terminated StringRef if it can be represented as such.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Twine(const unsigned long &Val)
Construct a twine to print Val as an unsigned decimal integer.
Definition: Twine.h:318
void printRepr(raw_ostream &OS) const
Write the representation of this twine to the stream OS.
Twine(const unsigned long long &Val)
Construct a twine to print Val as an unsigned decimal integer.
Definition: Twine.h:328
void print(raw_ostream &OS) const
Write the concatenated string represented by this twine to the stream OS.
Twine(const StringRef &Str)
Construct from a StringRef.
Definition: Twine.h:280
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:391
Twine(const std::string &Str)
Construct from an std::string.
Definition: Twine.h:274
std::string str() const
Return the twine contents as a std::string.
Twine(const long long &Val)
Construct a twine to print Val as a signed decimal integer.
Definition: Twine.h:333
void dumpRepr() const
Dump the representation of this twine to stderr.
Twine(const long &Val)
Construct a twine to print Val as a signed decimal integer.
Definition: Twine.h:323
void toVector(SmallVectorImpl< char > &Out) const
Append the concatenated string into the given SmallString or SmallVector.
Twine(const StringRef &LHS, const char *RHS)
Construct as the concatenation of a StringRef and a C string.
Definition: Twine.h:351
StringRef getSingleStringRef() const
This returns the twine as a single StringRef.
Definition: Twine.h:431
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:47
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:85
Twine(int Val)
Construct a twine to print Val as a signed decimal integer.
Definition: Twine.h:313