Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Core] Add function to convert container to string #1342

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading