From 3f527c67ac67d450abc371c2568a3baa167a8c2e Mon Sep 17 00:00:00 2001 From: DangitBen <30333928+DangitBen@users.noreply.github.com> Date: Wed, 27 Aug 2025 09:15:44 -0500 Subject: [PATCH] Improve whitespace trimming behavior (#859) moved MapvizPlugin::TrimString to protected from private make plugins use TrimString instead of remove_if with std::isspace Co-authored-by: Ben Andrew --- mapviz/include/mapviz/mapviz_plugin.h | 50 +++++++++---------- mapviz_plugins/src/laserscan_plugin.cpp | 7 +-- mapviz_plugins/src/marker_plugin.cpp | 6 +-- mapviz_plugins/src/pointcloud2_plugin.cpp | 6 +-- mapviz_plugins/src/textured_marker_plugin.cpp | 6 +-- tile_map/src/tile_map_plugin.cpp | 21 ++++++-- 6 files changed, 49 insertions(+), 47 deletions(-) diff --git a/mapviz/include/mapviz/mapviz_plugin.h b/mapviz/include/mapviz/mapviz_plugin.h index 8cfe5a7..47a1c69 100644 --- a/mapviz/include/mapviz/mapviz_plugin.h +++ b/mapviz/include/mapviz/mapviz_plugin.h @@ -349,6 +349,31 @@ protected: emitter << YAML::Key << prefix + "qos_durability" << YAML::Value << qos.durability; } + // Dealing with YAML frequently requires trimming whitespace from strings + inline std::string TrimString(const std::string& str) + { + auto begin = str.begin(); + auto end = str.end(); + + // Trim leading whitespace + while (begin != end && std::isspace(*begin)) + { + ++begin; + } + + // Trim trailing whitespace + if (begin != end) + { + do + { + --end; + } while (std::isspace(*end)); + ++end; + } + + return std::string(begin, end); + } + private: // Collect basic profiling info to know how much time each plugin // spends in Transform(), Paint(), and Draw(). @@ -358,31 +383,6 @@ private: }; typedef std::shared_ptr MapvizPluginPtr; -// Dealing with YAML frequently requires trimming whitespace from strings -inline std::string TrimString(const std::string& str) -{ - auto begin = str.begin(); - auto end = str.end(); - - // Trim leading whitespace - while (begin != end && std::isspace(*begin)) - { - ++begin; - } - - // Trim trailing whitespace - if (begin != end) - { - do - { - --end; - } while (std::isspace(*end)); - ++end; - } - - return std::string(begin, end); -} - // Implementation inline void MapvizPlugin::PrintErrorHelper(QLabel *status_label, const std::string &message, double throttle) diff --git a/mapviz_plugins/src/laserscan_plugin.cpp b/mapviz_plugins/src/laserscan_plugin.cpp index 0b47484..e89e81d 100644 --- a/mapviz_plugins/src/laserscan_plugin.cpp +++ b/mapviz_plugins/src/laserscan_plugin.cpp @@ -540,9 +540,7 @@ namespace mapviz_plugins LoadQosConfig(node, qos_); if (node["topic"]) { - std::string topic = node["topic"].as(); - std::string trimmed = topic; - trimmed.erase(std::remove_if(trimmed.begin(), trimmed.end(), ::isspace), trimmed.end()); + std::string trimmed = TrimString(node["topic"].as()); ui_.topic->setText(trimmed.c_str()); TopicEdited(); } @@ -666,8 +664,7 @@ namespace mapviz_plugins void LaserScanPlugin::SaveConfig(YAML::Emitter& emitter, const std::string& path) { - std::string trimmed = ui_.topic->text().toStdString(); - trimmed.erase(std::remove_if(trimmed.begin(), trimmed.end(), ::isspace), trimmed.end()); + std::string trimmed = TrimString(ui_.topic->text().toStdString()); emitter << YAML::Key << "topic" << YAML::Value << trimmed; diff --git a/mapviz_plugins/src/marker_plugin.cpp b/mapviz_plugins/src/marker_plugin.cpp index f9eefeb..83972b5 100644 --- a/mapviz_plugins/src/marker_plugin.cpp +++ b/mapviz_plugins/src/marker_plugin.cpp @@ -698,8 +698,7 @@ namespace mapviz_plugins LoadQosConfig(node, qos_); if (node["topic"]) { - std::string topic = node["topic"].as(); - topic.erase(std::remove_if(topic.begin(), topic.end(), ::isspace), topic.end()); + std::string topic = TrimString(node["topic"].as()); ui_.topic->setText(topic.c_str()); TopicEdited(); @@ -708,8 +707,7 @@ namespace mapviz_plugins void MarkerPlugin::SaveConfig(YAML::Emitter& emitter, const std::string& path) { - std::string trimmed = ui_.topic->text().toStdString(); - trimmed.erase(std::remove_if(trimmed.begin(), trimmed.end(), ::isspace), trimmed.end()); + std::string trimmed = TrimString(ui_.topic->text().toStdString()); emitter << YAML::Key << "topic" << YAML::Value diff --git a/mapviz_plugins/src/pointcloud2_plugin.cpp b/mapviz_plugins/src/pointcloud2_plugin.cpp index 56bb4cd..4eeec94 100644 --- a/mapviz_plugins/src/pointcloud2_plugin.cpp +++ b/mapviz_plugins/src/pointcloud2_plugin.cpp @@ -738,8 +738,7 @@ namespace mapviz_plugins if (node["topic"]) { - std::string topic = node["topic"].as(); - topic.erase(std::remove_if(topic.begin(), topic.end(), ::isspace), topic.end()); + std::string topic = TrimString(node["topic"].as()); ui_.topic->setText(topic.c_str()); TopicEdited(); } @@ -864,8 +863,7 @@ namespace mapviz_plugins void PointCloud2Plugin::SaveConfig(YAML::Emitter& emitter, const std::string& path) { - std::string topic = ui_.topic->text().toStdString(); - topic.erase(std::remove_if(topic.begin(), topic.end(), ::isspace), topic.end()); + std::string topic = TrimString(ui_.topic->text().toStdString()); emitter << YAML::Key << "topic" << YAML::Value << topic; emitter << YAML::Key << "size" << diff --git a/mapviz_plugins/src/textured_marker_plugin.cpp b/mapviz_plugins/src/textured_marker_plugin.cpp index e0992e0..19e2158 100755 --- a/mapviz_plugins/src/textured_marker_plugin.cpp +++ b/mapviz_plugins/src/textured_marker_plugin.cpp @@ -531,8 +531,7 @@ void TexturedMarkerPlugin::LoadConfig(const YAML::Node & node, const std::string { LoadQosConfig(node, qos_); if (node["topic"]) { - std::string topic = node["topic"].as(); - topic.erase(std::remove_if(topic.begin(), topic.end(), ::isspace), topic.end()); + std::string topic = TrimString(node["topic"].as()); ui_.topic->setText(topic.c_str()); } @@ -541,8 +540,7 @@ void TexturedMarkerPlugin::LoadConfig(const YAML::Node & node, const std::string void TexturedMarkerPlugin::SaveConfig(YAML::Emitter & emitter, const std::string & path) { - std::string topic = ui_.topic->text().toStdString(); - topic.erase(std::remove_if(topic.begin(), topic.end(), ::isspace), topic.end()); + std::string topic = TrimString(ui_.topic->text().toStdString()); emitter << YAML::Key << "topic" << YAML::Value << topic; SaveQosConfig(emitter, qos_); diff --git a/tile_map/src/tile_map_plugin.cpp b/tile_map/src/tile_map_plugin.cpp index 9c236e8..b5bd398 100644 --- a/tile_map/src/tile_map_plugin.cpp +++ b/tile_map/src/tile_map_plugin.cpp @@ -27,6 +27,7 @@ // // ***************************************************************************** +#include #include #include #include @@ -399,6 +400,19 @@ namespace tile_map } } + std::string trim_whitespace(const std::string& in) + { + std::string out; + const char* whitespace = " \t"; + auto begin = in.find_first_not_of(whitespace); + if (begin == std::string::npos) + { + return in; + } + auto end = in.find_last_not_of(whitespace); + return in.substr(begin, (end - begin + 1)); + } + void TileMapPlugin::SaveConfig(YAML::Emitter& emitter, const std::string&) { emitter << YAML::Key << CUSTOM_SOURCES_KEY << YAML::Value << YAML::BeginSeq; @@ -417,15 +431,12 @@ namespace tile_map } } emitter << YAML::EndSeq; - BingSource* bing_source = dynamic_cast(tile_sources_[BING_NAME].get()); - std::string bing_key = bing_source->GetApiKey().toStdString(); - bing_key.erase(std::remove_if(bing_key.begin(), bing_key.end(), ::isspace), bing_key.end()); + std::string bing_key = TrimString(bing_source->GetApiKey().toStdString()); emitter << YAML::Key << BING_API_KEY << YAML::Value << bing_key; - std::string combo_str = ui_.source_combo->currentText().toStdString(); - combo_str.erase(std::remove_if(combo_str.begin(), combo_str.end(), ::isspace), combo_str.end()); + std::string combo_str = TrimString(ui_.source_combo->currentText().toStdString()); emitter << YAML::Key << SOURCE_KEY << YAML::Value << combo_str; }