Skip to content

Commit 59b18d4

Browse files
create as_bin as_hex wrappers for display
Signed-off-by: Nikolaj Bjorner <[email protected]>
1 parent 999e67d commit 59b18d4

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

src/api/api_numeral.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ extern "C" {
188188
bool ok = Z3_get_numeral_rational(c, a, r);
189189
if (ok && r.is_int() && !r.is_neg()) {
190190
std::stringstream strm;
191-
r.display_bin(strm, r.get_num_bits());
191+
strm << r.as_bin(r.get_num_bits());
192192
return mk_c(c)->mk_external_string(std::move(strm).str());
193193
}
194194
else {

src/ast/bv_decl_plugin.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -912,13 +912,9 @@ app * bv_util::mk_numeral(rational const & val, unsigned bv_size) const {
912912

913913
if (m_plugin->log_constant_meaning_prelude(r)) {
914914
if (bv_size % 4 == 0) {
915-
m_manager.trace_stream() << "#x";
916-
val.display_hex(m_manager.trace_stream(), bv_size);
917-
m_manager.trace_stream() << "\n";
915+
m_manager.trace_stream() << "#x" << val.as_hex(bv_size) << "\n";
918916
} else {
919-
m_manager.trace_stream() << "#b";
920-
val.display_bin(m_manager.trace_stream(), bv_size);
921-
m_manager.trace_stream() << "\n";
917+
m_manager.trace_stream() << "#b" << val.as_bin(bv_size) << "\n";
922918
}
923919
}
924920

src/util/rational.h

+30-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ class rational {
3030

3131
static synch_mpq_manager & m() { return *g_mpq_manager; }
3232

33+
void display_hex(std::ostream & out, unsigned num_bits) const { SASSERT(is_int()); m().display_hex(out, m_val.numerator(), num_bits); }
34+
35+
void display_bin(std::ostream& out, unsigned num_bits) const { SASSERT(is_int()); m().display_bin(out, m_val.numerator(), num_bits); }
36+
3337
public:
3438
static void initialize();
3539
static void finalize();
@@ -96,9 +100,33 @@ class rational {
96100

97101
void display_smt2(std::ostream & out) const { return m().display_smt2(out, m_val, false); }
98102

99-
void display_hex(std::ostream & out, unsigned num_bits) const { SASSERT(is_int()); return m().display_hex(out, m_val.numerator(), num_bits); }
100103

101-
void display_bin(std::ostream & out, unsigned num_bits) const { SASSERT(is_int()); return m().display_bin(out, m_val.numerator(), num_bits); }
104+
struct as_hex_wrapper {
105+
rational const& r;
106+
unsigned bw;
107+
};
108+
109+
as_hex_wrapper as_hex(unsigned bw) const { return as_hex_wrapper{*this, bw}; }
110+
111+
friend inline std::ostream& operator<<(std::ostream& out, as_hex_wrapper const& ab) {
112+
ab.r.display_hex(out, ab.bw);
113+
return out;
114+
}
115+
116+
117+
118+
struct as_bin_wrapper {
119+
rational const& r;
120+
unsigned bw;
121+
};
122+
123+
as_bin_wrapper as_bin(unsigned bw) const { return as_bin_wrapper{*this, bw}; }
124+
125+
friend inline std::ostream& operator<<(std::ostream& out, as_bin_wrapper const& ab) {
126+
ab.r.display_bin(out, ab.bw);
127+
return out;
128+
}
129+
102130

103131
bool is_uint64() const { return m().is_uint64(m_val); }
104132

0 commit comments

Comments
 (0)