diff --git a/rmw_fastrtps_shared_cpp/src/namespace_prefix.cpp b/rmw_fastrtps_shared_cpp/src/namespace_prefix.cpp index 4945e8518..d02fd0d51 100644 --- a/rmw_fastrtps_shared_cpp/src/namespace_prefix.cpp +++ b/rmw_fastrtps_shared_cpp/src/namespace_prefix.cpp @@ -31,10 +31,8 @@ const std::vector _ros_prefixes = std::string _resolve_prefix(const std::string & name, const std::string & prefix) { - if (name.length() > prefix.length()) { - if (name.rfind(prefix, 0) == 0 && name.at(prefix.length()) == '/') { - return name.substr(prefix.length()); - } + if (name.rfind(prefix + "/", 0) == 0) { + return name.substr(prefix.length()); } return ""; } @@ -44,10 +42,8 @@ std::string _get_ros_prefix_if_exists(const std::string & topic_name) { for (const auto & prefix : _ros_prefixes) { - if (topic_name.length() > prefix.length()) { - if (topic_name.rfind(prefix, 0) == 0 && topic_name.at(prefix.length()) == '/') { - return prefix; - } + if (topic_name.rfind(prefix + "/", 0) == 0) { + return prefix; } } return ""; @@ -58,10 +54,8 @@ std::string _strip_ros_prefix_if_exists(const std::string & topic_name) { for (const auto & prefix : _ros_prefixes) { - if (topic_name.length() > prefix.length()) { - if (topic_name.rfind(prefix, 0) == 0 && topic_name.at(prefix.length()) == '/') { - return topic_name.substr(prefix.length()); - } + if (topic_name.rfind(prefix + "/", 0) == 0) { + return topic_name.substr(prefix.length()); } } return topic_name;