Skip to content

Commit

Permalink
Use rcpputils::find_and_replace instead of std::regex_replace (#291)
Browse files Browse the repository at this point in the history
The rcpputils implementation is more performant.

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
  • Loading branch information
jacobperron committed Jul 26, 2019
1 parent 2e8acff commit 5f92397
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 10 deletions.
3 changes: 3 additions & 0 deletions rmw_fastrtps_dynamic_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ endif()

find_package(ament_cmake_ros REQUIRED)

find_package(rcpputils REQUIRED)
find_package(rcutils REQUIRED)
find_package(rmw_fastrtps_shared_cpp REQUIRED)

Expand Down Expand Up @@ -90,6 +91,7 @@ target_link_libraries(rmw_fastrtps_dynamic_cpp

# specific order: dependents before dependencies
ament_target_dependencies(rmw_fastrtps_dynamic_cpp
"rcpputils"
"rcutils"
"rosidl_typesupport_fastrtps_c"
"rosidl_typesupport_fastrtps_cpp"
Expand All @@ -114,6 +116,7 @@ ament_export_libraries(rmw_fastrtps_dynamic_cpp)
ament_export_dependencies(rosidl_typesupport_introspection_cpp)
ament_export_dependencies(rosidl_typesupport_introspection_c)
ament_export_dependencies(rosidl_generator_c)
ament_export_dependencies(rcpputils)
ament_export_dependencies(rcutils)
ament_export_dependencies(rmw_fastrtps_shared_cpp)
ament_export_dependencies(rmw)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@

#include <cassert>
#include <memory>
#include <regex>
#include <sstream>
#include <string>

#include "rcpputils/find_and_replace.hpp"

#include "rmw_fastrtps_dynamic_cpp/MessageTypeSupport.hpp"
#include "rosidl_typesupport_introspection_cpp/field_types.hpp"

Expand All @@ -41,7 +42,7 @@ MessageTypeSupport<MembersType>::MessageTypeSupport(const MembersType * members)
std::string message_name(this->members_->message_name_);
if (!message_namespace.empty()) {
// Find and replace C namespace separator with C++, in case this is using C typesupport
message_namespace = std::regex_replace(message_namespace, std::regex("__"), "::");
message_namespace = rcpputils::find_and_replace(message_namespace, "__", "::");
ss << message_namespace << "::";
}
ss << "dds_::" << message_name << "_";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
#include <fastcdr/FastBuffer.h>
#include <fastcdr/Cdr.h>
#include <cassert>
#include <regex>
#include <sstream>
#include <string>

#include "rcpputils/find_and_replace.hpp"

#include "rmw_fastrtps_dynamic_cpp/ServiceTypeSupport.hpp"
#include "rosidl_typesupport_introspection_cpp/field_types.hpp"

Expand All @@ -45,7 +46,7 @@ RequestTypeSupport<ServiceMembersType, MessageMembersType>::RequestTypeSupport(
std::string service_name(members->service_name_);
if (!service_namespace.empty()) {
// Find and replace C namespace separator with C++, in case this is using C typesupport
service_namespace = std::regex_replace(service_namespace, std::regex("__"), "::");
service_namespace = rcpputils::find_and_replace(service_namespace, "__", "::");
ss << service_namespace << "::";
}
ss << "dds_::" << service_name << "_Request_";
Expand Down Expand Up @@ -74,7 +75,7 @@ ResponseTypeSupport<ServiceMembersType, MessageMembersType>::ResponseTypeSupport
std::string service_name(members->service_name_);
if (!service_namespace.empty()) {
// Find and replace C namespace separator with C++, in case this is using C typesupport
service_namespace = std::regex_replace(service_namespace, std::regex("__"), "::");
service_namespace = rcpputils::find_and_replace(service_namespace, "__", "::");
ss << service_namespace << "::";
}
ss << "dds_::" << service_name << "_Response_";
Expand Down
2 changes: 2 additions & 0 deletions rmw_fastrtps_dynamic_cpp/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<build_depend>fastcdr</build_depend>
<build_depend>fastrtps</build_depend>
<build_depend>fastrtps_cmake_module</build_depend>
<build_depend>rcpputils</build_depend>
<build_depend>rcutils</build_depend>
<build_depend>rmw</build_depend>
<build_depend>rmw_fastrtps_shared_cpp</build_depend>
Expand All @@ -28,6 +29,7 @@
<build_export_depend>fastcdr</build_export_depend>
<build_export_depend>fastrtps</build_export_depend>
<build_export_depend>fastrtps_cmake_module</build_export_depend>
<build_export_depend>rcpputils</build_export_depend>
<build_export_depend>rcutils</build_export_depend>
<build_export_depend>rmw</build_export_depend>
<build_export_depend>rmw_fastrtps_shared_cpp</build_export_depend>
Expand Down
5 changes: 3 additions & 2 deletions rmw_fastrtps_dynamic_cpp/src/type_support_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
#ifndef TYPE_SUPPORT_COMMON_HPP_
#define TYPE_SUPPORT_COMMON_HPP_

#include <regex>
#include <sstream>
#include <string>

#include "fastrtps/Domain.h"
#include "fastrtps/participant/Participant.h"

#include "rcpputils/find_and_replace.hpp"

#include "rmw/error_handling.h"

#include "rmw_fastrtps_shared_cpp/TypeSupport.hpp"
Expand Down Expand Up @@ -85,7 +86,7 @@ _create_type_name(
std::ostringstream ss;
std::string message_namespace(members->message_namespace_);
// Find and replace C namespace separator with C++, in case this is using C typesupport
message_namespace = std::regex_replace(message_namespace, std::regex("__"), "::");
message_namespace = rcpputils::find_and_replace(message_namespace, "__", "::");
std::string message_name(members->message_name_);
if (!message_namespace.empty()) {
ss << message_namespace << "::";
Expand Down
6 changes: 3 additions & 3 deletions rmw_fastrtps_shared_cpp/src/demangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
// limitations under the License.

#include <algorithm>
#include <regex>
#include <string>
#include <vector>

#include "rcpputils/find_and_replace.hpp"
#include "rcutils/logging_macros.h"
#include "rcutils/types.h"

Expand Down Expand Up @@ -46,7 +46,7 @@ _demangle_if_ros_type(const std::string & dds_type_string)
}

std::string type_namespace = dds_type_string.substr(0, substring_position);
type_namespace = std::regex_replace(type_namespace, std::regex("::"), "/");
type_namespace = rcpputils::find_and_replace(type_namespace, "::", "/");
size_t start = substring_position + substring.size();
std::string type_name = dds_type_string.substr(start, dds_type_string.length() - 1 - start);
return type_namespace + type_name;
Expand Down Expand Up @@ -145,7 +145,7 @@ _demangle_service_type_only(const std::string & dds_type_name)
// everything checks out, reformat it from '[type_namespace::]dds_::<type><suffix>'
// to '[type_namespace/]<type>'
std::string type_namespace = dds_type_name.substr(0, ns_substring_position);
type_namespace = std::regex_replace(type_namespace, std::regex("::"), "/");
type_namespace = rcpputils::find_and_replace(type_namespace, "::", "/");
size_t start = ns_substring_position + ns_substring.length();
std::string type_name = dds_type_name.substr(start, suffix_position - start);
return type_namespace + type_name;
Expand Down

0 comments on commit 5f92397

Please sign in to comment.