From 6ae4422facef5c7435e1f843695787894eba4470 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 15 Sep 2024 09:33:42 +0800 Subject: [PATCH] perf: debug output of RawCode --- src/core/raw_code/internal/raw_code.cc | 23 ++++++++++++----------- src/core_test/codec/raw_code.cc | 2 +- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/core/raw_code/internal/raw_code.cc b/src/core/raw_code/internal/raw_code.cc index 48a8949..bcca2c1 100644 --- a/src/core/raw_code/internal/raw_code.cc +++ b/src/core/raw_code/internal/raw_code.cc @@ -1,3 +1,5 @@ +#include + #include "utils/common.h" #include "raw_code/raw_code.h" @@ -5,18 +7,17 @@ using klotski::codec::RawCode; #ifndef KLSK_NDEBUG std::ostream& klotski::codec::operator<<(std::ostream &out, const RawCode self) { - char *code; - asprintf(&code, "%015llX\n", self.code_); // code length -> 15 - out << code; - free(code); - - constexpr char map[] = { - // 0x0 1x2 2x1 1x1 2x2 b101 b110 fill - '.', '~', '|', '*', '@', '?', '?', '+' - }; + constexpr auto char_map = std::to_array({ + '.', // space + '~', '|', // 1x2 | 2x1 + '*', '@', // 1x1 | 2x2 + '?', '?', // unknown + '+', // fill + }); + out << std::format("{:015X}\n", self.code_); for (int addr = 0; addr < 60; addr += 3) { - out << map[(self.code_ >> addr) & 0b111]; - out << " " << &"\n"[(addr & 0b11) != 0b01]; + out << char_map[(self.code_ >> addr) & 0b111]; + out << &" "[(addr & 0b11) == 0b01] << &"\n"[(addr & 0b11) != 0b01]; } return out; } diff --git a/src/core_test/codec/raw_code.cc b/src/core_test/codec/raw_code.cc index 2ab3167..4518658 100644 --- a/src/core_test/codec/raw_code.cc +++ b/src/core_test/codec/raw_code.cc @@ -33,7 +33,7 @@ TEST(RawCode, basic) { #ifndef KLSK_NDEBUG std::ostringstream out; out << RawCode::unsafe_create(TEST_R_CODE); // ostream capture - EXPECT_EQ(out.str(), "603EDF5CAFFF5E2\n| @ + | \n+ + + + \n| ~ + | \n+ * * + \n* . . * \n"); + EXPECT_EQ(out.str(), "603EDF5CAFFF5E2\n| @ + |\n+ + + +\n| ~ + |\n+ * * +\n* . . *\n"); #endif }