Skip to content

Commit 82696f6

Browse files
Improve the function extract_type_identifier (#2923)
Signed-off-by: Barry Xu <[email protected]>
1 parent e615c7c commit 82696f6

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

rclcpp/src/rclcpp/typesupport_helpers.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "rclcpp/typesupport_helpers.hpp"
1717

18+
#include <algorithm>
1819
#include <functional>
1920
#include <memory>
2021
#include <sstream>
@@ -73,6 +74,21 @@ const void * get_typesupport_handle_impl(
7374
}
7475
}
7576

77+
// Trim leading and trailing whitespace from the string.
78+
std::string string_trim(std::string_view str_v)
79+
{
80+
auto begin = std::find_if_not(str_v.begin(), str_v.end(), [](unsigned char ch) {
81+
return std::isspace(ch);
82+
});
83+
auto end = std::find_if_not(str_v.rbegin(), str_v.rend(), [](unsigned char ch) {
84+
return std::isspace(ch);
85+
}).base();
86+
if (begin >= end) {
87+
return {};
88+
}
89+
return std::string(begin, end);
90+
}
91+
7692
} // anonymous namespace
7793

7894
std::tuple<std::string, std::string, std::string>
@@ -82,6 +98,7 @@ extract_type_identifier(const std::string & full_type)
8298
auto sep_position_back = full_type.find_last_of(type_separator);
8399
auto sep_position_front = full_type.find_first_of(type_separator);
84100
if (sep_position_back == std::string::npos ||
101+
sep_position_front == 0 ||
85102
sep_position_back == 0 ||
86103
sep_position_back == full_type.length() - 1)
87104
{
@@ -97,7 +114,8 @@ extract_type_identifier(const std::string & full_type)
97114
}
98115
std::string type_name = full_type.substr(sep_position_back + 1);
99116

100-
return std::make_tuple(package_name, middle_module, type_name);
117+
return std::make_tuple(
118+
string_trim(package_name), string_trim(middle_module), string_trim(type_name));
101119
}
102120

103121
std::string get_typesupport_library_path(

rclcpp/test/rclcpp/test_typesupport_helpers.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,15 @@ TEST(TypesupportHelpersTest, test_throw_exception_with_invalid_type) {
158158
rclcpp::get_action_typesupport_handle(invalid_type, "rosidl_typesupport_cpp", *library),
159159
std::runtime_error);
160160
}
161+
162+
TEST(TypesupportHelpersTest, throws_exception_if_filetype_has_multiple_slashes_at_start) {
163+
EXPECT_ANY_THROW(rclcpp::extract_type_identifier("//name_with_slashes_at_start"));
164+
}
165+
166+
TEST(TypesupportHelpersTestV3, ProcessesValidTypeWithWhitespace) {
167+
std::string package, middle, name;
168+
std::tie(package, middle, name) = rclcpp::extract_type_identifier(" package/ name ");
169+
EXPECT_EQ(package, "package");
170+
EXPECT_TRUE(middle.empty());
171+
EXPECT_EQ(name, "name");
172+
}

0 commit comments

Comments
 (0)