Skip to content

Commit 6e39730

Browse files
authored
Merge pull request #2 from m7913d/fix-issue714-locale-comma
Correctly copy the null terminator
2 parents 297b331 + 5f2be68 commit 6e39730

5 files changed

Lines changed: 38 additions & 2 deletions

File tree

.circleci/config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ jobs:
2525
type: string
2626
steps:
2727
- checkout
28+
- run: apt-get update -y
29+
- run: apt-get install -y --no-install-recommends locales
30+
- run: sed -i '/^# de_DE /s/^# //' /etc/locale.gen
31+
- run: locale-gen
2832
- run: git submodule update --init --recursive
2933
- run: cmake -D XLNT_CXX_LANG=<< parameters.cxx-ver >> -D STATIC=<< parameters.static >> -D BENCHMARKS=<< parameters.benchmarks >> -D TESTS=ON -D SAMPLES=<< parameters.samples >> -D COVERAGE=<< parameters.coverage >> -D CMAKE_BUILD_TYPE=<< parameters.build-type >> .
3034
- run: cmake --build . -- -j2

include/xlnt/utils/numeric.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,9 @@ class number_serialiser
175175
return d;
176176
}
177177
char buf[30];
178-
assert(s.size() < sizeof(buf));
179-
auto copy_end = std::copy(s.begin(), s.end(), buf);
178+
assert(s.size() + 1 < sizeof(buf));
179+
const char *cstr = s.c_str();
180+
auto copy_end = std::copy(cstr, cstr + s.size() + 1, buf);
180181
convert_pt_to_comma(buf, static_cast<size_t>(copy_end - buf));
181182
double d = strtod(buf, &end_of_convert);
182183
*len_converted = end_of_convert - buf;
4.38 KB
Binary file not shown.

tests/detail/numeric_util_test_suite.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <xlnt/utils/numeric.hpp>
2525
#include <helpers/test_suite.hpp>
26+
#include <cstring>
2627

2728
class numeric_test_suite : public test_suite
2829
{
@@ -36,6 +37,7 @@ class numeric_test_suite : public test_suite
3637
register_test(test_min);
3738
register_test(test_max);
3839
register_test(test_abs);
40+
register_test(test_locale_comma);
3941
}
4042

4143
void test_serialise_number()
@@ -219,5 +221,18 @@ class numeric_test_suite : public test_suite
219221

220222
static_assert(xlnt::detail::abs(-1.23) == 1.23, "constexpr");
221223
}
224+
225+
void test_locale_comma ()
226+
{
227+
struct SetLocale
228+
{
229+
SetLocale() {xlnt_assert(setlocale(LC_ALL, "de_DE") != nullptr);} // If failed, please install de_DE locale to correctly run this test.
230+
~SetLocale() {setlocale(LC_ALL, "C");}
231+
} setLocale;
232+
233+
xlnt::detail::number_serialiser serialiser;
234+
xlnt_assert(serialiser.deserialise("1.99999999") == 1.99999999);
235+
xlnt_assert(serialiser.deserialise("1.1") == 1.1);
236+
}
222237
};
223238
static numeric_test_suite x;

tests/workbook/serialization_test_suite.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class serialization_test_suite : public test_suite
7373
register_test(test_Issue503_external_link_load);
7474
register_test(test_formatting);
7575
register_test(test_active_sheet);
76+
register_test(test_locale_comma);
7677
}
7778

7879
bool workbook_matches_file(xlnt::workbook &wb, const xlnt::path &file)
@@ -808,6 +809,21 @@ class serialization_test_suite : public test_suite
808809
wb.load(path_helper::test_file("20_active_sheet.xlsx"));
809810
xlnt_assert_equals(wb.active_sheet(), wb[2]);
810811
}
812+
813+
void test_locale_comma ()
814+
{
815+
struct SetLocale
816+
{
817+
SetLocale() {xlnt_assert(setlocale(LC_ALL, "de_DE") != nullptr);} // If failed, please install de_DE locale to correctly run this test.
818+
~SetLocale() {setlocale(LC_ALL, "C");}
819+
} setLocale;
820+
821+
xlnt::workbook wb;
822+
wb.load(path_helper::test_file("Issue714_local_comma.xlsx"));
823+
auto ws = wb.active_sheet();
824+
xlnt_assert_equals(ws.cell("A1").value<double>(), 1.9999999999);
825+
xlnt_assert_equals(ws.cell("A2").value<double>(), 1.1);
826+
}
811827
};
812828

813829
static serialization_test_suite x;

0 commit comments

Comments
 (0)