mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2024-11-14 10:39:05 +00:00
fix csv example
This commit is contained in:
parent
2ebfc158b9
commit
276b09f004
1 changed files with 16 additions and 5 deletions
|
@ -6,6 +6,7 @@
|
|||
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
=============================================================================*/
|
||||
|
||||
#include <stdexcept>
|
||||
#include <sprout/workaround/std/cstddef.hpp>
|
||||
#include <sprout/array.hpp>
|
||||
#include <sprout/sub_array.hpp>
|
||||
|
@ -48,6 +49,7 @@ parse_csv(String const& src, csv_parser_settings<typename String::value_type> se
|
|||
sprout::array_sub_t<sprout::array_sub_t<ResultString, N>, L> result = {};
|
||||
result.window(0, 1);
|
||||
result.back().window(0, 0);
|
||||
bool same_escape = settings.quote() == settings.escape();
|
||||
auto delimiters = sprout::make_string(settings.delimiter(), value_type('\r'), value_type('\n'));
|
||||
auto first = sprout::begin(src), last = sprout::end(src);
|
||||
while (first != last) {
|
||||
|
@ -57,18 +59,26 @@ parse_csv(String const& src, csv_parser_settings<typename String::value_type> se
|
|||
auto end_quote = sprout::find(first, last, settings.quote());
|
||||
while (true) {
|
||||
if (end_quote == last) {
|
||||
throw 0;
|
||||
throw std::runtime_error("end quote not found");
|
||||
}
|
||||
auto next = sprout::next(end_quote);
|
||||
if (!same_escape && first != end_quote) {
|
||||
auto prev = sprout::prev(end_quote);
|
||||
if (*prev == settings.escape()) {
|
||||
// escaped quote
|
||||
end_quote = sprout::find(next, last, settings.quote());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (next != last) {
|
||||
if (*next == settings.escape()) {
|
||||
if (*next == settings.quote()) {
|
||||
// escaped quote
|
||||
++next;
|
||||
end_quote = sprout::find(next, last, settings.quote());
|
||||
continue;
|
||||
}
|
||||
if (!sprout::any_of_equal(delimiters.begin(), delimiters.end(), *next)) {
|
||||
throw 0;
|
||||
throw std::runtime_error("invalid quote");
|
||||
}
|
||||
if (*next == value_type('\r') && sprout::next(next) != last && *sprout::next(next) == value_type('\n')) {
|
||||
++next;
|
||||
|
@ -109,17 +119,18 @@ parse_csv(String const& src, csv_parser_settings<typename String::value_type> se
|
|||
|
||||
int main() {
|
||||
using namespace sprout::udl;
|
||||
constexpr auto settings = csv_parser_settings<char>(',', '\"', '\\');
|
||||
constexpr auto src = R"(
|
||||
(no quoted),a,b,c
|
||||
(quoted),"d","e","f"
|
||||
(escaped),"""g""","""h""","""i"""
|
||||
(escaped),"""g""","\"h\"","""i"""
|
||||
(comma),"j,k","l,m","n,o"
|
||||
(new line),"p
|
||||
q","r
|
||||
s","t
|
||||
u"
|
||||
)"_sv;
|
||||
constexpr auto result = parse_csv<16, 16>(src);
|
||||
constexpr auto result = parse_csv<16, 16>(src, settings);
|
||||
for (auto const& field : result) {
|
||||
for (auto const& elem : field) {
|
||||
std::cout
|
||||
|
|
Loading…
Reference in a new issue