From 12b9e8bcbce47ef89ca4b18462b420ff5e7cf196 Mon Sep 17 00:00:00 2001 From: eric bryant Date: Sun, 9 Jun 2024 15:19:19 -0400 Subject: [PATCH 1/3] sprintf->fmt for NMPs squash into dade6dd to remove Python style floats: * e73a895 tm-master.nmp * a1925a2 Waypoint::str --- siteupdate/cplusplus/classes/Route/Route.cpp | 10 ++++++---- .../cplusplus/classes/Waypoint/Waypoint.cpp | 20 ++++++++----------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/siteupdate/cplusplus/classes/Route/Route.cpp b/siteupdate/cplusplus/classes/Route/Route.cpp index a4f68fc7..bacaf6af 100644 --- a/siteupdate/cplusplus/classes/Route/Route.cpp +++ b/siteupdate/cplusplus/classes/Route/Route.cpp @@ -1,3 +1,4 @@ +#define FMT_HEADER_ONLY #include "Route.h" #include "../Args/Args.h" #include "../ConnectedRoute/ConnectedRoute.h" @@ -9,6 +10,7 @@ #include "../Region/Region.h" #include "../Waypoint/Waypoint.h" #include "../../functions/tmstring.h" +#include #include std::unordered_map Route::root_hash, Route::pri_list_hash, Route::alt_list_hash; @@ -180,9 +182,9 @@ void Route::write_nmp_merged() for (std::string &a : w.alt_labels) wptfile << a << ' '; if (w.near_miss_points.empty()) { wptfile << "http://www.openstreetmap.org/?lat="; - sprintf(fstr, "%.6f", w.lat); + *fmt::format_to(fstr, "{:.6f}", w.lat) = 0; wptfile << fstr << "&lon="; - sprintf(fstr, "%.6f", w.lng); + *fmt::format_to(fstr, "{:.6f}", w.lng) = 0; wptfile << fstr << '\n'; } else { // for now, arbitrarily choose the northernmost @@ -195,9 +197,9 @@ void Route::write_nmp_merged() if (other_w->lng > lng) lng = other_w->lng; } wptfile << "https://www.openstreetmap.org/?lat="; - sprintf(fstr, "%.6f", lat); + *fmt::format_to(fstr, "{:.6f}", lat) = 0; wptfile << fstr << "&lon="; - sprintf(fstr, "%.6f", lng); + *fmt::format_to(fstr, "{:.6f}", lng) = 0; wptfile << fstr << '\n'; w.near_miss_points.clear(); } diff --git a/siteupdate/cplusplus/classes/Waypoint/Waypoint.cpp b/siteupdate/cplusplus/classes/Waypoint/Waypoint.cpp index 3ea01652..9faef1bd 100644 --- a/siteupdate/cplusplus/classes/Waypoint/Waypoint.cpp +++ b/siteupdate/cplusplus/classes/Waypoint/Waypoint.cpp @@ -1,3 +1,4 @@ +#define FMT_HEADER_ONLY #include "Waypoint.h" #include "../Datacheck/Datacheck.h" #include "../DBFieldLength/DBFieldLength.h" @@ -7,6 +8,7 @@ #include "../../templates/contains.cpp" #include #include +#include #define pi 3.141592653589793238 bool sort_root_at_label(Waypoint *w1, Waypoint *w2) @@ -74,11 +76,7 @@ Waypoint::Waypoint(char *line, Route *rte) } std::string Waypoint::str() -{ std::string ans = route->root + " " + label + " ("; - char s[51]; int - e=sprintf(s,"%.15g",lat); if (lat==int(lat)) strcpy(s+e,".0"); ans+=s; ans+=','; - e=sprintf(s,"%.15g",lng); if (lng==int(lng)) strcpy(s+e,".0"); ans+=s; - return ans + ')'; +{ return fmt::format("{} {} ({:.15},{:.15})", route->root, label, lat, lng); } bool Waypoint::same_coords(Waypoint *other) @@ -200,10 +198,9 @@ void Waypoint::nmplogs(std::unordered_set &nmpfps, std::ofstream &n // both ways (other_w in w's list, w in other_w's list) if (sort_root_at_label(this, other_w)) { char s[51]; - #define PYTHON_STYLE_FLOAT(F) e=sprintf(s," %.15g",F); if (F==int(F)) strcpy(s+e,".0"); nmpnmp< &nmpfps, std::ofstream &n nmpnmp << '\n'; nmpnmp << other_w->root_at_label(); - PYTHON_STYLE_FLOAT(other_w->lat) - PYTHON_STYLE_FLOAT(other_w->lng) + *fmt::format_to(s, " {:.15}", other_w->lat)=0; nmpnmp<lng)=0; nmpnmp< Date: Sun, 9 Jun 2024 19:00:48 -0400 Subject: [PATCH 2/3] sprintf->fmt for datachecks --- .../classes/ElapsedTime/ElapsedTime.cpp | 15 +++++---------- .../classes/ElapsedTime/ElapsedTime.h | 2 -- .../classes/GraphGeneration/GraphListEntry.cpp | 6 +++--- .../cplusplus/classes/Route/read_wpt.cpp | 18 +++++++----------- .../cplusplus/classes/Waypoint/Waypoint.cpp | 14 ++++---------- .../cplusplus/classes/Waypoint/Waypoint.h | 4 ++-- .../WaypointQuadtree/WaypointQuadtree.cpp | 16 +++++++--------- 7 files changed, 28 insertions(+), 47 deletions(-) diff --git a/siteupdate/cplusplus/classes/ElapsedTime/ElapsedTime.cpp b/siteupdate/cplusplus/classes/ElapsedTime/ElapsedTime.cpp index 38ac83c0..45caba36 100644 --- a/siteupdate/cplusplus/classes/ElapsedTime/ElapsedTime.cpp +++ b/siteupdate/cplusplus/classes/ElapsedTime/ElapsedTime.cpp @@ -1,20 +1,15 @@ +#define FMT_HEADER_ONLY #include "ElapsedTime.h" +#include ElapsedTime::ElapsedTime(int precision) { start_time = std::chrono::steady_clock::now(); - format = "[%.1f] "; - format[3] = '0' + precision; - str = new char[15+precision]; - // deleted by ~ElapsedTime + format = "[{:.1f}] "; + format[4] = '0' + precision; } std::string ElapsedTime::et() { using namespace std::chrono; duration elapsed = duration_cast>(steady_clock::now() - start_time); - sprintf(str, format.data(), elapsed.count()); - return str; -} - -ElapsedTime::~ElapsedTime() -{ delete[] str; + return fmt::format(format.data(), elapsed.count()); } diff --git a/siteupdate/cplusplus/classes/ElapsedTime/ElapsedTime.h b/siteupdate/cplusplus/classes/ElapsedTime/ElapsedTime.h index 9af048fd..3a1f70d8 100644 --- a/siteupdate/cplusplus/classes/ElapsedTime/ElapsedTime.h +++ b/siteupdate/cplusplus/classes/ElapsedTime/ElapsedTime.h @@ -5,10 +5,8 @@ class ElapsedTime { //To get a nicely-formatted elapsed time string for printing std::chrono::steady_clock::time_point start_time; std::string format; - char* str; public: ElapsedTime(int); - ~ElapsedTime(); std::string et(); }; diff --git a/siteupdate/cplusplus/classes/GraphGeneration/GraphListEntry.cpp b/siteupdate/cplusplus/classes/GraphGeneration/GraphListEntry.cpp index 07ceb2ca..e913e81a 100644 --- a/siteupdate/cplusplus/classes/GraphGeneration/GraphListEntry.cpp +++ b/siteupdate/cplusplus/classes/GraphGeneration/GraphListEntry.cpp @@ -1,7 +1,9 @@ +#define FMT_HEADER_ONLY #include "GraphListEntry.h" #include "PlaceRadius.h" #include "../HighwaySystem/HighwaySystem.h" #include "../Region/Region.h" +#include std::vector GraphListEntry::entries; size_t GraphListEntry::num; // iterator for entries @@ -51,9 +53,7 @@ std::string GraphListEntry::category() std::string GraphListEntry::tag() { switch (cat) - { case 'a': char fstr[51]; - sprintf(fstr, "(%.15g) ", placeradius->r); - return placeradius->title + fstr; + { case 'a': return fmt::format("{}({:.15}) ", placeradius->title, placeradius->r); case 'r': return regions->front()->code + ' '; // must have valid pointer case 's': return systems->front()->systemname + ' '; // must have valid pointer case 'S': diff --git a/siteupdate/cplusplus/classes/Route/read_wpt.cpp b/siteupdate/cplusplus/classes/Route/read_wpt.cpp index ec0eb2fc..d6a7e286 100644 --- a/siteupdate/cplusplus/classes/Route/read_wpt.cpp +++ b/siteupdate/cplusplus/classes/Route/read_wpt.cpp @@ -1,3 +1,4 @@ +#define FMT_HEADER_ONLY #include "Route.h" #include "../Args/Args.h" #include "../Datacheck/Datacheck.h" @@ -6,14 +7,13 @@ #include "../HighwaySystem/HighwaySystem.h" #include "../Waypoint/Waypoint.h" #include "../WaypointQuadtree/WaypointQuadtree.h" -#include +#include void Route::read_wpt(WaypointQuadtree *all_waypoints, ErrorList *el, bool usa_flag) { /* read data into the Route's waypoint list from a .wpt file */ std::string filename = Args::datapath + "/data/" + rg_str + "/" + system->systemname + "/" + root + ".wpt"; Waypoint *last_visible; double vis_dist = 0; - char fstr[112]; // remove full path from all_wpt_files list awf_mtx.lock(); @@ -73,7 +73,7 @@ void Route::read_wpt(WaypointQuadtree *all_waypoints, ErrorList *el, bool usa_fl all_waypoints->insert(w, 1); // single-point Datachecks, and HighwaySegment - w->out_of_bounds(fstr); + w->out_of_bounds(); w->label_invalid_char(); if (w > points.data) { // add HighwaySegment, if not first point @@ -83,9 +83,7 @@ void Route::read_wpt(WaypointQuadtree *all_waypoints, ErrorList *el, bool usa_fl double last_distance = s->length; vis_dist += last_distance; if (last_distance > 20) - { sprintf(fstr, "%.2f", last_distance); - Datacheck::add(this, w[-1].label, w->label, "", "LONG_SEGMENT", fstr); - } + Datacheck::add(this, w[-1].label, w->label, "", "LONG_SEGMENT", fmt::format("{:.2f}", last_distance)); s++; } else if (w->is_hidden) // look for hidden beginning @@ -107,7 +105,7 @@ void Route::read_wpt(WaypointQuadtree *all_waypoints, ErrorList *el, bool usa_fl w->label_slashes(slash); w->lacks_generic(); w->underscore_datachecks(slash); - w->visible_distance(fstr, vis_dist, last_visible); + w->visible_distance(vis_dist, last_visible); } ++w; } @@ -120,7 +118,7 @@ void Route::read_wpt(WaypointQuadtree *all_waypoints, ErrorList *el, bool usa_fl { Datacheck::add(this, points.back().label, "", "", "HIDDEN_TERMINUS", ""); // do one last check in case a VISIBLE_DISTANCE error coexists // here, as this was only checked earlier for visible points - points.back().visible_distance(fstr, vis_dist, last_visible); + points.back().visible_distance(vis_dist, last_visible); } // angle check is easier with a traditional for loop and array indices for (Waypoint* p = points.data+1; p < points.end()-1; p++) @@ -129,9 +127,7 @@ void Route::read_wpt(WaypointQuadtree *all_waypoints, ErrorList *el, bool usa_fl Datacheck::add(this, p[-1].label, p->label, p[1].label, "BAD_ANGLE", ""); else { double angle = p->angle(); if (angle > 135) - { sprintf(fstr, "%.2f", angle); - Datacheck::add(this, p[-1].label, p->label, p[1].label, "SHARP_ANGLE", fstr); - } + Datacheck::add(this, p[-1].label, p->label, p[1].label, "SHARP_ANGLE", fmt::format("{:.2f}", angle)); } } } diff --git a/siteupdate/cplusplus/classes/Waypoint/Waypoint.cpp b/siteupdate/cplusplus/classes/Waypoint/Waypoint.cpp index 9faef1bd..74935a61 100644 --- a/siteupdate/cplusplus/classes/Waypoint/Waypoint.cpp +++ b/siteupdate/cplusplus/classes/Waypoint/Waypoint.cpp @@ -381,14 +381,10 @@ void Waypoint::label_invalid_char() } } -void Waypoint::out_of_bounds(char *s) +void Waypoint::out_of_bounds() { // out-of-bounds coords if (lat > 90 || lat < -90 || lng > 180 || lng < -180) - { int - e=sprintf(s,"(%.15g",lat); if (int(lat)==lat) strcpy(s+e, ".0"); std::string info(s); - e=sprintf(s,",%.15g",lng); if (int(lng)==lng) strcpy(s+e, ".0"); info += s; - Datacheck::add(route, label, "", "", "OUT_OF_BOUNDS", info+')'); - } + Datacheck::add(route, label, "", "", "OUT_OF_BOUNDS", fmt::format("({:.15},{:.15})")); } /* checks for visible points */ @@ -561,13 +557,11 @@ void Waypoint::us_letter() Datacheck::add(route, label, "", "", "US_LETTER", ""); } -void Waypoint::visible_distance(char *fstr, double &vis_dist, Waypoint *&last_visible) +void Waypoint::visible_distance(double &vis_dist, Waypoint *&last_visible) { // complete visible distance check, omit report for active // systems to reduce clutter if (vis_dist > 10 && !route->system->active()) - { sprintf(fstr, "%.2f", vis_dist); - Datacheck::add(route, last_visible->label, label, "", "VISIBLE_DISTANCE", fstr); - } + Datacheck::add(route, last_visible->label, label, "", "VISIBLE_DISTANCE", fmt::format("{:.2f}", vis_dist)); last_visible = this; vis_dist = 0; } diff --git a/siteupdate/cplusplus/classes/Waypoint/Waypoint.h b/siteupdate/cplusplus/classes/Waypoint/Waypoint.h index 259c6001..02ad4c4f 100644 --- a/siteupdate/cplusplus/classes/Waypoint/Waypoint.h +++ b/siteupdate/cplusplus/classes/Waypoint/Waypoint.h @@ -57,7 +57,7 @@ class Waypoint void hidden_junction(); void invalid_url(const char* const, const char* const); void label_invalid_char(); - void out_of_bounds(char *); + void out_of_bounds(); // checks for visible points void bus_with_i(); void interstate_no_hyphen(); @@ -70,7 +70,7 @@ class Waypoint void lacks_generic(); void underscore_datachecks(const char *); void us_letter(); - void visible_distance(char *, double &, Waypoint *&); + void visible_distance(double &, Waypoint *&); }; bool sort_root_at_label(Waypoint*, Waypoint*); diff --git a/siteupdate/cplusplus/classes/WaypointQuadtree/WaypointQuadtree.cpp b/siteupdate/cplusplus/classes/WaypointQuadtree/WaypointQuadtree.cpp index 25106f7f..ac154f42 100644 --- a/siteupdate/cplusplus/classes/WaypointQuadtree/WaypointQuadtree.cpp +++ b/siteupdate/cplusplus/classes/WaypointQuadtree/WaypointQuadtree.cpp @@ -1,3 +1,4 @@ +#define FMT_HEADER_ONLY #include "WaypointQuadtree.h" #include "../Args/Args.h" #include "../Datacheck/Datacheck.h" @@ -6,6 +7,7 @@ #include "../Route/Route.h" #include "../Waypoint/Waypoint.h" #include +#include #ifdef threading_enabled #include #endif @@ -63,11 +65,8 @@ void WaypointQuadtree::insert(Waypoint *w, bool init) // DUPLICATE_COORDS datacheck for (Waypoint* p : *other_w->colocated) if (p->route == w->route) - { char s[48]; int - e=sprintf(s,"(%.15g",w->lat); if (int(w->lat)==w->lat) strcpy(s+e, ".0"); std::string info(s); - e=sprintf(s,",%.15g",w->lng); if (int(w->lng)==w->lng) strcpy(s+e, ".0"); info += s; - Datacheck::add(w->route, p->label, w->label, "", "DUPLICATE_COORDS", info+')'); - } + Datacheck::add(w->route, p->label, w->label, "", "DUPLICATE_COORDS", + fmt::format("({:.15},{:.15})", w->lat, w->lng)); other_w->colocated->push_back(w); w->colocated = other_w->colocated; } @@ -169,11 +168,10 @@ void WaypointQuadtree::nmplogs() } std::string WaypointQuadtree::str() -{ char s[139]; - sprintf(s, "WaypointQuadtree at (%.15g,%.15g) to (%.15g,%.15g)", min_lat, min_lng, max_lat, max_lng); +{ std::string s = fmt::format("WaypointQuadtree at ({},{}) to ({},{}", min_lat, min_lng, max_lat, max_lng); if (refined()) - return std::string(s) + " REFINED"; - else return std::string(s) + " contains " + std::to_string(points.size()) + " waypoints"; + return s + " REFINED"; + else return s + " contains " + std::to_string(points.size()) + " waypoints"; } unsigned int WaypointQuadtree::size() From d4a87166176a7f26b07dfc6d3fb2fde4e9bb67aa Mon Sep 17 00:00:00 2001 From: eric bryant Date: Sun, 9 Jun 2024 19:31:01 -0400 Subject: [PATCH 3/3] fix unqualified calls to std::move fix warnings reported by clang 17.0.6 --- .../cplusplus/classes/HighwaySystem/HighwaySystem.cpp | 2 +- siteupdate/cplusplus/classes/Region/read_csvs.cpp | 2 +- siteupdate/cplusplus/classes/Route/Route.cpp | 6 +++--- siteupdate/cplusplus/classes/TravelerList/TravelerList.cpp | 2 +- .../cplusplus/classes/WaypointQuadtree/WaypointQuadtree.cpp | 4 ++-- siteupdate/cplusplus/tasks/subgraphs/fullcustom.cpp | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/siteupdate/cplusplus/classes/HighwaySystem/HighwaySystem.cpp b/siteupdate/cplusplus/classes/HighwaySystem/HighwaySystem.cpp index 06c37e9b..4e57070b 100644 --- a/siteupdate/cplusplus/classes/HighwaySystem/HighwaySystem.cpp +++ b/siteupdate/cplusplus/classes/HighwaySystem/HighwaySystem.cpp @@ -123,7 +123,7 @@ void HighwaySystem::systems_csv(ErrorList& el) if (line[0] == '#') continue; if (strchr(line.data(), '"')) el.add_error("Double quotes in systems.csv line: "+line); - lines.emplace_back(move(line)); + lines.emplace_back(std::move(line)); } it = syslist.alloc(lines.size()); for (std::string& l : lines) diff --git a/siteupdate/cplusplus/classes/Region/read_csvs.cpp b/siteupdate/cplusplus/classes/Region/read_csvs.cpp index f2b2f9a4..b4050b78 100644 --- a/siteupdate/cplusplus/classes/Region/read_csvs.cpp +++ b/siteupdate/cplusplus/classes/Region/read_csvs.cpp @@ -52,7 +52,7 @@ void Region::read_csvs(ErrorList& el) std::list lines; while(getline(file, line)) { if (line.size() && line.back() == 0x0D) line.pop_back(); // trim DOS newlines - if (line.size()) lines.emplace_back(move(line)); + if (line.size()) lines.emplace_back(std::move(line)); } lines.sort(sort_1st_csv_field); it = allregions.alloc(lines.size()+1); diff --git a/siteupdate/cplusplus/classes/Route/Route.cpp b/siteupdate/cplusplus/classes/Route/Route.cpp index bacaf6af..37eb56c9 100644 --- a/siteupdate/cplusplus/classes/Route/Route.cpp +++ b/siteupdate/cplusplus/classes/Route/Route.cpp @@ -252,14 +252,14 @@ void Route::con_mismatch() void Route::mark_label_in_use(std::string& label) { unused_alt_labels.erase(label); - labels_in_use.insert(move(label)); + labels_in_use.insert(std::move(label)); } void Route::mark_labels_in_use(std::string& label1, std::string& label2) { unused_alt_labels.erase(label1); unused_alt_labels.erase(label2); - labels_in_use.insert(move(label1)); - labels_in_use.insert(move(label2)); + labels_in_use.insert(std::move(label1)); + labels_in_use.insert(std::move(label2)); } // sort routes by most recent update for use at end of user logs diff --git a/siteupdate/cplusplus/classes/TravelerList/TravelerList.cpp b/siteupdate/cplusplus/classes/TravelerList/TravelerList.cpp index 0445b00d..6532dd71 100644 --- a/siteupdate/cplusplus/classes/TravelerList/TravelerList.cpp +++ b/siteupdate/cplusplus/classes/TravelerList/TravelerList.cpp @@ -165,7 +165,7 @@ TravelerList::TravelerList(std::string& travname, ErrorList* el) TravelerList::~TravelerList() {delete[] traveler_num;} void TravelerList::get_ids(ErrorList& el) -{ ids = move(Args::userlist); +{ ids = std::move(Args::userlist); if (ids.empty()) { DIR *dir; dirent *ent; diff --git a/siteupdate/cplusplus/classes/WaypointQuadtree/WaypointQuadtree.cpp b/siteupdate/cplusplus/classes/WaypointQuadtree/WaypointQuadtree.cpp index ac154f42..e9a77c91 100644 --- a/siteupdate/cplusplus/classes/WaypointQuadtree/WaypointQuadtree.cpp +++ b/siteupdate/cplusplus/classes/WaypointQuadtree/WaypointQuadtree.cpp @@ -139,8 +139,8 @@ void WaypointQuadtree::nmplogs() nmpfps.emplace(line, 0, line.size()-20); else if (line.size() >= 55 && !strcmp(line.data()+line.size()-24, " [SOME LOOK INTENTIONAL]")) nmpfps.emplace(line, 0, line.size()-24); - else nmpfps.emplace(move(line)); - else nmpfps.emplace(move(line)); + else nmpfps.emplace(std::move(line)); + else nmpfps.emplace(std::move(line)); } file.close(); diff --git a/siteupdate/cplusplus/tasks/subgraphs/fullcustom.cpp b/siteupdate/cplusplus/tasks/subgraphs/fullcustom.cpp index 66f0bb6e..c0c2e611 100644 --- a/siteupdate/cplusplus/tasks/subgraphs/fullcustom.cpp +++ b/siteupdate/cplusplus/tasks/subgraphs/fullcustom.cpp @@ -84,7 +84,7 @@ if (file.is_open()) } delete[] field; } - if (ok) GraphListEntry::add_group(move(root), move(descr), 'f', regions, systems, a); + if (ok) GraphListEntry::add_group(std::move(root), std::move(descr), 'f', regions, systems, a); else { delete regions; delete systems; delete a;