mirror of
https://github.com/bolero-MURAKAMI/Sprout
synced 2024-11-12 21:09:01 +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)
|
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/workaround/std/cstddef.hpp>
|
||||||
#include <sprout/array.hpp>
|
#include <sprout/array.hpp>
|
||||||
#include <sprout/sub_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 = {};
|
sprout::array_sub_t<sprout::array_sub_t<ResultString, N>, L> result = {};
|
||||||
result.window(0, 1);
|
result.window(0, 1);
|
||||||
result.back().window(0, 0);
|
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 delimiters = sprout::make_string(settings.delimiter(), value_type('\r'), value_type('\n'));
|
||||||
auto first = sprout::begin(src), last = sprout::end(src);
|
auto first = sprout::begin(src), last = sprout::end(src);
|
||||||
while (first != last) {
|
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());
|
auto end_quote = sprout::find(first, last, settings.quote());
|
||||||
while (true) {
|
while (true) {
|
||||||
if (end_quote == last) {
|
if (end_quote == last) {
|
||||||
throw 0;
|
throw std::runtime_error("end quote not found");
|
||||||
}
|
}
|
||||||
auto next = sprout::next(end_quote);
|
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 != last) {
|
||||||
if (*next == settings.escape()) {
|
if (*next == settings.quote()) {
|
||||||
// escaped quote
|
// escaped quote
|
||||||
++next;
|
++next;
|
||||||
end_quote = sprout::find(next, last, settings.quote());
|
end_quote = sprout::find(next, last, settings.quote());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!sprout::any_of_equal(delimiters.begin(), delimiters.end(), *next)) {
|
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')) {
|
if (*next == value_type('\r') && sprout::next(next) != last && *sprout::next(next) == value_type('\n')) {
|
||||||
++next;
|
++next;
|
||||||
|
@ -109,17 +119,18 @@ parse_csv(String const& src, csv_parser_settings<typename String::value_type> se
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
using namespace sprout::udl;
|
using namespace sprout::udl;
|
||||||
|
constexpr auto settings = csv_parser_settings<char>(',', '\"', '\\');
|
||||||
constexpr auto src = R"(
|
constexpr auto src = R"(
|
||||||
(no quoted),a,b,c
|
(no quoted),a,b,c
|
||||||
(quoted),"d","e","f"
|
(quoted),"d","e","f"
|
||||||
(escaped),"""g""","""h""","""i"""
|
(escaped),"""g""","\"h\"","""i"""
|
||||||
(comma),"j,k","l,m","n,o"
|
(comma),"j,k","l,m","n,o"
|
||||||
(new line),"p
|
(new line),"p
|
||||||
q","r
|
q","r
|
||||||
s","t
|
s","t
|
||||||
u"
|
u"
|
||||||
)"_sv;
|
)"_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& field : result) {
|
||||||
for (auto const& elem : field) {
|
for (auto const& elem : field) {
|
||||||
std::cout
|
std::cout
|
||||||
|
|
Loading…
Reference in a new issue