a bit of refactoring

This commit is contained in:
2021-12-18 14:52:33 +01:00
parent 906df74847
commit eebb270f47
11 changed files with 96 additions and 96 deletions

View File

@@ -1,7 +1,6 @@
#include "table.h"
#include "csvreader.h"
#include "ml_string.h"
#include "fast_double_parser.h"
#include <charconv>
#include <fstream>
@@ -50,15 +49,14 @@ Row& Table::create_empty_row() {
}
std::string Table::csv_string() {
const size_t row_size_est = m_col_defs.size() * 16;
const size_t k_row_size_est = m_col_defs.size() * 16;
std::string out_string;
out_string.reserve(m_rows.size() * row_size_est);
// TODO improve it here https://www.cplusplus.com/reference/string/string/reserve/
out_string.reserve(m_rows.size() * k_row_size_est);
// header
for(int i = 0; i < m_col_defs.size(); i++) {
if (i > 0) out_string += ",";
if (i > 0) out_string += ',';
out_string += m_col_defs[i].name;
}
@@ -66,14 +64,14 @@ std::string Table::csv_string() {
for (auto & row : m_rows) {
if (row.is_visible()) {
std::string csv_line{"\n"};
csv_line.reserve(row_size_est);
csv_line.reserve(k_row_size_est);
for (int i = 0; i < m_col_defs.size(); i++) {
if (i > 0) csv_line += ",";
if (i > 0) csv_line += ',';
auto &col = row[i];
if (!col.isNull()) {
csv_line += col.getStringValue(); // TODO handle enclosing commas etc
csv_line += col.getCsvStringValue();
}
}
out_string += csv_line;
@@ -132,9 +130,9 @@ void Table::create_row_from_vector(const std::vector<ColDefNode> &colDefs, const
if (csv_line[i].empty()) {
new_row.setColumnNull(col_def.order);
} else if (col_def.type == ColumnType::integer_type) {
new_row.setIntColumnValue(col_def.order, string_to_long(csv_line[i]));
new_row.setIntColumnValue(col_def.order, Settings::string_to_long(csv_line[i]));
} else if (col_def.type == ColumnType::float_type) {
new_row.setFloatColumnValue(col_def.order, string_to_double(csv_line[i]));
new_row.setFloatColumnValue(col_def.order, Settings::string_to_double(csv_line[i]));
} else if (col_def.type == ColumnType::varchar_type) {
new_row.setStringColumnValue(col_def.order, csv_line[i]);
} else if (col_def.type == ColumnType::date_type) {
@@ -149,24 +147,7 @@ void Table::create_row_from_vector(const std::vector<ColDefNode> &colDefs, const
commit_row(new_row);
}
double Table::string_to_double(const std::string &s) {
double result;
const char * endptr = fast_double_parser::parse_number(s.c_str(), &result);
if (endptr == nullptr) {
throw Exception("error parsing as double: " + s);
}
return result;
}
long Table::string_to_long(const std::string &s) {
try {
return std::stol(s);
} catch (std::invalid_argument &e) {
throw Exception("error parsing as integer: " + s);
}
}
void Table::print() {
void Table::print() {
std::string out{"| "};
std::string out2{"+-"};