Twine ‐ A lightweight data structure for efficiently representing the concatenation of temporary values as strings.
Synopsis
Declared in <llvm/ADT/Twine.h>
class Twine;
Description
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 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
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
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.
Member Functions
Name |
Description |
|
Constructors |
|
Deleted assignment; Twines are temporary and must not be reassigned. |
Return a twine that concatenates this twine with |
|
Dump the concatenated string represented by this twine to stderr. |
|
Dump the representation of this twine to stderr. |
|
This returns the twine as a single StringRef. This method is only valid if isSingleStringRef() is true. |
|
Check if this twine is guaranteed to refer to single string literal. |
|
Return true if this twine can be dynamically accessed as a single StringRef value with getSingleStringRef(). |
|
Check if this twine is trivially empty; a false return value does not necessarily mean the twine is empty. |
|
Write the concatenated string represented by this twine to the stream |
|
Write the representation of this twine to the stream |
|
Return the twine contents as a std::string. |
|
Return this twine as a null‐terminated StringRef, using |
|
Return this twine as a StringRef, using |
|
Append the concatenated string into the given SmallString or SmallVector. |
Static Member Functions
Name |
Description |
Create a 'null' string, which is an empty string that always concatenates to form another empty string. |
|
Construct a twine to print |
Non-Member Functions
Name |
Description |
Print an error diagnostic with message |
|
Print an error with message |
|
Print a note with message |
|
Print a note diagnostic with message |
|
Print a warning diagnostic with message |
|
Create a StringError with the specified error code and prepend the file path to it. |
|
Concatenate a source file path and/or name with line number and std::error_code to form an Error object. |
|
Deleted overload: a successful Error cannot be wrapped as a FileError. |
|
Concatenate a source file path and/or name with a std::error_code to form an Error object. |
|
Create a StringError with the specified error code and prepend the file path to it. |
|
Concatenate a source file path and/or name with an Error. The resulting Error is unchecked. |
|
Concatenate a source file path and/or name with line number and an Error. The resulting Error is unchecked. |
|
Create a temporary file name suitable for writing a DOT graph. |
|
Create a StringError with an inconvertible error code. |
|
Get and identify path's type based on its content. |
|
Create a local file system cache which uses the given cache name, temporary file prefix, cache directory and file callback. |
|
Query whether a file needs conversion to the UTF‐8 codepage. |
|
Concatenate |
|
Concatenate a C string and a StringRef with simplified codegen. |
|
Concatenate a StringRef and a C string with simplified codegen. |
|
Report a fatal internal error with message |
|
Report a fatal usage error with message |
|
Report a fatal error from a Twine reason. |
|
Set the name of the current thread. |
|
Parse |
|
Parse |
|
Parse |
|
Determine whether to skip over symlink due to either too many symlink levels or is cyclic. |
|
Create a persistent on‐disk CAS at |
|
Report a codegen‐data warning with message |
|
|
True if |
Create a parse_failed StringError with message |
|
Return Error::success() or use |
|
Return Error::success() or use |
|
Can the file be accessed? |
|
Can we execute this file? |
|
Can we write this file? |
|
Copy the contents of From to To. |
|
Copy the contents of From to To. |
|
Create an empty temporary file without returning an open descriptor. |
|
Create a file in the system temporary directory. |
|
Create a unique directory whose name starts with |
|
Create a uniquely named empty file without returning an open descriptor. |
|
Create a uniquely named file. |
|
Create a potentially unique file name but does not create it. |
|
Create all the non‐existent directories in path. |
|
Create the directory in path. |
|
|
Create a hard link from from to to, or return an error. |
Create a link from from to to. |
|
Create a symbolic link from from to to. |
|
Get disk space usage information. |
|
Return whether paths |
|
Do paths represent the same thing? |
|
Does file exist? |
|
Expands ~ expressions to the user's home directory. On Unix ~user directories are resolved as well. |
|
Get file size. |
|
Get file permissions. |
|
|
Get a unique name, not currently exisiting in the filesystem. Subject to race conditions, prefer to use createUniqueFile instead. |
|
Get a unique temporary file name, not currently exisiting in the filesystem. Subject to race conditions, prefer to use createTemporaryFile instead. |
Fill |
|
|
Does status represent a directory? |
Is path a directory? |
|
Return whether |
|
Return whether |
|
Is the file mounted on a local filesystem? |
|
Is path something that exists but is not a directory, regular file, or symlink? |
|
|
Is path a regular file? |
|
Return whether |
|
Return whether |
|
Is path a symlink file? |
Compute an MD5 hash of the file at |
|
Opens a file with the specified creation disposition, access mode, and flags and returns a file descriptor. |
|
Opens the file with the given name in a read‐only mode, returning its open file descriptor. |
|
Opens the file for read‐write, creating it if it does not exist. |
|
Opens the file for write, creating it if it does not exist. |
|
Opens a file with the specified creation disposition, access mode, and flags and returns a platform‐specific file object. |
|
Opens the file with the given name in a read‐only mode, returning its open file descriptor. |
|
Opens the file for read‐write, creating it if it does not exist. |
|
Opens the file for write, creating it if it does not exist. |
|
Read the target of a symbolic link. |
|
Collapse all . and .. patterns, resolve all symlinks, and optionally expand ~ expressions to the user's home directory. |
|
Remove path. Equivalent to POSIX remove(). |
|
Recursively delete a directory. |
|
Rename from to to. |
|
|
Set the file modification and access time, by path. |
|
Set both access and modification time by path to the same value. |
Set file permissions. |
|
|
Set the current path. |
Get file status as if by POSIX stat(). |
|
Is status available? |
|
Has extension? |
|
Has filename? |
|
|
Has parent path? |
|
Has relative path? |
|
Has root directory? |
|
Has root name? |
|
Has root path? |
Has stem? |
|
Is path absolute? |
|
|
Is path absolute using GNU rules? |
Is path relative? |
|
Make path an absolute path. |
|
Convert path to the native form as a std::string. |
|
Convert path to the native form. |
See Also
isNullary(),
isUnary(), and
isBinary() predicates exist for testing the number of children.
isValid().
Created with MrDocs