csv-parser 1.1.0
Csv::Parser (csv-parser)
Loading...
Searching...
No Matches
csv_cell_string_buffer.h
Go to the documentation of this file.
1/**************************************************************************
2Copyright: (C) 2021 - 2025 Alexander Shaduri
3License: Zlib
4***************************************************************************/
5
6#ifndef CSV_CELL_STRING_BUFFER_H
7#define CSV_CELL_STRING_BUFFER_H
8
9#include <string_view>
10#include <stdexcept>
11#include <array>
12
13#include "csv_util.h"
14
22namespace Csv {
23
24
25
28template<std::size_t Size>
30 public:
31
37 constexpr inline explicit CellStringBuffer(std::string_view cell, bool has_escaped_quotes);
38
40 [[nodiscard]] constexpr std::string_view getStringView() const;
41
43 [[nodiscard]] constexpr std::size_t getBufferSize() const noexcept;
44
45
46 private:
47
48 struct Buffer {
49 std::array<char, Size> buffer = { };
50 std::size_t size = 0;
51 };
52
55 [[nodiscard]] constexpr static Buffer prepareBuffer(std::string_view input, bool has_escaped_quotes);
56
58 [[nodiscard]] constexpr static Buffer cleanString(std::string_view input);
59
60 Buffer buffer_;
61};
62
63
64
65// ----- Implementation
66
67
68
69template<std::size_t Size>
70constexpr CellStringBuffer<Size>::CellStringBuffer(std::string_view cell, bool has_escaped_quotes)
71 : buffer_(prepareBuffer(cell, has_escaped_quotes))
72{ }
73
74
75
76template<std::size_t Size>
77constexpr std::string_view CellStringBuffer<Size>::getStringView() const
78{
79 return {buffer_.buffer.data(), buffer_.size};
80}
81
82
83
84template<std::size_t Size>
85constexpr std::size_t CellStringBuffer<Size>::getBufferSize() const noexcept
86{
87 return Size;
88}
89
90
91
92template<std::size_t Size>
94 std::string_view input, bool has_escaped_quotes)
95{
96 if (has_escaped_quotes) {
97 // cleanString will throw if the buffer is too small
98 return cleanString(input);
99 }
100
101 if (Size < input.size()) {
102 throw std::out_of_range("Insufficient buffer size");
103 }
104 std::array<char, Size> buffer = { };
105 for (std::size_t pos = 0; pos < input.size(); ++pos) {
106 buffer[pos] = input[pos];
107 }
108 return Buffer{buffer, input.size()};
109}
110
111
112
113template<std::size_t Size>
114constexpr typename CellStringBuffer<Size>::Buffer CellStringBuffer<Size>::cleanString(std::string_view input)
115{
116 if (Size < getCleanStringSize(input)) {
117 throw std::out_of_range("Insufficient buffer size");
118 }
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;
124 ++output_pos;
125 if (c == '\"' && (input_pos + 1) < input.size() && input[input_pos + 1] == '\"') {
126 ++input_pos;
127 }
128 }
129 return {buffer, output_pos};
130}
131
132
133
134
135} // end ns
136
137
138
139#endif
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