Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
os: [windows-latest, ubuntu-latest]
build_type: [Release]
c_compiler: [gcc, cl]
cxx_standard: [17, 20]
include:
- os: windows-latest
c_compiler: cl
Expand Down Expand Up @@ -57,6 +58,7 @@ jobs:
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: >
cmake -B ${{ steps.strings.outputs.build-output-dir }}
-DCSV_CXX_STANDARD=${{ matrix.cxx_standard }}
-DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }}
-DCMAKE_C_COMPILER=${{ matrix.c_compiler }}
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
Expand Down
4 changes: 4 additions & 0 deletions include/internal/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ namespace csv {

#define STATIC_ASSERT(x) static_assert(x, "Assertion failed")

#if (defined(CMAKE_CXX_STANDARD) && CMAKE_CXX_STANDARD == 20) || __cplusplus >= 202002L
#define CSV_HAS_CXX20
#endif

#if (defined(CMAKE_CXX_STANDARD) && CMAKE_CXX_STANDARD == 17) || __cplusplus >= 201703L
#define CSV_HAS_CXX17
#endif
Expand Down
2 changes: 2 additions & 0 deletions include/internal/csv_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ namespace csv {

/** Access the CSVRow held by the iterator */
CONSTEXPR_14 reference operator*() { return this->row; }
CONSTEXPR_14 reference operator*() const { return const_cast<reference>(this->row); }

/** Return a pointer to the CSVRow the iterator has stopped at */
CONSTEXPR_14 pointer operator->() { return &(this->row); }
CONSTEXPR_14 pointer operator->() const { return const_cast<pointer>(&(this->row)); }

iterator& operator++(); /**< Pre-increment iterator */
iterator operator++(int); /**< Post-increment iterator */
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ target_sources(csv_test
test_raw_csv_data.cpp
test_round_trip.cpp
test_csv_delimeter.cpp
test_csv_ranges.cpp
)
target_link_libraries(csv_test csv)
target_link_libraries(csv_test Catch2::Catch2WithMain)
Expand Down
57 changes: 57 additions & 0 deletions tests/test_csv_ranges.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <catch2/catch_all.hpp>
#include <csv.hpp>

#ifdef CSV_HAS_CXX20
#include <ranges>

TEST_CASE("CSVReader C++20 Ranges Compatibility", "[ranges][cxx20]") {
SECTION("CSVReader works with std::ranges::distance") {
std::stringstream ss("A,B,C\n1,2,3\n4,5,6\n7,8,9");
csv::CSVReader reader(ss);

auto count = std::ranges::distance(reader);
REQUIRE(count == 3);
}

SECTION("CSVReader works with std::views") {
std::stringstream ss("A,B,C\n1,2,3\n4,5,6\n7,8,9\n10,11,12");
csv::CSVReader reader(ss);

auto filtered = reader |
std::views::filter([](const csv::CSVRow &row) {
return !row.empty() && row[0].get<int>() > 5;
});

int filtered_count = 0;
for (const auto &row : filtered) {
filtered_count++;
int val = row[0].get<int>();
REQUIRE(val > 5);
}
REQUIRE(filtered_count == 2); // rows with 7 and 10
}

SECTION("CSVReader iterator satisfies input_range requirements") {
std::stringstream ss("A,B\n1,2\n3,4");
csv::CSVReader reader(ss);

auto it = reader.begin();
auto end = reader.end();

static_assert(std::input_iterator<decltype(it)>);
static_assert(std::ranges::range<csv::CSVReader>);
static_assert(std::ranges::input_range<csv::CSVReader>);
static_assert(std::sentinel_for<decltype(end), decltype(it)>);

REQUIRE(it != end);
auto row = *it;
REQUIRE(row.size() == 2);

++it;
REQUIRE(it != end);

++it;
REQUIRE(it == end);
}
}
#endif