MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
csv/slice.hpp
Go to the documentation of this file.
1#ifndef MTCORE_CSV_SLICE_HPP
2#define MTCORE_CSV_SLICE_HPP
3
8
9namespace mtcore::csv {
10 struct SliceParser {
13
25
26 struct RowField {
28 bool rowEnd = false;
29 };
30
31 struct FieldParser {
33
37 struct {
41 u64 prevFieldSeps = static_cast<u64>(1) << (chunkSize - 1);
47 bool fieldStart = true;
48 bool skip = false;
49 } state = {};
50
52
53 [[nodiscard]] size_t start_pos() const;
54
55 [[nodiscard]] bool done() const;
56
57 [[nodiscard]] bool has_next_chunk() const;
58
60 };
61 static_assert(is_iterator<FieldParser>, "Not an iterator");
62
63 struct Row {
66 size_t len = 0;
67
74 static_assert(is_iterator<Iter>, "Not an iterator");
75
76 [[nodiscard]] Iter iter() const;
77
78 [[nodiscard]] size_t size() const;
79
80 Field operator[](size_t i) const;
81 };
82
83 using IterElem = Row;
84
85 struct Iter {
87
88 using IterElem = Row;
89
91 [[nodiscard]] Optional<ReadError> error() const { return parser.error; };
92 };
93
94 static_assert(is_iterator<Iter>, "Not an iterator");
95
96 [[nodiscard]] Iter iter() const;
97
98 struct RowReader {
99 using ReadElem = Row;
102
104 Row cur;
105 size_t read = 0;
106 while (read < out.len && iter.next().copy_if_present(cur)) {
107 out[read++] = cur;
108 }
109 if (iter.parser.error.has_value()) {
110 return error(iter.parser.error.value());
111 }
112 return success(out.sub(0, read).to_const());
113 }
114 };
115
116 [[nodiscard]] io::Reader<RowReader> rows() const { return {RowReader{iter()}}; }
117
118 struct FieldReader {
119 using ReadElem = std::variant<RowEnd, Field>;
122 bool rowEnd = false;
123
125 RowField cur;
126 size_t read = 0;
127 if (!out.empty() && rowEnd) {
128 out[read++] = RowEnd{};
129 rowEnd = false;
130 }
131
132 while (read < out.len && iter.next().copy_if_present(cur)) {
133 out[read++] = cur.field;
134 if (cur.rowEnd) {
135 if (read < out.len) {
136 out[read++] = RowEnd{};
137 }
138 else {
139 rowEnd = true;
140 }
141 }
142 }
143
144 if (iter.error.has_value()) {
145 return error(iter.error.value());
146 }
147 return success(out.sub(0, read).to_const());
148 }
149 };
150
151 [[nodiscard]] io::Reader<FieldReader> fields() const { return {FieldReader{iter().parser}}; }
152 };
153} // namespace mtcore::csv
154
155#endif // MTCORE_CSV_SLICE_HPP
constexpr auto nullopt
Placeholder value for an empty Optional.
Definition optional.hpp:409
io::Writer< csv::impl::Writer< WI > > writer(io::Writer< WI > &underlying, Options opts={})
Creates a CSV writer which will encode the data before writing it out.
AllocationError
Error indicating failed allocation.
Success< void > success()
Creates a successful void Result object.
Definition result.hpp:398
Error< Underlying > error(Underlying err)
Creates an error.
Definition result.hpp:425
constexpr bool is_iterator
Boolean check for if something has the trait Iterator.
uint64_t u64
Alias for 64-bit unsigned ints.
Result< size_t, typename io::Writer< WI >::ErrType > decode(io::Writer< WI > &writer, Slice< const char > field, const Options options={})
Definition common.hpp:94
CSV namespace for CSV-related utilities.
Definition common.hpp:6
static constexpr u64 chunkSize
Definition common.hpp:7
Represents a memory allocator Exact behavior depends on the underlying VTable used Should use the a_*...
Represents a value that may or may not exist (an "Optional" value) Similar concept to std::optional,...
Definition optional.hpp:235
Represents a Result that may have an error (error code) or a success value A type of "void" means the...
Definition result.hpp:170
A Slice which is just a pointer + length Accessing elements through the array operator will do bounds...
constexpr Slice sub(size_t start) const noexcept
Gets a sub Slice from start.
constexpr bool empty() const noexcept
Checks if a Slice is empty.
constexpr Slice< std::add_const_t< T > > to_const() const noexcept
Converts to a const Slice.
CSV options for defining the CSV format.
Definition common.hpp:78
Special output-only type for indicating the end of a row.
FieldParser(const Slice< const char > &data, const Options &opts)
struct mtcore::csv::SliceParser::FieldParser::@151121121035013331367032363301340277215317161027 state
Result< Slice< const ReadElem >, ErrType > read(Slice< ReadElem > out)
std::variant< RowEnd, Field > ReadElem
Result< Slice< char >, AllocationError > copy(Allocator alloc) const
Result< size_t, typename io::Writer< WI >::ErrType > decode(io::Writer< WI > &writer) const
Definition csv/slice.hpp:19
Optional< ReadError > error() const
Definition csv/slice.hpp:91
Result< Slice< const ReadElem >, ErrType > read(Slice< ReadElem > out)
Field operator[](size_t i) const
Slice< const char > data
Definition csv/slice.hpp:64
Slice< const char > slice
Definition csv/slice.hpp:11
io::Reader< FieldReader > fields() const
io::Reader< RowReader > rows() const
A reader that reads data from some sort of stream or buffer Note: the data elements read should be tr...
Definition io/reader.hpp:42
A writer that writes data to some sort of stream or buffer Note: the data elements written should be ...
Definition io/writer.hpp:51