44 CSV_CONST CONSTEXPR_14
45 long double pow10(
const T& n)
noexcept {
46 static_assert(std::is_integral<T>::value,
"pow10 only supports integral exponents");
48 long double multiplicand = n > 0 ? 10 : 0.1,
52 T iterations = n > 0 ? n : -n;
54 for (T i = 0; i < iterations; i++) {
63 CSV_CONST CONSTEXPR_14
64 long double pow10(
const unsigned& n)
noexcept {
65 long double multiplicand = n > 0 ? 10 : 0.1,
68 for (
unsigned i = 0; i < n; i++) {
75#ifndef DOXYGEN_SHOULD_SKIP_THIS
77 constexpr DataType int_type_arr[8] = {
90 static_assert(std::is_integral<T>::value,
"T should be an integral type.");
91 static_assert(
sizeof(T) <= 8,
"Byte size must be no greater than 8.");
92 return int_type_arr[
sizeof(T) - 1];
102 const char decimalsymbol =
'.');
111 template<
size_t Bytes>
113 static_assert(Bytes == 1 || Bytes == 2 || Bytes == 4 || Bytes == 8,
114 "Bytes must be a power of 2 below 8.");
116 CSV_MSVC_PUSH_DISABLE(4127)
118 return (
long double)std::numeric_limits<signed char>::max();
121 return (
long double)std::numeric_limits<short>::max();
124 return (
long double)std::numeric_limits<int>::max();
127 return (
long double)std::numeric_limits<long int>::max();
130 return (
long double)std::numeric_limits<long long int>::max();
138 template<
size_t Bytes>
140 static_assert(Bytes == 1 || Bytes == 2 || Bytes == 4 || Bytes == 8,
141 "Bytes must be a power of 2 below 8.");
143 CSV_MSVC_PUSH_DISABLE(4127)
145 return (
long double)std::numeric_limits<unsigned char>::max();
148 return (
long double)std::numeric_limits<unsigned short>::max();
151 return (
long double)std::numeric_limits<unsigned int>::max();
154 return (
long double)std::numeric_limits<unsigned long int>::max();
157 return (
long double)std::numeric_limits<unsigned long long int>::max();
190 CSV_PRIVATE CONSTEXPR_14
193 const long double& coeff,
194 long double *
const out) {
195 long double exponent = 0;
196 auto result =
data_type(exponential_part, &exponent);
200 if (out) *out = coeff *
pow10(
static_cast<long long>(exponent));
210 CSV_PRIVATE CSV_PURE CONSTEXPR_14
215 if (number <= internals::CSV_INT8_MAX)
217 else if (number <= internals::CSV_INT16_MAX)
219 else if (number <= internals::CSV_INT32_MAX)
221 else if (number <= internals::CSV_INT64_MAX)
246 bool ws_allowed =
true,
248 digit_allowed =
true,
253 unsigned places_after_decimal = 0;
254 long double integral_part = 0,
257 for (
size_t i = 0, ilen = in.size(); i < ilen; i++) {
258 const char& current = in[i];
263 if (isdigit(in[i - 1])) {
264 digit_allowed =
false;
292 if (prob_float || (i && i + 1 < ilen && isdigit(in[i - 1]))) {
293 size_t exponent_start_idx = i + 1;
297 if (in[i + 1] ==
'+') {
298 exponent_start_idx++;
302 in.substr(exponent_start_idx),
303 is_negative ? -(integral_part + decimal_part) : integral_part + decimal_part,
311 short digit =
static_cast<short>(current -
'0');
312 if (digit >= 0 && digit <= 9) {
323 decimal_part += digit /
pow10(++places_after_decimal);
325 integral_part = (integral_part * 10) + digit;
328 else if (dot_allowed && current == decimalSymbol) {
340 long double number = integral_part + decimal_part;
342 *out = is_negative ? -number : number;
A standalone header file containing shared code.
#define IF_CONSTEXPR
Expands to if constexpr in C++17 and if otherwise.
CONSTEXPR_VALUE_14 long double CSV_INT16_MAX
Largest number that can be stored in a 16-bit integer.
CONSTEXPR_VALUE_14 long double CSV_INT32_MAX
Largest number that can be stored in a 32-bit integer.
CONSTEXPR_VALUE_14 long double CSV_UINT16_MAX
Largest number that can be stored in a 16-bit unsigned integer.
CONSTEXPR_14 DataType data_type(csv::string_view in, long double *const out, const char decimalSymbol)
Distinguishes numeric from other text values.
CONSTEXPR_VALUE_14 long double CSV_UINT32_MAX
Largest number that can be stored in a 32-bit unsigned integer.
CSV_CONST CONSTEXPR_14 long double pow10(const T &n) noexcept
Compute 10 to the power of n.
CONSTEXPR_VALUE_14 long double CSV_INT64_MAX
Largest number that can be stored in a 64-bit integer.
CONSTEXPR_VALUE_14 long double CSV_INT8_MAX
Largest number that can be stored in a 8-bit integer.
CONSTEXPR_VALUE_14 long double CSV_UINT64_MAX
Largest number that can be stored in a 64-bit unsigned integer.
CONSTEXPR_VALUE_14 long double CSV_UINT8_MAX
Largest number that can be stored in a 8-bit ungisned integer.
CONSTEXPR_14 long double get_uint_max()
Given a byte size, return the largest number than can be stored in an unsigned integer of that size.
CSV_PRIVATE CONSTEXPR_14 DataType _process_potential_exponential(csv::string_view exponential_part, const long double &coeff, long double *const out)
Given a pointer to the start of what is start of the exponential part of a number written (possibly) ...
CONSTEXPR_14 long double get_int_max()
Given a byte size, return the largest number than can be stored in an integer of that size.
CSV_PRIVATE CSV_PURE CONSTEXPR_14 DataType _determine_integral_type(const long double &number) noexcept
Given the absolute value of an integer, determine what numeric type it fits in.
The all encompassing namespace.
DataType
Enumerates the different CSV field types that are recognized by this library.
@ CSV_INT64
64-bit integer (long long on MSVC/GCC)
@ CSV_DOUBLE
Floating point value.
@ CSV_BIGINT
Value too big to fit in a 64-bit in.
@ CSV_INT16
16-bit integer (short on MSVC/GCC)
@ CSV_INT32
32-bit integer (int on MSVC/GCC)
@ CSV_STRING
Non-numeric string.
nonstd::string_view string_view
The string_view class used by this library.