csv-parser 1.1.0
Csv::Parser (csv-parser)
Loading...
Searching...
No Matches
Classes | Functions
csv_util.h File Reference

Utility functions used by the library. More...

#include <string_view>
#include <string>
#include <optional>
#include <algorithm>
#include <charconv>

Go to the source code of this file.

Functions

std::string Csv::cleanString (std::string_view view)
 Unescape a string - collapse every occurrence of 2 consecutive double-quotes to one.
 
constexpr std::size_t Csv::getCleanStringSize (std::string_view uncollapsed_view)
 Get size of a cleaned-up string given the uncollapsed string.
 
template<typename Number >
std::optional< Number > Csv::readNumberLocale (std::string_view cell)
 Try to read a numeric value from string data.
 
template<typename Number >
std::optional< Number > Csv::readNumberNoLocale (std::string_view cell)
 Try to read a numeric value from string data.
 
template<typename Vector2D >
constexpr auto Csv::vector2DValue (const Vector2D &values, std::size_t row, std::size_t column)
 A helper function to get an element of a 2D vector in less error-prone way.
 

Detailed Description

Utility functions used by the library.

Function Documentation

◆ cleanString()

std::string Csv::cleanString ( std::string_view  view)
inline

Unescape a string - collapse every occurrence of 2 consecutive double-quotes to one.

Parameters
viewInput string
Returns
Unescaped string

◆ getCleanStringSize()

constexpr std::size_t Csv::getCleanStringSize ( std::string_view  uncollapsed_view)
inlineconstexpr

Get size of a cleaned-up string given the uncollapsed string.

Parameters
uncollapsed_viewInput string
Returns
Size of the cleaned-up string

◆ readNumberLocale()

template<typename Number >
std::optional< Number > Csv::readNumberLocale ( std::string_view  cell)

Try to read a numeric value from string data.

Unless the string data completely represents a serialized int/float/double/..., std::nullopt is returned.

Note
This function is locale-dependent.
Template Parameters
NumberType of number to read, e.g. int, float, double, etc.
Parameters
cellString data to parse, with optional whitespace on one or both sides.
Returns
Parsed number or std::nullopt if parsing failed.

◆ readNumberNoLocale()

template<typename Number >
std::optional< Number > Csv::readNumberNoLocale ( std::string_view  cell)

Try to read a numeric value from string data.

Unless the string data completely represents a serialized int/float/double/..., std::nullopt is returned.

Note
This function is locale-independent.
This function is constexpr in C++23+ when parsing integral types.
Template Parameters
NumberType of number to read, e.g. int, float, double, etc.
Parameters
cellString data to parse, with optional whitespace on one or both sides.
Returns
Parsed number or std::nullopt if parsing failed.

◆ vector2DValue()

template<typename Vector2D >
constexpr auto Csv::vector2DValue ( const Vector2D &  values,
std::size_t  row,
std::size_t  column 
)
constexpr

A helper function to get an element of a 2D vector in less error-prone way.

Template Parameters
Vector2DA 2D vector type, deduced from the argument.
Parameters
valuesA vector of columns
row0-based row number
column0-based column number
Returns
Value of Vector2D's innermost type