-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
missed hex .hpp + test .cpp in prev commit
- Loading branch information
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// hex.hpp | ||
|
||
#pragma once | ||
|
||
#include <iostream> | ||
|
||
struct hex { | ||
hex(std::uint8_t x, bool w_char = false) : x_{x}, with_char_{w_char} {} | ||
|
||
std::uint8_t x_; | ||
bool with_char_; | ||
}; | ||
|
||
struct hex_view { | ||
hex_view(std::uint8_t const * lo, std::uint8_t const * hi, bool as_text) : lo_{lo}, hi_{hi}, as_text_{as_text} {} | ||
hex_view(char const * lo, char const * hi, bool as_text) | ||
: lo_{reinterpret_cast<std::uint8_t const *>(lo)}, | ||
hi_{reinterpret_cast<std::uint8_t const *>(hi)}, | ||
as_text_{as_text} {} | ||
|
||
std::uint8_t const * lo_; | ||
std::uint8_t const * hi_; | ||
bool as_text_; | ||
}; | ||
|
||
inline std::ostream & | ||
operator<< (std::ostream & os, hex const & ins) { | ||
std::uint8_t lo = ins.x_ & 0xf; | ||
std::uint8_t hi = ins.x_ >> 4; | ||
|
||
char lo_ch = (lo < 10) ? '0' + lo : 'a' + lo - 10; | ||
char hi_ch = (hi < 10) ? '0' + hi : 'a' + hi - 10; | ||
|
||
os << hi_ch << lo_ch; | ||
|
||
if (ins.with_char_) { | ||
os << "("; | ||
if (std::isprint(ins.x_)) | ||
os << static_cast<char>(ins.x_); | ||
else | ||
os << "?"; | ||
os << ")"; | ||
} | ||
|
||
return os; | ||
} | ||
|
||
inline std::ostream & | ||
operator<< (std::ostream & os, hex_view const & ins) { | ||
os << "["; | ||
std::size_t i = 0; | ||
for (std::uint8_t const * p = ins.lo_; p < ins.hi_; ++p) { | ||
if (i > 0) | ||
os << " "; | ||
os << hex(*p, ins.as_text_); | ||
++i; | ||
} | ||
os << "]"; | ||
return os; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#define CATCH_CONFIG_ENABLE_BENCHMARKING | ||
|
||
#include "compression/hex.hpp" | ||
#include <catch2/catch.hpp> | ||
#include <sstream> | ||
|
||
using namespace std; | ||
|
||
TEST_CASE("hex", "[hex]") { | ||
stringstream ss; | ||
|
||
ss << ::hex(15, false); | ||
|
||
REQUIRE(ss.str() == "0f"); | ||
} | ||
|
||
TEST_CASE("hex_view", "[hex]") { | ||
stringstream ss; | ||
|
||
array<uint8_t, 3> v = {{ 10, 20, 30 }}; | ||
|
||
ss << ::hex_view(&v[0], &v[v.size()], false); | ||
|
||
REQUIRE(ss.str() == "[0a 14 1e]"); | ||
} | ||
|
||
TEST_CASE("hex_view_2", "[hex]") { | ||
stringstream ss; | ||
|
||
array<uint8_t, 3> v = {{ 'a', 'm', 'z' }}; | ||
|
||
ss << ::hex_view(&v[0], &v[v.size()], true); | ||
|
||
REQUIRE(ss.str() == "[61(a) 6d(m) 7a(z)]"); | ||
} |