diff --git a/rmw_opensplice_cpp/src/demangle.cpp b/rmw_opensplice_cpp/src/demangle.cpp index b6145511..1b769b3c 100644 --- a/rmw_opensplice_cpp/src/demangle.cpp +++ b/rmw_opensplice_cpp/src/demangle.cpp @@ -12,8 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include #include +#include +#include +#include #include #include "rcutils/logging_macros.h" @@ -36,19 +38,24 @@ _demangle_if_ros_topic(const std::string & topic_name) std::string _demangle_if_ros_type(const std::string & dds_type_string) { - std::string substring = "::msg::dds_::"; - size_t substring_position = dds_type_string.find(substring); + std::regex dds_namespace_pattern(".*(::.*::dds_::).*"); + std::smatch match; if ( - dds_type_string[dds_type_string.size() - 1] == '_' && - substring_position != std::string::npos) + dds_type_string[dds_type_string.size() - 1] != '_' || + !std::regex_match(dds_type_string, match, dds_namespace_pattern)) { - std::string pkg = dds_type_string.substr(0, substring_position); - size_t start = substring_position + substring.size(); - std::string type_name = dds_type_string.substr(start, dds_type_string.length() - 1 - start); - return pkg + "/" + type_name; + // not a ROS type + return dds_type_string; } - // not a ROS type - return dds_type_string; + + // The first submatch is the whole string and the second is the parenthesized expression + assert(2u == match.size()); + std::string substring = match[1].str(); + size_t substring_position = dds_type_string.find(substring); + std::string pkg = dds_type_string.substr(0, substring_position); + size_t start = substring_position + substring.size(); + std::string type_name = dds_type_string.substr(start, dds_type_string.length() - 1 - start); + return pkg + "/" + type_name; } std::string @@ -108,12 +115,16 @@ _demangle_service_from_topic(const std::string & topic_name) std::string _demangle_service_type_only(const std::string & dds_type_name) { - std::string ns_substring = "::srv::dds_::"; - size_t ns_substring_position = dds_type_name.find(ns_substring); - if (ns_substring_position == std::string::npos) { + std::regex dds_namespace_pattern(".*(::.*::dds_::).*"); + std::smatch match; + if (!std::regex_match(dds_type_name, match, dds_namespace_pattern)) { // not a ROS service type return ""; } + // The first submatch is the whole string and the second is the parenthesized expression + assert(2u == match.size()); + std::string ns_substring = match[1].str(); + size_t ns_substring_position = dds_type_name.find(ns_substring); auto suffixes = { std::string("_Response_"), std::string("_Request_"),