Skip to content
Snippets Groups Projects
Commit b71d96a6 authored by Oliver Heidmann's avatar Oliver Heidmann Committed by Uwe Schulzweida
Browse files

added tokenizer from string to T in util string

parent 7bd5e97d
No related branches found
No related tags found
2 merge requests!326convert_to_number not called string_to_number, fixed usage of .fail enabling...,!322Fixing and cleaning up handling of operator argument sourrounding the use of scientific notations
......@@ -61,6 +61,52 @@ string_contains(const std::string &s, unsigned char ch)
return (s.find(ch) != std::string::npos);
}
/** Tokenizes a string with delimiter ',' and returns a touple where first
* signals success and the second the result.
* If any of the tokens is not a integer the result is negative.
* With a negative result a empty
* list is returned with the result boolean.
**/
#include <sstream>
template<typename T>
std::pair<bool,T>
convert_to_number(const std::string &str)
{
std::stringstream ss(str);
T number = 0;
ss >> number;
if (ss.fail())
{
return {false,0};
}
return {true,number};
}
template<typename T>
std::pair<bool, std::vector<T>>
tokenize_comma_seperated_number_list(const std::string &args)
{
auto tokens = split_with_seperator(args, ',');
std::vector<T> numbers;
bool retval = true;
for (const auto &t : tokens)
{
std::pair<bool,T> res = convert_to_number<T>(t);
if (!res.first)
{
retval = false;
tokens = {};
break;
}
numbers.push_back(res.second);
}
return std::make_pair(retval, numbers);
}
namespace Util
{
namespace String
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment