13#if !defined(CSV_ENABLE_THREADS) || CSV_ENABLE_THREADS
18# ifndef WIN32_LEAN_AND_MEAN
19# define WIN32_LEAN_AND_MEAN
24#elif defined(__linux__)
34#if defined(__EMSCRIPTEN__)
35#undef CSV_ENABLE_THREADS
36#define CSV_ENABLE_THREADS 0
37#elif !defined(CSV_ENABLE_THREADS)
38#define CSV_ENABLE_THREADS 1
42#if defined(CSV_CODE_COVERAGE)
45 #define CSV_FORCE_INLINE inline
47 #define CSV_NON_NULL(...)
48#elif defined(__clang__) || defined(__GNUC__)
49 #define CSV_CONST __attribute__((__const__))
50 #define CSV_PURE __attribute__((__pure__))
51 #define CSV_FORCE_INLINE inline __attribute__((__always_inline__))
55 #define CSV_PRIVATE __attribute__((__visibility__("hidden")))
57 #define CSV_NON_NULL(...) __attribute__((__nonnull__(__VA_ARGS__)))
58#elif defined(_MSC_VER)
61 #define CSV_FORCE_INLINE __forceinline
63 #define CSV_NON_NULL(...)
67 #define CSV_FORCE_INLINE inline
69 #define CSV_NON_NULL(...)
75# define CSV_MSVC_PUSH_DISABLE(w) __pragma(warning(push)) __pragma(warning(disable: w))
76# define CSV_MSVC_POP __pragma(warning(pop))
78# define CSV_MSVC_PUSH_DISABLE(w)
83#if defined(__cpp_exceptions) || defined(_CPPUNWIND) || defined(__EXCEPTIONS)
84 #define CSV_EXCEPTIONS_ENABLED 1
86 #define CSV_EXCEPTIONS_ENABLED 0
89#if !CSV_EXCEPTIONS_ENABLED
90 #error "csv-parser requires C++ exceptions. Enable exception handling (for example, remove -fno-exceptions or use /EHsc)."
95#if defined(_MSVC_LANG) && _MSVC_LANG > __cplusplus
96# define CSV_CPLUSPLUS _MSVC_LANG
98# define CSV_CPLUSPLUS __cplusplus
101#if CSV_CPLUSPLUS >= 202002L
105#if CSV_CPLUSPLUS >= 202302L
109#if CSV_CPLUSPLUS >= 201703L
113#if CSV_CPLUSPLUS >= 201402L
119#if defined(CSV_HAS_CXX17)
120#define CSV_FALLTHROUGH [[fallthrough]]
121#elif defined(__clang__) && defined(__has_cpp_attribute)
122#if __has_cpp_attribute(clang::fallthrough)
123#define CSV_FALLTHROUGH [[clang::fallthrough]]
125#define CSV_FALLTHROUGH ((void)0)
127#elif defined(__GNUC__) && __GNUC__ >= 7
128#define CSV_FALLTHROUGH __attribute__((fallthrough))
130#define CSV_FALLTHROUGH ((void)0)
135#include <string_view>
137#include "../external/string_view.hpp"
146#pragma region Compatibility Macros
162#define STATIC_ASSERT(x) static_assert(x, "Assertion failed")
165 #define CSV_DEBUG_ASSERT(x) ((void)sizeof(x), (void)0)
167 #define CSV_DEBUG_ASSERT(x) assert(x)
183 #define IF_CONSTEXPR if constexpr
184 #define CONSTEXPR_VALUE constexpr
186 #define CONSTEXPR_17 constexpr
188 #define IF_CONSTEXPR if
189 #define CONSTEXPR_VALUE const
191 #define CONSTEXPR_17 inline
195 template<
bool B,
class T =
void>
196 using enable_if_t = std::enable_if_t<B, T>;
198 #define CONSTEXPR_14 constexpr
199 #define CONSTEXPR_VALUE_14 constexpr
201 template<
bool B,
class T =
void>
202 using enable_if_t =
typename std::enable_if<B, T>::type;
204 #define CONSTEXPR_14 inline
205 #define CONSTEXPR_VALUE_14 const
208 namespace internals {
209 template<
bool B,
class T =
void>
210 using enable_if_t = csv::enable_if_t<B, T>;
214 template<
typename F,
typename... Args>
215 using invoke_result_t =
typename std::invoke_result<F, Args...>::type;
217 template<
typename F,
typename... Args>
218 using invoke_result_t =
typename std::result_of<F(Args...)>::type;
221 template<
typename... Ts>
224 template<
typename F,
typename ReturnType,
typename Enable,
typename... Args>
227 template<
typename F,
typename ReturnType,
typename... Args>
231 void_t<invoke_result_t<F, Args...>>,
233 > : std::integral_constant<
235 std::is_convertible<invoke_result_t<F, Args...>, ReturnType>::value
238 template<
typename F,
typename ReturnType,
typename... Args>
245#if defined(__GNUC__) && !defined(__clang__)
246 #if defined(CSV_HAS_CXX17) && (((__GNUC__ == 7) && (__GNUC_MINOR__ >= 2)) || (__GNUC__ >= 8))
247 #define CONSTEXPR constexpr
251 #define CONSTEXPR constexpr
256#define CONSTEXPR inline
263 namespace internals {
268 static auto test(
int) ->
decltype(
269 std::hash<U>{}(std::declval<const U&>()),
274 static std::false_type test(...);
277 static constexpr bool value =
decltype(test<T>(0))::value;
284 static auto test(
int) ->
decltype(
285 std::declval<const U&>() == std::declval<const U&>(),
290 static std::false_type test(...);
293 static constexpr bool value =
decltype(test<T>(0))::value;
306 if (
this != &other) {
307 value_ = std::move(other.value_);
313 template<
typename Factory>
314 T& get_or_create(Factory&& factory)
const {
315#if CSV_ENABLE_THREADS
316 std::call_once(init_once_, [
this, &factory]() {
327 T* get()
const noexcept {
332 mutable std::shared_ptr<T> value_ =
nullptr;
333#if CSV_ENABLE_THREADS
334 mutable std::once_flag init_once_;
340 #pragma region CXX20 Concepts
345 std::ranges::input_range<std::remove_reference_t<T>>
346 && std::convertible_to<
347 std::ranges::range_reference_t<std::remove_reference_t<T>>,
353 { value.to_sv_range() } -> std::ranges::input_range;
354 requires std::convertible_to<
355 std::ranges::range_reference_t<
decltype(value.to_sv_range())>,
365 std::ranges::input_range<std::remove_reference_t<T>>
367 std::ranges::range_reference_t<std::remove_reference_t<T>>
376#if defined(PAGE_SIZE)
382 inline int getpagesize() {
383 _SYSTEM_INFO sys_info = {};
384 GetSystemInfo(&sys_info);
385 return std::max(sys_info.dwPageSize, sys_info.dwAllocationGranularity);
389#elif defined(__linux__)
421 inline bool is_equal(T a, T b, T epsilon = 0.001) {
423 static_assert(std::is_floating_point<T>::value,
"T must be a floating point type.");
424 return std::abs(a - b) < epsilon;
445 return (
ParseFlags)((int)flag & ~((
int)ParseFlags::QUOTE * quote_escape));
450 STATIC_ASSERT(ParseFlags::DELIMITER < ParseFlags::CARRIAGE_RETURN);
451 STATIC_ASSERT(ParseFlags::DELIMITER < ParseFlags::NEWLINE);
452 STATIC_ASSERT(ParseFlags::CARRIAGE_RETURN < ParseFlags::NEWLINE);
461 STATIC_ASSERT(
quote_escape_flag(ParseFlags::DELIMITER,
false) == ParseFlags::DELIMITER);
462 STATIC_ASSERT(
quote_escape_flag(ParseFlags::CARRIAGE_RETURN,
false) == ParseFlags::CARRIAGE_RETURN);
463 STATIC_ASSERT(
quote_escape_flag(ParseFlags::NEWLINE,
false) == ParseFlags::NEWLINE);
465 STATIC_ASSERT(
quote_escape_flag(ParseFlags::NOT_SPECIAL,
true) == ParseFlags::NOT_SPECIAL);
466 STATIC_ASSERT(
quote_escape_flag(ParseFlags::QUOTE,
true) == ParseFlags::QUOTE_ESCAPE_QUOTE);
467 STATIC_ASSERT(
quote_escape_flag(ParseFlags::DELIMITER,
true) == ParseFlags::NOT_SPECIAL);
468 STATIC_ASSERT(
quote_escape_flag(ParseFlags::CARRIAGE_RETURN,
true) == ParseFlags::NOT_SPECIAL);
469 STATIC_ASSERT(
quote_escape_flag(ParseFlags::NEWLINE,
true) == ParseFlags::NOT_SPECIAL);
482 constexpr unsigned CHAR_OFFSET = std::numeric_limits<char>::is_signed ? 128 : 0;
std::array< ParseFlags, 256 > ParseFlagMap
An array which maps ASCII chars to a parsing flag.
std::array< bool, 256 > WhitespaceMap
An array which maps ASCII chars to a flag indicating if it is whitespace.
bool is_equal(T a, T b, T epsilon=0.001)
const int PAGE_SIZE
Size of a memory page in bytes.
ParseFlags
An enum used for describing the significance of each character with respect to CSV parsing.
@ QUOTE_ESCAPE_QUOTE
A quote inside or terminating a quote_escaped field.
@ NOT_SPECIAL
Characters with no special meaning or escaped delimiters and newlines.
@ NEWLINE
Characters which signify a new row.
@ CARRIAGE_RETURN
Characters which signify a carriage return.
@ QUOTE
Characters which may signify a quote escape.
@ DELIMITER
Characters which signify a new field.
constexpr size_t CSV_CHUNK_SIZE_FLOOR
Minimum supported custom chunk size for CSVFormat::chunk_size().
constexpr ParseFlags 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.
constexpr size_t CSV_SPECULATIVE_PARALLEL_MIN_BYTES
Default minimum source size before speculative parallel parsing is considered.
constexpr size_t CSV_CHUNK_SIZE_DEFAULT
Default chunk size for lazy-loading large CSV files.
The all encompassing namespace.
constexpr int CSV_NOT_FOUND
Integer indicating a requested column wasn't found.
constexpr unsigned CHAR_OFFSET
Offset to convert char into array index.
std::string_view string_view
The string_view class used by this library.