refactor: bring utf8 patch for std::ostream back.

This commit is contained in:
2025-08-22 21:28:29 +08:00
parent d6034f8cb0
commit 9e994dd4f0
5 changed files with 75 additions and 1 deletions

View File

@ -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

View File

@ -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<char>(u8chr);
return os;
}
}

View File

@ -0,0 +1,17 @@
#pragma once
#include <ostream>
#include <string_view>
/**
* @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 <TT>using namespace yycc::string:stream;</TT> 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);
}

View File

@ -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

View File

@ -0,0 +1,31 @@
#include <gtest/gtest.h>
#include <yycc.hpp>
#include <yycc/string/stream.hpp>
#include <yycc/string/reinterpret.hpp>
#include <sstream>
#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");
}
}