base: helper for hex-formatted output of ranges

This commit is contained in:
Norman Feske 2016-08-16 12:51:33 +02:00 committed by Christian Helmuth
parent 9c2f229246
commit 26a4534a37
2 changed files with 65 additions and 7 deletions

View File

@ -167,6 +167,25 @@ namespace Genode {
*/
void print(Output &output, Hex const &);
/**
* Print range as hexadecimal format
*
* This helper is intended for the output for memory-address ranges. For
* brevity, it omits the '0x' prefix from the numbers. The numbers are
* padded with leading zeros to foster the horizontal alignment of
* consecutive outputs (like a table of address ranges).
*/
template <typename T>
struct Hex_range
{
T const base;
size_t const len;
Hex_range(T base, size_t len) : base(base), len(len) { }
void print(Output &out) const;
};
/**
* Helper for the output of an individual character
*
@ -208,4 +227,29 @@ namespace Genode {
}
}
template <typename T>
void Genode::Hex_range<T>::print(Output &out) const
{
using Genode::print;
Hex const from(base, Hex::OMIT_PREFIX, Hex::PAD);
T const end = base + len;
/* if end at integer limit, use ']' as closing delimiter */
if (base && end == 0) {
Hex const inclusive_to((T)(end - 1), Hex::OMIT_PREFIX, Hex::PAD);
print(out, "[", from, ",", inclusive_to, "]");
return;
}
/* use exclusive upper limit for ordinary ranges */
print(out, "[", from, ",", Hex(end, Hex::OMIT_PREFIX, Hex::PAD), ")");
/* output warning on integer-overflowing upper limit or empty range */
if (base && end < base) print(out, " (overflow!)");
if (len == 0) print(out, " (empty!)");
}
#endif /* _INCLUDE__BASE__OUTPUT_H_ */

View File

@ -12,15 +12,29 @@
* under the terms of the GNU General Public License version 2.
*/
#include <base/component.h>
#include <base/printf.h>
#include <base/log.h>
int main(int argc, char **argv)
{
/* test that unsupported commands don't crash the printf parser */
Genode::printf("%#x %s\n", 0x38, "test 1");
Genode::printf("%#lx %s\n", 0x38L, "test 2");
namespace Component {
Genode::printf("-1 = %d = %ld\n", -1, -1L);
Genode::size_t stack_size() { return 4*1024*sizeof(long); }
return 0;
void construct(Genode::Env &env);
}
void Component::construct(Genode::Env &env)
{
using namespace Genode;
log("hex range: ", Hex_range<uint16_t>(0xe00, 0x880));
log("empty hex range: ", Hex_range<uint32_t>(0xabc0000, 0));
log("hex range to limit: ", Hex_range<uint8_t>(0xf8, 8));
log("invalid hex range: ", Hex_range<uint8_t>(0xf8, 0x10));
/* test that unsupported commands don't crash the printf parser */
printf("%#x %s\n", 0x38, "test 1");
printf("%#lx %s\n", 0x38L, "test 2");
printf("-1 = %d = %ld\n", -1, -1L);
}