diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 384a802..95fd26e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -13,6 +13,7 @@ PRIVATE # Sources yycc/string/reinterpret.cpp yycc/string/op.cpp + yycc/string/stream.cpp yycc/patch/fopen.cpp yycc/rust/panic.cpp yycc/rust/env.cpp @@ -45,6 +46,7 @@ FILES yycc/flag_enum.hpp yycc/string/reinterpret.hpp yycc/string/op.hpp + yycc/string/stream.hpp yycc/patch/ptr_pad.hpp yycc/patch/fopen.hpp yycc/num/parse.hpp diff --git a/src/yycc/string/stream.cpp b/src/yycc/string/stream.cpp new file mode 100644 index 0000000..1090842 --- /dev/null +++ b/src/yycc/string/stream.cpp @@ -0,0 +1,23 @@ +#include "stream.hpp" +#include "reinterpret.hpp" + +#define REINTERPRET ::yycc::string::reinterpret + +namespace yycc::string::stream { + + std::ostream& operator<<(std::ostream& os, const std::u8string_view& u8str) { + os << REINTERPRET::as_ordinary_view(u8str); + return os; + } + + std::ostream& operator<<(std::ostream& os, const char8_t* u8str) { + os << REINTERPRET::as_ordinary(u8str); + return os; + } + + std::ostream& operator<<(std::ostream& os, char8_t u8chr) { + os << static_cast(u8chr); + return os; + } + +} diff --git a/src/yycc/string/stream.hpp b/src/yycc/string/stream.hpp new file mode 100644 index 0000000..4a5b4df --- /dev/null +++ b/src/yycc/string/stream.hpp @@ -0,0 +1,17 @@ +#pragma once +#include +#include + +/** + * @brief This namespace add UTF8 support for \c std::ostream. + * @details + * The operator overloads written in this namespace will give \c std::ostream ability to write UTF8 string and its char. + * For using this feature, please directly use using namespace yycc::string:stream; to import this namespace. + */ +namespace yycc::string::stream { + + std::ostream& operator<<(std::ostream& os, const std::u8string_view& u8str); + std::ostream& operator<<(std::ostream& os, const char8_t* u8str); + std::ostream& operator<<(std::ostream& os, char8_t u8chr); + +} diff --git a/testbench/CMakeLists.txt b/testbench/CMakeLists.txt index bad5efb..b6a649d 100644 --- a/testbench/CMakeLists.txt +++ b/testbench/CMakeLists.txt @@ -19,8 +19,9 @@ PRIVATE yycc/patch/ptr_pad.cpp yycc/patch/fopen.cpp yycc/rust/env.cpp - yycc/string/op.cpp yycc/string/reinterpret.cpp + yycc/string/op.cpp + yycc/string/stream.cpp yycc/num/parse.cpp yycc/num/stringify.cpp yycc/num/op.cpp diff --git a/testbench/yycc/string/stream.cpp b/testbench/yycc/string/stream.cpp new file mode 100644 index 0000000..001264a --- /dev/null +++ b/testbench/yycc/string/stream.cpp @@ -0,0 +1,31 @@ +#include +#include +#include +#include +#include + +#define REINTERPRET ::yycc::string::reinterpret +using namespace std::literals::string_view_literals; +using namespace ::yycc::string::stream; + +namespace yycctest::string::stream { + + TEST(StringStream, StringView) { + std::stringstream ss; + ss << u8"hello"sv; + EXPECT_EQ(REINTERPRET::as_utf8_view(ss.view()), u8"hello"sv); + } + + TEST(StringStream, CStrPtr) { + std::stringstream ss; + ss << u8"hello"; + EXPECT_EQ(REINTERPRET::as_utf8_view(ss.view()), u8"hello"); + } + + TEST(StringStream, Character) { + std::stringstream ss; + ss << u8'y'; + EXPECT_EQ(REINTERPRET::as_utf8_view(ss.view()), u8"y"); + } + +}