From 564379569c91890136ef6aa44f1a84bbb2237726 Mon Sep 17 00:00:00 2001 From: Michel Hidalgo Date: Wed, 20 May 2020 09:52:47 -0300 Subject: [PATCH] Update ROS prefix utilities impl and tests. Signed-off-by: Michel Hidalgo --- rmw_fastrtps_shared_cpp/src/namespace_prefix.cpp | 14 +++++++++----- rmw_fastrtps_shared_cpp/test/test_names.cpp | 6 ++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/rmw_fastrtps_shared_cpp/src/namespace_prefix.cpp b/rmw_fastrtps_shared_cpp/src/namespace_prefix.cpp index e5bbd8fad..4945e8518 100644 --- a/rmw_fastrtps_shared_cpp/src/namespace_prefix.cpp +++ b/rmw_fastrtps_shared_cpp/src/namespace_prefix.cpp @@ -31,7 +31,7 @@ const std::vector _ros_prefixes = std::string _resolve_prefix(const std::string & name, const std::string & prefix) { - if (!name.empty()) { + if (name.length() > prefix.length()) { if (name.rfind(prefix, 0) == 0 && name.at(prefix.length()) == '/') { return name.substr(prefix.length()); } @@ -44,8 +44,10 @@ std::string _get_ros_prefix_if_exists(const std::string & topic_name) { for (const auto & prefix : _ros_prefixes) { - if (topic_name.rfind(prefix, 0) == 0 && topic_name.at(prefix.length()) == '/') { - return prefix; + if (topic_name.length() > prefix.length()) { + if (topic_name.rfind(prefix, 0) == 0 && topic_name.at(prefix.length()) == '/') { + return prefix; + } } } return ""; @@ -56,8 +58,10 @@ std::string _strip_ros_prefix_if_exists(const std::string & topic_name) { for (const auto & prefix : _ros_prefixes) { - if (topic_name.rfind(prefix, 0) == 0 && topic_name.at(prefix.length()) == '/') { - return topic_name.substr(prefix.length()); + if (topic_name.length() > prefix.length()) { + if (topic_name.rfind(prefix, 0) == 0 && topic_name.at(prefix.length()) == '/') { + return topic_name.substr(prefix.length()); + } } } return topic_name; diff --git a/rmw_fastrtps_shared_cpp/test/test_names.cpp b/rmw_fastrtps_shared_cpp/test/test_names.cpp index 64b00794e..6be353ee6 100644 --- a/rmw_fastrtps_shared_cpp/test/test_names.cpp +++ b/rmw_fastrtps_shared_cpp/test/test_names.cpp @@ -23,8 +23,10 @@ TEST(NamespaceTest, get_prefix) { EXPECT_EQ("", _get_ros_prefix_if_exists("")); EXPECT_EQ("", _get_ros_prefix_if_exists("not/a_ros_prefix")); for (const auto & prefix : _get_all_ros_prefixes()) { + EXPECT_EQ("", _get_ros_prefix_if_exists(prefix)); EXPECT_EQ("", _get_ros_prefix_if_exists(prefix + "_should_not_match")); EXPECT_EQ("", _get_ros_prefix_if_exists("th/is_should_not_match/" + prefix)); + EXPECT_EQ(prefix, _get_ros_prefix_if_exists(prefix + "/")); EXPECT_EQ(prefix, _get_ros_prefix_if_exists(prefix + "/should_match")); } } @@ -33,6 +35,8 @@ TEST(NamespaceTest, strip_prefix) { EXPECT_EQ("", _strip_ros_prefix_if_exists("")); EXPECT_EQ("/no_ros_prefix/test", _strip_ros_prefix_if_exists("/no_ros_prefix/test")); for (const auto & prefix : _get_all_ros_prefixes()) { + EXPECT_EQ(prefix, _strip_ros_prefix_if_exists(prefix)); + EXPECT_EQ("/", _strip_ros_prefix_if_exists(prefix + "/")); EXPECT_EQ( prefix + "should_not_be_stripped", _strip_ros_prefix_if_exists(prefix + "should_not_be_stripped")); @@ -48,6 +52,8 @@ TEST(NamespaceTest, resolve_prefix) { EXPECT_EQ("", _resolve_prefix("", "some_ros_prefix")); EXPECT_EQ("", _resolve_prefix("/test", "some_ros_prefix")); EXPECT_EQ("/test", _resolve_prefix("/test", "")); + EXPECT_EQ("", _resolve_prefix("some_ros_prefix", "some_ros_prefix")); + EXPECT_EQ("/", _resolve_prefix("some_ros_prefix/", "some_ros_prefix")); EXPECT_EQ( "/test_some_ros_prefix", _resolve_prefix("some_ros_prefix/test_some_ros_prefix", "some_ros_prefix"));