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

removed mandatory double cast and added static type restrictions

parent 95ff1026
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
...@@ -14,6 +14,8 @@ ...@@ -14,6 +14,8 @@
#include <memory> #include <memory>
#include <stdexcept> #include <stdexcept>
#include <algorithm> #include <algorithm>
#include <sstream>
#include <concepts>
#define ADD_PLURAL(n) ((n) != 1 ? "s" : "") #define ADD_PLURAL(n) ((n) != 1 ? "s" : "")
...@@ -61,19 +63,14 @@ string_contains(const std::string &s, unsigned char ch) ...@@ -61,19 +63,14 @@ string_contains(const std::string &s, unsigned char ch)
return (s.find(ch) != std::string::npos); 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> template <typename T>
std::pair<bool, T> std::pair<bool, T>
convert_to_number(const std::string &str) convert_to_number(const std::string &str)
{ {
static_assert(std::is_same<T, double>::value || std::is_same<T, float>::value,
"tokenize_comma_seperated_number_list can only produce float or double lists");
std::stringstream ss(str); std::stringstream ss(str);
double number = 0; T number = 0;
ss >> number; ss >> number;
if (ss.fail()) { return { false, 0 }; } if (ss.fail()) { return { false, 0 }; }
...@@ -81,20 +78,18 @@ convert_to_number(const std::string &str) ...@@ -81,20 +78,18 @@ convert_to_number(const std::string &str)
return { true, number }; return { true, number };
} }
/* 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> template <typename T>
std::pair<bool, std::vector<T>> std::pair<bool, std::vector<T>>
tokenize_comma_seperated_number_list(const std::string &args) tokenize_comma_seperated_number_list(const std::string &args)
{ {
static_assert(std::is_same<T, double>::value || std::is_same<T, float>::value,
"tokenize_comma_seperated_number_list can only produce float or double lists");
std::stringstream ss(args); std::stringstream ss(args);
std::string token; std::string token;
std::vector<T> numbers; std::vector<T> numbers;
while (std::getline(ss, token, ',')) while (std::getline(ss, token, ','))
{ {
std::pair<bool, T> res = convert_to_number<T>(token); std::pair<bool, T> res = convert_to_number<T>(token);
std::cout << token <<" " << res.first << " " << res.second << std::endl;
if (!res.first) { return { false, {} }; } if (!res.first) { return { false, {} }; }
numbers.push_back(res.second); numbers.push_back(res.second);
} }
......
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