diff --git a/src/algorithms/heuristics/heuristics.cpp b/src/algorithms/heuristics/heuristics.cpp index 5e7751d2c..5f603660c 100644 --- a/src/algorithms/heuristics/heuristics.cpp +++ b/src/algorithms/heuristics/heuristics.cpp @@ -887,8 +887,8 @@ void initial_routes(const Input& input, std::vector& routes) { } } if (!(single_jobs_deliveries <= vehicle.capacity)) { - throw InputException("Route over capacity for vehicle " + - std::to_string(vehicle.id) + "."); + throw InputException( + std::format("Route over capacity for vehicle {}.", vehicle.id)); } // Track load and travel time during the route for validity. @@ -912,9 +912,11 @@ void initial_routes(const Input& input, std::vector& routes) { job_ranks.push_back(job_rank); if (!input.vehicle_ok_with_job(v, job_rank)) { - throw InputException("Missing skill or step out of reach for vehicle " + - std::to_string(vehicle.id) + " and job " + - std::to_string(job.id) + "."); + throw InputException( + std::format("Missing skill or step out of reach for vehicle {} and " + "job {}.", + vehicle.id, + job.id)); } // Update current travel time. @@ -940,8 +942,9 @@ void initial_routes(const Input& input, std::vector& routes) { case JOB_TYPE::DELIVERY: { auto search = expected_delivery_ranks.find(job_rank); if (search == expected_delivery_ranks.end()) { - throw InputException("Invalid shipment in route for vehicle " + - std::to_string(vehicle.id) + "."); + throw InputException( + std::format("Invalid shipment in route for vehicle {}.", + vehicle.id)); } expected_delivery_ranks.erase(search); @@ -952,8 +955,8 @@ void initial_routes(const Input& input, std::vector& routes) { // Check validity after this step wrt capacity. if (!(current_load <= vehicle.capacity)) { - throw InputException("Route over capacity for vehicle " + - std::to_string(vehicle.id) + "."); + throw InputException( + std::format("Route over capacity for vehicle {}.", vehicle.id)); } } @@ -964,22 +967,22 @@ void initial_routes(const Input& input, std::vector& routes) { vehicle.eval(previous_index.value(), vehicle.end.value().index()); } if (!vehicle.ok_for_travel_time(eval_sum.duration)) { - throw InputException("Route over max_travel_time for vehicle " + - std::to_string(vehicle.id) + "."); + throw InputException( + std::format("Route over max_travel_time for vehicle {}.", vehicle.id)); } if (!vehicle.ok_for_distance(eval_sum.distance)) { - throw InputException("Route over max_distance for vehicle " + - std::to_string(vehicle.id) + "."); + throw InputException( + std::format("Route over max_distance for vehicle {}.", vehicle.id)); } if (vehicle.max_tasks < job_ranks.size()) { - throw InputException("Too many tasks for vehicle " + - std::to_string(vehicle.id) + "."); + throw InputException( + std::format("Too many tasks for vehicle {}.", vehicle.id)); } if (!expected_delivery_ranks.empty()) { - throw InputException("Invalid shipment in route for vehicle " + - std::to_string(vehicle.id) + "."); + throw InputException( + std::format("Invalid shipment in route for vehicle {}.", vehicle.id)); } // Now route is OK with regard to capacity, max_travel_time, @@ -991,8 +994,8 @@ void initial_routes(const Input& input, std::vector& routes) { job_ranks.end(), 0, 0)) { - throw InputException("Infeasible route for vehicle " + - std::to_string(vehicle.id) + "."); + throw InputException( + std::format("Infeasible route for vehicle {}.", vehicle.id)); } current_r.replace(input, diff --git a/src/algorithms/validation/choose_ETA.cpp b/src/algorithms/validation/choose_ETA.cpp index b649ed8b4..c15d5a126 100644 --- a/src/algorithms/validation/choose_ETA.cpp +++ b/src/algorithms/validation/choose_ETA.cpp @@ -221,8 +221,8 @@ Route choose_ETA(const Input& input, if (latest_date != std::numeric_limits::max()) { const auto reach_time = relative_ETA[s]; if (latest_date < reach_time) { - throw InputException("Infeasible route for vehicle " + - std::to_string(v.id) + "."); + throw InputException( + std::format("Infeasible route for vehicle {}.", v.id)); } start_candidate = std::min(start_candidate, latest_date - reach_time); } @@ -264,8 +264,8 @@ Route choose_ETA(const Input& input, std::max(earliest_date, step.forced_service.after.value()); } if (earliest_date > latest_dates[s]) { - throw InputException("Infeasible route for vehicle " + - std::to_string(v.id) + "."); + throw InputException( + std::format("Infeasible route for vehicle {}.", v.id)); } switch (step.type) { @@ -610,7 +610,7 @@ Route choose_ETA(const Input& input, rank_in_J = 1; for (unsigned i = 0; i < n; ++i) { - auto p_name = "P" + std::to_string(i + 1); + auto p_name = std::format("P{}", i + 1); glp_set_row_name(lp, current_row, p_name.c_str()); double action; const auto& step = steps[1 + i]; @@ -635,7 +635,7 @@ Route choose_ETA(const Input& input, // Lead time ("earliest violation") constraints. for (unsigned i = 0; i < n; ++i) { - auto l_name = "L" + std::to_string(i + 1); + auto l_name = std::format("L{}", i + 1); glp_set_row_name(lp, current_row, l_name.c_str()); glp_set_row_bnds(lp, current_row, GLP_LO, 0.0, 0.0); ++current_row; @@ -644,14 +644,14 @@ Route choose_ETA(const Input& input, // Delay ("latest violation") constraints. for (unsigned i = 0; i < n; ++i) { - auto d_name = "D" + std::to_string(i + 1); + auto d_name = std::format("D{}", i + 1); glp_set_row_name(lp, current_row, d_name.c_str()); glp_set_row_bnds(lp, current_row, GLP_UP, 0.0, 0.0); ++current_row; } // Vehicle TW end violation constraint. - auto d_name = "D" + std::to_string(n + 1); + auto d_name = std::format("D{}", n + 1); glp_set_row_name(lp, current_row, d_name.c_str()); // Using v.tw.end is fine too for a default time window. glp_set_row_bnds(lp, current_row, GLP_UP, 0.0, v.tw.end - horizon_start); @@ -661,7 +661,7 @@ Route choose_ETA(const Input& input, // Binary variable decision constraints. for (unsigned i = 1; i <= n; ++i) { - auto s_name = "S" + std::to_string(i); + auto s_name = std::format("S{}", i); glp_set_row_name(lp, current_row, s_name.c_str()); glp_set_row_bnds(lp, current_row, GLP_FX, 1.0, 1.0); ++current_row; @@ -670,7 +670,7 @@ Route choose_ETA(const Input& input, // Delta constraints. for (unsigned r = 0; r < J.size(); ++r) { - auto delta_name = "Delta" + std::to_string(J[r]); + auto delta_name = std::format("Delta{}", J[r]); glp_set_row_name(lp, current_row, delta_name.c_str()); glp_set_row_bnds(lp, current_row, @@ -701,15 +701,15 @@ Route choose_ETA(const Input& input, unsigned current_col = 1; // Variables for time of services (t_i values). for (unsigned i = 0; i <= n + 1; ++i) { - auto t_name = "t" + std::to_string(i); + auto t_name = std::format("t{}", i); glp_set_col_name(lp, current_col, t_name.c_str()); const Duration LB = t_i_LB[i]; const Duration UB = t_i_UB[i]; if (UB < LB) { - throw InputException("Infeasible route for vehicle " + - std::to_string(v.id) + "."); + throw InputException( + std::format("Infeasible route for vehicle {}.", v.id)); } if (LB == UB) { @@ -728,7 +728,7 @@ Route choose_ETA(const Input& input, // Define variables for measure of TW violation. for (unsigned i = 0; i <= n + 1; ++i) { - auto y_name = "Y" + std::to_string(i); + auto y_name = std::format("Y{}", i); glp_set_col_name(lp, current_col, y_name.c_str()); glp_set_col_bnds(lp, current_col, GLP_LO, 0.0, 0.0); ++current_col; @@ -741,7 +741,7 @@ Route choose_ETA(const Input& input, const auto& tws = (step.type == STEP_TYPE::JOB) ? input.jobs[step.rank].tws : v.breaks[step.rank].tws; for (unsigned k = 0; k < tws.size(); ++k) { - auto x_name = "X" + std::to_string(i + 1) + "_" + std::to_string(k); + auto x_name = std::format("X{}_{}", i + 1, k); glp_set_col_name(lp, current_col, x_name.c_str()); glp_set_col_kind(lp, current_col, GLP_BV); if (k < first_relevant_tw_rank[i] || k > last_relevant_tw_rank[i]) { @@ -754,7 +754,7 @@ Route choose_ETA(const Input& input, // Delta variables. for (unsigned i = 0; i <= n; ++i) { - auto delta_name = "delta" + std::to_string(i); + auto delta_name = std::format("delta{}", i); glp_set_col_name(lp, current_col, delta_name.c_str()); glp_set_col_bnds(lp, current_col, GLP_LO, 0.0, 0.0); ++current_col; diff --git a/src/routing/ors_wrapper.cpp b/src/routing/ors_wrapper.cpp index 56f1ac193..b9dbb6fa5 100644 --- a/src/routing/ors_wrapper.cpp +++ b/src/routing/ors_wrapper.cpp @@ -34,8 +34,7 @@ std::string OrsWrapper::build_query(const std::vector& locations, } body += "\":["; for (auto const& location : locations) { - body += "[" + std::to_string(location.lon()) + "," + - std::to_string(location.lat()) + "],"; + body += std::format("[{},{}],", location.lon(), location.lat()); } body.pop_back(); // Remove trailing ','. body += "]"; @@ -53,7 +52,7 @@ std::string OrsWrapper::build_query(const std::vector& locations, query += " HTTP/1.0\r\n"; query += "Accept: */*\r\n"; query += "Content-Type: application/json\r\n"; - query += "Content-Length: " + std::to_string(body.size()) + "\r\n"; + query += std::format("Content-Length: {}\r\n", body.size()); query += "Host: " + _server.host + ":" + _server.port + "\r\n"; query += "Connection: close\r\n"; query += "\r\n" + body; diff --git a/src/routing/osrm_routed_wrapper.cpp b/src/routing/osrm_routed_wrapper.cpp index 7d93833ec..e44f9d3f0 100644 --- a/src/routing/osrm_routed_wrapper.cpp +++ b/src/routing/osrm_routed_wrapper.cpp @@ -39,8 +39,7 @@ OsrmRoutedWrapper::build_query(const std::vector& locations, // Adding locations and radiuses values. for (auto const& location : locations) { - query += std::to_string(location.lon()) + "," + - std::to_string(location.lat()) + ";"; + query += std::format("{},{};", location.lon(), location.lat()); radiuses += DEFAULT_OSRM_SNAPPING_RADIUS + ";"; } // Remove trailing ';'. @@ -76,9 +75,8 @@ void OsrmRoutedWrapper::check_response(const rapidjson::Document& json_result, const auto error_loc = std::stoul(message.substr(snapping_error_base.size(), message.size() - snapping_error_base.size())); - const auto coordinates = "[" + std::to_string(locs[error_loc].lon()) + - "," + std::to_string(locs[error_loc].lat()) + - "]"; + const auto coordinates = + std::format("[{},{}]", locs[error_loc].lon(), locs[error_loc].lat()); throw RoutingException("Could not find route near location " + coordinates); } diff --git a/src/routing/valhalla_wrapper.cpp b/src/routing/valhalla_wrapper.cpp index 3f225bde7..d73353665 100644 --- a/src/routing/valhalla_wrapper.cpp +++ b/src/routing/valhalla_wrapper.cpp @@ -37,8 +37,8 @@ std::string ValhallaWrapper::get_matrix_query( // List locations. std::string all_locations; for (auto const& location : locations) { - all_locations += "{\"lon\":" + std::to_string(location.lon()) + "," + - "\"lat\":" + std::to_string(location.lat()) + "},"; + all_locations += + std::format(R"({{"lon":{},"lat":{}}},)", location.lon(), location.lat()); } all_locations.pop_back(); // Remove trailing ','. @@ -61,9 +61,9 @@ ValhallaWrapper::get_route_query(const std::vector& locations) const { "GET /" + _server.path + _route_service + "?json={\"locations\":["; for (auto const& location : locations) { - query += "{\"lon\":" + std::to_string(location.lon()) + "," + - "\"lat\":" + std::to_string(location.lat()) + - R"(,"type":"break"},)"; + query += std::format(R"({{"lon":{},"lat":{},"type":"break"}},)", + location.lon(), + location.lat()); } query.pop_back(); // Remove trailing ','. diff --git a/src/routing/wrapper.h b/src/routing/wrapper.h index e7585f404..0049caad0 100644 --- a/src/routing/wrapper.h +++ b/src/routing/wrapper.h @@ -65,8 +65,9 @@ class Wrapper { if (max_unfound_routes_for_a_loc > 0) { std::string error_msg = "Unfound route(s) "; error_msg += error_direction; - error_msg += "location [" + std::to_string(locs[error_loc].lon()) + "," + - std::to_string(locs[error_loc].lat()) + "]"; + error_msg += std::format("location [{},{}]", + locs[error_loc].lon(), + locs[error_loc].lat()); throw RoutingException(error_msg); } diff --git a/src/structures/vroom/cost_wrapper.cpp b/src/structures/vroom/cost_wrapper.cpp index 68803a6e4..44f837e94 100644 --- a/src/structures/vroom/cost_wrapper.cpp +++ b/src/structures/vroom/cost_wrapper.cpp @@ -22,8 +22,7 @@ CostWrapper::CostWrapper(double speed_factor, Cost per_hour, Cost per_km) std::round(1 / speed_factor * DURATION_FACTOR * per_hour)), discrete_distance_cost_factor(DISTANCE_FACTOR * per_km) { if (speed_factor <= 0 || speed_factor > MAX_SPEED_FACTOR) { - throw InputException("Invalid speed factor: " + - std::to_string(speed_factor)); + throw InputException(std::format("Invalid speed factor: {}", speed_factor)); } } diff --git a/src/structures/vroom/input/input.cpp b/src/structures/vroom/input/input.cpp index b51627321..f9a6ae07f 100644 --- a/src/structures/vroom/input/input.cpp +++ b/src/structures/vroom/input/input.cpp @@ -108,15 +108,17 @@ void Input::check_job(Job& job) { if (const auto delivery_size = job.delivery.size(); delivery_size != _amount_size) { throw InputException( - "Inconsistent delivery length: " + std::to_string(delivery_size) + - " instead of " + std::to_string(_amount_size) + '.'); + std::format("Inconsistent delivery length: {} instead of {}.", + delivery_size, + _amount_size)); } // Ensure pickup size consistency. if (const auto pickup_size = job.pickup.size(); pickup_size != _amount_size) { throw InputException( - "Inconsistent pickup length: " + std::to_string(pickup_size) + - " instead of " + std::to_string(_amount_size) + '.'); + std::format("Inconsistent pickup length: {} instead of {}.", + pickup_size, + _amount_size)); } // Ensure that location index are either always or never provided. @@ -176,7 +178,7 @@ void Input::add_job(const Job& job) { throw InputException("Wrong job type."); } if (job_id_to_rank.contains(job.id)) { - throw InputException("Duplicate job id: " + std::to_string(job.id) + "."); + throw InputException(std::format("Duplicate job id: {}.", job.id)); } job_id_to_rank[job.id] = jobs.size(); jobs.push_back(job); @@ -186,47 +188,50 @@ void Input::add_job(const Job& job) { void Input::add_shipment(const Job& pickup, const Job& delivery) { if (pickup.priority != delivery.priority) { - throw InputException("Inconsistent shipment priority for pickup " + - std::to_string(pickup.id) + " and delivery " + - std::to_string(delivery.id) + "."); + throw InputException( + std:: + format("Inconsistent shipment priority for pickup {} and delivery {}.", + pickup.id, + delivery.id)); } if (!(pickup.pickup == delivery.delivery)) { - throw InputException("Inconsistent shipment amount for pickup " + - std::to_string(pickup.id) + " and delivery " + - std::to_string(delivery.id) + "."); + throw InputException( + std::format("Inconsistent shipment amount for pickup {} and delivery {}.", + pickup.id, + delivery.id)); } if (pickup.skills.size() != delivery.skills.size()) { - throw InputException("Inconsistent shipment skills for pickup " + - std::to_string(pickup.id) + " and delivery " + - std::to_string(delivery.id) + "."); + throw InputException( + std::format("Inconsistent shipment skills for pickup {} and delivery {}.", + pickup.id, + delivery.id)); } for (const auto s : pickup.skills) { if (!delivery.skills.contains(s)) { - throw InputException("Inconsistent shipment skills for pickup " + - std::to_string(pickup.id) + " and " + - std::to_string(delivery.id) + "."); + throw InputException( + std::format("Inconsistent shipment skills for pickup {} and {}.", + pickup.id, + delivery.id)); } } if (pickup.type != JOB_TYPE::PICKUP) { - throw InputException("Wrong type for pickup " + std::to_string(pickup.id) + - "."); + throw InputException(std::format("Wrong type for pickup {}.", pickup.id)); } if (pickup_id_to_rank.contains(pickup.id)) { - throw InputException("Duplicate pickup id: " + std::to_string(pickup.id) + - "."); + throw InputException(std::format("Duplicate pickup id: {}.", pickup.id)); } pickup_id_to_rank[pickup.id] = jobs.size(); jobs.push_back(pickup); check_job(jobs.back()); if (delivery.type != JOB_TYPE::DELIVERY) { - throw InputException("Wrong type for delivery " + - std::to_string(delivery.id) + "."); + throw InputException( + std::format("Wrong type for delivery {}.", delivery.id)); } if (delivery_id_to_rank.contains(delivery.id)) { throw InputException( - "Duplicate delivery id: " + std::to_string(delivery.id) + "."); + std::format("Duplicate delivery id: {}.", delivery.id)); } delivery_id_to_rank[delivery.id] = jobs.size(); jobs.push_back(delivery); @@ -243,8 +248,9 @@ void Input::add_vehicle(const Vehicle& vehicle) { if (const auto vehicle_amount_size = current_v.capacity.size(); vehicle_amount_size != _amount_size) { throw InputException( - "Inconsistent capacity length: " + std::to_string(vehicle_amount_size) + - " instead of " + std::to_string(_amount_size) + '.'); + std::format("Inconsistent capacity length: {} instead of {}.", + vehicle_amount_size, + _amount_size)); } // Check for time-windows and skills. @@ -783,9 +789,10 @@ void Input::set_vehicle_steps_ranks() { if (step.type == STEP_TYPE::BREAK) { auto search = current_vehicle.break_id_to_rank.find(step.id); if (search == current_vehicle.break_id_to_rank.end()) { - throw InputException("Invalid break id " + std::to_string(step.id) + - " for vehicle " + - std::to_string(current_vehicle.id) + "."); + throw InputException( + std::format("Invalid break id {} for vehicle {}.", + step.id, + current_vehicle.id)); } step.rank = search->second; } @@ -796,16 +803,18 @@ void Input::set_vehicle_steps_ranks() { case JOB_TYPE::SINGLE: { auto search = job_id_to_rank.find(step.id); if (search == job_id_to_rank.end()) { - throw InputException("Invalid job id " + std::to_string(step.id) + - " for vehicle " + - std::to_string(current_vehicle.id) + "."); + throw InputException( + std::format("Invalid job id {} for vehicle {}.", + step.id, + current_vehicle.id)); } step.rank = search->second; if (planned_job_ids.contains(step.id)) { - throw InputException("Duplicate job id " + std::to_string(step.id) + - " in input steps for vehicle " + - std::to_string(current_vehicle.id) + "."); + throw InputException( + std::format("Duplicate job id {} in input steps for vehicle {}.", + step.id, + current_vehicle.id)); } planned_job_ids.insert(step.id); break; @@ -813,17 +822,19 @@ void Input::set_vehicle_steps_ranks() { case JOB_TYPE::PICKUP: { auto search = pickup_id_to_rank.find(step.id); if (search == pickup_id_to_rank.end()) { - throw InputException("Invalid pickup id " + - std::to_string(step.id) + " for vehicle " + - std::to_string(current_vehicle.id) + "."); + throw InputException( + std::format("Invalid pickup id {} for vehicle {}.", + step.id, + current_vehicle.id)); } step.rank = search->second; if (planned_pickup_ids.contains(step.id)) { - throw InputException("Duplicate pickup id " + - std::to_string(step.id) + - " in input steps for vehicle " + - std::to_string(current_vehicle.id) + "."); + throw InputException( + std:: + format("Duplicate pickup id {} in input steps for vehicle {}.", + step.id, + current_vehicle.id)); } planned_pickup_ids.insert(step.id); break; @@ -831,17 +842,19 @@ void Input::set_vehicle_steps_ranks() { case JOB_TYPE::DELIVERY: { auto search = delivery_id_to_rank.find(step.id); if (search == delivery_id_to_rank.end()) { - throw InputException("Invalid delivery id " + - std::to_string(step.id) + " for vehicle " + - std::to_string(current_vehicle.id) + "."); + throw InputException( + std::format("Invalid delivery id {} for vehicle {}.", + step.id, + current_vehicle.id)); } step.rank = search->second; if (planned_delivery_ids.contains(step.id)) { - throw InputException("Duplicate delivery id " + - std::to_string(step.id) + - " in input steps for vehicle " + - std::to_string(current_vehicle.id) + "."); + throw InputException( + std::format("Duplicate delivery id {} in input steps for vehicle " + "{}.", + step.id, + current_vehicle.id)); } planned_delivery_ids.insert(step.id); break; diff --git a/src/structures/vroom/time_window.cpp b/src/structures/vroom/time_window.cpp index a9c210499..b613a191c 100644 --- a/src/structures/vroom/time_window.cpp +++ b/src/structures/vroom/time_window.cpp @@ -27,8 +27,8 @@ TimeWindow::TimeWindow(UserDuration start, UserDuration end) end(utils::scale_from_user_duration(end)), length(utils::scale_from_user_duration(end - start)) { if (start > end) { - throw InputException("Invalid time window: [" + std::to_string(start) + - ", " + std::to_string(end) + "]"); + throw InputException( + std::format("Invalid time window: [{}, {}]", start, end)); } } diff --git a/src/structures/vroom/tw_route.cpp b/src/structures/vroom/tw_route.cpp index f5937272a..6cd15a276 100644 --- a/src/structures/vroom/tw_route.cpp +++ b/src/structures/vroom/tw_route.cpp @@ -26,8 +26,8 @@ TWRoute::TWRoute(const Input& input, Index v, unsigned amount_size) breaks_travel_margin_after(input.vehicles[v].breaks.size()), fwd_smallest_breaks_load_margin(input.vehicles[v].breaks.size()), bwd_smallest_breaks_load_margin(input.vehicles[v].breaks.size()) { - const std::string break_error = "Inconsistent breaks for vehicle " + - std::to_string(input.vehicles[v].id) + "."; + const std::string break_error = + std::format("Inconsistent breaks for vehicle {}.", input.vehicles[v].id); const auto& breaks = input.vehicles[v].breaks; diff --git a/src/structures/vroom/vehicle.cpp b/src/structures/vroom/vehicle.cpp index 15eb34cbb..4ec8dc61e 100644 --- a/src/structures/vroom/vehicle.cpp +++ b/src/structures/vroom/vehicle.cpp @@ -51,22 +51,22 @@ Vehicle::Vehicle(Id id, return b.max_load.has_value(); })) { if (!static_cast(start) && !static_cast(end)) { - throw InputException("No start or end specified for vehicle " + - std::to_string(id) + '.'); + throw InputException( + std::format("No start or end specified for vehicle {}.", id)); } for (unsigned i = 0; i < breaks.size(); ++i) { const auto& b = breaks[i]; if (break_id_to_rank.contains(b.id)) { - throw InputException("Duplicate break id: " + std::to_string(b.id) + "."); + throw InputException(std::format("Duplicate break id: {}.", b.id)); } break_id_to_rank[b.id] = i; if (b.max_load.has_value() && b.max_load.value().size() != capacity.size()) { - throw InputException("Inconsistent break max_load size for break: " + - std::to_string(b.id) + "."); + throw InputException( + std::format("Inconsistent break max_load size for break: {}.", b.id)); } } @@ -87,12 +87,12 @@ Vehicle::Vehicle(Id id, for (unsigned i = rank_after_start; i < input_steps.size(); ++i) { if (input_steps[i].type == START) { - throw InputException("Unexpected start in input steps for vehicle " + - std::to_string(id) + "."); + throw InputException( + std::format("Unexpected start in input steps for vehicle {}.", id)); } if (input_steps[i].type == END && (i != input_steps.size() - 1)) { - throw InputException("Unexpected end in input steps for vehicle " + - std::to_string(id) + "."); + throw InputException( + std::format("Unexpected end in input steps for vehicle {}.", id)); } steps.push_back(input_steps[i]); diff --git a/src/utils/helpers.cpp b/src/utils/helpers.cpp index 800417394..47374852f 100644 --- a/src/utils/helpers.cpp +++ b/src/utils/helpers.cpp @@ -226,15 +226,17 @@ void check_tws(const std::vector& tws, const Id id, const std::string& type) { if (tws.empty()) { - throw InputException("Empty time-windows for " + type + " " + - std::to_string(id) + "."); + throw InputException( + std::format("Empty time-windows for {} {}.", type, id)); } if (tws.size() > 1) { for (std::size_t i = 0; i < tws.size() - 1; ++i) { if (tws[i + 1].start <= tws[i].end) { - throw InputException("Unsorted or overlapping time-windows for " + - type + " " + std::to_string(id) + "."); + throw InputException( + std::format("Unsorted or overlapping time-windows for {} {}.", + type, + id)); } } } @@ -244,8 +246,8 @@ void check_priority(const Priority priority, const Id id, const std::string& type) { if (priority > MAX_PRIORITY) { - throw InputException("Invalid priority value for " + type + " " + - std::to_string(id) + "."); + throw InputException( + std::format("Invalid priority value for {} {}.", type, id)); } } diff --git a/src/utils/input_parser.cpp b/src/utils/input_parser.cpp index 529e33769..83f64a092 100644 --- a/src/utils/input_parser.cpp +++ b/src/utils/input_parser.cpp @@ -60,9 +60,10 @@ inline Amount get_amount(const rapidjson::Value& object, } if (object[key].Size() != amount_size) { - throw InputException("Inconsistent " + std::string(key) + - " length: " + std::to_string(object[key].Size()) + - " and " + std::to_string(amount_size) + '.'); + throw InputException(std::format("Inconsistent {} length: {} and {}.", + key, + object[key].Size(), + amount_size)); } for (rapidjson::SizeType i = 0; i < object[key].Size(); ++i) { @@ -152,8 +153,8 @@ inline void check_shipment(const rapidjson::Value& v) { inline void check_location(const rapidjson::Value& v, const std::string& type) { if (!v.HasMember("location") || !v["location"].IsArray()) { - throw InputException("Invalid location for " + type + " " + - std::to_string(v["id"].GetUint64()) + "."); + throw InputException( + std::format("Invalid location for {} {}.", type, v["id"].GetUint64())); } } @@ -176,8 +177,9 @@ inline std::vector get_time_windows(const rapidjson::Value& o) { std::vector tws; if (o.HasMember("time_windows")) { if (!o["time_windows"].IsArray() || o["time_windows"].Empty()) { - throw InputException("Invalid time_windows array for object " + - std::to_string(o["id"].GetUint64()) + "."); + throw InputException( + std::format("Invalid time_windows array for object {}.", + o["id"].GetUint64())); } std::transform(o["time_windows"].Begin(), @@ -212,8 +214,8 @@ inline std::vector get_vehicle_breaks(const rapidjson::Value& v, std::vector breaks; if (v.HasMember("breaks")) { if (!v["breaks"].IsArray()) { - throw InputException("Invalid breaks for vehicle " + - std::to_string(v["id"].GetUint64()) + "."); + throw InputException( + std::format("Invalid breaks for vehicle {}.", v["id"].GetUint64())); } std::transform(v["breaks"].Begin(), @@ -237,14 +239,14 @@ inline VehicleCosts get_vehicle_costs(const rapidjson::Value& v) { if (v.HasMember("costs")) { if (!v["costs"].IsObject()) { - throw InputException("Invalid costs for vehicle " + - std::to_string(v["id"].GetUint64()) + "."); + throw InputException( + std::format("Invalid costs for vehicle {}.", v["id"].GetUint64())); } if (v["costs"].HasMember("fixed")) { if (!v["costs"]["fixed"].IsUint()) { - throw InputException("Invalid fixed cost for vehicle " + - std::to_string(v["id"].GetUint64()) + "."); + throw InputException(std::format("Invalid fixed cost for vehicle {}.", + v["id"].GetUint64())); } fixed = v["costs"]["fixed"].GetUint(); @@ -252,8 +254,9 @@ inline VehicleCosts get_vehicle_costs(const rapidjson::Value& v) { if (v["costs"].HasMember("per_hour")) { if (!v["costs"]["per_hour"].IsUint()) { - throw InputException("Invalid per_hour cost for vehicle " + - std::to_string(v["id"].GetUint64()) + "."); + throw InputException( + std::format("Invalid per_hour cost for vehicle {}.", + v["id"].GetUint64())); } per_hour = v["costs"]["per_hour"].GetUint(); @@ -261,8 +264,8 @@ inline VehicleCosts get_vehicle_costs(const rapidjson::Value& v) { if (v["costs"].HasMember("per_km")) { if (!v["costs"]["per_km"].IsUint()) { - throw InputException("Invalid per_km cost for vehicle " + - std::to_string(v["id"].GetUint64()) + "."); + throw InputException(std::format("Invalid per_km cost for vehicle {}.", + v["id"].GetUint64())); } per_km = v["costs"]["per_km"].GetUint(); @@ -277,8 +280,8 @@ inline std::vector get_vehicle_steps(const rapidjson::Value& v) { if (v.HasMember("steps")) { if (!v["steps"].IsArray()) { - throw InputException("Invalid steps for vehicle " + - std::to_string(v["id"].GetUint64()) + "."); + throw InputException( + std::format("Invalid steps for vehicle {}.", v["id"].GetUint64())); } steps.reserve(v["steps"].Size()); @@ -324,8 +327,8 @@ inline std::vector get_vehicle_steps(const rapidjson::Value& v) { } if (!json_step.HasMember("id") || !json_step["id"].IsUint64()) { - throw InputException("Invalid id in steps for vehicle " + - std::to_string(v["id"].GetUint64()) + "."); + throw InputException(std::format("Invalid id in steps for vehicle {}.", + v["id"].GetUint64())); } if (type_str == "job") { @@ -345,8 +348,9 @@ inline std::vector get_vehicle_steps(const rapidjson::Value& v) { json_step["id"].GetUint64(), std::move(forced_service)); } else { - throw InputException("Invalid type in steps for vehicle " + - std::to_string(v["id"].GetUint64()) + "."); + throw InputException( + std::format("Invalid type in steps for vehicle {}.", + v["id"].GetUint64())); } } } @@ -364,8 +368,8 @@ inline Vehicle get_vehicle(const rapidjson::Value& json_vehicle, bool has_start_coords = json_vehicle.HasMember("start"); bool has_start_index = json_vehicle.HasMember("start_index"); if (has_start_index && !json_vehicle["start_index"].IsUint()) { - throw InputException("Invalid start_index for vehicle " + - std::to_string(v_id) + "."); + throw InputException( + std::format("Invalid start_index for vehicle {}.", v_id)); } std::optional start; @@ -388,8 +392,8 @@ inline Vehicle get_vehicle(const rapidjson::Value& json_vehicle, bool has_end_coords = json_vehicle.HasMember("end"); bool has_end_index = json_vehicle.HasMember("end_index"); if (has_end_index && !json_vehicle["end_index"].IsUint()) { - throw InputException("Invalid end_index for vehicle" + - std::to_string(v_id) + "."); + throw InputException( + std::format("Invalid end_index for vehicle {}.", v_id)); } std::optional end; @@ -435,8 +439,9 @@ inline Location get_task_location(const rapidjson::Value& v, bool has_location_coords = v.HasMember("location"); bool has_location_index = v.HasMember("location_index"); if (has_location_index && !v["location_index"].IsUint()) { - throw InputException("Invalid location_index for " + type + " " + - std::to_string(v["id"].GetUint64()) + "."); + throw InputException(std::format("Invalid location_index for {} {}.", + type, + v["id"].GetUint64())); } if (has_location_index) { @@ -505,8 +510,9 @@ void parse(Input& input, const std::string& input_str, bool geometry) { // Parsing input string to populate the input object. if (json_input.Parse(input_str.c_str()).HasParseError()) { std::string error_msg = - std::string(rapidjson::GetParseError_En(json_input.GetParseError())) + - " (offset: " + std::to_string(json_input.GetErrorOffset()) + ")"; + std::format("{} (offset: {})", + rapidjson::GetParseError_En(json_input.GetParseError()), + json_input.GetErrorOffset()); throw InputException(error_msg); } diff --git a/src/utils/version.cpp b/src/utils/version.cpp index 71cd09cc1..d1b3705e2 100644 --- a/src/utils/version.cpp +++ b/src/utils/version.cpp @@ -12,13 +12,12 @@ All rights reserved (see LICENSE). namespace vroom { std::string get_version() { - std::string version = std::to_string(MAJOR) + "." + std::to_string(MINOR) + - "." + std::to_string(PATCH); + std::string version = std::format("{}.{}.{}", MAJOR, MINOR, PATCH); if (DEV) { version += "-dev"; } else { if (RC > 0) { - version += "-rc." + std::to_string(RC); + version += std::format("-rc.{}", RC); } } return version; diff --git a/src/utils/version.h b/src/utils/version.h index db440a00b..450a12748 100644 --- a/src/utils/version.h +++ b/src/utils/version.h @@ -10,6 +10,7 @@ All rights reserved (see LICENSE). */ +#include #include constexpr unsigned MAJOR = 1;