Skip to content

Commit

Permalink
Cleanup liveliness_utils
Browse files Browse the repository at this point in the history
Signed-off-by: Yadunund <[email protected]>
  • Loading branch information
Yadunund committed Nov 27, 2023
1 parent c4fa36c commit 6bc2f39
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 37 deletions.
67 changes: 36 additions & 31 deletions rmw_zenoh_cpp/src/detail/liveliness_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "liveliness_utils.hpp"

#include <sstream>
#include <vector>

#include "rcpputils/scope_exit.hpp"
#include "rcutils/logging_macros.h"
Expand Down Expand Up @@ -158,6 +159,41 @@ std::optional<Entity> Entity::make(
return std::move(entity);
}

namespace
{
///=============================================================================
std::vector<std::string> split_keyexpr(
const std::string & keyexpr,
const char delim = '/')
{
std::vector<std::size_t> delim_idx = {};
// Insert -1 for starting position to make the split easier when using substr.
delim_idx.push_back(-1);
std::size_t idx = 0;
for (auto it = keyexpr.begin(); it != keyexpr.end(); ++it) {
if (*it == delim) {
delim_idx.push_back(idx);
}
++idx;
}
std::vector<std::string> result = {};
try {
for (std::size_t i = 1; i < delim_idx.size(); ++i) {
const auto & prev_idx = delim_idx.at(i - 1);
const auto & idx = delim_idx.at(i);
result.push_back(keyexpr.substr(prev_idx + 1, idx - prev_idx - 1));
}
} catch (const std::exception & e) {
printf("%s\n", e.what());
return {};
}
// Finally add the last substr.
result.push_back(keyexpr.substr(delim_idx.back() + 1));
return result;
}

} // namespace

///=============================================================================
std::optional<Entity> Entity::make(const std::string & keyexpr)
{
Expand Down Expand Up @@ -308,35 +344,4 @@ bool PublishToken::del(
return true;
}

///=============================================================================
std::vector<std::string> split_keyexpr(
const std::string & keyexpr,
const char delim)
{
std::vector<std::size_t> delim_idx = {};
// Insert -1 for starting position to make the split easier when using substr.
delim_idx.push_back(-1);
std::size_t idx = 0;
for (auto it = keyexpr.begin(); it != keyexpr.end(); ++it) {
if (*it == delim) {
delim_idx.push_back(idx);
}
++idx;
}
std::vector<std::string> result = {};
try {
for (std::size_t i = 1; i < delim_idx.size(); ++i) {
const auto & prev_idx = delim_idx.at(i - 1);
const auto & idx = delim_idx.at(i);
result.push_back(keyexpr.substr(prev_idx + 1, idx - prev_idx - 1));
}
} catch (const std::exception & e) {
printf("%s\n", e.what());
return {};
}
// Finally add the last substr.
result.push_back(keyexpr.substr(delim_idx.back() + 1));
return result;
}

} // namespace liveliness
6 changes: 0 additions & 6 deletions rmw_zenoh_cpp/src/detail/liveliness_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,6 @@ class PublishToken
const std::string & token);
};

///=============================================================================
/// Split a liveliness token's expression into parts based on a delimiter.
std::vector<std::string> split_keyexpr(
const std::string & keyexpr,
const char delim = '/');

} // namespace liveliness

#endif // DETAIL__LIVELINESS_UTILS_HPP_

0 comments on commit 6bc2f39

Please sign in to comment.