2021-01-17 02:15:06 +01:00
|
|
|
#pragma once
|
2021-02-14 01:59:06 +01:00
|
|
|
#include "common.hpp"
|
2023-06-29 23:41:03 +02:00
|
|
|
#include "exception.hpp"
|
2021-02-02 21:43:36 +01:00
|
|
|
#include "setup.hpp"
|
2021-01-17 02:15:06 +01:00
|
|
|
#include "type_traits.hpp"
|
2021-02-06 00:55:05 +01:00
|
|
|
#include <algorithm>
|
2021-01-17 02:15:06 +01:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2021-02-05 18:28:10 +01:00
|
|
|
#include <memory>
|
2021-01-17 02:15:06 +01:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace ss {
|
|
|
|
|
2023-06-29 23:41:03 +02:00
|
|
|
template <typename... Options>
|
2021-01-17 02:15:06 +01:00
|
|
|
class splitter {
|
2021-01-25 00:16:55 +01:00
|
|
|
private:
|
2023-06-29 23:41:03 +02:00
|
|
|
using quote = typename setup<Options...>::quote;
|
|
|
|
using trim_left = typename setup<Options...>::trim_left;
|
|
|
|
using trim_right = typename setup<Options...>::trim_right;
|
|
|
|
using escape = typename setup<Options...>::escape;
|
|
|
|
using multiline = typename setup<Options...>::multiline;
|
|
|
|
|
|
|
|
constexpr static auto string_error = setup<Options...>::string_error;
|
|
|
|
constexpr static auto throw_on_error = setup<Options...>::throw_on_error;
|
2021-01-25 00:16:55 +01:00
|
|
|
constexpr static auto is_const_line = !quote::enabled && !escape::enabled;
|
2021-01-31 23:08:46 +01:00
|
|
|
|
2023-02-12 12:45:49 +01:00
|
|
|
using error_type = std::conditional_t<string_error, std::string, bool>;
|
2021-02-13 01:14:25 +01:00
|
|
|
|
2021-01-31 23:08:46 +01:00
|
|
|
public:
|
2023-02-12 12:45:49 +01:00
|
|
|
using line_ptr_type = std::conditional_t<is_const_line, const char*, char*>;
|
2021-01-25 00:16:55 +01:00
|
|
|
|
|
|
|
bool valid() const {
|
2021-02-13 01:14:25 +01:00
|
|
|
if constexpr (string_error) {
|
|
|
|
return error_.empty();
|
2023-07-16 20:26:09 +02:00
|
|
|
} else if constexpr (throw_on_error) {
|
|
|
|
return true;
|
2021-02-13 01:14:25 +01:00
|
|
|
} else {
|
|
|
|
return !error_;
|
|
|
|
}
|
2021-01-25 00:16:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::string& error_msg() const {
|
2021-02-14 01:59:06 +01:00
|
|
|
assert_string_error_defined<string_error>();
|
2021-02-13 01:14:25 +01:00
|
|
|
return error_;
|
2021-01-25 00:16:55 +01:00
|
|
|
}
|
|
|
|
|
2021-02-13 01:14:25 +01:00
|
|
|
bool unterminated_quote() const {
|
|
|
|
return unterminated_quote_;
|
2021-01-25 00:16:55 +01:00
|
|
|
}
|
|
|
|
|
2021-02-14 01:59:06 +01:00
|
|
|
const split_data& split(line_ptr_type new_line,
|
|
|
|
const std::string& delimiter = default_delimiter) {
|
|
|
|
split_data_.clear();
|
2021-02-21 02:49:23 +01:00
|
|
|
line_ = new_line;
|
|
|
|
begin_ = line_;
|
|
|
|
return split_impl_select_delim(delimiter);
|
2021-01-25 00:16:55 +01:00
|
|
|
}
|
|
|
|
|
2021-02-13 01:14:25 +01:00
|
|
|
private:
|
|
|
|
////////////////
|
|
|
|
// resplit
|
|
|
|
////////////////
|
|
|
|
|
2021-02-21 02:49:23 +01:00
|
|
|
// number of characters the end of line is shifted backwards
|
|
|
|
size_t size_shifted() const {
|
|
|
|
return escaped_;
|
|
|
|
}
|
|
|
|
|
2021-01-25 00:16:55 +01:00
|
|
|
void adjust_ranges(const char* old_line) {
|
2021-02-14 01:59:06 +01:00
|
|
|
for (auto& [begin, end] : split_data_) {
|
2021-01-25 00:16:55 +01:00
|
|
|
begin = begin - old_line + line_;
|
|
|
|
end = end - old_line + line_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 01:59:06 +01:00
|
|
|
const split_data& resplit(
|
2021-02-02 02:17:31 +01:00
|
|
|
line_ptr_type new_line, ssize_t new_size,
|
|
|
|
const std::string& delimiter = default_delimiter) {
|
2021-01-25 00:16:55 +01:00
|
|
|
|
|
|
|
// resplitting, continue from last slice
|
2023-07-10 22:21:07 +02:00
|
|
|
if (!quote::enabled || !multiline::enabled || split_data_.empty() ||
|
|
|
|
!unterminated_quote()) {
|
2023-08-05 11:45:31 +02:00
|
|
|
handle_error_invalid_resplit();
|
2023-07-10 22:21:07 +02:00
|
|
|
return split_data_;
|
2021-02-21 02:49:23 +01:00
|
|
|
}
|
2021-01-25 00:16:55 +01:00
|
|
|
|
2021-02-21 02:49:23 +01:00
|
|
|
const auto [old_line, old_begin] = *std::prev(split_data_.end());
|
|
|
|
size_t begin = old_begin - old_line - 1;
|
|
|
|
|
|
|
|
// safety measure
|
|
|
|
if (new_size != -1 && static_cast<size_t>(new_size) < begin) {
|
2023-08-05 11:45:31 +02:00
|
|
|
handle_error_invalid_resplit();
|
2021-02-21 02:49:23 +01:00
|
|
|
return split_data_;
|
2021-01-25 00:16:55 +01:00
|
|
|
}
|
|
|
|
|
2021-02-21 02:49:23 +01:00
|
|
|
// if unterminated quote, the last element is junk
|
|
|
|
split_data_.pop_back();
|
|
|
|
|
|
|
|
line_ = new_line;
|
|
|
|
adjust_ranges(old_line);
|
|
|
|
|
|
|
|
begin_ = line_ + begin;
|
|
|
|
end_ = line_ - old_line + end_ - escaped_;
|
|
|
|
curr_ = end_;
|
|
|
|
|
|
|
|
resplitting_ = true;
|
|
|
|
|
2021-01-25 00:16:55 +01:00
|
|
|
return split_impl_select_delim(delimiter);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////
|
|
|
|
// error
|
|
|
|
////////////////
|
|
|
|
|
|
|
|
void clear_error() {
|
2021-02-13 01:14:25 +01:00
|
|
|
if constexpr (string_error) {
|
|
|
|
error_.clear();
|
2023-07-10 02:39:24 +02:00
|
|
|
} else if constexpr (!throw_on_error) {
|
2021-02-13 01:14:25 +01:00
|
|
|
error_ = false;
|
|
|
|
}
|
2021-01-25 00:16:55 +01:00
|
|
|
unterminated_quote_ = false;
|
|
|
|
}
|
|
|
|
|
2023-08-05 11:45:31 +02:00
|
|
|
void handle_error_empty_delimiter() {
|
2023-07-10 02:39:24 +02:00
|
|
|
constexpr static auto error_msg = "empty delimiter";
|
|
|
|
|
2021-02-13 01:14:25 +01:00
|
|
|
if constexpr (string_error) {
|
|
|
|
error_.clear();
|
2023-07-10 02:39:24 +02:00
|
|
|
error_.append(error_msg);
|
|
|
|
} else if constexpr (throw_on_error) {
|
|
|
|
throw ss::exception{error_msg};
|
2021-01-25 00:16:55 +01:00
|
|
|
} else {
|
2021-02-13 01:14:25 +01:00
|
|
|
error_ = true;
|
2021-01-25 00:16:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-05 11:45:31 +02:00
|
|
|
void handle_error_mismatched_quote(size_t n) {
|
2023-07-10 02:39:24 +02:00
|
|
|
constexpr static auto error_msg = "mismatched quote at position: ";
|
|
|
|
|
2021-02-13 01:14:25 +01:00
|
|
|
if constexpr (string_error) {
|
|
|
|
error_.clear();
|
2023-07-10 02:39:24 +02:00
|
|
|
error_.append(error_msg + std::to_string(n));
|
|
|
|
} else if constexpr (throw_on_error) {
|
|
|
|
throw ss::exception{error_msg + std::to_string(n)};
|
2021-02-02 02:17:31 +01:00
|
|
|
} else {
|
2021-02-13 01:14:25 +01:00
|
|
|
error_ = true;
|
2021-02-02 02:17:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-05 11:45:31 +02:00
|
|
|
void handle_error_unterminated_escape() {
|
2023-07-10 02:39:24 +02:00
|
|
|
constexpr static auto error_msg =
|
|
|
|
"unterminated escape at the end of the line";
|
|
|
|
|
2021-02-21 02:49:23 +01:00
|
|
|
if constexpr (string_error) {
|
|
|
|
error_.clear();
|
2023-07-10 02:39:24 +02:00
|
|
|
error_.append(error_msg);
|
|
|
|
} else if constexpr (throw_on_error) {
|
|
|
|
throw ss::exception{error_msg};
|
2021-02-21 02:49:23 +01:00
|
|
|
} else {
|
|
|
|
error_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-05 11:45:31 +02:00
|
|
|
void handle_error_unterminated_quote() {
|
2023-07-10 02:39:24 +02:00
|
|
|
constexpr static auto error_msg = "unterminated quote";
|
|
|
|
|
2021-02-13 01:14:25 +01:00
|
|
|
if constexpr (string_error) {
|
|
|
|
error_.clear();
|
2023-07-10 02:39:24 +02:00
|
|
|
error_.append(error_msg);
|
|
|
|
} else if constexpr (throw_on_error) {
|
|
|
|
throw ss::exception{error_msg};
|
2021-01-25 00:16:55 +01:00
|
|
|
} else {
|
2021-02-13 01:14:25 +01:00
|
|
|
error_ = true;
|
2021-01-25 00:16:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-05 11:45:31 +02:00
|
|
|
void handle_error_invalid_resplit() {
|
2023-07-10 02:39:24 +02:00
|
|
|
constexpr static auto error_msg =
|
|
|
|
"invalid resplit, new line must be longer"
|
|
|
|
"than the end of the last slice";
|
|
|
|
|
2021-02-13 01:14:25 +01:00
|
|
|
if constexpr (string_error) {
|
|
|
|
error_.clear();
|
2023-07-10 02:39:24 +02:00
|
|
|
error_.append(error_msg);
|
|
|
|
} else if constexpr (throw_on_error) {
|
|
|
|
throw ss::exception{error_msg};
|
2021-01-25 00:16:55 +01:00
|
|
|
} else {
|
2021-02-13 01:14:25 +01:00
|
|
|
error_ = true;
|
2021-01-25 00:16:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////
|
|
|
|
// matching
|
|
|
|
////////////////
|
|
|
|
|
|
|
|
bool match(const char* const curr, char delim) {
|
|
|
|
return *curr == delim;
|
2021-01-17 02:15:06 +01:00
|
|
|
};
|
|
|
|
|
2021-01-25 00:16:55 +01:00
|
|
|
bool match(const char* const curr, const std::string& delim) {
|
|
|
|
return strncmp(curr, delim.c_str(), delim.size()) == 0;
|
2021-01-17 02:15:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
size_t delimiter_size(char) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t delimiter_size(const std::string& delim) {
|
|
|
|
return delim.size();
|
|
|
|
}
|
|
|
|
|
2021-02-14 01:59:06 +01:00
|
|
|
void trim_left_if_enabled(line_ptr_type& curr) {
|
|
|
|
if constexpr (trim_left::enabled) {
|
|
|
|
while (trim_left::match(*curr)) {
|
|
|
|
++curr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void trim_right_if_enabled(line_ptr_type& curr) {
|
|
|
|
if constexpr (trim_right::enabled) {
|
|
|
|
while (trim_right::match(*curr)) {
|
2021-01-17 02:15:06 +01:00
|
|
|
++curr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Delim>
|
2021-01-25 00:16:55 +01:00
|
|
|
std::tuple<size_t, bool> match_delimiter(line_ptr_type begin,
|
|
|
|
const Delim& delim) {
|
|
|
|
line_ptr_type end = begin;
|
2021-01-17 02:15:06 +01:00
|
|
|
|
2021-02-14 01:59:06 +01:00
|
|
|
trim_right_if_enabled(end);
|
2021-01-17 02:15:06 +01:00
|
|
|
|
|
|
|
// just spacing
|
2021-01-25 00:16:55 +01:00
|
|
|
if (*end == '\0') {
|
2021-01-17 02:15:06 +01:00
|
|
|
return {0, false};
|
|
|
|
}
|
|
|
|
|
|
|
|
// not a delimiter
|
2021-01-25 00:16:55 +01:00
|
|
|
if (!match(end, delim)) {
|
|
|
|
shift_if_escaped(end);
|
|
|
|
return {1 + end - begin, false};
|
2021-01-17 02:15:06 +01:00
|
|
|
}
|
|
|
|
|
2021-01-25 00:16:55 +01:00
|
|
|
end += delimiter_size(delim);
|
2021-02-14 01:59:06 +01:00
|
|
|
trim_left_if_enabled(end);
|
2021-01-17 02:15:06 +01:00
|
|
|
|
|
|
|
// delimiter
|
2021-01-25 00:16:55 +01:00
|
|
|
return {end - begin, true};
|
2021-01-17 02:15:06 +01:00
|
|
|
}
|
|
|
|
|
2021-01-25 00:16:55 +01:00
|
|
|
////////////////
|
2021-02-06 00:55:05 +01:00
|
|
|
// shifting
|
2021-01-25 00:16:55 +01:00
|
|
|
////////////////
|
|
|
|
|
2021-02-06 21:08:59 +01:00
|
|
|
void shift_if_escaped(line_ptr_type& curr) {
|
|
|
|
if constexpr (escape::enabled) {
|
|
|
|
if (escape::match(*curr)) {
|
2021-02-21 02:49:23 +01:00
|
|
|
if (curr[1] == '\0') {
|
2023-07-10 02:39:24 +02:00
|
|
|
if constexpr (!multiline::enabled) {
|
2023-08-05 11:45:31 +02:00
|
|
|
handle_error_unterminated_escape();
|
2023-07-10 02:39:24 +02:00
|
|
|
}
|
2021-02-21 02:49:23 +01:00
|
|
|
done_ = true;
|
|
|
|
return;
|
|
|
|
}
|
2021-02-06 21:08:59 +01:00
|
|
|
shift_and_jump_escape();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 00:55:05 +01:00
|
|
|
void shift_and_jump_escape() {
|
|
|
|
shift_and_set_current();
|
2021-02-06 21:08:59 +01:00
|
|
|
if constexpr (!is_const_line) {
|
|
|
|
++escaped_;
|
|
|
|
}
|
2021-02-06 00:55:05 +01:00
|
|
|
++end_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void shift_push_and_start_next(size_t n) {
|
|
|
|
shift_and_push();
|
2021-01-25 00:16:55 +01:00
|
|
|
begin_ = end_ + n;
|
|
|
|
}
|
|
|
|
|
2021-02-21 02:49:23 +01:00
|
|
|
void shift_and_push() {
|
|
|
|
shift_and_set_current();
|
|
|
|
split_data_.emplace_back(begin_, curr_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void shift_and_set_current() {
|
|
|
|
if constexpr (!is_const_line) {
|
|
|
|
if (escaped_ > 0) {
|
|
|
|
std::copy_n(curr_ + escaped_, end_ - curr_ - escaped_, curr_);
|
|
|
|
curr_ = end_ - escaped_;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
curr_ = end_;
|
|
|
|
}
|
|
|
|
|
2021-02-06 00:55:05 +01:00
|
|
|
////////////////
|
|
|
|
// split impl
|
|
|
|
////////////////
|
|
|
|
|
2021-02-14 01:59:06 +01:00
|
|
|
const split_data& split_impl_select_delim(
|
2021-01-25 00:16:55 +01:00
|
|
|
const std::string& delimiter = default_delimiter) {
|
2021-01-30 21:52:32 +01:00
|
|
|
clear_error();
|
2021-01-25 00:16:55 +01:00
|
|
|
switch (delimiter.size()) {
|
2021-01-17 02:15:06 +01:00
|
|
|
case 0:
|
2023-08-05 11:45:31 +02:00
|
|
|
handle_error_empty_delimiter();
|
2021-02-14 01:59:06 +01:00
|
|
|
return split_data_;
|
2021-01-17 02:15:06 +01:00
|
|
|
case 1:
|
2021-01-25 00:16:55 +01:00
|
|
|
return split_impl(delimiter[0]);
|
2021-01-17 02:15:06 +01:00
|
|
|
default:
|
2021-01-25 00:16:55 +01:00
|
|
|
return split_impl(delimiter);
|
2021-01-17 02:15:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Delim>
|
2021-02-14 01:59:06 +01:00
|
|
|
const split_data& split_impl(const Delim& delim) {
|
2021-01-30 21:52:32 +01:00
|
|
|
|
2021-02-14 01:59:06 +01:00
|
|
|
trim_left_if_enabled(begin_);
|
2021-01-17 02:15:06 +01:00
|
|
|
|
2021-02-06 00:55:05 +01:00
|
|
|
for (done_ = false; !done_; read(delim))
|
2021-02-05 18:28:10 +01:00
|
|
|
;
|
2021-01-17 02:15:06 +01:00
|
|
|
|
2021-02-14 01:59:06 +01:00
|
|
|
return split_data_;
|
2021-01-17 02:15:06 +01:00
|
|
|
}
|
|
|
|
|
2021-01-25 00:16:55 +01:00
|
|
|
////////////////
|
2021-02-06 00:55:05 +01:00
|
|
|
// reading
|
2021-01-25 00:16:55 +01:00
|
|
|
////////////////
|
|
|
|
|
2021-02-05 18:28:10 +01:00
|
|
|
template <typename Delim>
|
2021-02-06 00:55:05 +01:00
|
|
|
void read(const Delim& delim) {
|
|
|
|
escaped_ = 0;
|
2021-01-17 02:15:06 +01:00
|
|
|
if constexpr (quote::enabled) {
|
2021-02-21 02:49:23 +01:00
|
|
|
if constexpr (multiline::enabled) {
|
|
|
|
if (resplitting_) {
|
|
|
|
resplitting_ = false;
|
|
|
|
++begin_;
|
|
|
|
read_quoted(delim);
|
|
|
|
return;
|
|
|
|
}
|
2021-02-20 15:53:18 +01:00
|
|
|
}
|
2021-01-25 00:16:55 +01:00
|
|
|
if (quote::match(*begin_)) {
|
2021-02-05 18:28:10 +01:00
|
|
|
curr_ = end_ = ++begin_;
|
2021-02-06 00:55:05 +01:00
|
|
|
read_quoted(delim);
|
2021-01-17 02:15:06 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-02-05 18:28:10 +01:00
|
|
|
curr_ = end_ = begin_;
|
2021-02-06 00:55:05 +01:00
|
|
|
read_normal(delim);
|
2021-01-17 02:15:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Delim>
|
2021-02-06 00:55:05 +01:00
|
|
|
void read_normal(const Delim& delim) {
|
2021-01-17 02:15:06 +01:00
|
|
|
while (true) {
|
2021-01-25 00:16:55 +01:00
|
|
|
auto [width, valid] = match_delimiter(end_, delim);
|
2021-01-17 02:15:06 +01:00
|
|
|
|
|
|
|
if (!valid) {
|
2021-02-05 18:28:10 +01:00
|
|
|
// not a delimiter
|
2021-01-17 02:15:06 +01:00
|
|
|
if (width == 0) {
|
|
|
|
// eol
|
2021-02-06 00:55:05 +01:00
|
|
|
shift_and_push();
|
2021-02-05 18:28:10 +01:00
|
|
|
done_ = true;
|
2021-01-17 02:15:06 +01:00
|
|
|
break;
|
|
|
|
} else {
|
2021-02-06 00:55:05 +01:00
|
|
|
end_ += width;
|
2021-01-17 02:15:06 +01:00
|
|
|
continue;
|
|
|
|
}
|
2021-02-05 18:28:10 +01:00
|
|
|
} else {
|
|
|
|
// found delimiter
|
2021-02-06 00:55:05 +01:00
|
|
|
shift_push_and_start_next(width);
|
2021-02-05 18:28:10 +01:00
|
|
|
break;
|
2021-01-17 02:15:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Delim>
|
2021-02-06 00:55:05 +01:00
|
|
|
void read_quoted(const Delim& delim) {
|
2021-01-17 02:15:06 +01:00
|
|
|
if constexpr (quote::enabled) {
|
|
|
|
while (true) {
|
2021-02-05 18:28:10 +01:00
|
|
|
if (!quote::match(*end_)) {
|
|
|
|
if constexpr (escape::enabled) {
|
|
|
|
if (escape::match(*end_)) {
|
2021-02-21 02:49:23 +01:00
|
|
|
if (end_[1] == '\0') {
|
|
|
|
// eol, unterminated escape
|
|
|
|
// eg: ... "hel\\0
|
2023-07-10 02:39:24 +02:00
|
|
|
if constexpr (!multiline::enabled) {
|
2023-08-05 11:45:31 +02:00
|
|
|
handle_error_unterminated_escape();
|
2023-07-10 02:39:24 +02:00
|
|
|
}
|
2021-02-21 02:49:23 +01:00
|
|
|
done_ = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// not eol
|
|
|
|
|
2021-02-06 00:55:05 +01:00
|
|
|
shift_and_jump_escape();
|
2021-02-05 18:28:10 +01:00
|
|
|
++end_;
|
|
|
|
continue;
|
|
|
|
}
|
2021-01-17 02:15:06 +01:00
|
|
|
}
|
2021-02-21 02:49:23 +01:00
|
|
|
// not escaped
|
2021-01-17 02:15:06 +01:00
|
|
|
|
2021-02-21 02:49:23 +01:00
|
|
|
// eol, unterminated quote error
|
2021-02-05 18:28:10 +01:00
|
|
|
// eg: ..."hell\0 -> quote not terminated
|
|
|
|
if (*end_ == '\0') {
|
2021-02-20 15:53:18 +01:00
|
|
|
shift_and_set_current();
|
2023-07-10 02:39:24 +02:00
|
|
|
unterminated_quote_ = true;
|
|
|
|
if constexpr (!multiline::enabled) {
|
2023-08-05 11:45:31 +02:00
|
|
|
handle_error_unterminated_quote();
|
2023-07-10 02:39:24 +02:00
|
|
|
}
|
2021-02-14 01:59:06 +01:00
|
|
|
split_data_.emplace_back(line_, begin_);
|
2021-02-05 18:28:10 +01:00
|
|
|
done_ = true;
|
2021-01-17 02:15:06 +01:00
|
|
|
break;
|
|
|
|
}
|
2021-02-21 02:49:23 +01:00
|
|
|
// not eol
|
|
|
|
|
2021-02-06 00:55:05 +01:00
|
|
|
++end_;
|
2021-02-05 18:28:10 +01:00
|
|
|
continue;
|
|
|
|
}
|
2021-02-21 02:49:23 +01:00
|
|
|
// quote found
|
|
|
|
// ...
|
2021-01-17 02:15:06 +01:00
|
|
|
|
2021-02-05 18:28:10 +01:00
|
|
|
auto [width, valid] = match_delimiter(end_ + 1, delim);
|
|
|
|
|
|
|
|
// delimiter
|
|
|
|
if (valid) {
|
2021-02-06 00:55:05 +01:00
|
|
|
shift_push_and_start_next(width + 1);
|
2021-01-17 02:15:06 +01:00
|
|
|
break;
|
|
|
|
}
|
2021-02-21 02:49:23 +01:00
|
|
|
// not delimiter
|
2021-01-17 02:15:06 +01:00
|
|
|
|
2021-02-05 18:28:10 +01:00
|
|
|
// double quote
|
|
|
|
// eg: ...,"hel""lo",... -> hel"lo
|
|
|
|
if (quote::match(end_[1])) {
|
2021-02-06 00:55:05 +01:00
|
|
|
shift_and_jump_escape();
|
2021-02-05 18:28:10 +01:00
|
|
|
++end_;
|
|
|
|
continue;
|
2021-01-17 02:15:06 +01:00
|
|
|
}
|
2021-02-21 02:49:23 +01:00
|
|
|
// not double quote
|
2021-01-17 02:15:06 +01:00
|
|
|
|
2021-02-05 18:28:10 +01:00
|
|
|
if (width == 0) {
|
|
|
|
// eol
|
|
|
|
// eg: ...,"hello" \0 -> hello
|
|
|
|
// eg no trim: ...,"hello"\0 -> hello
|
2021-02-06 00:55:05 +01:00
|
|
|
shift_and_push();
|
2021-02-05 18:28:10 +01:00
|
|
|
} else {
|
|
|
|
// mismatched quote
|
|
|
|
// eg: ...,"hel"lo,... -> error
|
2023-08-05 11:45:31 +02:00
|
|
|
handle_error_mismatched_quote(end_ - line_);
|
2021-02-14 01:59:06 +01:00
|
|
|
split_data_.emplace_back(line_, begin_);
|
2021-01-17 02:15:06 +01:00
|
|
|
}
|
2021-02-05 18:28:10 +01:00
|
|
|
done_ = true;
|
|
|
|
break;
|
2021-01-17 02:15:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-25 00:16:55 +01:00
|
|
|
////////////////
|
|
|
|
// members
|
|
|
|
////////////////
|
|
|
|
|
2021-02-20 15:53:18 +01:00
|
|
|
public:
|
2021-02-14 01:59:06 +01:00
|
|
|
error_type error_{};
|
2021-02-01 00:56:42 +01:00
|
|
|
bool unterminated_quote_{false};
|
2021-02-20 15:53:18 +01:00
|
|
|
bool done_{true};
|
|
|
|
bool resplitting_{false};
|
2021-02-14 01:59:06 +01:00
|
|
|
size_t escaped_{0};
|
|
|
|
split_data split_data_;
|
|
|
|
|
2021-01-25 00:16:55 +01:00
|
|
|
line_ptr_type begin_;
|
|
|
|
line_ptr_type curr_;
|
|
|
|
line_ptr_type end_;
|
|
|
|
line_ptr_type line_;
|
2021-02-05 18:28:10 +01:00
|
|
|
|
2021-02-06 21:08:59 +01:00
|
|
|
template <typename...>
|
2021-02-06 01:44:46 +01:00
|
|
|
friend class converter;
|
2021-01-17 02:15:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} /* ss */
|