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

added double conversion before cast to string to T conversion

parent ad447ac9
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
......@@ -68,45 +68,39 @@ string_contains(const std::string &s, unsigned char ch)
* list is returned with the result boolean.
**/
#include <sstream>
template<typename T>
std::pair<bool,T>
template <typename T>
std::pair<bool, T>
convert_to_number(const std::string &str)
{
std::stringstream ss(str);
T number = 0;
double number = 0;
ss >> number;
if (ss.fail())
{
return {false,0};
}
if (ss.fail()) { return { false, 0 }; }
return {true,number};
return { true, number };
}
template<typename T>
/* This function seperates a string into multiple tokens converted to T
* CAUTION if T=int and scientific notation is used a 1.01e30 would be converted
* to a simple 1 */
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::stringstream ss(args);
std::string token;
std::vector<T> numbers;
bool retval = true;
for (const auto &t : tokens)
while (std::getline(ss, token, ','))
{
std::pair<bool,T> res = convert_to_number<T>(t);
if (!res.first)
{
retval = false;
tokens = {};
break;
}
std::pair<bool, T> res = convert_to_number<T>(token);
std::cout << token <<" " << res.first << " " << res.second << std::endl;
if (!res.first) { return { false, {} }; }
numbers.push_back(res.second);
}
return std::make_pair(retval, numbers);
return std::make_pair(true, 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