From 33dd551598fc63c463c4e1335acfb6c487b07c4c Mon Sep 17 00:00:00 2001 From: Dries Mys Date: Sat, 23 Sep 2023 19:33:10 +0200 Subject: [PATCH] Correctly copy the null terminator Fixes #714 --- include/xlnt/utils/numeric.hpp | 3 ++- tests/data/Issue714_local_comma.xlsx | Bin 0 -> 4481 bytes tests/detail/numeric_util_test_suite.cpp | 15 +++++++++++++++ tests/workbook/serialization_test_suite.cpp | 16 ++++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 tests/data/Issue714_local_comma.xlsx diff --git a/include/xlnt/utils/numeric.hpp b/include/xlnt/utils/numeric.hpp index 901291338..30dd45a40 100644 --- a/include/xlnt/utils/numeric.hpp +++ b/include/xlnt/utils/numeric.hpp @@ -176,7 +176,8 @@ class number_serialiser } char buf[30]; assert(s.size() < sizeof(buf)); - auto copy_end = std::copy(s.begin(), s.end(), buf); + const char *cstr = s.c_str(); + auto copy_end = std::copy(cstr, cstr + s.size() + 1, buf); convert_pt_to_comma(buf, static_cast(copy_end - buf)); double d = strtod(buf, &end_of_convert); *len_converted = end_of_convert - buf; diff --git a/tests/data/Issue714_local_comma.xlsx b/tests/data/Issue714_local_comma.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..8d2d81f76b052386900ec07f8ca2647a2eceb7b2 GIT binary patch literal 4481 zcmaJ^2Q=K@*2NeldbH?0dQWtbgb=;=5WS2t4B@YiHi&KzJ$i&75kYie^p-*N8bl{L zA$%kGzK@spUe0f=DQn$xf9vdX?!J3#C<8Fb&~R{Y(DLj>_0g^c9_sf~h_#~|Ki}nN zDSSW)AV?gx?H3W}<5@*6XI0S<{yF`zUqDLlxw&Yn0M#=;pI{uLq5|w#MgP4%QQ5TV zn>Gg#843~d;9?{0sF<@`K6zP$N9!~V4*Gp8{d!8QM3&vf!wAQHMz}Njdm4eUCz>q= zrV?Mx3TSJ>tfsAeLYz#V0xqmp4#SHMXY6K9qxHg$LXzJVt;>gbE}CM)cE@_U~6sNryo7?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ite.cpp index cf420a4a1..7c33817d1 100644 --- a/tests/detail/numeric_util_test_suite.cpp +++ b/tests/detail/numeric_util_test_suite.cpp @@ -23,6 +23,7 @@ #include #include +#include class numeric_test_suite : public test_suite { @@ -36,6 +37,7 @@ class numeric_test_suite : public test_suite register_test(test_min); register_test(test_max); register_test(test_abs); + register_test(test_locale_comma); } void test_serialise_number() @@ -219,5 +221,18 @@ class numeric_test_suite : public test_suite static_assert(xlnt::detail::abs(-1.23) == 1.23, "constexpr"); } + + void test_locale_comma () + { + struct SetLocale + { + SetLocale() {xlnt_assert(setlocale(LC_ALL, "de_DE") != nullptr);} // If failed, please install de_DE locale to correctly run this test. + ~SetLocale() {setlocale(LC_ALL, "C");} + } setLocale; + + xlnt::detail::number_serialiser serialiser; + xlnt_assert(serialiser.deserialise("1.99999999") == 1.99999999); + xlnt_assert(serialiser.deserialise("1.1") == 1.1); + } }; static numeric_test_suite x; diff --git a/tests/workbook/serialization_test_suite.cpp b/tests/workbook/serialization_test_suite.cpp index ca6383a18..d82689eee 100644 --- a/tests/workbook/serialization_test_suite.cpp +++ b/tests/workbook/serialization_test_suite.cpp @@ -73,6 +73,7 @@ class serialization_test_suite : public test_suite register_test(test_Issue503_external_link_load); register_test(test_formatting); register_test(test_active_sheet); + register_test(test_locale_comma); } bool workbook_matches_file(xlnt::workbook &wb, const xlnt::path &file) @@ -808,6 +809,21 @@ class serialization_test_suite : public test_suite wb.load(path_helper::test_file("20_active_sheet.xlsx")); xlnt_assert_equals(wb.active_sheet(), wb[2]); } + + void test_locale_comma () + { + struct SetLocale + { + SetLocale() {xlnt_assert(setlocale(LC_ALL, "de_DE") != nullptr);} // If failed, please install de_DE locale to correctly run this test. + ~SetLocale() {setlocale(LC_ALL, "C");} + } setLocale; + + xlnt::workbook wb; + wb.load(path_helper::test_file("Issue714_local_comma.xlsx")); + auto ws = wb.active_sheet(); + xlnt_assert_equals(ws.cell("A1").value(), 1.9999999999); + xlnt_assert_equals(ws.cell("A2").value(), 1.1); + } }; static serialization_test_suite x;