REMOVED USER_STD macros, code formating, csv changes, few file functions
This commit is contained in:
@@ -12,11 +12,13 @@ CsvParser::CsvParser(bool skip_hdr, char field_sep, char quote_ch, char line_sep
|
||||
header_skiped = false;
|
||||
}
|
||||
|
||||
void CsvParser::parseCSV(const std::string &csvSource, std::vector< std::vector<std::string> > &lines) {
|
||||
MlValue CsvParser::parseCSV(const std::string &csvSource) {
|
||||
bool inQuote(false);
|
||||
bool newLine(false);
|
||||
std::string field;
|
||||
lines.clear();
|
||||
|
||||
// PERF optimize it for memory usage and performance
|
||||
std::vector<std::vector<std::string>> parsed_data; // TODO some default size here
|
||||
std::vector<std::string> line;
|
||||
|
||||
std::string::const_iterator aChar = csvSource.begin();
|
||||
@@ -38,7 +40,7 @@ void CsvParser::parseCSV(const std::string &csvSource, std::vector< std::vector<
|
||||
} else {
|
||||
if (newLine == false) {
|
||||
line.push_back(field);
|
||||
addLine(line, lines);
|
||||
addLine(line, parsed_data);
|
||||
field.clear();
|
||||
line.clear();
|
||||
newLine = true;
|
||||
@@ -55,37 +57,39 @@ void CsvParser::parseCSV(const std::string &csvSource, std::vector< std::vector<
|
||||
if (field.size())
|
||||
line.push_back(field);
|
||||
|
||||
addLine(line, lines);
|
||||
addLine(line, parsed_data);
|
||||
|
||||
return ivalualize(parsed_data);
|
||||
}
|
||||
|
||||
MlValue CsvParser::ivalualize(std::vector< std::vector<std::string> > &parsed_data) const {
|
||||
int rows = parsed_data.size();
|
||||
int cols = rows > 0 ? parsed_data[0].size() : 0;
|
||||
MlValue CsvParser::ivalualize(std::vector<std::vector<std::string> > &parsed_data) const {
|
||||
int rows = parsed_data.size();
|
||||
int cols = rows > 0 ? parsed_data[0].size() : 0;
|
||||
|
||||
std::vector<MlValue> result;
|
||||
std::vector<MlValue> result;
|
||||
|
||||
if (rows > 0 && cols > 0) {
|
||||
for (int r = 0; r < rows; r++) {
|
||||
std::vector<MlValue> row;
|
||||
for (int c = 0; c < cols; c++) {
|
||||
std::string value = parsed_data[r][c];
|
||||
if (is_string_int(value)) {
|
||||
row.push_back(MlValue(stoi(value)));
|
||||
}
|
||||
if (is_string_float(value)) {
|
||||
row.push_back(MlValue(std::stod(value)));
|
||||
} else {
|
||||
row.push_back(MlValue::string(value));
|
||||
}
|
||||
}
|
||||
result.push_back(row);
|
||||
}
|
||||
}
|
||||
if (rows > 0 && cols > 0) {
|
||||
for (int r = 0; r < rows; r++) {
|
||||
std::vector<MlValue> row;
|
||||
for (int c = 0; c < cols; c++) {
|
||||
std::string value = parsed_data[r][c];
|
||||
if (is_string_int(value)) {
|
||||
row.push_back(MlValue(stoi(value)));
|
||||
}
|
||||
if (is_string_float(value)) {
|
||||
row.push_back(MlValue(std::stod(value)));
|
||||
} else {
|
||||
row.push_back(MlValue::string(value));
|
||||
}
|
||||
}
|
||||
result.push_back(row);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
void CsvParser::addLine(const std::vector<std::string> &line, std::vector< std::vector<std::string> > &lines) {
|
||||
void CsvParser::addLine(const std::vector<std::string> &line, std::vector<std::vector<std::string> > &lines) {
|
||||
if (skip_header && !header_skiped) {
|
||||
header_skiped = true;
|
||||
} else {
|
||||
@@ -101,10 +105,10 @@ std::regex double_regex("[0-9]+\\.[0-9]+");
|
||||
|
||||
// Is string representing int value
|
||||
bool CsvParser::is_string_int(const std::string &str) const {
|
||||
return std::regex_match(str, int_regex);
|
||||
return std::regex_match(str, int_regex);
|
||||
}
|
||||
|
||||
// Is string representing float value
|
||||
bool CsvParser::is_string_float(const std::string &str) const {
|
||||
return std::regex_match(str, double_regex);
|
||||
return std::regex_match(str, double_regex);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user