Skip to content

Commit

Permalink
Code consolidation between to_chars, ostream out and num_put
Browse files Browse the repository at this point in the history
uppercase works in ostream now too

#42
  • Loading branch information
malachib committed Jun 18, 2024
1 parent de46905 commit 802b398
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/estd/internal/ios_policy.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ struct istream_blocking_policy<TStreambuf, estd::internal::istream_flags::runtim
};


template <class TStreambuf,
template <class Streambuf,
estd::internal::stream_flags::flag_type flags>
struct ios_base_policy :
istream_blocking_policy<TStreambuf, flags & estd::internal::istream_flags::block_mask>
istream_blocking_policy<Streambuf, flags & estd::internal::istream_flags::block_mask>
{
// DEBT: Clearly we don't want this unconfigurable and always set to en_US.UTF-8
typedef locale<internal::locale_code::en_US, encodings::UTF8>
Expand Down
12 changes: 2 additions & 10 deletions src/estd/internal/iosfwd.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,9 @@ class basic_iostream;

namespace internal {

// Sorry c++03, for you this is a breaking change
#if __cpp_alias_templates
template <class TStreambuf, class TBase = basic_ios<TStreambuf> >
using basic_ostream = detail::basic_ostream<TStreambuf, TBase>;

template <class TStreambuf, class TBase = basic_ios<TStreambuf> >
using basic_istream = detail::basic_istream<TStreambuf, TBase>;

#endif

// For our non-standard << overload which takes a functor
// NOTE: Works great, it's time to put him into 'detail' or a special 'detail::tag'
// namespace
struct ostream_functor_tag {};

}
Expand Down
31 changes: 31 additions & 0 deletions src/estd/internal/ostream/integer.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,37 @@ inline detail::basic_ostream<Streambuf, Base>& write_int(detail::basic_ostream<S

template <class Streambuf, class Base, typename Int>
detail::basic_ostream<Streambuf, Base>& out_int_helper(
detail::basic_ostream<Streambuf, Base>& out, const Int& value)
{
using char_type = typename remove_cvref<Streambuf>::type::char_type;
using num_put = internal::num_put<char_type, char_type*>;

// base 8 for biggest possible string
// +1 for potential - sign
// +0 for null terminator, none required
#if FEATURE_ESTD_OSTREAM_OCTAL
constexpr unsigned N = estd::numeric_limits<Int>::template length<8>::value + 1;
#else
constexpr unsigned N = estd::numeric_limits<Int>::template length<10>::value + 1;
#endif
char_type buffer[N];

const to_chars_result result = num_put::put_integer_nofill(buffer, buffer + N, out, value);

if(result.ec == 0)
{
const unsigned sz = &buffer[N] - result.ptr;
write_filled_buffer(out, result.ptr, sz);
}
else
out.setstate(ios_base::badbit);

return out;
}


template <class Streambuf, class Base, typename Int>
detail::basic_ostream<Streambuf, Base>& out_int_helper_old(
detail::basic_ostream<Streambuf, Base>& out, Int value)
{
// DEBT: another typical enum -> traits/template conversion - a framework
Expand Down
8 changes: 4 additions & 4 deletions src/estd/port/arduino/ostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ namespace estd {

typedef estd::detail::basic_ostream<estd::arduino_ostreambuf> arduino_ostream;

namespace internal {
namespace detail {

template<class TStreambuf, class TBase>
inline basic_ostream<TStreambuf, TBase>& operator <<(
basic_ostream<TStreambuf, TBase>& os, const __FlashStringHelper* rhs)
template<class Streambuf, class Base>
inline basic_ostream<Streambuf, Base>& operator <<(
basic_ostream<Streambuf, Base>& os, const __FlashStringHelper* rhs)
{
os.rdbuf()->underlying().print(rhs);

Expand Down
2 changes: 1 addition & 1 deletion test/catch/ios-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ TEST_CASE("ios")
_cout << str;

// wrap reference to streambuf with (basically) real istream
internal::basic_istream<streambuf_type&> _cin(*rdbuf);
detail::basic_istream<streambuf_type&> _cin(*rdbuf);

// pull data out, using same rdbuf as _cout
_cin.read(localbuf, raw_str_len);
Expand Down
10 changes: 10 additions & 0 deletions test/catch/ostream-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@ TEST_CASE("ostream")
REQUIRE(s[0] == ' ');
}
}
SECTION("int (hex)")
{
out.setf(ios_base::uppercase);

out << hex << 12;

auto s = out.rdbuf()->str();

REQUIRE(s[0] == 'C');
}
SECTION("clock style")
{
// TODO: Consider time facet stuff, but mate it more to steady_clock and friends rather than
Expand Down

0 comments on commit 802b398

Please sign in to comment.