Skip to content

Commit

Permalink
Move nullptr check earlier for #107
Browse files Browse the repository at this point in the history
  • Loading branch information
cschreib committed Jul 11, 2023
1 parent a1439d5 commit e8c7dad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
12 changes: 6 additions & 6 deletions include/snitch/snitch_append.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,15 +323,15 @@ namespace snitch {

template<typename T>
[[nodiscard]] constexpr bool append(small_string_span ss, T* ptr) noexcept {
if (ptr == nullptr) {
return append(ss, nullptr);
}

if constexpr (std::is_same_v<std::remove_cv_t<T>, char>) {
return append(ss, std::string_view(ptr));
} else if constexpr (std::is_function_v<T>) {
if (ptr != nullptr) {
constexpr std::string_view function_ptr_str = "0x????????";
return append(ss, function_ptr_str);
} else {
return append(ss, nullptr);
}
constexpr std::string_view function_ptr_str = "0x????????";
return append(ss, function_ptr_str);
} else {
return append(ss, static_cast<const void*>(ptr));
}
Expand Down
8 changes: 8 additions & 0 deletions tests/runtime_tests/string_utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,14 @@ TEST_CASE("append misc", "[utility]") {
CONSTEXPR_CHECK(a(nullptr) == ae{"nul"sv, false});
}

SECTION("nullptr const char*") {
constexpr auto a = [](const char* value) constexpr {
return append_test::to_string<3, true>(value);
};

CONSTEXPR_CHECK(a(nullptr) == ae{"nul"sv, false});
}

SECTION("pointers do fit") {
constexpr auto a = [](const auto& value) constexpr {
return append_test::to_string<21, true>(static_cast<void*>(value));
Expand Down

0 comments on commit e8c7dad

Please sign in to comment.