mirror of
https://github.com/ETLCPP/etl.git
synced 2026-04-30 19:09:10 +08:00
Implement << operator for std basic_ostream and etl string_view (#1040)
* Implement << operator for std basic_ostream and etl string_view * Implement << operator for std basic_ostream and etl ibasic_string. Still working through tests * Should be all tests * Fix comment
This commit is contained in:
parent
4b3987c5e1
commit
418513f3f4
@ -54,6 +54,10 @@ SOFTWARE.
|
||||
#include <string_view>
|
||||
#endif
|
||||
|
||||
#if ETL_USING_STL
|
||||
#include <ostream>
|
||||
#endif
|
||||
|
||||
#include "private/minmax_push.h"
|
||||
|
||||
//*****************************************************************************
|
||||
@ -2962,6 +2966,23 @@ namespace etl
|
||||
{
|
||||
return !(lhs < rhs);
|
||||
}
|
||||
|
||||
//***************************************************************************
|
||||
/// Operator overload to write to std basic_ostream
|
||||
///\param os Reference to the output stream.
|
||||
///\param str Reference to the string to write.
|
||||
///\return Reference to the output stream, for chaining write operations.
|
||||
///\ingroup string
|
||||
//***************************************************************************
|
||||
#if ETL_USING_STL
|
||||
template <typename T>
|
||||
std::basic_ostream<T, std::char_traits<T> > &operator<<(std::basic_ostream<T, std::char_traits<T> > &os,
|
||||
const etl::ibasic_string<T>& str)
|
||||
{
|
||||
os.write(str.data(), str.size());
|
||||
return os;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#include "private/minmax_pop.h"
|
||||
|
||||
@ -47,6 +47,10 @@ SOFTWARE.
|
||||
#include <string_view>
|
||||
#endif
|
||||
|
||||
#if ETL_USING_STL
|
||||
#include <ostream>
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace etl
|
||||
@ -972,6 +976,19 @@ void swap(etl::basic_string_view<T, etl::char_traits<T> >& lhs, etl::basic_strin
|
||||
lhs.swap(rhs);
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
/// Operator overload to write to std basic_ostream
|
||||
//*************************************************************************
|
||||
#if ETL_USING_STL
|
||||
template <typename T>
|
||||
std::basic_ostream<T, std::char_traits<T> > &operator<<(std::basic_ostream<T, std::char_traits<T> > &os,
|
||||
etl::basic_string_view<T, etl::char_traits<T> > text)
|
||||
{
|
||||
os.write(text.data(), text.size());
|
||||
return os;
|
||||
}
|
||||
#endif
|
||||
|
||||
#include "private/minmax_pop.h"
|
||||
|
||||
#endif
|
||||
|
||||
@ -5310,5 +5310,23 @@ namespace
|
||||
#endif
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
#if ETL_USING_STL
|
||||
TEST_FIXTURE(SetupFixture, test_write_string_to_std_basic_ostream)
|
||||
{
|
||||
Text text1 = STR("Hello World");
|
||||
|
||||
std::stringstream sstream;
|
||||
|
||||
sstream << text1;
|
||||
|
||||
TextSTD sstream_string = sstream.str();
|
||||
|
||||
View sstream_view(sstream_string.data(), sstream_string.size());
|
||||
|
||||
CHECK(text1 == sstream_view);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
@ -5689,5 +5689,24 @@ namespace
|
||||
#endif
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
#if ETL_USING_STL
|
||||
TEST_FIXTURE(SetupFixture, test_write_string_to_std_basic_ostream)
|
||||
{
|
||||
TextBuffer buffer1{0};
|
||||
Text text1(STR("Hello World"), buffer1.data(), buffer1.size());
|
||||
|
||||
std::stringstream sstream;
|
||||
|
||||
sstream << text1;
|
||||
|
||||
TextSTD sstream_string = sstream.str();
|
||||
|
||||
View sstream_view(sstream_string.data(), sstream_string.size());
|
||||
|
||||
CHECK(text1 == sstream_view);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
@ -5324,5 +5324,23 @@ namespace
|
||||
#endif
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
#if ETL_USING_STL
|
||||
TEST_FIXTURE(SetupFixture, test_write_string_to_std_basic_ostream)
|
||||
{
|
||||
Text text1 = STR("Hello World");
|
||||
|
||||
std::basic_stringstream<char16_t> sstream;
|
||||
|
||||
sstream << text1;
|
||||
|
||||
TextSTD sstream_string = sstream.str();
|
||||
|
||||
View sstream_view(sstream_string.data(), sstream_string.size());
|
||||
|
||||
CHECK(text1 == sstream_view);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
@ -5677,5 +5677,24 @@ namespace
|
||||
#endif
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
#if ETL_USING_STL
|
||||
TEST_FIXTURE(SetupFixture, test_write_string_to_std_basic_ostream)
|
||||
{
|
||||
TextBuffer buffer1{0};
|
||||
Text text1(STR("Hello World"), buffer1.data(), buffer1.size());
|
||||
|
||||
std::basic_stringstream<char16_t> sstream;
|
||||
|
||||
sstream << text1;
|
||||
|
||||
TextSTD sstream_string = sstream.str();
|
||||
|
||||
View sstream_view(sstream_string.data(), sstream_string.size());
|
||||
|
||||
CHECK(text1 == sstream_view);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
@ -5324,5 +5324,23 @@ namespace
|
||||
#endif
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
#if ETL_USING_STL
|
||||
TEST_FIXTURE(SetupFixture, test_write_string_to_std_basic_ostream)
|
||||
{
|
||||
Text text1 = STR("Hello World");
|
||||
|
||||
std::basic_stringstream<char32_t> sstream;
|
||||
|
||||
sstream << text1;
|
||||
|
||||
TextSTD sstream_string = sstream.str();
|
||||
|
||||
View sstream_view(sstream_string.data(), sstream_string.size());
|
||||
|
||||
CHECK(text1 == sstream_view);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
@ -5677,5 +5677,24 @@ namespace
|
||||
#endif
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
#if ETL_USING_STL
|
||||
TEST_FIXTURE(SetupFixture, test_write_string_to_std_basic_ostream)
|
||||
{
|
||||
TextBuffer buffer1{0};
|
||||
Text text1(STR("Hello World"), buffer1.data(), buffer1.size());
|
||||
|
||||
std::basic_stringstream<char32_t> sstream;
|
||||
|
||||
sstream << text1;
|
||||
|
||||
TextSTD sstream_string = sstream.str();
|
||||
|
||||
View sstream_view(sstream_string.data(), sstream_string.size());
|
||||
|
||||
CHECK(text1 == sstream_view);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
@ -5327,6 +5327,24 @@ namespace
|
||||
#endif
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
#if ETL_USING_STL
|
||||
TEST_FIXTURE(SetupFixture, test_write_string_to_std_basic_ostream)
|
||||
{
|
||||
Text text1 = STR("Hello World");
|
||||
|
||||
std::basic_stringstream<char8_t> sstream;
|
||||
|
||||
sstream << text1;
|
||||
|
||||
TextSTD sstream_string = sstream.str();
|
||||
|
||||
View sstream_view(sstream_string.data(), sstream_string.size());
|
||||
|
||||
CHECK(text1 == sstream_view);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@ -5706,6 +5706,25 @@ namespace
|
||||
#endif
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
#if ETL_USING_STL
|
||||
TEST_FIXTURE(SetupFixture, test_write_string_to_std_basic_ostream)
|
||||
{
|
||||
TextBuffer buffer1{0};
|
||||
Text text1(STR("Hello World"), buffer1.data(), buffer1.size());
|
||||
|
||||
std::basic_stringstream<char8_t> sstream;
|
||||
|
||||
sstream << text1;
|
||||
|
||||
TextSTD sstream_string = sstream.str();
|
||||
|
||||
View sstream_view(sstream_string.data(), sstream_string.size());
|
||||
|
||||
CHECK(text1 == sstream_view);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@ -1120,5 +1120,40 @@ namespace
|
||||
CHECK_TRUE((u16view == U16View{ u"Hello World", etl::strlen(u"Hello World") }));
|
||||
CHECK_TRUE((u32view == U32View{ U"Hello World", etl::strlen(U"Hello World") }));
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
#if ETL_USING_STL
|
||||
TEST(write_to_std_stream)
|
||||
{
|
||||
View view{ "Hello World" };
|
||||
WView wview{ L"Hello World" };
|
||||
U16View u16view{ u"Hello World" };
|
||||
U32View u32view{ U"Hello World" };
|
||||
|
||||
std::stringstream sstream;
|
||||
std::wstringstream wsstream;
|
||||
std::basic_stringstream<char16_t> u16sstream;
|
||||
std::basic_stringstream<char32_t> u32sstream;
|
||||
|
||||
sstream << view;
|
||||
std::string sstream_string = sstream.str();
|
||||
wsstream << wview;
|
||||
std::wstring wsstream_string = wsstream.str();
|
||||
u16sstream << u16view;
|
||||
std::u16string u16sstream_string = u16sstream.str();
|
||||
u32sstream << u32view;
|
||||
std::u32string u32sstream_string = u32sstream.str();
|
||||
|
||||
View sstream_view(sstream_string.data(), sstream_string.size());
|
||||
WView wsstream_view(wsstream_string.data(), wsstream_string.size());
|
||||
U16View u16sstream_view(u16sstream_string.data(), u16sstream_string.size());
|
||||
U32View u32sstream_view(u32sstream_string.data(), u32sstream_string.size());
|
||||
|
||||
CHECK_TRUE(view == sstream_view);
|
||||
CHECK_TRUE(wview == wsstream_view);
|
||||
CHECK_TRUE(u16view == u16sstream_view);
|
||||
CHECK_TRUE(u32view == u32sstream_view);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
@ -5325,5 +5325,23 @@ namespace
|
||||
#endif
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
#if ETL_USING_STL
|
||||
TEST_FIXTURE(SetupFixture, test_write_string_to_std_basic_ostream)
|
||||
{
|
||||
Text text1 = STR("Hello World");
|
||||
|
||||
std::wstringstream sstream;
|
||||
|
||||
sstream << text1;
|
||||
|
||||
TextSTD sstream_string = sstream.str();
|
||||
|
||||
View sstream_view(sstream_string.data(), sstream_string.size());
|
||||
|
||||
CHECK(text1 == sstream_view);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
@ -5680,5 +5680,24 @@ namespace
|
||||
#endif
|
||||
CHECK_EQUAL(text.max_size(), text.size());
|
||||
}
|
||||
|
||||
//*************************************************************************
|
||||
#if ETL_USING_STL
|
||||
TEST_FIXTURE(SetupFixture, test_write_string_to_std_basic_ostream)
|
||||
{
|
||||
TextBuffer buffer1{0};
|
||||
Text text1(STR("Hello World"), buffer1.data(), buffer1.size());
|
||||
|
||||
std::wstringstream sstream;
|
||||
|
||||
sstream << text1;
|
||||
|
||||
TextSTD sstream_string = sstream.str();
|
||||
|
||||
View sstream_view(sstream_string.data(), sstream_string.size());
|
||||
|
||||
CHECK(text1 == sstream_view);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user