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/*
2
3Copyright 2025 Matthew Tolman
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17*/
18
19#ifndef MTCORE_CSV_SLICE_HPP
20#define MTCORE_CSV_SLICE_HPP
21
23#include "mtcore/csv/common.hpp"
24#include "mtcore/csv/writer.hpp"
25#include "mtcore/io/reader.hpp"
26
27namespace mtcore::csv {
28 struct SliceParser {
31
43
44 struct RowField {
46 bool rowEnd = false;
47 };
48
49 struct FieldParser {
51
55 struct {
59 u64 prevFieldSeps = static_cast<u64>(1) << (chunkSize - 1);
65 bool fieldStart = true;
66 bool skip = false;
67 } state = {};
68
70
71 [[nodiscard]] size_t start_pos() const;
72
73 [[nodiscard]] bool done() const;
74
75 [[nodiscard]] bool has_next_chunk() const;
76
78 };
79 static_assert(is_iterator<FieldParser>, "Not an iterator");
80
81 struct Row {
84 size_t len = 0;
85
92 static_assert(is_iterator<Iter>, "Not an iterator");
93
94 [[nodiscard]] Iter iter() const;
95
96 [[nodiscard]] size_t size() const;
97
98 Field operator[](size_t i) const;
99 };
100
101 using IterElem = Row;
102
103 struct Iter {
105
106 using IterElem = Row;
107
109 [[nodiscard]] Optional<ReadError> error() const { return parser.error; };
110 };
111
112 static_assert(is_iterator<Iter>, "Not an iterator");
113
114 [[nodiscard]] Iter iter() const;
115
116 struct RowReader {
117 using ReadElem = Row;
120
122 Row cur;
123 size_t read = 0;
124 while (read < out.len && iter.next().copy_if_present(cur)) {
125 out[read++] = cur;
126 }
127 if (iter.parser.error.has_value()) {
128 return error(iter.parser.error.value());
129 }
130 return success(out.sub(0, read).to_const());
131 }
132 };
133
134 [[nodiscard]] io::Reader<RowReader> rows() const { return {RowReader{iter()}}; }
135
136 struct FieldReader {
137 using ReadElem = std::variant<RowEnd, Field>;
140 bool rowEnd = false;
141
143 RowField cur;
144 size_t read = 0;
145 if (!out.empty() && rowEnd) {
146 out[read++] = RowEnd{};
147 rowEnd = false;
148 }
149
150 while (read < out.len && iter.next().copy_if_present(cur)) {
151 out[read++] = cur.field;
152 if (cur.rowEnd) {
153 if (read < out.len) {
154 out[read++] = RowEnd{};
155 }
156 else {
157 rowEnd = true;
158 }
159 }
160 }
161
162 if (iter.error.has_value()) {
163 return error(iter.error.value());
164 }
165 return success(out.sub(0, read).to_const());
166 }
167 };
168
169 [[nodiscard]] io::Reader<FieldReader> fields() const { return {FieldReader{iter().parser}}; }
170 };
171} // namespace mtcore::csv
172
173#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:112
CSV namespace for CSV-related utilities.
Definition common.hpp:24
static constexpr u64 chunkSize
Definition common.hpp:25
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:96
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:37
Optional< ReadError > error() const
Result< Slice< const ReadElem >, ErrType > read(Slice< ReadElem > out)
Field operator[](size_t i) const
Slice< const char > data
Definition csv/slice.hpp:82
Slice< const char > slice
Definition csv/slice.hpp:29
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 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