From 68a21c5a5952913d6192be747efd245887bc36cc Mon Sep 17 00:00:00 2001 From: Alexis Placet <2400067+Alex-PLACET@users.noreply.github.com> Date: Thu, 30 Apr 2026 13:24:54 +0200 Subject: [PATCH 1/3] Fix substring calculation in CSV cell parsing and add lexical_cast for signed and unsigned char --- include/xtensor/io/xcsv.hpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/include/xtensor/io/xcsv.hpp b/include/xtensor/io/xcsv.hpp index 080ccaf54..2fd347745 100644 --- a/include/xtensor/io/xcsv.hpp +++ b/include/xtensor/io/xcsv.hpp @@ -66,7 +66,7 @@ namespace xt } size_t last = cell.find_last_not_of(' '); - return cell.substr(first, last == std::string::npos ? cell.size() : last + 1); + return cell.substr(first, last == std::string::npos ? cell.size() : last - first + 1); } template <> @@ -93,6 +93,18 @@ namespace xt return std::stoi(cell); } + template <> + inline signed char lexical_cast(const std::string& cell) + { + return static_cast(std::stoi(cell)); + } + + template <> + inline unsigned char lexical_cast(const std::string& cell) + { + return static_cast(std::stoul(cell)); + } + template <> inline long lexical_cast(const std::string& cell) { From 8e4cee3a47f41bf9e1584304ef23c8bfccb3d893 Mon Sep 17 00:00:00 2001 From: Alexis Placet <2400067+Alex-PLACET@users.noreply.github.com> Date: Wed, 6 May 2026 13:31:14 +0200 Subject: [PATCH 2/3] Fix drop function to handle decayed types and add unit test for 1D array drop --- include/xtensor/views/xslice.hpp | 2 +- test/test_xview.cpp | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/include/xtensor/views/xslice.hpp b/include/xtensor/views/xslice.hpp index 3b4d3a6fb..775123934 100644 --- a/include/xtensor/views/xslice.hpp +++ b/include/xtensor/views/xslice.hpp @@ -484,7 +484,7 @@ namespace xt template inline auto drop(T&& indices) { - if constexpr (xtl::is_integral::value) + if constexpr (xtl::is_integral>::value) { using slice_type = xdrop_slice; using container_type = typename slice_type::container_type; diff --git a/test/test_xview.cpp b/test/test_xview.cpp index c98993bf9..90647ba85 100644 --- a/test/test_xview.cpp +++ b/test/test_xview.cpp @@ -1861,4 +1861,16 @@ namespace xt XT_ASSERT_THROW(const auto col = xt::col(arr, 0), std::invalid_argument); } + + TEST(xview, drop_on_1dim_array) + { + const auto my_array = xt::xtensor({1, 2, 3}); + + xt::view(my_array, xt::drop(1)); + EXPECT_EQ(my_array, (xt::xtensor{1, 3})); + + const size_t index = 1; + xt::view(my_array, xt::drop(index)); + EXPECT_EQ(my_array, (xt::xtensor{1})); + } } From 31d19816f8cb1173508fe37cac425780c6805237 Mon Sep 17 00:00:00 2001 From: Alexis Placet <2400067+Alex-PLACET@users.noreply.github.com> Date: Wed, 6 May 2026 15:12:43 +0200 Subject: [PATCH 3/3] fix --- test/test_xoperation.cpp | 23 +++++++++++++++++++++++ test/test_xview.cpp | 17 ++++++++++++----- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/test/test_xoperation.cpp b/test/test_xoperation.cpp index fa97a0946..21a9c77bd 100644 --- a/test/test_xoperation.cpp +++ b/test/test_xoperation.cpp @@ -858,6 +858,29 @@ namespace xt EXPECT_EQ(expected1, res3); EXPECT_EQ(expected2, res4); } + + TEST_CASE("divide_4d") + { + using T4 = xt::xtensor; + using shape_type = typename T4::shape_type; + + shape_type shape = {2, 3, 2, 2}; + T4 a(shape, 4.5f); + T4 b(shape, 1.3f); + + EXPECT_EQ((a / b)(0, 0, 0, 0), a(0, 0, 0, 0) / b(0, 0, 0, 0)); + + float sb = 1.2f; + EXPECT_EQ((a / sb)(0, 0, 0, 0), a(0, 0, 0, 0) / sb); + + float sa = 4.6f; + EXPECT_EQ((sa / b)(0, 0, 0, 0), sa / b(0, 0, 0, 0)); + + // self-divide assignment: a = a / b (no zeros in b) + auto a_before = a(1, 2, 1, 1); + a = a / b; + EXPECT_EQ(a(1, 2, 1, 1), a_before / b(1, 2, 1, 1)); + } } #undef XOPERATION_TEST_TYPES diff --git a/test/test_xview.cpp b/test/test_xview.cpp index 90647ba85..18449b742 100644 --- a/test/test_xview.cpp +++ b/test/test_xview.cpp @@ -1864,13 +1864,20 @@ namespace xt TEST(xview, drop_on_1dim_array) { - const auto my_array = xt::xtensor({1, 2, 3}); + auto my_array = xt::xtensor({1, 2, 3}); - xt::view(my_array, xt::drop(1)); - EXPECT_EQ(my_array, (xt::xtensor{1, 3})); + // drop(1) creates a view excluding index 1 + auto v1 = xt::view(my_array, xt::drop(1)); + EXPECT_EQ(v1, (xt::xtensor{1, 3})); + // Assign through the drop view + xt::view(my_array, xt::drop(1)) = 0.; + EXPECT_EQ(my_array, (xt::xtensor{0, 2, 0})); + + // Reset, then test drop with a variable (the original compilation issue) + my_array = xt::xtensor({1, 2, 3}); const size_t index = 1; - xt::view(my_array, xt::drop(index)); - EXPECT_EQ(my_array, (xt::xtensor{1})); + auto v2 = xt::view(my_array, xt::drop(index)); + EXPECT_EQ(v2, (xt::xtensor{1, 3})); } }