Vince's CSV Parser
Loading...
Searching...
No Matches
csv::CSVRow Class Reference

Data structure for representing CSV rows. More...

#include <csv_row.hpp>

Classes

class  iterator
 A random access iterator over the contents of a CSV row. More...
 

Public Types

using reverse_iterator = std::reverse_iterator< iterator >
 A reverse iterator over the contents of a CSVRow.
 

Public Member Functions

 CSVRow (internals::RawCSVDataPtr _data)
 Construct a CSVRow view over parsed row storage.
 
 CSVRow (internals::RawCSVDataPtr _data, size_t _data_start, size_t _field_bounds)
 
 CSVRow (internals::RawCSVDataPtr _data, size_t _data_start, size_t _field_bounds, size_t _row_length)
 
CONSTEXPR bool empty () const noexcept
 Indicates whether row is empty or not.
 
CONSTEXPR size_t size () const noexcept
 Return the number of fields in this row.
 
Value Retrieval
CSVField operator[] (size_t n) const
 Return a CSVField object corrsponding to the nth value in the row.
 
CSVField operator[] (csv::string_view) const
 Retrieve a value by its associated column name.
 
std::string to_json (const std::vector< std::string > &subset={}) const
 Convert a CSV row to a JSON object, i.e.
 
std::string to_json_array (const std::vector< std::string > &subset={}) const
 Convert a CSV row to a JSON array, i.e.
 
std::vector< std::string > get_col_names () const
 Retrieve this row's associated column names.
 
std::unordered_map< std::string, std::string > to_unordered_map () const
 Convert this CSVRow into an unordered map.
 
std::unordered_map< std::string, std::string > to_unordered_map (const std::vector< std::string > &subset) const
 Convert a selected subset of columns into an unordered map.
 
auto to_sv_range () const
 Convert this CSVRow into a std::ranges::input_range of string_views.
 
 operator std::vector< std::string > () const
 Convert this row into a std::vector<std::string>.
 
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, up to (but not including) the trailing newline character.
 
Iterators

Each iterator points to a CSVField object.

iterator begin () const
 Return an iterator pointing to the first field.
 
iterator end () const noexcept
 Return an iterator pointing to just after the end of the CSVRow.
 
reverse_iterator rbegin () const noexcept
 
reverse_iterator rend () const
 

Detailed Description

Data structure for representing CSV rows.

Definition at line 264 of file csv_row.hpp.

Member Typedef Documentation

◆ reverse_iterator

using csv::CSVRow::reverse_iterator = std::reverse_iterator<iterator>

A reverse iterator over the contents of a CSVRow.

Definition at line 376 of file csv_row.hpp.

Constructor & Destructor Documentation

◆ CSVRow() [1/3]

csv::CSVRow::CSVRow ( internals::RawCSVDataPtr  _data)
inline

Construct a CSVRow view over parsed row storage.

Definition at line 271 of file csv_row.hpp.

◆ CSVRow() [2/3]

csv::CSVRow::CSVRow ( internals::RawCSVDataPtr  _data,
size_t  _data_start,
size_t  _field_bounds 
)
inline

Definition at line 272 of file csv_row.hpp.

◆ CSVRow() [3/3]

csv::CSVRow::CSVRow ( internals::RawCSVDataPtr  _data,
size_t  _data_start,
size_t  _field_bounds,
size_t  _row_length 
)
inline

Definition at line 274 of file csv_row.hpp.

Member Function Documentation

◆ begin()

CSVRow::iterator csv::CSVRow::begin ( ) const

Return an iterator pointing to the first field.

Definition at line 138 of file csv_row.cpp.

◆ empty()

CONSTEXPR bool csv::CSVRow::empty ( ) const
inlinenoexcept

Indicates whether row is empty or not.

Definition at line 278 of file csv_row.hpp.

◆ end()

CSVRow::iterator csv::CSVRow::end ( ) const
noexcept

Return an iterator pointing to just after the end of the CSVRow.

Warning
Attempting to dereference the end iterator results in dereferencing a null pointer.

Definition at line 147 of file csv_row.cpp.

◆ get_col_names()

std::vector< std::string > csv::CSVRow::get_col_names ( ) const
inline

Retrieve this row's associated column names.

Definition at line 291 of file csv_row.hpp.

◆ operator std::vector< std::string >()

csv::CSVRow::operator std::vector< std::string > ( ) const

Convert this row into a std::vector<std::string>.

This conversion is primarily intended for write-side workflows, such as reordering or selecting columns before forwarding the row to CSVWriter.

Note
This is less efficient than indexed access via operator[] because it materializes all fields as owning strings.

◆ operator[]() [1/2]

CSVField csv::CSVRow::operator[] ( csv::string_view  col_name) const

Retrieve a value by its associated column name.

If the column specified can't be round, a runtime error is thrown.

Complexity:\n
Constant. This calls the other CSVRow::operator[]() after converting column names into indices using a hash table.

Definition at line 48 of file csv_row.cpp.

◆ operator[]() [2/2]

CSVField csv::CSVRow::operator[] ( size_t  n) const

Return a CSVField object corrsponding to the nth value in the row.

Note
This method performs bounds checking, and will throw an std::runtime_error if n is invalid.
Complexity:\n
Constant, by calling csv::CSVRow::get_csv::string_view()

Definition at line 37 of file csv_row.cpp.

◆ raw_str()

csv::string_view csv::CSVRow::raw_str ( ) const
noexcept

Return a string_view of the raw bytes of this row as they appear in the underlying parse buffer, up to (but not including) the trailing newline character.

Warning
The view is only valid for as long as the CSVRow (and its associated data chunk) remains alive.

Definition at line 66 of file csv_row.cpp.

◆ rbegin()

CSVRow::reverse_iterator csv::CSVRow::rbegin ( ) const
noexcept

Definition at line 151 of file csv_row.cpp.

◆ rend()

CSVRow::reverse_iterator csv::CSVRow::rend ( ) const

Definition at line 155 of file csv_row.cpp.

◆ size()

CONSTEXPR size_t csv::CSVRow::size ( ) const
inlinenoexcept

Return the number of fields in this row.

Definition at line 281 of file csv_row.hpp.

◆ to_json()

std::string csv::CSVRow::to_json ( const std::vector< std::string > &  subset = {}) const

Convert a CSV row to a JSON object, i.e.

{"col1":"value1","col2":"value2"}

Note
All strings are properly escaped. Numeric values are not quoted. Leave subset empty to include the row's original columns.

Definition at line 195 of file csv_row_json.cpp.

◆ to_json_array()

std::string csv::CSVRow::to_json_array ( const std::vector< std::string > &  subset = {}) const

Convert a CSV row to a JSON array, i.e.

["value1","value2",...]

Note
All strings are properly escaped. Numeric values are not quoted. Leave subset empty to include all columns.

Definition at line 232 of file csv_row_json.cpp.

◆ to_sv_range()

auto csv::CSVRow::to_sv_range ( ) const
inline

Convert this CSVRow into a std::ranges::input_range of string_views.

Definition at line 307 of file csv_row.hpp.

◆ to_unordered_map() [1/2]

std::unordered_map< std::string, std::string > csv::CSVRow::to_unordered_map ( ) const

Convert this CSVRow into an unordered map.

Build a map from column names to values for a given row.

The keys are the column names and the values are the corresponding field values.

Definition at line 78 of file csv_row.cpp.

◆ to_unordered_map() [2/2]

std::unordered_map< std::string, std::string > csv::CSVRow::to_unordered_map ( const std::vector< std::string > &  subset) const

Convert a selected subset of columns into an unordered map.

Build a map from a subset of column names to values for a given row.

Definition at line 91 of file csv_row.cpp.


The documentation for this class was generated from the following files: