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

Data type representing individual CSV values. More...

#include <csv_row.hpp>

Inherited by csv::DataFrameCell.

Public Member Functions

constexpr CSVField (csv::string_view _sv) noexcept
 Constructs a CSVField from a string_view.
 
 CSVField (csv::string_view _sv, const internals::CSVFieldScalar &scalar) noexcept
 
 operator csv::string_view () const noexcept
 
 operator std::string () const
 
template<typename T = std::string>
get ()
 Returns the value casted to the requested type, performing type checking before.
 
template<typename T = std::string>
std::expected< T, CSVConversionErroras ()
 Return this field as T, preserving conversion failure as CSVConversionError.
 
template<typename T = std::string>
bool try_get (T &out) noexcept
 Non-throwing equivalent of get().
 
template<typename T >
 operator std::optional< T > ()
 Convert this field to std::optional<T>, returning std::nullopt when conversion fails.
 
template<typename T = long long>
bool try_parse_hex (T &parsedValue)
 Parse a hexadecimal value, returning false if the value is not hex.
 
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 numeric.
 
bool try_parse_timestamp (std::uint64_t &out) noexcept
 Parse this field as Unix milliseconds.
 
template<typename T >
bool try_parse_timestamp (T &out) noexcept
 Parse this field as Unix milliseconds in a 64-bit unsigned integer.
 
template<typename Rep , typename Period >
bool try_parse_timestamp (std::chrono::duration< Rep, Period > &out) noexcept
 Parse this field as a timestamp duration since the Unix epoch.
 
template<typename Duration >
bool try_parse_timestamp (std::chrono::time_point< std::chrono::system_clock, Duration > &out) noexcept
 Parse this field as a std::chrono::system_clock time point.
 
template<typename T >
bool operator== (T other) const noexcept
 Compares the contents of this field to a numeric value.
 
CONSTEXPR csv::string_view get_sv () const noexcept
 Return a string view over the field's contents.
 
bool is_null () noexcept
 Returns true if field is an empty string or string of whitespace characters.
 
bool is_str () noexcept
 Returns true if field is a non-numeric, non-empty string.
 
bool is_num () noexcept
 Returns true if field is an integer or float.
 
bool is_int () noexcept
 Returns true if field is an integer.
 
bool is_float () noexcept
 Returns true if field is a floating point value.
 
bool is_bool () noexcept
 Returns true if field is a boolean value.
 
bool is_timestamp () noexcept
 Returns true if field is a timestamp value.
 
DataType type () noexcept
 Return the type of the underlying CSV data.
 
template<>
long double get ()
 Retrieve this field's value as a long double.
 
template<>
bool try_get (long double &out) noexcept
 Non-throwing retrieval of field as long double.
 
template<>
CONSTEXPR bool operator== (const char *other) const noexcept
 Compares the contents of this field to a string.
 
template<>
CONSTEXPR bool operator== (csv::string_view other) const noexcept
 Compares the contents of this field to a string.
 

Detailed Description

Data type representing individual CSV values.

CSVFields can be obtained by using CSVRow::operator[]

Definition at line 114 of file csv_row.hpp.

Constructor & Destructor Documentation

◆ CSVField() [1/2]

constexpr csv::CSVField::CSVField ( csv::string_view  _sv)
inlineexplicitconstexprnoexcept

Constructs a CSVField from a string_view.

Definition at line 117 of file csv_row.hpp.

◆ CSVField() [2/2]

csv::CSVField::CSVField ( csv::string_view  _sv,
const internals::CSVFieldScalar scalar 
)
inlinenoexcept

Definition at line 120 of file csv_row.hpp.

Member Function Documentation

◆ as()

template<typename T = std::string>
std::expected< T, CSVConversionError > csv::CSVField::as ( )
inline

Return this field as T, preserving conversion failure as CSVConversionError.

Returns
std::expected containing T on success, or CSVConversionError describing why conversion failed.
Note
Requires C++23 and a standard library that provides std::expected.
See also
CSVConversionError
csv_conversion_error_message()

Definition at line 200 of file csv_row.hpp.

◆ get() [1/2]

template<typename T = std::string>
T csv::CSVField::get ( )
inline

Returns the value casted to the requested type, performing type checking before.

Valid options for T
  • std::string or csv::string_view
  • signed integral types (signed char, short, int, long int, long long int)
  • unsigned integral types (unsigned char, unsigned short, unsigned int, unsigned long long)
  • floating point types (float, double, long double)
Invalid conversions
  • Converting non-numeric values to any numeric type
  • Converting floating point values to integers
  • Converting a large integer to a smaller type that will not hold it
Note
This method is capable of parsing scientific E-notation. See Scalar Conversion Reference for more details.
Exceptions
std::runtime_errorThrown if an invalid conversion is performed.
Warning
Currently, conversions to floating point types are not checked for loss of precision
Any string_views returned are only guaranteed to be valid if the parent CSVRow is still alive. If you are concerned about object lifetimes, then grab a std::string or a numeric value.

Definition at line 180 of file csv_row.hpp.

◆ get() [2/2]

template<>
long double csv::CSVField::get ( )
inline

Retrieve this field's value as a long double.

Definition at line 773 of file csv_row.hpp.

◆ get_sv()

CONSTEXPR csv::string_view csv::CSVField::get_sv ( ) const
inlinenoexcept

Return a string view over the field's contents.

Definition at line 344 of file csv_row.hpp.

◆ is_bool()

bool csv::CSVField::is_bool ( )
inlinenoexcept

Returns true if field is a boolean value.

Definition at line 366 of file csv_row.hpp.

◆ is_float()

bool csv::CSVField::is_float ( )
inlinenoexcept

Returns true if field is a floating point value.

Definition at line 363 of file csv_row.hpp.

◆ is_int()

bool csv::CSVField::is_int ( )
inlinenoexcept

Returns true if field is an integer.

Definition at line 358 of file csv_row.hpp.

◆ is_null()

bool csv::CSVField::is_null ( )
inlinenoexcept

Returns true if field is an empty string or string of whitespace characters.

Definition at line 347 of file csv_row.hpp.

◆ is_num()

bool csv::CSVField::is_num ( )
inlinenoexcept

Returns true if field is an integer or float.

Definition at line 353 of file csv_row.hpp.

◆ is_str()

bool csv::CSVField::is_str ( )
inlinenoexcept

Returns true if field is a non-numeric, non-empty string.

Definition at line 350 of file csv_row.hpp.

◆ is_timestamp()

bool csv::CSVField::is_timestamp ( )
inlinenoexcept

Returns true if field is a timestamp value.

Definition at line 369 of file csv_row.hpp.

◆ operator csv::string_view()

csv::CSVField::operator csv::string_view ( ) const
inlinenoexcept

Definition at line 145 of file csv_row.hpp.

◆ operator std::optional< T >()

template<typename T >
csv::CSVField::operator std::optional< T > ( )
inline

Convert this field to std::optional<T>, returning std::nullopt when conversion fails.

This is a value-returning wrapper around try_get(), useful for C++17 callers that want non-throwing conversion without an output parameter.

Note
Requires C++17 or later.

Definition at line 239 of file csv_row.hpp.

◆ operator std::string()

csv::CSVField::operator std::string ( ) const
inline

Definition at line 149 of file csv_row.hpp.

◆ operator==() [1/3]

template<>
CONSTEXPR bool csv::CSVField::operator== ( const char *  other) const
noexcept

Compares the contents of this field to a string.

Definition at line 809 of file csv_row.hpp.

◆ operator==() [2/3]

template<>
CONSTEXPR bool csv::CSVField::operator== ( csv::string_view  other) const
noexcept

Compares the contents of this field to a string.

Definition at line 816 of file csv_row.hpp.

◆ operator==() [3/3]

template<typename T >
bool csv::CSVField::operator== ( other) const
inlinenoexcept

Compares the contents of this field to a numeric value.

If this field does not contain a numeric value, then all comparisons return false.

Note
Floating point values are considered equal if they are within 0.000001 of each other.
Warning
Multiple numeric comparisons involving the same field can be done more efficiently by calling the CSVField::get<>() method.
See also
csv::CSVField::operator==(const char * other)
csv::CSVField::operator==(csv::string_view other)

Definition at line 330 of file csv_row.hpp.

◆ try_get() [1/2]

template<>
bool csv::CSVField::try_get ( long double &  out)
inlinenoexcept

Non-throwing retrieval of field as long double.

Definition at line 796 of file csv_row.hpp.

◆ try_get() [2/2]

template<typename T = std::string>
bool csv::CSVField::try_get ( T &  out)
inlinenoexcept

Non-throwing equivalent of get().

Applies the same type checks and conversions; returns true and writes to out on success, or returns false without throwing.

See also
get() for the full description of valid types, conversion rules, and warnings.

Example:

int value;
if (field.try_get(value)) {
// Use value safely
} else {
// Handle conversion failure
}

Definition at line 225 of file csv_row.hpp.

◆ try_parse_decimal()

bool csv::CSVField::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 numeric.

Note
This method also updates this field's type

Definition at line 131 of file csv_row.cpp.

◆ try_parse_hex()

template<typename T = long long>
bool csv::CSVField::try_parse_hex ( T &  parsedValue)
inline

Parse a hexadecimal value, returning false if the value is not hex.

Template Parameters
TAn integral type (int, long, long long, etc.)

Definition at line 249 of file csv_row.hpp.

◆ try_parse_timestamp() [1/3]

template<typename Rep , typename Period >
bool csv::CSVField::try_parse_timestamp ( std::chrono::duration< Rep, Period > &  out)
inlinenoexcept

Parse this field as a timestamp duration since the Unix epoch.

Definition at line 294 of file csv_row.hpp.

◆ try_parse_timestamp() [2/3]

template<typename Duration >
bool csv::CSVField::try_parse_timestamp ( std::chrono::time_point< std::chrono::system_clock, Duration > &  out)
inlinenoexcept

Parse this field as a std::chrono::system_clock time point.

Definition at line 306 of file csv_row.hpp.

◆ try_parse_timestamp() [3/3]

bool csv::CSVField::try_parse_timestamp ( std::uint64_t &  out)
noexcept

Parse this field as Unix milliseconds.

Timestamp-classified values return their parsed epoch value. Integral values are treated as already being Unix milliseconds.

Definition at line 165 of file csv_row.cpp.

◆ type()

DataType csv::CSVField::type ( )
inlinenoexcept

Return the type of the underlying CSV data.

Definition at line 372 of file csv_row.hpp.


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