diff options
Diffstat (limited to 'src/gui_hexdump.cc')
| -rw-r--r-- | src/gui_hexdump.cc | 34 |
1 files changed, 25 insertions, 9 deletions
diff --git a/src/gui_hexdump.cc b/src/gui_hexdump.cc index e7da2d4..8f6b172 100644 --- a/src/gui_hexdump.cc +++ b/src/gui_hexdump.cc @@ -19,13 +19,18 @@ inline size_t append(char* out, size_t max, char const* in, size_t len) { return 0; } -inline size_t safe(char* out, size_t max, char const* in, size_t len) { +inline size_t safe(char* out, size_t max, char const* in, size_t len, + bool ascii) { size_t ret = 0; for (; len--; ++in) { if (*in >= ' ' && *in < '\x7f') { ret += append(out + ret, max - ret, in, 1); continue; } + if (ascii) { + ret += append(out + ret, max - ret, ".", 1); + continue; + } switch (*in) { case '\r': ret += append(out + ret, max - ret, "\xe2\x86\xb5", 3); // U+21B5 @@ -51,17 +56,17 @@ inline size_t safe(char* out, size_t max, char const* in, size_t len) { // static -void HexDump::write(AttributedText* text, uint8_t flags, std::string const& str, +void HexDump::write(AttributedText* text, uint8_t flags, char const* data, size_t start, size_t length) { - if (start >= str.size()) return; - length = std::min(length, str.size() - start); - if (length == 0) return; + if (!data || length == 0) return; AttributedText::Attribute box(0x90, 0x90, 0x90); box.set_bold(true); + bool const ascii = flags & ASCII; + size_t i = 0; - auto data = str.data() + start; + data = data + start; char tmp[80]; char tmp2[64]; int len; @@ -77,7 +82,7 @@ void HexDump::write(AttributedText* text, uint8_t flags, std::string const& str, data[i + 3] & 0xff, data[i + 4] & 0xff, data[i + 5] & 0xff, data[i + 6] & 0xff, data[i + 7] & 0xff); if (flags & CHARS) { - x += safe(x, sizeof(tmp2) - (x - tmp2), data + i, 8); + x += safe(x, sizeof(tmp2) - (x - tmp2), data + i, 8, ascii); } i += 8; text->append(tmp, len); @@ -103,7 +108,7 @@ void HexDump::write(AttributedText* text, uint8_t flags, std::string const& str, data[i + 3] & 0xff, data[i + 4] & 0xff, data[i + 5] & 0xff, data[i + 6] & 0xff, data[i + 7] & 0xff); if (flags & CHARS) { - x += safe(x, sizeof(tmp2) - (x - tmp2), data + i, 8); + x += safe(x, sizeof(tmp2) - (x - tmp2), data + i, 8, ascii); } i += 8; text->append(tmp, len); @@ -111,7 +116,8 @@ void HexDump::write(AttributedText* text, uint8_t flags, std::string const& str, } for (; i < length; ++i) { len = snprintf(tmp, sizeof(tmp), "%02x ", data[i] & 0xff); - if (flags & CHARS) x += safe(x, sizeof(tmp2) - (x - tmp2), data + i, 1); + if (flags & CHARS) x += safe(x, sizeof(tmp2) - (x - tmp2), data + i, 1, + ascii); text->append(tmp, len); } if (flags & CHARS) { @@ -134,6 +140,16 @@ void HexDump::write(AttributedText* text, uint8_t flags, std::string const& str, } // static +void HexDump::write(AttributedText* text, uint8_t flags, std::string const& str, + size_t start, size_t length) { + if (start >= str.size()) return; + length = std::min(length, str.size() - start); + write(text, flags, str.data(), start, length); +} + +// static uint8_t const HexDump::ADDRESS = 1 << 0; // static uint8_t const HexDump::CHARS = 1 << 1; +// static +uint8_t const HexDump::ASCII = 1 << 2; |
