-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathcommon.hpp
More file actions
274 lines (234 loc) · 8.94 KB
/
common.hpp
File metadata and controls
274 lines (234 loc) · 8.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/** @file
* A standalone header file containing shared code
*/
#pragma once
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdlib>
#include <deque>
#if defined(_WIN32)
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# include <windows.h>
# undef max
# undef min
#elif defined(__linux__)
# include <unistd.h>
#endif
/** Helper macro which should be #defined as "inline"
* in the single header version
*/
#define CSV_INLINE
#pragma once
#include <type_traits>
// Minimal portability macros (Hedley subset) with CSV_ prefix.
#if defined(__clang__) || defined(__GNUC__)
#define CSV_CONST __attribute__((__const__))
#define CSV_PURE __attribute__((__pure__))
#define CSV_PRIVATE __attribute__((__visibility__("hidden")))
#define CSV_NON_NULL(...) __attribute__((__nonnull__(__VA_ARGS__)))
#elif defined(_MSC_VER)
#define CSV_CONST
#define CSV_PURE
#define CSV_PRIVATE
#define CSV_NON_NULL(...)
#else
#define CSV_CONST
#define CSV_PURE
#define CSV_PRIVATE
#define CSV_NON_NULL(...)
#endif
#if defined(__GNUC__) || defined(__clang__)
#define CSV_UNREACHABLE() __builtin_unreachable()
#elif defined(_MSC_VER)
#define CSV_UNREACHABLE() __assume(0)
#else
#define CSV_UNREACHABLE() abort()
#endif
// This library uses C++ exceptions for error reporting in public APIs.
#if defined(__cpp_exceptions) || defined(_CPPUNWIND) || defined(__EXCEPTIONS)
#define CSV_EXCEPTIONS_ENABLED 1
#else
#define CSV_EXCEPTIONS_ENABLED 0
#endif
#if !CSV_EXCEPTIONS_ENABLED
#error "csv-parser requires C++ exceptions. Enable exception handling (for example, remove -fno-exceptions or use /EHsc)."
#endif
// Detect C++ standard version BEFORE namespace to properly include string_view
// MSVC: __cplusplus == 199711L unless /Zc:__cplusplus is set; use _MSVC_LANG instead.
#if defined(_MSVC_LANG) && _MSVC_LANG > __cplusplus
# define CSV_CPLUSPLUS _MSVC_LANG
#else
# define CSV_CPLUSPLUS __cplusplus
#endif
#if (defined(CMAKE_CXX_STANDARD) && CMAKE_CXX_STANDARD == 20) || CSV_CPLUSPLUS >= 202002L
#define CSV_HAS_CXX20
#endif
#if (defined(CMAKE_CXX_STANDARD) && CMAKE_CXX_STANDARD == 17) || CSV_CPLUSPLUS >= 201703L
#define CSV_HAS_CXX17
#endif
#if (defined(CMAKE_CXX_STANDARD) && CMAKE_CXX_STANDARD >= 14) || CSV_CPLUSPLUS >= 201402L
#define CSV_HAS_CXX14
#endif
// Include string_view BEFORE csv namespace to avoid namespace pollution issues
#ifdef CSV_HAS_CXX17
#include <string_view>
#else
#include "../external/string_view.hpp"
#endif
namespace csv {
#ifdef _MSC_VER
#pragma region Compatibility Macros
#endif
/**
* @def IF_CONSTEXPR
* Expands to `if constexpr` in C++17 and `if` otherwise
*
* @def CONSTEXPR_VALUE
* Expands to `constexpr` in C++17 and `const` otherwise.
* Mainly used for global variables.
*
* @def CONSTEXPR
* Expands to `constexpr` in decent compilers and `inline` otherwise.
* Intended for functions and methods.
*/
#define STATIC_ASSERT(x) static_assert(x, "Assertion failed")
#ifdef CSV_HAS_CXX17
/** @typedef string_view
* The string_view class used by this library.
*/
using string_view = std::string_view;
#else
/** @typedef string_view
* The string_view class used by this library.
*/
using string_view = nonstd::string_view;
#endif
#ifdef CSV_HAS_CXX17
#define IF_CONSTEXPR if constexpr
#define CONSTEXPR_VALUE constexpr
#define CONSTEXPR_17 constexpr
#else
#define IF_CONSTEXPR if
#define CONSTEXPR_VALUE const
#define CONSTEXPR_17 inline
#endif
#ifdef CSV_HAS_CXX14
template<bool B, class T = void>
using enable_if_t = std::enable_if_t<B, T>;
#define CONSTEXPR_14 constexpr
#define CONSTEXPR_VALUE_14 constexpr
#else
template<bool B, class T = void>
using enable_if_t = typename std::enable_if<B, T>::type;
#define CONSTEXPR_14 inline
#define CONSTEXPR_VALUE_14 const
#endif
#ifdef CSV_HAS_CXX17
template<typename F, typename... Args>
using invoke_result_t = typename std::invoke_result<F, Args...>::type;
#else
template<typename F, typename... Args>
using invoke_result_t = typename std::result_of<F(Args...)>::type;
#endif
// Resolves g++ bug with regard to constexpr methods
// See: https://stackoverflow.com/questions/36489369/constexpr-non-static-member-function-with-non-constexpr-constructor-gcc-clang-d
#if defined __GNUC__ && !defined __clang__
#if (__GNUC__ >= 7 &&__GNUC_MINOR__ >= 2) || (__GNUC__ >= 8)
#define CONSTEXPR constexpr
#endif
#else
#ifdef CSV_HAS_CXX17
#define CONSTEXPR constexpr
#endif
#endif
#ifndef CONSTEXPR
#define CONSTEXPR inline
#endif
#ifdef _MSC_VER
#pragma endregion
#endif
namespace internals {
// PAGE_SIZE macro could be already defined by the host system.
#if defined(PAGE_SIZE)
#undef PAGE_SIZE
#endif
// Get operating system specific details
#if defined(_WIN32)
inline int getpagesize() {
_SYSTEM_INFO sys_info = {};
GetSystemInfo(&sys_info);
return std::max(sys_info.dwPageSize, sys_info.dwAllocationGranularity);
}
const int PAGE_SIZE = getpagesize();
#elif defined(__linux__)
const int PAGE_SIZE = getpagesize();
#else
/** Size of a memory page in bytes. Used by
* csv::internals::CSVFieldArray when allocating blocks.
*/
const int PAGE_SIZE = 4096;
#endif
/** Chunk size for lazy-loading large CSV files
*
* The worker thread reads this many bytes at a time (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 basic_csv_parser.cpp MmapParser::next() for chunk transition logic
*/
constexpr size_t ITERATION_CHUNK_SIZE = 10000000; // 10MB
template<typename T>
inline bool is_equal(T a, T b, T epsilon = 0.001) {
/** Returns true if two floating point values are about the same */
static_assert(std::is_floating_point<T>::value, "T must be a floating point type.");
return std::abs(a - b) < epsilon;
}
/** @typedef ParseFlags
* An enum used for describing the significance of each character
* with respect to CSV parsing
*
* @see quote_escape_flag
*/
enum class ParseFlags {
QUOTE_ESCAPE_QUOTE = 0, /**< A quote inside or terminating a quote_escaped field */
QUOTE = 2 | 1, /**< Characters which may signify a quote escape */
NOT_SPECIAL = 4, /**< Characters with no special meaning or escaped delimiters and newlines */
DELIMITER = 4 | 2, /**< Characters which signify a new field */
NEWLINE = 4 | 2 | 1 /**< Characters which signify a new row */
};
/** Transform the ParseFlags given the context of whether or not the current
* field is quote escaped */
constexpr ParseFlags quote_escape_flag(ParseFlags flag, bool quote_escape) noexcept {
return (ParseFlags)((int)flag & ~((int)ParseFlags::QUOTE * quote_escape));
}
// Assumed to be true by parsing functions: allows for testing
// if an item is DELIMITER or NEWLINE with a >= statement
STATIC_ASSERT(ParseFlags::DELIMITER < ParseFlags::NEWLINE);
/** 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)
*/
STATIC_ASSERT(quote_escape_flag(ParseFlags::NOT_SPECIAL, false) == ParseFlags::NOT_SPECIAL);
STATIC_ASSERT(quote_escape_flag(ParseFlags::QUOTE, false) == ParseFlags::QUOTE);
STATIC_ASSERT(quote_escape_flag(ParseFlags::DELIMITER, false) == ParseFlags::DELIMITER);
STATIC_ASSERT(quote_escape_flag(ParseFlags::NEWLINE, false) == ParseFlags::NEWLINE);
STATIC_ASSERT(quote_escape_flag(ParseFlags::NOT_SPECIAL, true) == ParseFlags::NOT_SPECIAL);
STATIC_ASSERT(quote_escape_flag(ParseFlags::QUOTE, true) == ParseFlags::QUOTE_ESCAPE_QUOTE);
STATIC_ASSERT(quote_escape_flag(ParseFlags::DELIMITER, true) == ParseFlags::NOT_SPECIAL);
STATIC_ASSERT(quote_escape_flag(ParseFlags::NEWLINE, true) == ParseFlags::NOT_SPECIAL);
/** An array which maps ASCII chars to a parsing flag */
using ParseFlagMap = std::array<ParseFlags, 256>;
/** An array which maps ASCII chars to a flag indicating if it is whitespace */
using WhitespaceMap = std::array<bool, 256>;
}
/** Integer indicating a requested column wasn't found. */
constexpr int CSV_NOT_FOUND = -1;
/** Offset to convert char into array index. */
constexpr unsigned CHAR_OFFSET = std::numeric_limits<char>::is_signed ? 128 : 0;
}