Skip to content
Snippets Groups Projects
Commit 92d451a6 authored by Martin Beseda's avatar Martin Beseda
Browse files

ENH: String-to-number cast was made more robust using Boost.

parent 965984a1
No related branches found
No related tags found
No related merge requests found
...@@ -7,10 +7,14 @@ ...@@ -7,10 +7,14 @@
#include <sstream> #include <sstream>
#include <filesystem> #include <filesystem>
#include <regex> #include <regex>
#include <algorithm>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string/erase.hpp>
#include "CSVReader.h" #include "CSVReader.h"
#include "../exceptions.h" #include "../exceptions.h"
namespace lib4neuro { namespace lib4neuro {
CSVReader::CSVReader(std::string file_path, std::string delimiter, bool ignore_first_line) { CSVReader::CSVReader(std::string file_path, std::string delimiter, bool ignore_first_line) {
if(!std::filesystem::exists(file_path)) { if(!std::filesystem::exists(file_path)) {
...@@ -79,17 +83,29 @@ namespace lib4neuro { ...@@ -79,17 +83,29 @@ namespace lib4neuro {
//TODO check empty values in data //TODO check empty values in data
std::vector<double> input; std::vector<double> input;
for(auto ind : *input_col_indices) { for(auto ind : *input_col_indices) {
std::string s;
try { try {
/* Remove remaining spaces */
s = line.at(ind);
boost::algorithm::erase_all(s, " ");
/* Strip BOM */
// TODO solve in another way - work properly with different encodings!
boost::algorithm::erase_all(s, "\uEFBBBF"); // UTF-8
boost::algorithm::erase_all(s, "\uFEFF"); // UTF-16
/* Check, if the string is a number */ /* Check, if the string is a number */
if(!std::regex_match( line.at(ind), std::regex( ( "((\\+|-)?[[:digit:]]+)(\\.(([[:digit:]]+)?))?" ) ))) { double tmp = boost::lexical_cast<double>(s);
THROW_RUNTIME_ERROR(std::string("Value \"") + line.at(ind) + "\" is not numerical and so it cannot be used in Data Set!");
}
/* Add loaded number to the vector of inputs */ /* Add loaded number to the vector of inputs */
input.push_back(std::stod(line.at(ind))); input.push_back(tmp);
} catch(const std::out_of_range& e) { } catch(const std::out_of_range& e) {
THROW_OUT_OF_RANGE_ERROR("Non-existing index specified (" + std::to_string(ind) + ")!"); THROW_OUT_OF_RANGE_ERROR("Non-existing index specified (" + std::to_string(ind) + ")!");
} catch (const boost::bad_lexical_cast& e) {
THROW_RUNTIME_ERROR("Value \"" + s + "\" is not numerical and so it cannot be used in Data Set!");
} }
} }
......
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