PERF: optimize read_cell

This commit is contained in:
Chris 2017-08-26 10:49:48 -05:00
parent 0ac12101fc
commit 5cc24fda39
3 changed files with 29 additions and 23 deletions

View File

@ -22,7 +22,7 @@
// @license: http://www.opensource.org/licenses/mit-license.php // @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file // @author: see AUTHORS file
#include <locale> #include <cctype>
#include <xlnt/cell/cell_reference.hpp> #include <xlnt/cell/cell_reference.hpp>
#include <xlnt/utils/exceptions.hpp> #include <xlnt/utils/exceptions.hpp>
@ -122,9 +122,9 @@ std::pair<std::string, row_t> cell_reference::split_reference(
for (auto character : reference_string) for (auto character : reference_string)
{ {
char upper = std::toupper(character, std::locale::classic()); char upper = std::toupper(character);
if (std::isalpha(character, std::locale::classic())) if (std::isalpha(character))
{ {
if (column_part) if (column_part)
{ {
@ -155,7 +155,7 @@ std::pair<std::string, row_t> cell_reference::split_reference(
{ {
column_part = false; column_part = false;
} }
else if (!std::isdigit(character, std::locale::classic())) else if (!std::isdigit(character))
{ {
throw invalid_cell_reference(reference_string); throw invalid_cell_reference(reference_string);
} }

View File

@ -21,7 +21,7 @@
// //
// @license: http://www.opensource.org/licenses/mit-license.php // @license: http://www.opensource.org/licenses/mit-license.php
// @author: see AUTHORS file // @author: see AUTHORS file
#include <locale> #include <cctype>
#include <xlnt/cell/index_types.hpp> #include <xlnt/cell/index_types.hpp>
#include <xlnt/utils/exceptions.hpp> #include <xlnt/utils/exceptions.hpp>
@ -41,12 +41,12 @@ column_t::index_t column_t::column_index_from_string(const std::string &column_s
for (int i = static_cast<int>(column_string.length()) - 1; i >= 0; i--) for (int i = static_cast<int>(column_string.length()) - 1; i >= 0; i--)
{ {
if (!std::isalpha(column_string[static_cast<std::size_t>(i)], std::locale::classic())) if (!std::isalpha(column_string[static_cast<std::size_t>(i)]))
{ {
throw invalid_column_index(); throw invalid_column_index();
} }
auto char_index = std::toupper(column_string[static_cast<std::size_t>(i)], std::locale::classic()) - 'A'; auto char_index = std::toupper(column_string[static_cast<std::size_t>(i)]) - 'A';
column_index += static_cast<column_t::index_t>((char_index + 1) * place); column_index += static_cast<column_t::index_t>((char_index + 1) * place);
place *= 26; place *= 26;

View File

@ -149,6 +149,12 @@ void xlsx_consumer::open(std::istream &source)
populate_workbook(true); populate_workbook(true);
} }
// caching frequently used names
static const auto sheetData_el = qn("spreadsheetml", "sheetData");
static const auto row_el = qn("spreadsheetml", "row");
static const auto cell_el = qn("spreadsheetml", "c");
static const auto val_el = qn("spreadsheetml", "v");
cell xlsx_consumer::read_cell() cell xlsx_consumer::read_cell()
{ {
if (!has_cell()) if (!has_cell())
@ -158,10 +164,11 @@ cell xlsx_consumer::read_cell()
auto ws = worksheet(current_worksheet_); auto ws = worksheet(current_worksheet_);
if (in_element(qn("spreadsheetml", "sheetData"))) if (in_element(sheetData_el))
{ {
expect_start_element(qn("spreadsheetml", "row"), xml::content::complex); // CT_Row expect_start_element(row_el, xml::content::complex); // CT_Row
auto row_index = parser().attribute<row_t>("r"); // auto row_index = parser().attribute<row_t>("r");
auto row_index = static_cast<row_t>(std::stoul(parser().attribute("r")));
if (parser().attribute_present("ht")) if (parser().attribute_present("ht"))
{ {
@ -177,19 +184,18 @@ cell xlsx_consumer::read_cell()
{ {
ws.row_properties(row_index).hidden = true; ws.row_properties(row_index).hidden = true;
} }
skip_attributes({ qn("x14ac", "dyDescent") }); skip_attributes({ qn("x14ac", "dyDescent") });
skip_attributes({ "customFormat", "s", "customFont", skip_attributes({ "customFormat", "s", "customFont",
"outlineLevel", "collapsed", "thickTop", "thickBot", "outlineLevel", "collapsed", "thickTop", "thickBot",
"ph", "spans" }); "ph", "spans" });
} }
if (!in_element(qn("spreadsheetml", "row"))) if (!in_element(row_el))
{ {
return cell(nullptr); return cell(nullptr);
} }
expect_start_element(qn("spreadsheetml", "c"), xml::content::complex); expect_start_element(cell_el, xml::content::complex);
auto cell = streaming_ ? xlnt::cell(streaming_cell_.get()) auto cell = streaming_ ? xlnt::cell(streaming_cell_.get())
: ws.cell(cell_reference(parser().attribute("r"))); : ws.cell(cell_reference(parser().attribute("r")));
@ -211,11 +217,11 @@ cell xlsx_consumer::read_cell()
auto has_shared_formula = false; auto has_shared_formula = false;
auto formula_value_string = std::string(); auto formula_value_string = std::string();
while (in_element(qn("spreadsheetml", "c"))) while (in_element(cell_el))
{ {
auto current_element = expect_start_element(xml::content::mixed); auto current_element = expect_start_element(xml::content::mixed);
if (current_element == qn("spreadsheetml", "v")) // s:ST_Xstring if (current_element == val_el) // s:ST_Xstring
{ {
has_value = true; has_value = true;
value_string = read_text(); value_string = read_text();
@ -248,7 +254,7 @@ cell xlsx_consumer::read_cell()
expect_end_element(current_element); expect_end_element(current_element);
} }
expect_end_element(qn("spreadsheetml", "c")); expect_end_element(cell_el);
if (has_formula && !has_shared_formula) if (has_formula && !has_shared_formula)
{ {
@ -291,13 +297,13 @@ cell xlsx_consumer::read_cell()
cell.format(target_.format(format_id)); cell.format(target_.format(format_id));
} }
if (!in_element(qn("spreadsheetml", "row"))) if (!in_element(row_el))
{ {
expect_end_element(qn("spreadsheetml", "row")); expect_end_element(row_el);
if (!in_element(qn("spreadsheetml", "sheetData"))) if (!in_element(sheetData_el))
{ {
expect_end_element(qn("spreadsheetml", "sheetData")); expect_end_element(sheetData_el);
} }
} }
@ -1024,8 +1030,8 @@ xml::parser &xlsx_consumer::parser()
bool xlsx_consumer::has_cell() bool xlsx_consumer::has_cell()
{ {
return in_element(qn("spreadsheetml", "row")) return in_element(row_el)
|| in_element(qn("spreadsheetml", "sheetData")); || in_element(sheetData_el);
} }
std::vector<relationship> xlsx_consumer::read_relationships(const path &part) std::vector<relationship> xlsx_consumer::read_relationships(const path &part)