Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions doc/modules/ROOT/pages/stream.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ The following flags from `<ios>` are supported for both streaming directions:
- `std::oct` - Octal Numbers
- `std::dec` - Decimal Numbers
- `std::hex` - Hexadecimal Numbers
- `std::uppercase` - Upper Case Formatting (e.g. 0XFFFF)
- `std::nouppercase` - Lower Case Formatting (e.g. 0xffff)
- `std::uppercase` - Upper Case Formatting (e.g. FFFF)
- `std::nouppercase` - Lower Case Formatting (e.g. ffff)
- `std::showbase` - Adds a leading base for hex or oct numbers (e.g. 0xffff)
- `std::noshowbase` - Removes the leading base for hex or oct numbers (e.g. ffff)

See the xref:examples.adoc#examples_io[IO streaming example] for usage demonstrations.
17 changes: 10 additions & 7 deletions include/boost/int128/iostream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,17 @@ auto operator<<(std::basic_ostream<charT, traits>& os, const LibIntegerType& v)

auto first {detail::mini_to_chars(buffer, v, base, uppercase)};

if (base == 8)
if (flags & std::ios_base::showbase)
{
*--first = '0';
}
else if (base == 16)
{
*--first = uppercase ? 'X' : 'x';
*--first = '0';
if (base == 8)
{
*--first = '0';
}
else if (base == 16)
{
*--first = uppercase ? 'X' : 'x';
*--first = '0';
}
}

BOOST_INT128_IF_CONSTEXPR (!std::is_same<charT, char>::value)
Expand Down
21 changes: 18 additions & 3 deletions test/test_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,38 @@ void test_ostream()
std::stringstream hex_out;
hex_out.flags(std::ios_base::hex);
hex_out << hex_val;
BOOST_TEST_CSTR_EQ(hex_out.str().c_str(), "0xff");
BOOST_TEST_CSTR_EQ(hex_out.str().c_str(), "ff");

// 32-bit windows does not set the flags correctly in CI
#ifndef _M_IX86

std::stringstream hex_out_base;
hex_out_base.flags(std::ios_base::hex | std::ios_base::showbase);
hex_out_base << hex_val;
BOOST_TEST_CSTR_EQ(hex_out_base.str().c_str(), "0xff");

std::stringstream hex_out_upper;
hex_out_upper.flags(std::ios_base::hex | std::ios_base::uppercase);
hex_out_upper << hex_val;
BOOST_TEST_CSTR_EQ(hex_out_upper.str().c_str(), "0XFF");
BOOST_TEST_CSTR_EQ(hex_out_upper.str().c_str(), "FF");

std::stringstream hex_out_upper_base;
hex_out_upper_base.flags(std::ios_base::hex | std::ios_base::uppercase | std::ios_base::showbase);
hex_out_upper_base << hex_val;
BOOST_TEST_CSTR_EQ(hex_out_upper_base.str().c_str(), "0XFF");

#endif

T octal_val {04};
std::stringstream octal_out;
octal_out.flags(std::ios_base::oct);
octal_out << octal_val;
BOOST_TEST_CSTR_EQ(octal_out.str().c_str(), "04");
BOOST_TEST_CSTR_EQ(octal_out.str().c_str(), "4");

std::stringstream octal_out_upper;
octal_out_upper.flags(std::ios_base::hex | std::ios_base::showbase);
octal_out_upper << octal_val;
BOOST_TEST_CSTR_EQ(octal_out.str().c_str(), "4");

BOOST_INT128_IF_CONSTEXPR (std::is_same<T, boost::int128::uint128_t>::value)
{
Expand Down
Loading