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 from a RawCSVDataPtr.
 
 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[] (const std::string &) 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 this CSVRow into an unordered map.
 
 operator std::vector< std::string > () const
 Convert this CSVRow into a vector of strings.
 
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 280 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 375 of file csv_row.hpp.

Constructor & Destructor Documentation

◆ CSVRow() [1/3]

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

Construct a CSVRow from a RawCSVDataPtr.

Definition at line 287 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 288 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 290 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 116 of file csv_row.cpp.

◆ empty()

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

Indicates whether row is empty or not.

Definition at line 294 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 125 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 307 of file csv_row.hpp.

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

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

Convert this CSVRow into a vector of strings.

Note: This is a less efficient method of accessing data than using the [] operator.

◆ operator[]() [1/2]

CSVField csv::CSVRow::operator[] ( const std::string &  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.
Parameters
[in]col_nameThe column to look for

Definition at line 33 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 20 of file csv_row.cpp.

◆ rbegin()

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

Definition at line 129 of file csv_row.cpp.

◆ rend()

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

Definition at line 133 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 297 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.
Parameters
[in]subsetA subset of columns to contain in the JSON. Leave empty for original columns.

Definition at line 199 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.
Parameters
[in]subsetA subset of columns to contain in the JSON. Leave empty for all columns.

Definition at line 237 of file csv_row_json.cpp.

◆ 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 52 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 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.

Parameters
[in]subsetVector of column names to include in the map.
[in]subsetVector of column names to include in the map.

Definition at line 69 of file csv_row.cpp.


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