Skip to content

Commit

Permalink
[Core] Add function to convert container to string (#1342)
Browse files Browse the repository at this point in the history
* Add helper function to convert C++ container to string

Signed-off-by: Tim Moon <tmoon@nvidia.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Signed-off-by: Tim Moon <tmoon@nvidia.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
timmoon10 and pre-commit-ci[bot] authored Nov 22, 2024
1 parent 6b98768 commit 8952bc4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
10 changes: 10 additions & 0 deletions tests/cpp/util/test_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
************************************************************************/

#include <string>
#include <vector>

#include <gtest/gtest.h>

Expand Down Expand Up @@ -57,6 +58,12 @@ TEST(UtilTest, ToStringLike) { // to_string_like
EXPECT_EQ(std::stof(to_string_like(-2.5f)), -2.5f);
EXPECT_EQ(std::stod(to_string_like(2.25)), 2.25);
EXPECT_EQ(std::stod(to_string_like(-4.5)), -4.5);

// Container types
EXPECT_EQ(to_string_like(std::vector<int>{-3,1,-4}), "(-3,1,-4)");
EXPECT_EQ(to_string_like(std::vector<std::string>{"Accept", "no", "substitutes", ".",
"Buy", "N", "V", "IDIA"}),
"(Accept,no,substitutes,.,Buy,N,V,IDIA)");
}

TEST(UtilTest, ConcatStringsTest) { // concat_strings
Expand Down Expand Up @@ -88,6 +95,9 @@ TEST(UtilTest, ConcatStringsTest) { // concat_strings
EXPECT_EQ(std::stof(concat_strings(6.5f)), 6.5f);
EXPECT_EQ(std::stod(concat_strings("-", 4.25)), -4.25);
EXPECT_EQ(std::stod(concat_strings(8.5)), 8.5);

// Container types
EXPECT_EQ(concat_strings("vector ", std::vector<int>{1,-2,3}), "vector (1,-2,3)");
}

TEST(UtilTest, RegexReplaceTest) { // regex_replace
Expand Down
27 changes: 23 additions & 4 deletions transformer_engine/common/util/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,34 @@

namespace transformer_engine {

/*! \brief Convert to C-style or C++-style string */
inline const std::string &to_string_like(const std::string &val) noexcept { return val; }

constexpr const char *to_string_like(const char *val) noexcept { return val; }

/* \brief Convert arithmetic type to string */
template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value>::type>
inline std::string to_string_like(const T &val) {
return std::to_string(val);
}

inline const std::string &to_string_like(const std::string &val) noexcept { return val; }

constexpr const char *to_string_like(const char *val) noexcept { return val; }
/* \brief Convert container to string */
template <typename T, typename = typename std::enable_if<!std::is_arithmetic<T>::value>::type,
typename = decltype(std::declval<T>().begin())>
inline std::string to_string_like(const T &container) {
std::string str;
str.reserve(1024); // Assume strings are <1 KB
str += "(";
bool first = true;
for (const auto &val : container) {
if (!first) {
str += ",";
}
str += to_string_like(val);
first = false;
}
str += ")";
return str;
}

/*! \brief Convert arguments to strings and concatenate */
template <typename... Ts>
Expand Down

0 comments on commit 8952bc4

Please sign in to comment.