![]() |
Project
|
Tokenize a string according to delimiter ',' and extract values of type T. More...
#include <RangeTokenizer.h>
Static Public Member Functions | |
template<typename T > | |
static std::vector< T > | tokenize (std::string input, std::function< T(std::string const &)> convert=[](std::string const &token) {T value;std::istringstream(token) > > value;return value;}) |
template<typename T , typename std::enable_if_t< std::is_integral< T >::value==true, int > = 0> | |
static void | insertRange (std::vector< T > &res, std::string token, std::function< T(std::string const &)> convert) |
extract a range of an integral type from a token string and add to vector | |
Tokenize a string according to delimiter ',' and extract values of type T.
Extract a sequence of elements of specified type T from a string argument. Elements are separated by comma. If T is an integral type, also ranges are supported using '-'.
The default conversion from token to type is using std stringstream operator>> which supports a variety of built-in conversions. A custom handler function of type std::function<T(std::string const&)> can be provided to convert string tokens to the specified output type.
Usage: // the simple case using integral type std::vector<int> tokens = RangeTokenizer::tokenize<int>("0-5,10,13");
// simple case using string type std::vector<std::string> tokens = RangeTokenizer::tokenize<std::string>("apple,strawberry,tomato");
// process a custom type according to a map // use a custom mapper function, this evetually throws an exception if the token is not in the map enum struct Food { Apple, Strawberry, Tomato }; const std::map<std::string, Food> FoodMap { { "apple", Food::Apple }, { "strawberry", Food::Strawberry }, { "tomato", Food::Tomato }, }; std::vector<Food> tokens = RangeTokenizer::tokenize<Food>("apple,tomato", [FoodMap](auto const& token) { return FoodMap.at(token); } );
Definition at line 62 of file RangeTokenizer.h.
|
inlinestatic |
extract a range of an integral type from a token string and add to vector
Definition at line 85 of file RangeTokenizer.h.
|
inlinestatic |
Definition at line 64 of file RangeTokenizer.h.