Vince's CSV Parser
Loading...
Searching...
No Matches
common.hpp File Reference

A standalone header file containing shared code. More...

#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <deque>
#include <limits>
#include <memory>
#include <mutex>
#include <type_traits>
#include <string_view>
#include <ranges>

Go to the source code of this file.

Classes

struct  csv::is_invocable_returning_impl< F, ReturnType, Enable, Args >
 
struct  csv::is_invocable_returning_impl< F, ReturnType, void_t< invoke_result_t< F, Args... > >, Args... >
 
struct  csv::is_invocable_returning< F, ReturnType, Args >
 
class  csv::internals::is_hashable< T >
 
class  csv::internals::is_equality_comparable< T >
 
class  csv::internals::lazy_shared_ptr< T >
 

Namespaces

namespace  csv
 The all encompassing namespace.
 

Concepts

concept  csv::internals::csv_string_field_range
 
concept  csv::internals::has_to_sv_range
 
concept  csv::internals::csv_row_like
 
concept  csv::internals::csv_write_rows_input_range
 

Macros

#define CSV_ENABLE_THREADS   1
 
#define CSV_CONST
 
#define CSV_PURE
 
#define CSV_FORCE_INLINE   inline
 
#define CSV_PRIVATE
 
#define CSV_NON_NULL(...)
 
#define CSV_MSVC_PUSH_DISABLE(w)
 
#define CSV_MSVC_POP
 
#define CSV_EXCEPTIONS_ENABLED   0
 
#define CSV_TRY   try
 
#define CSV_CATCH(exception_declaration, ...)   catch exception_declaration __VA_ARGS__
 
#define CSV_CPLUSPLUS   __cplusplus
 
#define CSV_FALLTHROUGH   [[fallthrough]]
 
#define STATIC_ASSERT(x)   static_assert(x, "Assertion failed")
 
#define CSV_DEBUG_ASSERT(x)   assert(x)
 
#define IF_CONSTEXPR   if constexpr
 Expands to if constexpr in C++17 and if otherwise.
 
#define CONSTEXPR_VALUE   constexpr
 Expands to constexpr in C++17 and const otherwise.
 
#define CONSTEXPR_17   constexpr
 
#define CONSTEXPR_14   inline
 
#define CONSTEXPR_VALUE_14   const
 
#define CONSTEXPR   constexpr
 Expands to constexpr in decent compilers and inline otherwise.
 

Typedefs

using csv::string_view = std::string_view
 The string_view class used by this library.
 
template<bool B, class T = void>
using csv::enable_if_t = typename std::enable_if< B, T >::type
 
template<bool B, class T = void>
using csv::internals::enable_if_t = csv::enable_if_t< B, T >
 
template<typename F , typename... Args>
using csv::invoke_result_t = typename std::invoke_result< F, Args... >::type
 
template<typename... Ts>
using csv::void_t = void
 
typedef std::uint32_t csv::internals::CSVChunkIndex
 Type used to represent the location of a CSV byte within a larger chunk.
 
using csv::internals::ParseFlagMap = std::array< ParseFlags, 256 >
 An array which maps ASCII chars to a parsing flag.
 
using csv::internals::WhitespaceMap = std::array< bool, 256 >
 An array which maps ASCII chars to a flag indicating if it is whitespace.
 

Enumerations

enum class  csv::internals::ParseFlags {
  QUOTE_ESCAPE_QUOTE = 0 , QUOTE = 2 | 1 , NOT_SPECIAL = 4 , DELIMITER = 4 | 1 ,
  CARRIAGE_RETURN = 4 | 2 , NEWLINE = 4 | 2 | 1
}
 An enum used for describing the significance of each character with respect to CSV parsing. More...
 

Functions

template<typename T >
bool csv::internals::is_equal (T a, T b, T epsilon=0.001)
 
constexpr ParseFlags csv::internals::quote_escape_flag (ParseFlags flag, bool quote_escape) noexcept
 Transform the ParseFlags given the context of whether or not the current field is quote escaped.
 
 csv::internals::STATIC_ASSERT (ParseFlags::DELIMITER< ParseFlags::CARRIAGE_RETURN)
 
 csv::internals::STATIC_ASSERT (quote_escape_flag(ParseFlags::NOT_SPECIAL, false)==ParseFlags::NOT_SPECIAL)
 Optimizations for reducing branching in parsing loop.
 
 csv::internals::STATIC_ASSERT (quote_escape_flag(ParseFlags::QUOTE, false)==ParseFlags::QUOTE)
 
 csv::internals::STATIC_ASSERT (quote_escape_flag(ParseFlags::DELIMITER, false)==ParseFlags::DELIMITER)
 
 csv::internals::STATIC_ASSERT (quote_escape_flag(ParseFlags::CARRIAGE_RETURN, false)==ParseFlags::CARRIAGE_RETURN)
 
 csv::internals::STATIC_ASSERT (quote_escape_flag(ParseFlags::NEWLINE, false)==ParseFlags::NEWLINE)
 
 csv::internals::STATIC_ASSERT (quote_escape_flag(ParseFlags::NOT_SPECIAL, true)==ParseFlags::NOT_SPECIAL)
 
 csv::internals::STATIC_ASSERT (quote_escape_flag(ParseFlags::QUOTE, true)==ParseFlags::QUOTE_ESCAPE_QUOTE)
 
 csv::internals::STATIC_ASSERT (quote_escape_flag(ParseFlags::DELIMITER, true)==ParseFlags::NOT_SPECIAL)
 
 csv::internals::STATIC_ASSERT (quote_escape_flag(ParseFlags::CARRIAGE_RETURN, true)==ParseFlags::NOT_SPECIAL)
 
 csv::internals::STATIC_ASSERT (quote_escape_flag(ParseFlags::NEWLINE, true)==ParseFlags::NOT_SPECIAL)
 

Variables

const int csv::internals::PAGE_SIZE = 4096
 Size of a memory page in bytes.
 
constexpr size_t csv::internals::CSV_CHUNK_SIZE_DEFAULT = 10000000
 Default chunk size for lazy-loading large CSV files.
 
CONSTEXPR_VALUE_14 CSVChunkIndex csv::internals::CSV_CHUNK_INDEX_MAX = (std::numeric_limits<CSVChunkIndex>::max)()
 
CONSTEXPR_VALUE_14 size_t csv::internals::CSV_CHUNK_SIZE_MAX = CSV_CHUNK_INDEX_MAX
 
constexpr size_t csv::internals::CSV_CHUNK_SIZE_FLOOR = 500 * 1024
 Minimum supported custom chunk size for CSVFormat::chunk_size().
 
constexpr size_t csv::internals::CSV_HEAD_PREVIEW_SIZE = 500000
 Bytes read from the start of a source to resolve its format (header, delimiter guessing) before the first parsing window.
 
constexpr size_t csv::internals::CSV_SPECULATIVE_PARALLEL_MIN_BYTES = 50ull * 1024ull * 1024ull
 Default minimum source size before speculative parallel parsing is considered.
 
constexpr int csv::CSV_NOT_FOUND = -1
 Integer indicating a requested column wasn't found.
 
constexpr unsigned csv::CHAR_OFFSET = std::numeric_limits<char>::is_signed ? 128 : 0
 Offset to convert char into array index.
 

Detailed Description

A standalone header file containing shared code.

Definition in file common.hpp.

Macro Definition Documentation

◆ CONSTEXPR

#define CONSTEXPR   constexpr

Expands to constexpr in decent compilers and inline otherwise.

Intended for functions and methods.

Definition at line 259 of file common.hpp.

◆ CONSTEXPR_14

#define CONSTEXPR_14   inline

Definition at line 212 of file common.hpp.

◆ CONSTEXPR_17

#define CONSTEXPR_17   constexpr

Definition at line 194 of file common.hpp.

◆ CONSTEXPR_VALUE

#define CONSTEXPR_VALUE   constexpr

Expands to constexpr in C++17 and const otherwise.

Mainly used for global variables.

Definition at line 192 of file common.hpp.

◆ CONSTEXPR_VALUE_14

#define CONSTEXPR_VALUE_14   const

Definition at line 213 of file common.hpp.

◆ CSV_CATCH

#define CSV_CATCH (   exception_declaration,
  ... 
)    catch exception_declaration __VA_ARGS__

Definition at line 94 of file common.hpp.

◆ CSV_CONST

#define CSV_CONST

Definition at line 63 of file common.hpp.

◆ CSV_CPLUSPLUS

#define CSV_CPLUSPLUS   __cplusplus

Definition at line 106 of file common.hpp.

◆ CSV_DEBUG_ASSERT

#define CSV_DEBUG_ASSERT (   x)    assert(x)

Definition at line 175 of file common.hpp.

◆ CSV_ENABLE_THREADS

#define CSV_ENABLE_THREADS   1

Definition at line 36 of file common.hpp.

◆ CSV_EXCEPTIONS_ENABLED

#define CSV_EXCEPTIONS_ENABLED   0

Definition at line 84 of file common.hpp.

◆ CSV_FALLTHROUGH

#define CSV_FALLTHROUGH   [[fallthrough]]

Definition at line 128 of file common.hpp.

◆ CSV_FORCE_INLINE

#define CSV_FORCE_INLINE   inline

Definition at line 65 of file common.hpp.

◆ CSV_MSVC_POP

#define CSV_MSVC_POP

Definition at line 77 of file common.hpp.

◆ CSV_MSVC_PUSH_DISABLE

#define CSV_MSVC_PUSH_DISABLE (   w)

Definition at line 76 of file common.hpp.

◆ CSV_NON_NULL

#define CSV_NON_NULL (   ...)

Definition at line 67 of file common.hpp.

◆ CSV_PRIVATE

#define CSV_PRIVATE

Definition at line 66 of file common.hpp.

◆ CSV_PURE

#define CSV_PURE

Definition at line 64 of file common.hpp.

◆ CSV_TRY

#define CSV_TRY   try

Definition at line 93 of file common.hpp.

◆ IF_CONSTEXPR

#define IF_CONSTEXPR   if constexpr

Expands to if constexpr in C++17 and if otherwise.

Definition at line 191 of file common.hpp.

◆ STATIC_ASSERT

#define STATIC_ASSERT (   x)    static_assert(x, "Assertion failed")

Definition at line 170 of file common.hpp.

Typedef Documentation

◆ CSVChunkIndex

typedef std::uint32_t csv::internals::CSVChunkIndex

Type used to represent the location of a CSV byte within a larger chunk.

Definition at line 419 of file common.hpp.

◆ enable_if_t

template<bool B, class T = void>
using csv::internals::enable_if_t = typedef csv::enable_if_t<B, T>

Definition at line 218 of file common.hpp.

◆ ParseFlagMap

using csv::internals::ParseFlagMap = typedef std::array<ParseFlags, 256>

An array which maps ASCII chars to a parsing flag.

Definition at line 495 of file common.hpp.

◆ WhitespaceMap

using csv::internals::WhitespaceMap = typedef std::array<bool, 256>

An array which maps ASCII chars to a flag indicating if it is whitespace.

Definition at line 498 of file common.hpp.

Enumeration Type Documentation

◆ ParseFlags

enum class csv::internals::ParseFlags
strong

An enum used for describing the significance of each character with respect to CSV parsing.

See also
quote_escape_flag
Enumerator
QUOTE_ESCAPE_QUOTE 

A quote inside or terminating a quote_escaped field.

QUOTE 

Characters which may signify a quote escape.

NOT_SPECIAL 

Characters with no special meaning or escaped delimiters and newlines.

DELIMITER 

Characters which signify a new field.

CARRIAGE_RETURN 

Characters which signify a carriage return.

NEWLINE 

Characters which signify a new row.

Definition at line 456 of file common.hpp.

Function Documentation

◆ is_equal()

template<typename T >
bool csv::internals::is_equal ( T  a,
T  b,
T  epsilon = 0.001 
)
inline

Returns true if two floating point values are about the same

Definition at line 444 of file common.hpp.

◆ quote_escape_flag()

constexpr ParseFlags csv::internals::quote_escape_flag ( ParseFlags  flag,
bool  quote_escape 
)
constexprnoexcept

Transform the ParseFlags given the context of whether or not the current field is quote escaped.

Definition at line 467 of file common.hpp.

◆ STATIC_ASSERT()

Optimizations for reducing branching in parsing loop.

Idea: The meaning of all non-quote characters changes depending on whether or not the parser is in a quote-escaped mode (0 or 1)

Variable Documentation

◆ CSV_CHUNK_INDEX_MAX

CONSTEXPR_VALUE_14 CSVChunkIndex csv::internals::CSV_CHUNK_INDEX_MAX = (std::numeric_limits<CSVChunkIndex>::max)()

Definition at line 421 of file common.hpp.

◆ CSV_CHUNK_SIZE_DEFAULT

constexpr size_t csv::internals::CSV_CHUNK_SIZE_DEFAULT = 10000000
constexpr

Default chunk size for lazy-loading large CSV files.

The worker thread reads this many bytes at a time by default (10MB).

CRITICAL INVARIANT: Field boundaries at chunk transitions must be preserved. Bug #280 was caused by fields spanning chunk boundaries being corrupted.

Note
Tests must write >10MB of data to cross chunk boundaries
See also
parser/mmap.cpp MmapParser::next() for chunk transition logic

Definition at line 416 of file common.hpp.

◆ CSV_CHUNK_SIZE_FLOOR

constexpr size_t csv::internals::CSV_CHUNK_SIZE_FLOOR = 500 * 1024
constexpr

Minimum supported custom chunk size for CSVFormat::chunk_size().

This lower bound allows memory-constrained environments to reduce parser buffer size while avoiding pathological tiny-buffer overhead.

Definition at line 430 of file common.hpp.

◆ CSV_CHUNK_SIZE_MAX

CONSTEXPR_VALUE_14 size_t csv::internals::CSV_CHUNK_SIZE_MAX = CSV_CHUNK_INDEX_MAX

Definition at line 423 of file common.hpp.

◆ CSV_HEAD_PREVIEW_SIZE

constexpr size_t csv::internals::CSV_HEAD_PREVIEW_SIZE = 500000
constexpr

Bytes read from the start of a source to resolve its format (header, delimiter guessing) before the first parsing window.

The mmap backend rewinds this preview to its last complete row, so the boundary fuzzer uses this value to place syntax at mmap's first window.

Definition at line 438 of file common.hpp.

◆ CSV_SPECULATIVE_PARALLEL_MIN_BYTES

constexpr size_t csv::internals::CSV_SPECULATIVE_PARALLEL_MIN_BYTES = 50ull * 1024ull * 1024ull
constexpr

Default minimum source size before speculative parallel parsing is considered.

Definition at line 441 of file common.hpp.

◆ PAGE_SIZE

const int csv::internals::PAGE_SIZE = 4096

Size of a memory page in bytes.

Used by csv::internals::CSVFieldArray when allocating blocks.

Definition at line 403 of file common.hpp.