Skip to content

Commit

Permalink
Update ROS prefix utilities impl and tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Michel Hidalgo <[email protected]>
  • Loading branch information
hidmic committed May 20, 2020
1 parent 47693f9 commit 5643795
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
14 changes: 9 additions & 5 deletions rmw_fastrtps_shared_cpp/src/namespace_prefix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const std::vector<std::string> _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());
}
Expand All @@ -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 "";
Expand All @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions rmw_fastrtps_shared_cpp/test/test_names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
Expand All @@ -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"));
Expand All @@ -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"));
Expand Down

0 comments on commit 5643795

Please sign in to comment.