From 22f9410d177ad41d22979444d769f180f891b05d Mon Sep 17 00:00:00 2001 From: VaclavT Date: Tue, 9 Feb 2021 18:58:15 +0100 Subject: [PATCH] csv parse numbers --- ml.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/ml.cpp b/ml.cpp index 6762f13..d193c84 100644 --- a/ml.cpp +++ b/ml.cpp @@ -18,6 +18,7 @@ #include #include #include +#include std::string read_file_contents(std::string filename) { std::ifstream f; @@ -107,6 +108,21 @@ bool is_symbol(char ch) { return (isalpha(ch) || ispunct(ch)) && ch != '(' && ch != ')' && ch != '"' && ch != '\''; } +// std::regex int_underscored_regex("[0-9][0-9_]+[0-9]"); +std::regex int_regex("[0-9]+"); +std::regex double_regex("[0-9]+\\.[0-9]+"); + +// Is string representing int value +bool is_string_int(const std::string &str) { + return std::regex_match(str, int_regex); +} + +// Is string representing float value +bool is_string_float(const std::string &str) { + return std::regex_match(str, double_regex); +} + + //////////////////////////////////////////////////////////////////////////////// /// LISP CONSTRUCTS //////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// @@ -889,7 +905,7 @@ Value Value::eval(Environment &env) { } } -void skip_whitespace(std::string &s, int &ptr) { +void skip_whitespace(const std::string &s, int &ptr) { while (isspace(s[ptr])) { ptr++; } } @@ -1214,7 +1230,13 @@ namespace builtin { std::vector row; for (int c = 0; c < cols; c++) { std::string value = parsed_data[r][c]; - row.push_back(Value::string(value)); + if (is_string_int(value)) { + row.push_back(Value(stoi(value))); + } if (is_string_float(value)) { + row.push_back(Value(std::stod(value))); + } else { + row.push_back(Value::string(value)); + } } result.push_back(row); }