Skip to content

Commit

Permalink
Added CellStringReference::getRequiredBufferSize() to make compile-ti…
Browse files Browse the repository at this point in the history
…me buffer creation easier.
  • Loading branch information
ashaduri committed Jan 4, 2024
1 parent 2f32771 commit 17ef55e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ static_assert(matrix[1][1].getOriginalStringView() == "6"sv);
// buffer to place the clean string inside. The buffer size has to be at least that
// of an uncollapsed string value.
// If the buffer is too small, the code will simply not compile.
constexpr auto buffer_size = R"(with ""quote inside)"sv.size(); // uncollapsed size
constexpr auto buffer_size = matrix[0][1].getRequiredBufferSize(); // uncollapsed size
constexpr auto buffer = matrix[0][1].getCleanStringBuffer<buffer_size>();
static_assert(buffer.getStringView() == R"(with "quote inside)"sv);
```
Expand Down
24 changes: 20 additions & 4 deletions csv_parser/csv_cell.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,11 @@ class CellStringReference {
/// Reserving additional character for terminating null is not required.
/// \return invalid buffer if BufferSize is too small.
template<std::size_t BufferSize>
[[nodiscard]] constexpr CellStringBuffer<BufferSize> getCleanStringBuffer() const
{
return CellStringBuffer<BufferSize>(value_, has_escaped_quotes_);
}
[[nodiscard]] constexpr CellStringBuffer<BufferSize> getCleanStringBuffer() const;

/// Get required buffer size to use as getCleanStringBuffer()'s template argument.
/// This function is useful in constexpr context.
[[nodiscard]] constexpr std::size_t getRequiredBufferSize() const;


private:
Expand Down Expand Up @@ -582,6 +583,21 @@ std::string CellStringReference::getCleanString()



template<std::size_t BufferSize>
constexpr CellStringBuffer<BufferSize> CellStringReference::getCleanStringBuffer() const
{
return CellStringBuffer<BufferSize>(value_, has_escaped_quotes_);
}



constexpr std::size_t CellStringReference::getRequiredBufferSize() const
{
return value_.size();
}





CellStringValue::CellStringValue(std::string_view cell, CellTypeHint hint)
Expand Down
8 changes: 8 additions & 0 deletions examples/example_compiletime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ NaN, -Inf
static_assert(matrix[0][2].getOriginalStringView() == "NaN"sv);
static_assert(matrix[1][2].getOriginalStringView() == " -Inf"sv);

// To support consecutive double-quote collapsing at compile-time, allocate a compile-time
// buffer to place the clean string inside. The buffer size has to be at least that
// of an uncollapsed string value.
// If the buffer is too small, the code will simply not compile.
constexpr auto buffer_size = matrix[1][1].getRequiredBufferSize(); // uncollapsed size
constexpr auto buffer = matrix[1][1].getCleanStringBuffer<buffer_size>();
static_assert(buffer.getStringView() == R"(with "quote inside)"sv);

return EXIT_SUCCESS;
}

Expand Down
2 changes: 1 addition & 1 deletion examples/readme_examples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ License: 0BSD (Zero-Clause BSD)
// buffer to place the clean string inside. The buffer size has to be at least that
// of an uncollapsed string value.
// If the buffer is too small, the code will simply not compile.
constexpr auto buffer_size = R"(with ""quote inside)"sv.size(); // uncollapsed size
constexpr auto buffer_size = matrix[0][1].getRequiredBufferSize(); // uncollapsed size
constexpr auto buffer = matrix[0][1].getCleanStringBuffer<buffer_size>();
static_assert(buffer.getStringView() == R"(with "quote inside)"sv);
}
Expand Down
23 changes: 18 additions & 5 deletions tests/test_csv_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,11 +402,24 @@ R"(abc, "def"
static_assert(matrix[0][1].getOriginalStringView() == R"(with ""quote inside)"sv);
static_assert(matrix[1][1].getOriginalStringView() == "6"sv);

constexpr auto buffer_size = R"(with ""quote inside)"sv.size();
constexpr auto buffer = matrix[0][1].getCleanStringBuffer<buffer_size>();
static_assert(buffer.getStringView() == R"(with "quote inside)"sv);
static_assert(buffer.isValid());
static_assert(buffer.getOptionalStringView().has_value());
{
// constexpr auto buffer_size = R"(with ""quote inside)"sv.size();
constexpr auto buffer_size = matrix[0][1].getRequiredBufferSize();
static_assert(buffer_size == 19);

constexpr auto buffer = matrix[0][1].getCleanStringBuffer<buffer_size>();
static_assert(buffer.getStringView() == R"(with "quote inside)"sv);
static_assert(buffer.isValid());
static_assert(buffer.getOptionalStringView().has_value());
}

{
constexpr auto buffer_size = 1024; // large buffer size
constexpr auto buffer = matrix[0][1].getCleanStringBuffer<buffer_size>();
static_assert(buffer.getStringView() == R"(with "quote inside)"sv);
static_assert(buffer.isValid());
static_assert(buffer.getOptionalStringView().has_value());
}

// These will fail to compile due to small buffer size
// constexpr auto small_buffer_size = "with \"\"quote inside"sv.size() - 1;
Expand Down

0 comments on commit 17ef55e

Please sign in to comment.