csv parse numbers
This commit is contained in:
parent
7cd3155397
commit
22f9410d17
24
ml.cpp
24
ml.cpp
|
|
@ -18,6 +18,7 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
#include <regex>
|
||||||
|
|
||||||
std::string read_file_contents(std::string filename) {
|
std::string read_file_contents(std::string filename) {
|
||||||
std::ifstream f;
|
std::ifstream f;
|
||||||
|
|
@ -107,6 +108,21 @@ bool is_symbol(char ch) {
|
||||||
return (isalpha(ch) || ispunct(ch)) && ch != '(' && ch != ')' && ch != '"' && 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 ////////////////////////////////////////////////////////////
|
/// 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++; }
|
while (isspace(s[ptr])) { ptr++; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1214,8 +1230,14 @@ namespace builtin {
|
||||||
std::vector<Value> row;
|
std::vector<Value> row;
|
||||||
for (int c = 0; c < cols; c++) {
|
for (int c = 0; c < cols; c++) {
|
||||||
std::string value = parsed_data[r][c];
|
std::string value = parsed_data[r][c];
|
||||||
|
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));
|
row.push_back(Value::string(value));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
result.push_back(row);
|
result.push_back(row);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue