Skip to content

Latest commit

 

History

History
97 lines (69 loc) · 2.4 KB

README.md

File metadata and controls

97 lines (69 loc) · 2.4 KB

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 template
const string templ = "The quick ${color} fox.";

// variables
const std::unordered_map<string, string> vars = {
    { "color", "brown" }
};

// interpolate and print to std out
cout << tstrings::interpolate_braces(templ, vars);

Numeric interpolation

// the template
const string templ = "The ${0} ${1} fox.";

// variables
const std::vector<string> vars = { "quick", "brown" };

// interpolate and print to cout
std::cout << tstrings::interpolate_braces(templ, vars);

Interpolate a stream

// variables
const 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 variables
tstrings::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):

$ mkdir build_tests && cd build_tests
$ cmake .. -G "Visual Studio 12 2013 Win64" \
    -Dtstrings_cpp_WITH_TESTS:BOOL=on

Build & Run Tests

$ cd test && cmake --build .. --config Debug
$ ctest . -VV -C Debug

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.txt
project (demo)
add_definitions ("--std=c++11")

find_package (tstrings_cpp)
add_executable (demo main.cpp)
target_link_libraries (demo tstrings_cpp)

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