Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std::from_chars for number parsing #431

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 15 additions & 18 deletions include/cgimap/api06/changeset_upload/node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@

#include "cgimap/api06/changeset_upload/osmobject.hpp"

#include <iostream>
#include <charconv>
#include <optional>
#include <cmath>

namespace api06 {

Expand All @@ -31,32 +30,30 @@ class Node : public OSMObject {

void set_lat(const std::string &lat) {

double _lat = -200.0;
double _lat;

try {
_lat = std::stod(lat);
} catch (std::invalid_argument &e) {
auto [_, ec] = std::from_chars(lat.data(), lat.data() + lat.size(), _lat);

if (ec == std::errc())
set_lat(_lat);
else if (ec == std::errc::invalid_argument)
throw xml_error("Latitude is not numeric");
} catch (std::out_of_range &e) {
else if (ec == std::errc::result_out_of_range)
throw xml_error("Latitude value is too large");
}
mmd-osm marked this conversation as resolved.
Show resolved Hide resolved

set_lat(_lat);
}

void set_lon(const std::string &lon) {

double _lon = -200.0;
double _lon;

try {
_lon = std::stod(lon);
} catch (std::invalid_argument &e) {
auto [_, ec] = std::from_chars(lon.data(), lon.data() + lon.size(), _lon);

if (ec == std::errc())
set_lon(_lon);
else if (ec == std::errc::invalid_argument)
throw xml_error("Longitude is not numeric");
} catch (std::out_of_range &e) {
else if (ec == std::errc::result_out_of_range)
throw xml_error("Longitude value is too large");
}

set_lon(_lon);
}

void set_lat(double lat) {
Expand Down
90 changes: 49 additions & 41 deletions include/cgimap/api06/changeset_upload/osmobject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@
#include "cgimap/types.hpp"
#include "cgimap/util.hpp"

#include <fmt/core.h>
#include <charconv>
#include <map>
#include <optional>

#include <fmt/core.h>

namespace api06 {

struct xml_error : public http::bad_request {
Expand All @@ -35,69 +37,75 @@ namespace api06 {

virtual ~OSMObject() = default;

void set_changeset(osm_changeset_id_t changeset) { m_changeset = changeset; }
void set_changeset(osm_changeset_id_t changeset) {

if (changeset <= 0) {
throw xml_error("Changeset must be a positive number");
}

m_changeset = changeset;
}

void set_version(osm_version_t version) {

if (version < 0) {
throw xml_error("Version may not be negative");
}

m_version = version;
}

void set_id(osm_nwr_signed_id_t id) {

void set_version(osm_version_t version) { m_version = version; }
if (id == 0) {
throw xml_error("Id must be different from 0");
}

void set_id(osm_nwr_signed_id_t id) { m_id = id; }
m_id = id;
}

// Setters with string conversions

void set_changeset(const std::string &changeset) {

osm_changeset_id_t _changeset = 0;

try {
_changeset = std::stol(changeset);
} catch (std::invalid_argument& e) {
throw xml_error("Changeset is not numeric");
} catch (std::out_of_range& e) {
throw xml_error("Changeset number is too large");
}

if (_changeset <= 0) {
throw xml_error("Changeset must be a positive number");
}
auto [_, ec] = std::from_chars(changeset.data(), changeset.data() + changeset.size(), _changeset);

set_changeset(_changeset);
if (ec == std::errc())
set_changeset(_changeset);
else if (ec == std::errc::invalid_argument)
throw xml_error("Changeset is not numeric");
else if (ec == std::errc::result_out_of_range)
throw xml_error("Changeset number is too large");
}

void set_version(const std::string &version) {

int64_t _version = 0;

try {
_version = std::stoi(version);
} catch (std::invalid_argument& e) {
throw xml_error("Version is not numeric");
} catch (std::out_of_range& e) {
throw xml_error("Version value is too large");
}

if (_version < 0) {
throw xml_error("Version may not be negative");
}
auto [_, ec] = std::from_chars(version.data(), version.data() + version.size(), _version);

set_version(_version);
if (ec == std::errc())
set_version(_version);
else if (ec == std::errc::invalid_argument)
throw xml_error("Version is not numeric");
else if (ec == std::errc::result_out_of_range)
throw xml_error("Version value is too large");
}

void set_id(const std::string &id) {

osm_nwr_signed_id_t _id = 0;

try {
_id = std::stol(id);
} catch (std::invalid_argument& e) {
throw xml_error("Id is not numeric");
} catch (std::out_of_range& e) {
throw xml_error("Id number is too large");
}

if (_id == 0) {
throw xml_error("Id must be different from 0");
}
auto [_, ec] = std::from_chars(id.data(), id.data() + id.size(), _id);

set_id(_id);
if (ec == std::errc())
set_id(_id);
else if (ec == std::errc::invalid_argument)
throw xml_error("Id is not numeric");
else if (ec == std::errc::result_out_of_range)
throw xml_error("Id number is too large");
}

osm_changeset_id_t changeset() const { return *m_changeset; }
Expand Down Expand Up @@ -129,7 +137,7 @@ namespace api06 {
fmt::format("Value has more than 255 unicode characters in {}", to_string()));
}

if (!(m_tags.insert(std::pair<std::string, std::string>(key, value)))
if (!(m_tags.insert({key, value}))
.second) {
throw xml_error(
fmt::format("{} has duplicate tags with key {}", to_string(), key));
Expand Down
28 changes: 16 additions & 12 deletions include/cgimap/api06/changeset_upload/relation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,28 @@ class RelationMember {
m_role = role;
}

void set_ref(osm_nwr_signed_id_t ref) {

if (ref == 0) {
throw xml_error("Relation member 'ref' attribute may not be 0");
}

m_ref = ref;
}

void set_ref(const std::string &ref) {

osm_nwr_signed_id_t _ref = 0;

try {
_ref = std::stol(ref);
} catch (std::invalid_argument &e) {
throw xml_error("Relation member 'ref' attribute is not numeric");
} catch (std::out_of_range &e) {
throw xml_error(
"Relation member 'ref' attribute value is too large");
}
auto [_, ec] = std::from_chars(ref.data(), ref.data() + ref.size(), _ref);

if (_ref == 0) {
throw xml_error("Relation member 'ref' attribute may not be 0");
if (ec == std::errc()) {
set_ref(_ref);
}

m_ref = _ref;
else if (ec == std::errc::invalid_argument)
throw xml_error("Relation member 'ref' attribute is not numeric");
else if (ec == std::errc::result_out_of_range)
throw xml_error("Relation member 'ref' attribute value is too large");
}

bool is_valid() const {
Expand Down
21 changes: 10 additions & 11 deletions include/cgimap/api06/changeset_upload/way.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,25 @@ class Way : public OSMObject {
~Way() override = default;

void add_way_node(osm_nwr_signed_id_t waynode) {
if (waynode == 0) {
throw xml_error("Way node value may not be 0");
}

m_way_nodes.emplace_back(waynode);
}

void add_way_node(const std::string &waynode) {

osm_nwr_signed_id_t _waynode = 0;

try {
_waynode = std::stol(waynode);
} catch (std::invalid_argument& e) {
auto [_, ec] = std::from_chars(waynode.data(), waynode.data() + waynode.size(), _waynode);

if (ec == std::errc())
add_way_node(_waynode);
else if (ec == std::errc::invalid_argument)
throw xml_error("Way node is not numeric");
} catch (std::out_of_range& e) {
else if (ec == std::errc::result_out_of_range)
throw xml_error("Way node value is too large");
}

if (_waynode == 0) {
throw xml_error("Way node value may not be 0");
}

add_way_node(_waynode);
}

const std::vector<osm_nwr_signed_id_t> &nodes() const { return m_way_nodes; }
Expand Down