6#ifndef CSV_CELL_STRING_BUFFER_H
7#define CSV_CELL_STRING_BUFFER_H
28template<std::
size_t Size>
37 constexpr inline explicit CellStringBuffer(std::string_view cell,
bool has_escaped_quotes);
40 [[nodiscard]]
constexpr std::string_view
getStringView()
const;
43 [[nodiscard]]
constexpr std::size_t
getBufferSize()
const noexcept;
49 std::array<char, Size> buffer = { };
55 [[nodiscard]]
constexpr static Buffer prepareBuffer(std::string_view input,
bool has_escaped_quotes);
58 [[nodiscard]]
constexpr static Buffer cleanString(std::string_view input);
69template<std::
size_t Size>
71 : buffer_(prepareBuffer(cell, has_escaped_quotes))
76template<std::
size_t Size>
79 return {buffer_.buffer.data(), buffer_.size};
84template<std::
size_t Size>
92template<std::
size_t Size>
94 std::string_view input,
bool has_escaped_quotes)
96 if (has_escaped_quotes) {
101 if (Size < input.size()) {
102 throw std::out_of_range(
"Insufficient buffer size");
104 std::array<char, Size> buffer = { };
105 for (std::size_t pos = 0; pos < input.size(); ++pos) {
106 buffer[pos] = input[pos];
108 return Buffer{buffer, input.size()};
113template<std::
size_t Size>
114constexpr typename CellStringBuffer<Size>::Buffer CellStringBuffer<Size>::cleanString(std::string_view input)
117 throw std::out_of_range(
"Insufficient buffer size");
119 std::array<char, Size> buffer = { };
120 std::size_t output_pos = 0;
121 for (std::size_t input_pos = 0; input_pos < input.size(); ++input_pos) {
122 char c = input[input_pos];
123 buffer[output_pos] = c;
125 if (c ==
'\"' && (input_pos + 1) < input.size() && input[input_pos + 1] ==
'\"') {
129 return {buffer, output_pos};
A helper class for compile-time buffer construction and string unescaping.
Definition csv_cell_string_buffer.h:29
constexpr std::size_t getBufferSize() const noexcept
Return buffer size, as determined by the template parameter.
Definition csv_cell_string_buffer.h:85
constexpr CellStringBuffer(std::string_view cell, bool has_escaped_quotes)
Constructor.
Definition csv_cell_string_buffer.h:70
constexpr std::string_view getStringView() const
Return a string view to the stored data.
Definition csv_cell_string_buffer.h:77
Utility functions used by the library.
constexpr std::size_t getCleanStringSize(std::string_view uncollapsed_view)
Get size of a cleaned-up string given the uncollapsed string.
Definition csv_util.h:104
std::string cleanString(std::string_view view)
Unescape a string - collapse every occurrence of 2 consecutive double-quotes to one.
Definition csv_util.h:88