Skip to content

Commit

Permalink
Add to_string_for_print for string literals.
Browse files Browse the repository at this point in the history
  • Loading branch information
bbannier committed Nov 17, 2023
1 parent 842a3df commit eff3e56
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions hilti/runtime/include/types/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,13 @@ inline std::string detail::to_string_for_print<std::string_view>(const std::stri
return escapeUTF8(x, false, false, true);
}

// Specialization for string literals. Since `to_string_for_print` is not
// implemented with ADL like e.g., `to_string` provide an overload for string
// literals. This is needed since we cannot partially specialize
// `to_string_for_print`.
template<typename CharT, size_t N>
inline std::string to_string_for_print(const CharT (&x)[N]) {
return x;
}

} // namespace hilti::rt
12 changes: 12 additions & 0 deletions hilti/runtime/src/tests/string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,16 @@ TEST_CASE("upper") {
"illegal UTF8 sequence in string", const RuntimeError&);
}

TEST_CASE("to_string") {
CHECK_EQ(to_string(std::string("abc")), "\"abc\"");
CHECK_EQ(to_string(std::string_view("abc")), "\"abc\"");
CHECK_EQ(to_string("abc"), "\"abc\"");
}

TEST_CASE("to_string_for_print") {
CHECK_EQ(to_string_for_print(std::string("abc")), "abc");
CHECK_EQ(to_string_for_print(std::string_view("abc")), "abc");
CHECK_EQ(to_string_for_print("abc"), "abc");
}

TEST_SUITE_END();

0 comments on commit eff3e56

Please sign in to comment.