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 94 of file common.hpp.

95 {}) {
96 ensure(options.valid());
97 const auto quote = options.quote;
98 if (field.len < 2 || field[0] != quote) {
99 return writer.write_all(field);
100 }
101
102 const auto data = unquote_quoted(field, options);
103 Optional<char> nextChar;
104 Optional<char> curChar;
105 size_t ni = 1;
106 size_t written = 0;
107
108 if (0 < data.len) {
109 curChar = data[0];
110 if (1 < data.len) {
111 nextChar = data[1];
112 }
113 }
114
115 char c;
116 while (curChar.copy_if_present(c)) {
117 mtdefer {
118 ni++;
119 curChar = nextChar;
120 if (ni < data.len) {
121 nextChar = data[ni];
122 }
123 else {
124 nextChar = nullopt;
125 }
126 };
127
128 if (c != quote) {
129 if (auto r = writer.write(c); r.is_error()) {
130 return r.error();
131 }
132 ++written;
133 continue;
134 }
135
136 if (nextChar == quote) {
137 mtdefer {
138 ni += 1;
139 curChar = nextChar;
140 if (ni < data.len) {
141 nextChar = data[ni];
142 }
143 else {
144 nextChar = nullopt;
145 }
146 };
147
148 if (auto r = writer.write(c); r.is_error()) {
149 return r.error();
150 }
151 ++written;
152 }
153 }
154
155 return success(written);
156 }
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 )