Vince's CSV Parser
Loading...
Searching...
No Matches
csv_row.cpp
Go to the documentation of this file.
1
5#include <cassert>
6#include <functional>
7#include "csv_row.hpp"
8#include "csv_exceptions.hpp"
9
10namespace csv {
11 namespace internals {
12 CSV_INLINE csv::string_view get_trimmed(csv::string_view sv, const WhitespaceMap& ws_flags) noexcept
13 {
14 // Lazy trim only when requested
15 size_t start = 0;
16 while (start < sv.size() && ws_flags[sv[start] + CHAR_OFFSET]) {
17 ++start;
18 }
19
20 size_t end = sv.size();
21 while (end > start && ws_flags[sv[end - 1] + CHAR_OFFSET]) {
22 --end;
23 }
24
25 return sv.substr(start, end - start);
26 }
27 }
28
30 return this->make_field(n, this->data);
31 }
32
34 auto & col_names = this->data->col_names;
35 auto col_pos = col_names->index_of(col_name);
36 if (col_pos > -1) {
37 return this->operator[](col_pos);
38 }
39
40 internals::throw_column_not_found(col_name);
41 }
42 CSV_INLINE CSVRow::operator std::vector<std::string>() const {
43 std::vector<std::string> ret;
44 for (size_t i = 0; i < size(); i++)
45 ret.push_back(std::string(this->get_field(i)));
46
47 return ret;
48 }
49
51 if (!data) return csv::string_view();
52 const csv::string_view full = data->data;
53 if (data_start >= full.size()) return csv::string_view();
54
55 if (data_end != (std::numeric_limits<size_t>::max)()
56 && data_end >= data_start
57 && data_end <= full.size()) {
58 return full.substr(data_start, data_end - data_start);
59 }
60
61 const size_t end = full.find('\n', data_start);
62 const size_t len = (end == csv::string_view::npos)
63 ? (full.size() - data_start)
64 : (end - data_start);
65 return full.substr(data_start, len);
66 }
67
69 CSV_INLINE std::unordered_map<std::string, std::string> CSVRow::to_unordered_map() const {
70 std::unordered_map<std::string, std::string> row_map;
71 row_map.reserve(this->size());
72
73 for (size_t i = 0; i < this->size(); i++) {
74 auto col_name = (*this->data->col_names)[i];
75 row_map[col_name] = this->operator[](i).get<std::string>();
76 }
77
78 return row_map;
79 }
80
82 CSV_INLINE std::unordered_map<std::string, std::string> CSVRow::to_unordered_map(
83 const std::vector<std::string>& subset
84 ) const {
85 std::unordered_map<std::string, std::string> row_map;
86 row_map.reserve(subset.size());
87
88 for (const auto& col_name : subset)
89 row_map[col_name] = this->operator[](col_name).get<std::string>();
90
91 return row_map;
92 }
93
94 CSV_INLINE csv::string_view CSVRow::get_field(size_t index) const
95 {
96 return this->get_field_impl(index, this->data);
97 }
98
99 CSV_INLINE csv::string_view CSVRow::get_field_safe(size_t index, internals::RawCSVDataPtr _data) const
100 {
101 return this->get_field_impl(index, _data);
102 }
103
104 CSV_INLINE CSVField CSVRow::make_field(size_t index, const internals::RawCSVDataPtr& _data) const
105 {
106 const csv::string_view field = this->get_field_impl(index, _data);
107 const size_t field_index = this->fields_start + index;
108 if (_data->has_field_scalars() && field_index < _data->field_scalars.size()) {
109 return CSVField(field, _data->field_scalars[field_index]);
110 }
111
112 return CSVField(field);
113 }
114
115 CSV_INLINE bool CSVField::try_parse_decimal(long double& dVal, const char decimalSymbol) {
116 // If field has already been parsed to empty, no need to do it aagin:
117 if (this->type_ == DataType::CSV_NULL)
118 return false;
119
120 if (this->type_ == DataType::UNKNOWN)
121 this->get_value();
122
123 if (this->type_ == DataType::CSV_NULL)
124 return false;
125
126 if (this->type_ == DataType::CSV_STRING || this->type_ == DataType::CSV_DOUBLE) {
127 double parsed_value = 0;
128 if (!classify_scalar::parse_float(this->sv.data(), this->sv.data() + this->sv.size(), parsed_value, decimalSymbol)) {
129 if (this->type_ == DataType::CSV_DOUBLE)
130 this->type_ = DataType::CSV_STRING;
131 return false;
132 }
133
134 this->cache_parsed_value(DataType::CSV_DOUBLE, parsed_value);
135 }
136
137 // Integral types are not affected by decimalSymbol and need not be parsed again
138
139 // Either we already had an integral type before, or we we just got any numeric type now.
140 if (this->type_ >= DataType::CSV_INT8 && this->type_ <= DataType::CSV_DOUBLE) {
141 dVal = this->numeric_value_as_long_double();
142 return true;
143 }
144
145 // CSV_NULL or CSV_STRING, not numeric
146 return false;
147 }
148
149 CSV_INLINE bool CSVField::try_parse_timestamp(std::uint64_t& out) noexcept {
150 if (this->type_ == DataType::UNKNOWN)
151 this->get_value();
152
153 if (this->type_ == DataType::CSV_TIMESTAMP) {
154 out = this->value_.timestamp;
155 return true;
156 }
157
158 if (this->stores_integral() && this->value_.integer >= 0) {
159 out = static_cast<std::uint64_t>(this->value_.integer);
160 return true;
161 }
162
163 return false;
164 }
165
166#ifdef _MSC_VER
167#pragma region CSVRow Iterator
168#endif
171 return CSVRow::iterator(this, 0);
172 }
173
180 return CSVRow::iterator(this, (int)this->size());
181 }
182
183 CSV_INLINE CSVRow::reverse_iterator CSVRow::rbegin() const noexcept {
184 return std::reverse_iterator<CSVRow::iterator>(this->end());
185 }
186
187 CSV_INLINE CSVRow::reverse_iterator CSVRow::rend() const {
188 return std::reverse_iterator<CSVRow::iterator>(this->begin());
189 }
190
191 CSV_INLINE CSV_NON_NULL(2)
192 CSVRow::iterator::iterator(const CSVRow* _reader, int _i)
193 : daddy(_reader), data(_reader->data), i(_i) {
194 if (_i < (int)this->daddy->size())
195 this->field = std::make_shared<CSVField>(
196 this->daddy->make_field(_i, this->data));
197 else
198 this->field = nullptr;
199 }
200
201 CSV_INLINE CSVRow::iterator::reference CSVRow::iterator::operator*() const {
202 return *(this->field.get());
203 }
204
205 CSV_INLINE CSVRow::iterator::pointer CSVRow::iterator::operator->() const {
206 return this->field;
207 }
208
209 CSV_INLINE CSVRow::iterator& CSVRow::iterator::operator++() {
210 // Pre-increment operator
211 this->i++;
212 if (this->i < (int)this->daddy->size())
213 this->field = std::make_shared<CSVField>(
214 this->daddy->make_field(i, this->data));
215 else // Reached the end of row
216 this->field = nullptr;
217 return *this;
218 }
219
220 CSV_INLINE CSVRow::iterator CSVRow::iterator::operator++(int) {
221 // Post-increment operator
222 auto temp = *this;
223 this->operator++();
224 return temp;
225 }
226
227 CSV_INLINE CSVRow::iterator& CSVRow::iterator::operator--() {
228 // Pre-decrement operator
229 this->i--;
230 this->field = std::make_shared<CSVField>(
231 this->daddy->make_field(this->i, this->data));
232 return *this;
233 }
234
235 CSV_INLINE CSVRow::iterator CSVRow::iterator::operator--(int) {
236 // Post-decrement operator
237 auto temp = *this;
238 this->operator--();
239 return temp;
240 }
241
242 CSV_INLINE CSVRow::iterator CSVRow::iterator::operator+(difference_type n) const {
243 // Allows for iterator arithmetic
244 return CSVRow::iterator(this->daddy, i + (int)n);
245 }
246
247 CSV_INLINE CSVRow::iterator CSVRow::iterator::operator-(difference_type n) const {
248 // Allows for iterator arithmetic
249 return CSVRow::iterator::operator+(-n);
250 }
251
252 CSV_INLINE CSVRow::iterator& CSVRow::iterator::operator+=(difference_type n) {
253 *this = *this + n;
254 return *this;
255 }
256
257 CSV_INLINE CSVRow::iterator& CSVRow::iterator::operator-=(difference_type n) {
258 *this = *this - n;
259 return *this;
260 }
261
262 CSV_INLINE CSVRow::iterator::difference_type CSVRow::iterator::operator-(const iterator& other) const noexcept {
263 return this->i - other.i;
264 }
265#ifdef _MSC_VER
266#pragma endregion CSVRow Iterator
267#endif
268}
Data type representing individual CSV values.
Definition csv_row.hpp:114
bool try_parse_timestamp(std::uint64_t &out) noexcept
Parse this field as Unix milliseconds.
Definition csv_row.cpp:149
bool try_parse_decimal(long double &dVal, const char decimalSymbol='.')
Attempts to parse a decimal (or integer) value using the given symbol, returning true if the value is...
Definition csv_row.cpp:115
T get()
Returns the value casted to the requested type, performing type checking before.
Definition csv_row.hpp:180
A random access iterator over the contents of a CSV row.
Definition csv_row.hpp:643
iterator end() const noexcept
Return an iterator pointing to just after the end of the CSVRow.
Definition csv_row.cpp:179
std::reverse_iterator< iterator > reverse_iterator
A reverse iterator over the contents of a CSVRow.
Definition csv_row.hpp:686
csv::string_view raw_str() const noexcept
Return a string_view of the raw bytes of this row as they appear in the underlying parse buffer,...
Definition csv_row.cpp:50
std::unordered_map< std::string, std::string > to_unordered_map() const
Convert this CSVRow into an unordered map.
Definition csv_row.cpp:69
CONSTEXPR size_t size() const noexcept
Return the number of fields in this row.
Definition csv_row.hpp:565
CSVField operator[](size_t n) const
Return the field at n.
Definition csv_row.cpp:29
iterator begin() const
Return an iterator pointing to the first field.
Definition csv_row.cpp:170
#define CSV_INLINE
Helper macro which should be #defined as "inline" in the single header version.
Definition common.hpp:33
Shared exception message templates and throw helpers.
Defines the data type used for storing information about a CSV row.
The all encompassing namespace.
@ CSV_TIMESTAMP
Timestamp value.
@ CSV_DOUBLE
Floating point value.
@ CSV_NULL
Empty string.
@ CSV_INT8
8-bit integer
@ CSV_STRING
Non-scalar string.
constexpr unsigned CHAR_OFFSET
Offset to convert char into array index.
Definition common.hpp:491
std::string_view string_view
The string_view class used by this library.
Definition common.hpp:176