Skip to content

Commit ce7385c

Browse files
committed
aoc_lib: make pretty_print::repr aware of aoc::as_number
This will apply the `char_as_number` flag for the entire argument of `as_number`.
1 parent 8fd0b10 commit ce7385c

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

aoc_lib/src/lib.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ ExpectInputHelper<std::string> expect_input(const char *expected) {
402402
// I/O manipulator that extracts an 8-bit number into a char instead of
403403
// a single ASCII character, and inserts a char as an 8-bit number.
404404
template <typename T>
405-
class as_number {
405+
struct as_number {
406406
T &dest;
407407
friend std::istream &operator>>(std::istream &is, as_number h) {
408408
static_assert(!std::is_const_v<T>,
@@ -435,7 +435,6 @@ class as_number {
435435
return os;
436436
}
437437

438-
public:
439438
explicit as_number(T &dest) : dest(dest) {}
440439
};
441440

aoc_lib/src/test_pretty_print.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,10 @@ std::size_t test_repr() {
7676
test('\x10', R"(16)", {.char_as_number = true});
7777
test(std::initializer_list<std::int8_t>{-2, 0, 15, -42, 127, -128},
7878
"{-2, 0, 15, -42, 127, -128}", {.char_as_number = true});
79-
test(std::vector<std::uint8_t>{254, 0, 15, 214, 127, 128}, "{254, 0, 15, 214, 127, 128}", {.char_as_number = true});
79+
std::vector<std::uint8_t> uint8_vec{254, 0, 15, 214, 127, 128};
80+
test(uint8_vec, "{254, 0, 15, 214, 127, 128}", {.char_as_number = true});
81+
test(aoc::as_number(uint8_vec), "{254, 0, 15, 214, 127, 128}");
82+
8083
return suite.done(), suite.num_failed();
8184
}
8285

aoc_lib/src/unit_test/pretty_print.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef PRETTY_PRINT_HPP
22
#define PRETTY_PRINT_HPP
33

4+
#include "lib.hpp" // for as_number
45
#include "util/concepts.hpp"
56
#include <array> // for array
67
#include <cctype> // for isprint
@@ -154,6 +155,15 @@ std::ostream &print_repr(std::ostream &os, const std::optional<T> &opt,
154155
return os;
155156
}
156157

158+
// aoc::as_number
159+
template <pretty_print::has_print_repr T>
160+
std::ostream &print_repr(std::ostream &os, const aoc::as_number<T> &h,
161+
pretty_print::repr_state state) {
162+
state.char_as_number = true;
163+
os << pretty_print::repr(h.dest, state);
164+
return os;
165+
}
166+
157167
/*
158168
* Base/leaf types
159169
*/
@@ -328,6 +338,8 @@ void _lint_helper_template(std::ostream &os, const T &t = T()) {
328338
_lint_helper_template<std::tuple<std::string, long, double>>(os);
329339
_lint_helper_template<std::optional<int>>(os);
330340
_lint_helper_template(os, std::strong_ordering::equal);
341+
std::vector<char> vec{'a', 'b', 'c'};
342+
_lint_helper_template(os, aoc::as_number(vec));
331343
}
332344
} // namespace
333345

0 commit comments

Comments
 (0)