3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-05-05 23:05:46 +00:00

nicer lit_pp

This commit is contained in:
Jakob Rath 2022-09-23 15:58:44 +02:00
parent a4f0e3a228
commit 4312611bd6
2 changed files with 69 additions and 21 deletions

View file

@ -46,23 +46,62 @@ show_deref_t<T> show_deref(Ptr const& ptr) {
return show_deref_t<T>{ptr.get()};
}
template <typename T>
struct repeat {
unsigned count;
T const& obj;
repeat(unsigned count, T const& obj): count(count), obj(obj) {}
};
template <typename T>
std::ostream& operator<<(std::ostream& out, repeat<T> const& r) {
for (unsigned i = r.count; i-- > 0; ) {
out << r.obj;
}
return out;
}
enum class pad_direction {
left,
right,
};
template <typename T>
struct pad {
pad_direction dir;
unsigned width;
T const& obj;
pad(pad_direction dir, unsigned width, T const& obj): dir(dir), width(width), obj(obj) {}
};
template <typename T>
std::ostream& operator<<(std::ostream& out, pad<T> const& p) {
std::stringstream tmp;
tmp << p.obj;
std::string s = tmp.str();
unsigned n = (s.length() < p.width) ? (p.width - s.length()) : 0;
switch (p.dir) {
case pad_direction::left:
out << repeat(n, ' ') << s;
break;
case pad_direction::right:
out << s << repeat(n, ' ');
break;
}
return out;
}
/// Fill with spaces to the right:
/// out << rpad(8, "hello")
/// writes "hello ".
template <typename T>
struct rpad {
unsigned width;
T const& obj;
rpad(unsigned width, T const& obj): width(width), obj(obj) {}
};
template <typename T>
std::ostream& operator<<(std::ostream& out, rpad<T> const& pad) {
std::stringstream tmp;
tmp << pad.obj;
auto s = tmp.str();
out << s;
if (s.length() < pad.width)
out << std::string(pad.width - s.length(), ' ');
return out;
pad<T> rpad(unsigned width, T const& obj) {
return pad<T>(pad_direction::right, width, obj);
}
/// Fill with spaces to the left.
template <typename T>
pad<T> lpad(unsigned width, T const& obj) {
return pad<T>(pad_direction::left, width, obj);
}