Skip to content

Commit

Permalink
operator<<
Browse files Browse the repository at this point in the history
  • Loading branch information
serges147 committed Oct 21, 2024
1 parent c47a8f9 commit 040182a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
30 changes: 30 additions & 0 deletions cetlvast/suites/unittest/test_pf17_string_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <array>
#include <cassert>
#include <cstdlib>
#include <iomanip>
#include <sstream>
#include <string>
#include <cetl/cetl.hpp>

Expand Down Expand Up @@ -54,6 +56,7 @@ class TestStringView<wchar_t> : public testing::Test
std::array<wchar_t, 256> wstr{};
const auto result = std::mbstowcs(wstr.data(), cstr, wstr.size());
assert(result == std::strlen(cstr));
(void) result;
return {wstr.data()};
}

Expand Down Expand Up @@ -408,3 +411,30 @@ TYPED_TEST(TestStringView, CopyOutOfBounds)
GTEST_SKIP() << "Not applicable when exceptions are disabled.";
#endif
}

TYPED_TEST(TestStringView, stream_operator)
{
using SV = basic_string_view<TypeParam>;

const auto test_str = TestFixture::toStr("Test");
{
std::basic_stringstream<TypeParam> ss;
ss << SV{test_str} << SV{test_str};
EXPECT_THAT(ss.str(), TestFixture::toStr("TestTest"));
}
{
std::basic_stringstream<TypeParam> ss;
ss << std::setw(9) << std::setfill(TestFixture::toChar('-')) << std::left << SV{test_str};
EXPECT_THAT(ss.str(), TestFixture::toStr("Test-----"));
}
{
std::basic_stringstream<TypeParam> ss;
ss << std::setw(9) << std::setfill(TestFixture::toChar('-')) << std::right << SV{test_str};
EXPECT_THAT(ss.str(), TestFixture::toStr("-----Test"));
}
{
std::basic_stringstream<TypeParam> ss;
ss << std::setw(2) << SV{test_str};
EXPECT_THAT(ss.str(), test_str);
}
}
48 changes: 47 additions & 1 deletion include/cetl/pf17/string_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <cstddef>
#include <cstring>
#include <exception> // We need this even if exceptions are disabled for std::terminate.
#include <ios>
#include <stdexcept>
#include <string>

Expand Down Expand Up @@ -335,6 +336,50 @@ class basic_string_view
return result != data_ + size_ ? static_cast<size_type>(result - data_) : npos;
}

/// Performs stream output on string view.
///
friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
const basic_string_view& sv)
{
const auto sv_length = static_cast<std::streamsize>(sv.length());

typename std::basic_ostream<CharT, Traits>::sentry s{os};
if (s)
{
const std::streamsize width = os.width();

if (sv_length < width)
{
auto pad = [&os, fill = os.fill()](std::streamsize padding_width) {
while (padding_width--)
{
os.put(fill);
}
};

const std::streamsize padding_len = width - sv_length;
const bool left = os.flags() & std::basic_ios<CharT, Traits>::left;
const bool right = os.flags() & std::basic_ios<CharT, Traits>::right;

if (right)
{
pad(padding_len);
}
os.write(sv.data(), sv_length);
if (left)
{
pad(padding_len);
}
}
else
{
os.write(sv.data(), sv_length);
}
}
os.width(0);
return os;
}

private:
const CharT* data_;
size_type size_;
Expand All @@ -343,7 +388,8 @@ class basic_string_view

// non-member functions

// swap
/// Exchanges the given string views.
///
template <typename CharT>
void swap(basic_string_view<CharT>& lhs, basic_string_view<CharT>& rhs) noexcept
{
Expand Down

0 comments on commit 040182a

Please sign in to comment.