Skip to content

Repository files navigation

tstrings: c++ template strings

Build Status

Header-only utilities for performing simple string interpolation; compatible with ES6 template strings/literals (but without arbitrary expression evaluation).

Examples

std::string interpolation

// the templateconst string templ = "The quick ${color} fox.";
// variablesconst std::unordered_map<string, string> vars = {
{ "color", "brown" }
};
// interpolate and print to std out
cout << tstrings::interpolate_braces(templ, vars);

Numeric interpolation

// the templateconst string templ = "The ${0} ${1} fox.";
// variablesconst std::vector<string> vars = { "quick", "brown" };
// interpolate and print to cout
std::cout << tstrings::interpolate_braces(templ, vars);

Interpolate a stream

// variablesconst std::unordered_map<string, string> vars = {
{ "username", "..." },
{ "DOB", "..." }
};
// open file
std::ifstream template_file("my_template.html");
// pipe the template file to the output stream,// interpolating variableststrings::interpolate_braces(vars, std::cout) << template_file.rdbuf() << std::flush;

Requirements

  • cmake >= 2.8.12
  • A c++11 compatible compiler

Tests

To run the tests, first configure with the cmake option tstrings_cpp_WITH_TESTS For example (with MSVC):

$mkdirbuild_tests&&cdbuild_tests$cmake..-G"Visual Studio 12 2013 Win64" \
-Dtstrings_cpp_WITH_TESTS:BOOL=on

Build & Run Tests

$cdtest&&cmake--build..--configDebug$ctest.-VV-CDebug

Use in your own projects

If you're using cmake, it should be straight-forward to use tstrings in your own project. To make the package tstrings_cpp available to other projects on your system:

$cmake--install.

Other projects can now find tstrings_cpp via the find_package command. Here is a minimal example of what another project's CMakeLists.txt could look like:

# CMakeLists.txtproject (demo)
add_definitions ("--std=c++11")
find_package (tstrings_cpp)
add_executable (demomain.cpp)
target_link_libraries (demotstrings_cpp)

Alternatively, you can just add the tstrings/include path as an include directory to your project.

About

C++11 ES6 Template Literals

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages