Skip to content

Commit

Permalink
Prevent color tags being truncated for #82
Browse files Browse the repository at this point in the history
  • Loading branch information
cschreib committed Apr 12, 2023
1 parent ed8874d commit 581c1b4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
5 changes: 4 additions & 1 deletion include/snitch/snitch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2102,8 +2102,11 @@ class registry {
template<typename... Args>
void print(Args&&... args) const noexcept {
small_string<max_message_length> message;
append_or_truncate(message, std::forward<Args>(args)...);
const bool could_fit = append(message, std::forward<Args>(args)...);
this->print_callback(message);
if (!could_fit) {
this->print_callback("...");
}
}

// Requires: number of tests + 1 <= max_test_cases, well-formed test ID.
Expand Down
14 changes: 12 additions & 2 deletions src/snitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace {
using namespace std::literals;
using color_t = const char*;
using color_t = std::string_view;

namespace color {
constexpr color_t error [[maybe_unused]] = "\x1b[1;31m";
Expand Down Expand Up @@ -312,7 +312,17 @@ using snitch::small_string;

template<typename T>
bool append(small_string_span ss, const colored<T>& colored_value) noexcept {
return append(ss, colored_value.color_start, colored_value.value, colored_value.color_end);
if (ss.available() <= colored_value.color_start.size() + colored_value.color_end.size()) {
return false;
}

bool could_fit = true;
if (!append(ss, colored_value.color_start, colored_value.value)) {
ss.resize(ss.capacity() - colored_value.color_end.size());
could_fit = false;
}

return append(ss, colored_value.color_end) && could_fit;
}

template<typename... Args>
Expand Down

0 comments on commit 581c1b4

Please sign in to comment.