Loading...
Searching...
No Matches
TfTemplateString Class Reference

TfTemplateString provides simple string substitutions based on named placeholders. More...

#include <templateString.h>

Public Types

typedef std::map< std::string, std::string > Mapping
 

Public Member Functions

TF_API TfTemplateString ()
 Constructs a new template string.
 
TF_API TfTemplateString (const std::string &template_)
 Constructs a new template string.
 
const std::string & GetTemplate () const
 Returns the template source string supplied to the constructor.
 
TF_API std::string Substitute (const Mapping &) const
 Performs the template substitution, returning a new string.
 
TF_API std::string SafeSubstitute (const Mapping &) const
 Like Substitute(), except that if placeholders are missing from the mapping, instead of raising a coding error, the original placeholder will appear in the resulting string intact.
 
TF_API Mapping GetEmptyMapping () const
 Returns an empty mapping for the current template.
 
TF_API bool IsValid () const
 Returns true if the current template is well formed.
 
TF_API std::vector< std::string > GetParseErrors () const
 Returns any error messages generated during template parsing.
 

Detailed Description

TfTemplateString provides simple string substitutions based on named placeholders.

Instead of the ''-based substitutions used by printf, template strings use '$'-based substitutions, using the following rules:

  • "$$" is replaced with a single "$"
  • "$identifier" names a substitution placeholder matching a mapping key of "identifier". The first non-identifier character after the "$" character terminates the placeholder specification.
  • "${identifier}" is equivalent to "$identifier". It is required when valid identifier characters follow the placeholder but are not part of the placeholder, such as "${noun}ification".
  • An identifier is a sequence of characters "[A-Z][a-z][0-9]_".

TfTemplateString is immutable: once one is created it may not be modified. TfTemplateString is fast to copy, since it shares state internally between copies. TfTemplateString is thread-safe. It may be read freely by multiple threads concurrently.

Definition at line 63 of file templateString.h.

Member Typedef Documentation

◆ Mapping

typedef std::map<std::string, std::string> Mapping

Definition at line 65 of file templateString.h.

Constructor & Destructor Documentation

◆ TfTemplateString() [1/2]

TF_API TfTemplateString ( )

Constructs a new template string.

◆ TfTemplateString() [2/2]

TF_API TfTemplateString ( const std::string &  template_)

Constructs a new template string.

Member Function Documentation

◆ GetEmptyMapping()

TF_API Mapping GetEmptyMapping ( ) const

Returns an empty mapping for the current template.

This method first calls IsValid to ensure that the template is valid.

◆ GetParseErrors()

TF_API std::vector< std::string > GetParseErrors ( ) const

Returns any error messages generated during template parsing.

◆ GetTemplate()

const std::string & GetTemplate ( ) const
inline

Returns the template source string supplied to the constructor.

Definition at line 76 of file templateString.h.

◆ IsValid()

TF_API bool IsValid ( ) const

Returns true if the current template is well formed.

Empty templates are valid.

◆ SafeSubstitute()

TF_API std::string SafeSubstitute ( const Mapping &  ) const

Like Substitute(), except that if placeholders are missing from the mapping, instead of raising a coding error, the original placeholder will appear in the resulting string intact.

◆ Substitute()

TF_API std::string Substitute ( const Mapping &  ) const

Performs the template substitution, returning a new string.

The mapping contains keys which match the placeholders in the template. If a placeholder is found for which no mapping is present, a coding error is raised.


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