Skip to content

Commit

Permalink
fixup: Fix support for smaller integer types like char
Browse files Browse the repository at this point in the history
  • Loading branch information
rousskov committed Dec 6, 2023
1 parent efd5e4b commit d7b52b1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/base/IoManip.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,11 @@ operator <<(std::ostream &os, const AsHex<Integer> number)
os.fill('0');
}

os << std::hex << number.io_manip;
// When Integer is smaller than int, the unary plus converts the stored
// value into an equivalent integer because C++ "arithmetic operators do not
// accept types smaller than int as arguments, and integral promotions are
// automatically applied". For larger integer types, plus is a no-op.
os << std::hex << +number.io_manip;

os.fill(oldFill);
os.flags(oldFlags);
Expand Down
14 changes: 14 additions & 0 deletions src/tests/testIoManip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ TestIoManip::testAsHex()
// negative values as negative hex integers. AsHex has the same limitation.
CPPUNIT_ASSERT_EQUAL(std::string("80000000"), toStdString(asHex(std::numeric_limits<int32_t>::min())));

// integer and integer-like types that std::ostream formats specially by default
CPPUNIT_ASSERT_EQUAL(std::string("0"), toStdString(asHex(false)));
CPPUNIT_ASSERT_EQUAL(std::string("1"), toStdString(asHex(true)));
CPPUNIT_ASSERT_EQUAL(std::string("5a"), toStdString(asHex('Z')));
CPPUNIT_ASSERT_EQUAL(std::string("77"), toStdString(asHex(int8_t(0x77))));
CPPUNIT_ASSERT_EQUAL(std::string("ff"), toStdString(asHex(uint8_t(0xFF))));

// other interesting integer-like types we may want to print
enum { enumValue = 0xABCD };
CPPUNIT_ASSERT_EQUAL(std::string("abcd"), toStdString(asHex(enumValue)));
struct { uint8_t bitField:2; } s;
s.bitField = 3; // TODO: Convert to default initializer after switching to C++20.
CPPUNIT_ASSERT_EQUAL(std::string("3"), toStdString(asHex(s.bitField)));

// padding with zeros works
CPPUNIT_ASSERT_EQUAL(std::string("1"), toStdString(asHex(1).minDigits(1)));
CPPUNIT_ASSERT_EQUAL(std::string("01"), toStdString(asHex(1).minDigits(2)));
Expand Down

0 comments on commit d7b52b1

Please sign in to comment.