Vince's CSV Parser
Loading...
Searching...
No Matches
data_type.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <cstdint>
8
9#include "common.hpp"
10#include "../external/classify_scalar.hpp"
11
12namespace csv {
14 enum class DataType {
15 UNKNOWN = classify_scalar::scalar_invalid,
16 CSV_NULL = classify_scalar::scalar_null,
17 CSV_STRING = classify_scalar::scalar_string,
18 CSV_BOOL = classify_scalar::scalar_bool,
19 CSV_INT8 = classify_scalar::scalar_int8,
20 CSV_INT16 = classify_scalar::scalar_int16,
21 CSV_INT32 = classify_scalar::scalar_int32,
22 CSV_INT64 = classify_scalar::scalar_int64,
23 CSV_BIGINT = classify_scalar::scalar_bigint,
24 CSV_DOUBLE = classify_scalar::scalar_float,
25 CSV_TIMESTAMP = classify_scalar::scalar_timestamp,
26 scalar_custom_begin = classify_scalar::scalar_custom_begin - 1
27 };
28
29 static_assert(DataType::CSV_STRING < DataType::CSV_INT8, "String type should come before numeric types.");
30 static_assert(DataType::CSV_INT8 < DataType::CSV_INT64, "Smaller integer types should come before larger integer types.");
31 static_assert(DataType::CSV_INT64 < DataType::CSV_DOUBLE, "Integer types should come before floating point value types.");
32
33 namespace internals {
36 DataType type = DataType::UNKNOWN;
37 std::int64_t integer = 0;
38 long double floating = 0;
39 std::uint64_t timestamp = 0;
40 bool boolean = false;
41 };
42
44 CSVFieldScalar& value;
45
46 template<classify_scalar::ScalarKind Kind>
47 typename std::enable_if<
48 Kind == classify_scalar::scalar_int8
49 || Kind == classify_scalar::scalar_int16
50 || Kind == classify_scalar::scalar_int32
51 || Kind == classify_scalar::scalar_int64,
52 void>::type set(std::int64_t parsed) const noexcept {
53 value.integer = parsed;
54 }
55
56 template<classify_scalar::ScalarKind Kind>
57 typename std::enable_if<Kind == classify_scalar::scalar_float, void>::type set(long double parsed) const noexcept {
58 value.floating = parsed;
59 }
60
61 template<classify_scalar::ScalarKind Kind>
62 typename std::enable_if<Kind == classify_scalar::scalar_bool, void>::type set(bool parsed) const noexcept {
63 value.boolean = parsed;
64 }
65
66 template<classify_scalar::ScalarKind Kind>
67 typename std::enable_if<Kind == classify_scalar::scalar_timestamp, void>::type set(std::uint64_t parsed) const noexcept {
68 value.timestamp = parsed;
69 }
70 };
71
72 inline CSVFieldScalar classify_field_scalar(csv::string_view in) {
73 CSVFieldScalar scalar;
74 if (in.empty()) {
75 scalar.type = DataType::CSV_NULL;
76 return scalar;
77 }
78
79 const char* first = in.data();
80 const char* last = first + in.size();
81 typedef classify_scalar::policy_pack<
82 classify_scalar::builtin_numeric_policy<'.', false>,
83 classify_scalar::builtin_timestamp_policy,
84 classify_scalar::builtin_bool_policy
85 > csv_field_policy_pack;
86
87 scalar.type = classify_scalar::classify_scalar<
89 true>(first, last, CSVFieldScalarOutput{ scalar }, csv_field_policy_pack());
90 return scalar;
91 }
92
93#ifndef DOXYGEN_SHOULD_SKIP_THIS
95#endif
96
99 if (in.empty())
100 return DataType::CSV_NULL;
101
102 const char* first = in.data();
103 const char* last = first + in.size();
104 typedef classify_scalar::policy_pack<
105 classify_scalar::builtin_numeric_policy<'.', false>,
106 classify_scalar::builtin_timestamp_policy,
107 classify_scalar::builtin_bool_policy
108 > csv_field_policy_pack;
109
110 return classify_scalar::classify_scalar<
111 DataType,
112 true>(first, last, classify_scalar::classify_only_output(), csv_field_policy_pack());
113 }
114 }
115}
A standalone header file containing shared code.
DataType data_type(csv::string_view in)
Classify values using the CSVField scalar policy without materializing parsed output.
Definition data_type.hpp:98
The all encompassing namespace.
DataType
Enumerates the different CSV field types recognized by this library.
Definition data_type.hpp:14
@ CSV_TIMESTAMP
Timestamp value.
@ CSV_INT64
64-bit integer
@ CSV_DOUBLE
Floating point value.
@ CSV_BOOL
Boolean value.
@ CSV_NULL
Empty string.
@ CSV_BIGINT
Integer too large to fit in 64 bits.
@ CSV_INT16
16-bit integer
@ CSV_INT32
32-bit integer
@ CSV_INT8
8-bit integer
@ CSV_STRING
Non-scalar string.
std::string_view string_view
The string_view class used by this library.
Definition common.hpp:174
Cached scalar classification and parsed value for one CSV field.
Definition data_type.hpp:35