WPILibC++  2019.3.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
wpi::Twine Class Reference

Twine - A lightweight data structure for efficiently representing the concatenation of temporary values as strings. More...

#include <Twine.h>

Public Member Functions

Predicate Operations
bool isNull () const
 Check for the null twine.
 
bool isTriviallyEmpty () const
 Check if this twine is trivially empty; a false return value does not necessarily mean the twine is empty. More...
 
bool isSingleStringRef () const
 Return true if this twine can be dynamically accessed as a single StringRef value with getSingleStringRef(). More...
 
String Operations
Twine concat (const Twine &Suffix) const
 
Output & Conversion.
std::string str () const
 Return the twine contents as a std::string.
 
void toVector (SmallVectorImpl< char > &Out) const
 Append the concatenated string into the given SmallString or SmallVector.
 
StringRef getSingleStringRef () const
 This returns the twine as a single StringRef. More...
 
StringRef toStringRef (SmallVectorImpl< char > &Out) const
 This returns the twine as a single StringRef if it can be represented as such. More...
 
StringRef toNullTerminatedStringRef (SmallVectorImpl< char > &Out) const
 This returns the twine as a single null terminated StringRef if it can be represented as such. More...
 
void print (raw_ostream &OS) const
 Write the concatenated string represented by this twine to the stream OS. More...
 
void dump () const
 Dump the concatenated string represented by this twine to stderr.
 
void printRepr (raw_ostream &OS) const
 Write the representation of this twine to the stream OS.
 
void dumpRepr () const
 Dump the representation of this twine to stderr.
 

Static Public Member Functions

Numeric Conversions
static Twine utohexstr (const uint64_t &Val)
 

Constructors

 Twine ()
 Construct from an empty string.
 
 Twine (const Twine &)=default
 
 Twine (const char *Str)
 Construct from a C string. More...
 
 Twine (const std::string &Str)
 Construct from an std::string.
 
 Twine (const StringRef &Str)
 Construct from a StringRef.
 
 Twine (const SmallVectorImpl< char > &Str)
 Construct from a SmallString.
 
 Twine (char Val)
 Construct from a char.
 
 Twine (signed char Val)
 Construct from a signed char.
 
 Twine (unsigned char Val)
 Construct from an unsigned char.
 
 Twine (unsigned Val)
 Construct a twine to print Val as an unsigned decimal integer.
 
 Twine (int Val)
 Construct a twine to print Val as a signed decimal integer.
 
 Twine (const unsigned long &Val)
 Construct a twine to print Val as an unsigned decimal integer.
 
 Twine (const long &Val)
 Construct a twine to print Val as a signed decimal integer.
 
 Twine (const unsigned long long &Val)
 Construct a twine to print Val as an unsigned decimal integer.
 
 Twine (const long long &Val)
 Construct a twine to print Val as a signed decimal integer.
 
 Twine (const char *LHS, const StringRef &RHS)
 Construct as the concatenation of a C string and a StringRef.
 
 Twine (const StringRef &LHS, const char *RHS)
 Construct as the concatenation of a StringRef and a C string.
 
Twineoperator= (const Twine &)=delete
 Since the intended use of twines is as temporary objects, assignments when concatenating might cause undefined behavior or stack corruptions.
 
static Twine createNull ()
 Create a 'null' string, which is an empty string that always concatenates to form another empty string. More...
 

Detailed Description

Twine - A lightweight data structure for efficiently representing the concatenation of temporary values as strings.

A Twine is a kind of rope, it represents a concatenated string using a binary-tree, where the string is the preorder of the nodes. Since the Twine can be efficiently rendered into a buffer when its result is used, it avoids the cost of generating temporary values for intermediate string results – particularly in cases when the Twine result is never required. By explicitly tracking the type of leaf nodes, we can also avoid the creation of temporary strings for conversions operations (such as appending an integer to a string).

A Twine is not intended for use directly and should not be stored, its implementation relies on the ability to store pointers to temporary stack objects which may be deallocated at the end of a statement. Twines should only be used accepted as const references in arguments, when an API wishes to accept possibly-concatenated strings.

Twines support a special 'null' value, which always concatenates to form itself, and renders as an empty string. This can be returned from APIs to effectively nullify any concatenations performed on the result.

Implementation

Given the nature of a Twine, it is not possible for the Twine's concatenation method to construct interior nodes; the result must be represented inside the returned value. For this reason a Twine object actually holds two values, the left- and right-hand sides of a concatenation. We also have nullary Twine objects, which are effectively sentinel values that represent empty strings.

Thus, a Twine can effectively have zero, one, or two children. The

See also
isNullary(),
isUnary(), and
isBinary() predicates exist for testing the number of children.

We maintain a number of invariants on Twine objects (FIXME: Why):

  • Nullary twines are always represented with their Kind on the left-hand side, and the Empty kind on the right-hand side.
  • Unary twines are always represented with the value on the left-hand side, and the Empty kind on the right-hand side.
  • If a Twine has another Twine as a child, that child should always be binary (otherwise it could have been folded into the parent).

These invariants are check by

See also
isValid().

Efficiency Considerations

The Twine is designed to yield efficient and small code for common situations. For this reason, the concat() method is inlined so that concatenations of leaf nodes can be optimized into stores directly into a single stack allocated object.

In practice, not all compilers can be trusted to optimize concat() fully, so we provide two additional methods (and accompanying operator+ overloads) to guarantee that particularly important cases (cstring plus StringRef) codegen as desired.

Constructor & Destructor Documentation

wpi::Twine::Twine ( const char *  Str)
inline

Construct from a C string.

We take care here to optimize "" into the empty twine – this will be optimized out for string constants. This allows Twine arguments have default "" values, without introducing unnecessary string constants.

Member Function Documentation

static Twine wpi::Twine::createNull ( )
inlinestatic

Create a 'null' string, which is an empty string that always concatenates to form another empty string.

StringRef wpi::Twine::getSingleStringRef ( ) const
inline

This returns the twine as a single StringRef.

This method is only valid if isSingleStringRef() is true.

bool wpi::Twine::isSingleStringRef ( ) const
inline

Return true if this twine can be dynamically accessed as a single StringRef value with getSingleStringRef().

bool wpi::Twine::isTriviallyEmpty ( ) const
inline

Check if this twine is trivially empty; a false return value does not necessarily mean the twine is empty.

void wpi::Twine::print ( raw_ostream OS) const

Write the concatenated string represented by this twine to the stream OS.

StringRef wpi::Twine::toNullTerminatedStringRef ( SmallVectorImpl< char > &  Out) const

This returns the twine as a single null terminated StringRef if it can be represented as such.

Otherwise the twine is written into the given SmallVector and a StringRef to the SmallVector's data is returned.

The returned StringRef's size does not include the null terminator.

StringRef wpi::Twine::toStringRef ( SmallVectorImpl< char > &  Out) const
inline

This returns the twine as a single StringRef if it can be represented as such.

Otherwise the twine is written into the given SmallVector and a StringRef to the SmallVector's data is returned.


The documentation for this class was generated from the following file: