update documentation

This commit is contained in:
ado 2021-01-03 03:07:39 +01:00
parent 60c8156fb7
commit a657670920

View File

@ -275,7 +275,7 @@ for performance reasons.
The parser can also be used to effectively parse files whose rows are not The parser can also be used to effectively parse files whose rows are not
always in the same format (not a classical csv but still csv-like). always in the same format (not a classical csv but still csv-like).
An example would be the best way to demonstrate such a scenario. A more complicated example would be the best way to demonstrate such a scenario.
Supposing we have a file containing different shapes in given formats: Supposing we have a file containing different shapes in given formats:
* circle radius * circle radius
@ -294,45 +294,33 @@ if (!p.valid()) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
p.set_error_mode(ss::error_mode::error_string);
std::vector<std::pair<shape, double>> shapes; std::vector<std::pair<shape, double>> shapes;
auto insert_circle = [&shapes](shape s, double r) { while (!p.eof()) {
if (s != shape::circle) { using ss::nx;
return false; auto [circle, rectangle, triangle] =
} p.try_next<nx<shape, shape::circle>, double>()
.or_else<nx<shape, shape::rectangle>, double, double>()
.or_else<nx<shape, shape::triangle>, double, double, double>()
.values();
if (circle) {
auto& [s, r] = circle.value();
shapes.emplace_back(s, r * r * M_PI); shapes.emplace_back(s, r * r * M_PI);
return true;
};
auto insert_rectangle = [&shapes](shape s, double a, double b) {
if (s != shape::rectangle) {
return false;
} }
if (rectangle) {
auto& [s, a, b] = rectangle.value();
shapes.emplace_back(s, a * b); shapes.emplace_back(s, a * b);
return true;
};
auto insert_triangle = [&shapes](shape s, double a, double b, double c) {
if (s != shape::triangle) {
return false;
} }
if (triangle) {
auto& [s, a, b, c] = triangle.value();
double sh = (a + b + c) / 2; double sh = (a + b + c) / 2;
shapes.emplace_back(s, sqrt(sh * (sh - a) * (sh - b) * (sh - c))); shapes.emplace_back(s, sqrt(sh * (sh - a) * (sh - b) * (sh - c)));
return true; }
};
while (!p.eof()) {
p.try_next<shape, double>(insert_circle)
.or_else<shape, double, double>(insert_rectangle)
.or_else<shape, double, double, double>(insert_triangle)
.on_error([](const std::string& error) {
std::cout << error << std::endl;
});
} }
// do something with the stored data ... /* do something with the stored shapes */
/* ... */
``` ```