MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
mtcore::csv::common Namespace Reference

Functions

u64 quoted_regions (u64 m)
 
Slice< const char > unquote_quoted (Slice< const char > data, Options opts)
 
template<WriterImpl WI>
Result< size_t, typename io::Writer< WI >::ErrType > decode (io::Writer< WI > &writer, Slice< const char > field, const Options options={})
 

Function Documentation

◆ decode()

template<WriterImpl WI>
Result< size_t, typename io::Writer< WI >::ErrType > mtcore::csv::common::decode ( io::Writer< WI > & writer,
Slice< const char > field,
const Options options = {} )

Definition at line 112 of file common.hpp.

113 {}) {
114 ensure(options.valid());
115 const auto quote = options.quote;
116 if (field.len < 2 || field[0] != quote) {
117 return writer.write_all(field);
118 }
119
120 const auto data = unquote_quoted(field, options);
121 Optional<char> nextChar;
122 Optional<char> curChar;
123 size_t ni = 1;
124 size_t written = 0;
125
126 if (0 < data.len) {
127 curChar = data[0];
128 if (1 < data.len) {
129 nextChar = data[1];
130 }
131 }
132
133 char c;
134 while (curChar.copy_if_present(c)) {
135 mtdefer {
136 ni++;
137 curChar = nextChar;
138 if (ni < data.len) {
139 nextChar = data[ni];
140 }
141 else {
142 nextChar = nullopt;
143 }
144 };
145
146 if (c != quote) {
147 if (auto r = writer.write(c); r.is_error()) {
148 return r.error();
149 }
150 ++written;
151 continue;
152 }
153
154 if (nextChar == quote) {
155 mtdefer {
156 ni += 1;
157 curChar = nextChar;
158 if (ni < data.len) {
159 nextChar = data[ni];
160 }
161 else {
162 nextChar = nullopt;
163 }
164 };
165
166 if (auto r = writer.write(c); r.is_error()) {
167 return r.error();
168 }
169 ++written;
170 }
171 }
172
173 return success(written);
174 }
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.
#define ensure(check,...)
Ensures that a check holds true, aborts the program if not true Will print error if the condition is ...
#define mtdefer
Defer statement that will mtdefer execution until the scope is left, at which point the code will run...
Success< void > success()
Creates a successful void Result object.
Definition result.hpp:398
Slice< const char > unquote_quoted(Slice< const char > data, Options opts)
Represents a value that may or may not exist (an "Optional" value) Similar concept to std::optional,...
Definition optional.hpp:235
bool copy_if_present(std::remove_const_t< T > &out) const noexcept
Copies a value to a reference output destination if a value is present Designed to be used by simple ...
Definition optional.hpp:327
bool valid() const
Here is the call graph for this function:
Here is the caller graph for this function:

◆ quoted_regions()

u64 mtcore::csv::common::quoted_regions ( u64 m)
nodiscard

◆ unquote_quoted()

Slice< const char > mtcore::csv::common::unquote_quoted ( Slice< const char > data,
Options opts )