From 39f498f40128850ce6b0d76ae45e65770818656e Mon Sep 17 00:00:00 2001 From: JCrawfy Date: Sun, 1 Mar 2020 22:01:53 +1300 Subject: [PATCH] use the new faster serialisation everywhere in xlsx_producer --- .../header_footer/header_footer_code.cpp | 10 ++-- .../header_footer/header_footer_code.hpp | 5 +- source/detail/serialization/xlsx_consumer.cpp | 60 +++++++++---------- source/detail/serialization/xlsx_consumer.hpp | 2 +- source/detail/serialization/xlsx_producer.cpp | 24 ++++---- source/detail/serialization/xlsx_producer.hpp | 2 + 6 files changed, 53 insertions(+), 50 deletions(-) diff --git a/source/detail/header_footer/header_footer_code.cpp b/source/detail/header_footer/header_footer_code.cpp index f7560eb8..014234de 100644 --- a/source/detail/header_footer/header_footer_code.cpp +++ b/source/detail/header_footer/header_footer_code.cpp @@ -27,7 +27,7 @@ namespace xlnt { namespace detail { -std::array, 3> decode_header_footer(const std::string &hf_string) +std::array, 3> decode_header_footer(const std::string &hf_string, const number_serialiser &serialiser) { std::array, 3> result; @@ -216,7 +216,7 @@ std::array, 3> decode_header_footer(const std::s tokens.push_back(token); } - const auto parse_section = [&tokens, &result](hf_code code) { + const auto parse_section = [&tokens, &result, &serialiser](hf_code code) { std::vector end_codes{hf_code::left_section, hf_code::center_section, hf_code::right_section}; end_codes.erase(std::find(end_codes.begin(), end_codes.end(), code)); @@ -297,7 +297,7 @@ std::array, 3> decode_header_footer(const std::s current_run.second = xlnt::font(); } - current_run.second.get().size(std::stod(current_token.value)); + current_run.second.get().size(serialiser.deserialise(current_token.value)); break; } @@ -460,7 +460,7 @@ std::array, 3> decode_header_footer(const std::s return result; } -std::string encode_header_footer(const rich_text &t, header_footer::location where) +std::string encode_header_footer(const rich_text &t, header_footer::location where, const number_serialiser &serialiser) { const auto location_code_map = std::unordered_map #include #include +#include namespace xlnt { namespace detail { -std::array, 3> decode_header_footer(const std::string &hf_string); -std::string encode_header_footer(const rich_text &t, header_footer::location where); +std::array, 3> decode_header_footer(const std::string &hf_string, const number_serialiser &serialiser); +std::string encode_header_footer(const rich_text &t, header_footer::location where, const number_serialiser& serialiser); } // namespace detail } // namespace xlnt diff --git a/source/detail/serialization/xlsx_consumer.cpp b/source/detail/serialization/xlsx_consumer.cpp index ea64b131..bde76349 100644 --- a/source/detail/serialization/xlsx_consumer.cpp +++ b/source/detail/serialization/xlsx_consumer.cpp @@ -307,14 +307,14 @@ Cell parse_cell(xlnt::row_t row_arg, xml::parser *parser) } // inside element -std::pair parse_row(xml::parser *parser, xlnt::detail::number_converter &converter, std::vector &parsed_cells) +std::pair parse_row(xml::parser *parser, xlnt::detail::number_serialiser &converter, std::vector &parsed_cells) { std::pair props; for (auto &attr : parser->attribute_map()) { if (string_equal(attr.first.name(), "dyDescent")) { - props.first.dy_descent = converter.stold(attr.second.value); + props.first.dy_descent = converter.deserialise(attr.second.value); } else if (string_equal(attr.first.name(), "spans")) { @@ -322,7 +322,7 @@ std::pair parse_row(xml::parser *parser, xlnt::detail } else if (string_equal(attr.first.name(), "ht")) { - props.first.height = converter.stold(attr.second.value); + props.first.height = converter.deserialise(attr.second.value); } else if (string_equal(attr.first.name(), "s")) { @@ -382,7 +382,7 @@ std::pair parse_row(xml::parser *parser, xlnt::detail } // inside element -Sheet_Data parse_sheet_data(xml::parser *parser, xlnt::detail::number_converter &converter) +Sheet_Data parse_sheet_data(xml::parser *parser, xlnt::detail::number_serialiser &converter) { Sheet_Data sheet_data; int level = 1; // nesting level @@ -480,7 +480,7 @@ cell xlsx_consumer::read_cell() if (parser().attribute_present("ht")) { - row_properties.height = converter_.stold(parser().attribute("ht")); + row_properties.height = converter_.deserialise(parser().attribute("ht")); } if (parser().attribute_present("customHeight")) @@ -495,7 +495,7 @@ cell xlsx_consumer::read_cell() if (parser().attribute_present(qn("x14ac", "dyDescent"))) { - row_properties.dy_descent = converter_.stold(parser().attribute(qn("x14ac", "dyDescent"))); + row_properties.dy_descent = converter_.deserialise(parser().attribute(qn("x14ac", "dyDescent"))); } if (parser().attribute_present("spans")) @@ -602,7 +602,7 @@ cell xlsx_consumer::read_cell() } else if (type == "s") { - cell.d_->value_numeric_ = converter_.stold(value_string); + cell.d_->value_numeric_ = converter_.deserialise(value_string); cell.data_type(cell::type::shared_string); } else if (type == "b") // boolean @@ -611,7 +611,7 @@ cell xlsx_consumer::read_cell() } else if (type == "n") // numeric { - cell.value(converter_.stold(value_string)); + cell.value(converter_.deserialise(value_string)); } else if (!value_string.empty() && value_string[0] == '#') { @@ -863,23 +863,23 @@ std::string xlsx_consumer::read_worksheet_begin(const std::string &rel_id) if (parser().attribute_present("baseColWidth")) { ws.d_->format_properties_.base_col_width = - converter_.stold(parser().attribute("baseColWidth")); + converter_.deserialise(parser().attribute("baseColWidth")); } if (parser().attribute_present("defaultColWidth")) { ws.d_->format_properties_.default_column_width = - converter_.stold(parser().attribute("defaultColWidth")); + converter_.deserialise(parser().attribute("defaultColWidth")); } if (parser().attribute_present("defaultRowHeight")) { ws.d_->format_properties_.default_row_height = - converter_.stold(parser().attribute("defaultRowHeight")); + converter_.deserialise(parser().attribute("defaultRowHeight")); } if (parser().attribute_present(qn("x14ac", "dyDescent"))) { ws.d_->format_properties_.dy_descent = - converter_.stold(parser().attribute(qn("x14ac", "dyDescent"))); + converter_.deserialise(parser().attribute(qn("x14ac", "dyDescent"))); } skip_attributes(); @@ -899,7 +899,7 @@ std::string xlsx_consumer::read_worksheet_begin(const std::string &rel_id) optional width = [this](xml::parser &p) -> xlnt::optional { if (p.attribute_present("width")) { - return (converter_.stold(p.attribute("width")) * 7 - 5) / 7; + return (converter_.deserialise(p.attribute("width")) * 7 - 5) / 7; } return xlnt::optional(); }(parser()); @@ -1000,7 +1000,7 @@ void xlsx_consumer::read_worksheet_sheetdata() case cell::type::empty: case cell::type::number: case cell::type::date: { - ws_cell_impl->value_numeric_ = converter_.stold(cell.value); + ws_cell_impl->value_numeric_ = converter_.deserialise(cell.value); break; } case cell::type::shared_string: { @@ -1196,12 +1196,12 @@ worksheet xlsx_consumer::read_worksheet_end(const std::string &rel_id) { page_margins margins; - margins.top(converter_.stold(parser().attribute("top"))); - margins.bottom(converter_.stold(parser().attribute("bottom"))); - margins.left(converter_.stold(parser().attribute("left"))); - margins.right(converter_.stold(parser().attribute("right"))); - margins.header(converter_.stold(parser().attribute("header"))); - margins.footer(converter_.stold(parser().attribute("footer"))); + margins.top(converter_.deserialise(parser().attribute("top"))); + margins.bottom(converter_.deserialise(parser().attribute("bottom"))); + margins.left(converter_.deserialise(parser().attribute("left"))); + margins.right(converter_.deserialise(parser().attribute("right"))); + margins.header(converter_.deserialise(parser().attribute("header"))); + margins.footer(converter_.deserialise(parser().attribute("footer"))); ws.page_margins(margins); } @@ -1251,27 +1251,27 @@ worksheet xlsx_consumer::read_worksheet_end(const std::string &rel_id) if (current_hf_element == qn("spreadsheetml", "oddHeader")) { - odd_header = decode_header_footer(read_text()); + odd_header = decode_header_footer(read_text(), converter_); } else if (current_hf_element == qn("spreadsheetml", "oddFooter")) { - odd_footer = decode_header_footer(read_text()); + odd_footer = decode_header_footer(read_text(), converter_); } else if (current_hf_element == qn("spreadsheetml", "evenHeader")) { - even_header = decode_header_footer(read_text()); + even_header = decode_header_footer(read_text(), converter_); } else if (current_hf_element == qn("spreadsheetml", "evenFooter")) { - even_footer = decode_header_footer(read_text()); + even_footer = decode_header_footer(read_text(), converter_); } else if (current_hf_element == qn("spreadsheetml", "firstHeader")) { - first_header = decode_header_footer(read_text()); + first_header = decode_header_footer(read_text(), converter_); } else if (current_hf_element == qn("spreadsheetml", "firstFooter")) { - first_footer = decode_header_footer(read_text()); + first_footer = decode_header_footer(read_text(), converter_); } else { @@ -2308,7 +2308,7 @@ void xlsx_consumer::read_stylesheet() while (in_element(qn("spreadsheetml", "gradientFill"))) { expect_start_element(qn("spreadsheetml", "stop"), xml::content::complex); - auto position = converter_.stold(parser().attribute("position")); + auto position = converter_.deserialise(parser().attribute("position")); expect_start_element(qn("spreadsheetml", "color"), xml::content::complex); auto color = read_color(); expect_end_element(qn("spreadsheetml", "color")); @@ -2356,7 +2356,7 @@ void xlsx_consumer::read_stylesheet() if (font_property_element == qn("spreadsheetml", "sz")) { - new_font.size(converter_.stold(parser().attribute("val"))); + new_font.size(converter_.deserialise(parser().attribute("val"))); } else if (font_property_element == qn("spreadsheetml", "name")) { @@ -3169,7 +3169,7 @@ rich_text xlsx_consumer::read_rich_text(const xml::qname &parent) if (current_run_property_element == xml::qname(xmlns, "sz")) { - run.second.get().size(converter_.stold(parser().attribute("val"))); + run.second.get().size(converter_.deserialise(parser().attribute("val"))); } else if (current_run_property_element == xml::qname(xmlns, "rFont")) { @@ -3307,7 +3307,7 @@ xlnt::color xlsx_consumer::read_color() if (parser().attribute_present("tint")) { - result.tint(converter_.stold(parser().attribute("tint"))); + result.tint(converter_.deserialise(parser().attribute("tint"))); } return result; diff --git a/source/detail/serialization/xlsx_consumer.hpp b/source/detail/serialization/xlsx_consumer.hpp index 963e1361..c9a987ee 100644 --- a/source/detail/serialization/xlsx_consumer.hpp +++ b/source/detail/serialization/xlsx_consumer.hpp @@ -416,7 +416,7 @@ private: detail::cell_impl *current_cell_; detail::worksheet_impl *current_worksheet_; - number_converter converter_; + number_serialiser converter_; }; } // namespace detail diff --git a/source/detail/serialization/xlsx_producer.cpp b/source/detail/serialization/xlsx_producer.cpp index 743839ac..a0455cef 100644 --- a/source/detail/serialization/xlsx_producer.cpp +++ b/source/detail/serialization/xlsx_producer.cpp @@ -2420,7 +2420,7 @@ void xlsx_producer::write_worksheet(const relationship &rel) if (props.width.is_set()) { double width = (props.width.get() * 7 + 5) / 7; - write_attribute("width", serialize_number_to_string(width)); + write_attribute("width", converter_.serialise(width)); } if (props.best_fit) @@ -2522,7 +2522,7 @@ void xlsx_producer::write_worksheet(const relationship &rel) if (props.height.is_set()) { auto height = props.height.get(); - write_attribute("ht", serialize_number_to_string(height)); + write_attribute("ht", converter_.serialise(height)); } if (props.hidden) @@ -2649,7 +2649,7 @@ void xlsx_producer::write_worksheet(const relationship &rel) case cell::type::number: write_start_element(xmlns, "v"); - write_characters(serialize_number_to_string(cell.value())); + write_characters(converter_.serialise(cell.value())); write_end_element(xmlns, "v"); break; @@ -2886,26 +2886,26 @@ void xlsx_producer::write_worksheet(const relationship &rel) { if (hf.has_odd_even_header(location)) { - odd_header.append(encode_header_footer(hf.odd_header(location), location)); - even_header.append(encode_header_footer(hf.even_header(location), location)); + odd_header.append(encode_header_footer(hf.odd_header(location), location, converter_)); + even_header.append(encode_header_footer(hf.even_header(location), location, converter_)); } if (hf.has_odd_even_footer(location)) { - odd_footer.append(encode_header_footer(hf.odd_footer(location), location)); - even_footer.append(encode_header_footer(hf.even_footer(location), location)); + odd_footer.append(encode_header_footer(hf.odd_footer(location), location, converter_)); + even_footer.append(encode_header_footer(hf.even_footer(location), location, converter_)); } } else { if (hf.has_header(location)) { - odd_header.append(encode_header_footer(hf.header(location), location)); + odd_header.append(encode_header_footer(hf.header(location), location, converter_)); } if (hf.has_footer(location)) { - odd_footer.append(encode_header_footer(hf.footer(location), location)); + odd_footer.append(encode_header_footer(hf.footer(location), location, converter_)); } } @@ -2913,12 +2913,12 @@ void xlsx_producer::write_worksheet(const relationship &rel) { if (hf.has_first_page_header(location)) { - first_header.append(encode_header_footer(hf.first_page_header(location), location)); + first_header.append(encode_header_footer(hf.first_page_header(location), location, converter_)); } if (hf.has_first_page_footer(location)) { - first_footer.append(encode_header_footer(hf.first_page_footer(location), location)); + first_footer.append(encode_header_footer(hf.first_page_footer(location), location, converter_)); } } } @@ -3385,7 +3385,7 @@ void xlsx_producer::write_color(const xlnt::color &color) } if (color.has_tint()) { - write_attribute("tint", serialize_number_to_string(color.tint())); + write_attribute("tint", converter_.serialise(color.tint())); } } diff --git a/source/detail/serialization/xlsx_producer.hpp b/source/detail/serialization/xlsx_producer.hpp index 296025b9..4660f3c2 100644 --- a/source/detail/serialization/xlsx_producer.hpp +++ b/source/detail/serialization/xlsx_producer.hpp @@ -30,6 +30,7 @@ #include #include +#include namespace xml { class serializer; @@ -208,6 +209,7 @@ private: detail::cell_impl *current_cell_; detail::worksheet_impl *current_worksheet_; + detail::number_serialiser converter_; }; } // namespace detail