[traffxml] Faster MessageFromXml

Signed-off-by: x7z4w <x7z4w@noreply.codeberg.org>
This commit is contained in:
x7z4w
2025-11-03 15:32:26 +00:00
committed by mvglasow
parent af20e2b42b
commit 615f57c604
2 changed files with 16 additions and 14 deletions

View File

@@ -3,13 +3,15 @@
#include "base/logging.hpp" #include "base/logging.hpp"
#include <iomanip> #include <iomanip>
#include <regex> #include <unordered_map>
#include <boost/regex.hpp>
using namespace std; using namespace std;
namespace traffxml namespace traffxml
{ {
const std::map<EventType, traffic::SpeedGroup> kEventSpeedGroupMap{ const std::unordered_map<EventType, traffic::SpeedGroup> kEventSpeedGroupMap{
// TODO Activity*, Authority*, Carpool* (not in enum yet) // TODO Activity*, Authority*, Carpool* (not in enum yet)
{EventType::CongestionHeavyTraffic, traffic::SpeedGroup::G4}, {EventType::CongestionHeavyTraffic, traffic::SpeedGroup::G4},
{EventType::CongestionLongQueue, traffic::SpeedGroup::G0}, {EventType::CongestionLongQueue, traffic::SpeedGroup::G0},
@@ -54,7 +56,7 @@ const std::map<EventType, traffic::SpeedGroup> kEventSpeedGroupMap{
// none of the currently define events imply an explicit maxspeed // none of the currently define events imply an explicit maxspeed
#if 0 #if 0
const std::map<EventType, uint8_t> kEventMaxspeedMap{ const std::unordered_map<EventType, uint8_t> kEventMaxspeedMap{
// TODO Activity*, Authority*, Carpool* (not in enum yet) // TODO Activity*, Authority*, Carpool* (not in enum yet)
// TODO Construction* (not in enum yet) // TODO Construction* (not in enum yet)
// TODO Environment*, EquipmentStatus*, Hazard*, Incident* (not in enum yet) // TODO Environment*, EquipmentStatus*, Hazard*, Incident* (not in enum yet)
@@ -63,7 +65,7 @@ const std::map<EventType, uint8_t> kEventMaxspeedMap{
}; };
#endif #endif
const std::map<EventType, uint16_t> kEventDelayMap{ const std::unordered_map<EventType, uint16_t> kEventDelayMap{
// TODO Activity*, Authority*, Carpool* (not in enum yet) // TODO Activity*, Authority*, Carpool* (not in enum yet)
// TODO Construction* (not in enum yet) // TODO Construction* (not in enum yet)
//{EventType::DelayDelay, }, // mapped to speed group //{EventType::DelayDelay, }, // mapped to speed group
@@ -121,10 +123,10 @@ std::optional<IsoTime> IsoTime::ParseIsoTime(std::string timeString)
* 11: :00 (UTC offset, minutes, prefixed with separator) * 11: :00 (UTC offset, minutes, prefixed with separator)
* 12: 00 (UTC offset, minutes, unsigned; blank for Z or if not specified) * 12: 00 (UTC offset, minutes, unsigned; blank for Z or if not specified)
*/ */
std::regex iso8601Regex("([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2}(\\.[0-9]*)?)(Z|(([+-][0-9]{2})(:?([0-9]{2}))?))?"); static boost::regex iso8601Regex("([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2}(\\.[0-9]*)?)(Z|(([+-][0-9]{2})(:?([0-9]{2}))?))?");
std::smatch iso8601Matcher; boost::smatch iso8601Matcher;
if (std::regex_search(timeString, iso8601Matcher, iso8601Regex)) if (boost::regex_search(timeString, iso8601Matcher, iso8601Regex))
{ {
int offset_h = iso8601Matcher[10].matched ? std::stoi(iso8601Matcher[10]) : 0; int offset_h = iso8601Matcher[10].matched ? std::stoi(iso8601Matcher[10]) : 0;
int offset_m = iso8601Matcher[12].matched ? std::stoi(iso8601Matcher[12]) : 0; int offset_m = iso8601Matcher[12].matched ? std::stoi(iso8601Matcher[12]) : 0;

View File

@@ -6,11 +6,11 @@
#include <cstring> #include <cstring>
#include <iomanip> #include <iomanip>
#include <optional> #include <optional>
#include <regex>
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
#include <boost/bimap.hpp> #include <boost/bimap.hpp>
#include <boost/regex.hpp>
#include <pugixml.hpp> #include <pugixml.hpp>
@@ -504,9 +504,9 @@ bool LatLonFromXml(pugi::xml_node const & node, ms::LatLon & latLon)
std::string string; std::string string;
if (StringFromXml(node, string)) if (StringFromXml(node, string))
{ {
std::regex latLonRegex("([+-]?[0-9]*\\.?[0-9]*)\\s+([+-]?[0-9]*\\.?[0-9]*)"); static boost::regex latLonRegex("([+-]?[0-9]*\\.?[0-9]*)\\s+([+-]?[0-9]*\\.?[0-9]*)");
std::smatch latLonMatcher; boost::smatch latLonMatcher;
if (std::regex_search(string, latLonMatcher, latLonRegex) && latLonMatcher[1].matched && latLonMatcher[2].matched) if (boost::regex_search(string, latLonMatcher, latLonRegex) && latLonMatcher[1].matched && latLonMatcher[2].matched)
{ {
try try
{ {
@@ -689,10 +689,10 @@ std::optional<uint16_t> OptionalDurationFromXml(pugi::xml_attribute const & attr
* 1 h * 1 h
* 30 min * 30 min
*/ */
std::regex durationRegex("(([0-9]+):([0-9]{2}))|(([0-9]+) *h)|(([0-9]+) *min)"); static boost::regex durationRegex("(([0-9]+):([0-9]{2}))|(([0-9]+) *h)|(([0-9]+) *min)");
std::smatch durationMatcher; boost::smatch durationMatcher;
if (std::regex_search(durationString, durationMatcher, durationRegex)) if (boost::regex_search(durationString, durationMatcher, durationRegex))
{ {
if (!durationMatcher.str(2).empty() && !durationMatcher.str(3).empty()) if (!durationMatcher.str(2).empty() && !durationMatcher.str(3).empty())
return std::stoi(durationMatcher[2]) * 60 + std::stoi(durationMatcher[3]); return std::stoi(durationMatcher[2]) * 60 + std::stoi(durationMatcher[3]);