From 8d7eff2a15a73c6c5a74c69ad4589b93d3085428 Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Sun, 28 Sep 2025 21:37:05 +0800 Subject: [PATCH] fix: fix all old usage of UTF8 in std::ostream. - use patch stream instead of all old use of UTF8 in std::ostream (reinterpret way). --- src/yycc/carton/tabulate.cpp | 21 +++++++++------------ src/yycc/carton/termcolor.cpp | 6 +++--- src/yycc/patch/stream.hpp | 3 +-- src/yycc/rust/panic.cpp | 9 +++++---- 4 files changed, 18 insertions(+), 21 deletions(-) diff --git a/src/yycc/carton/tabulate.cpp b/src/yycc/carton/tabulate.cpp index 238b2cf..d894607 100644 --- a/src/yycc/carton/tabulate.cpp +++ b/src/yycc/carton/tabulate.cpp @@ -1,14 +1,15 @@ #include "tabulate.hpp" #include "wcwidth.hpp" #include "../num/safe_op.hpp" -#include "../string/reinterpret.hpp" +#include "../patch/stream.hpp" #include #include #define WCWIDTH ::yycc::carton::wcwidth -#define REINTERPRET ::yycc::string::reinterpret #define SAFEOP ::yycc::num::safe_op +using namespace yycc::patch::stream; + namespace yycc::carton::tabulate { #pragma region Tabulate Width @@ -94,38 +95,34 @@ namespace yycc::carton::tabulate { std::u8string_view spaces_view(spaces); // Print table - // Define a convenient macro -#define CVT(data) REINTERPRET::as_ordinary_view(data) // Show header if (this->header_display) { - dst << CVT(this->prefix_string); + dst << this->prefix_string; for (const auto [index, item] : std::views::enumerate(header)) { auto diff = SAFEOP::saturating_sub(widths.get_column_width(index), item.get_text_width()); - dst << CVT(item.get_text()) << CVT(spaces_view.substr(0, diff)) << " "; + dst << item.get_text() << spaces_view.substr(0, diff) << " "; } dst << std::endl; } // Show bar if (this->bar_display) { - dst << CVT(this->prefix_string); + dst << this->prefix_string; auto bar_width = this->bar.get_text_width(); for (auto index : std::views::iota(ZERO, n)) { auto diff = SAFEOP::saturating_sub(widths.get_column_width(index), bar_width); - dst << CVT(this->bar.get_text()) << CVT(spaces_view.substr(0, diff)) << " "; + dst << this->bar.get_text() << spaces_view.substr(0, diff) << " "; } dst << std::endl; } // Show data for (const auto& row : this->rows) { - dst << CVT(this->prefix_string); + dst << this->prefix_string; for (const auto [index, item] : std::views::enumerate(row)) { auto diff = SAFEOP::saturating_sub(widths.get_column_width(index), item.get_text_width()); - dst << CVT(item.get_text()) << CVT(spaces_view.substr(0, diff)) << " "; + dst << item.get_text() << spaces_view.substr(0, diff) << " "; } dst << std::endl; } - // Undef macro -#undef CVT } size_t Tabulate::get_column_count() const { diff --git a/src/yycc/carton/termcolor.cpp b/src/yycc/carton/termcolor.cpp index b90364f..d39430b 100644 --- a/src/yycc/carton/termcolor.cpp +++ b/src/yycc/carton/termcolor.cpp @@ -1,13 +1,13 @@ #include "termcolor.hpp" #include "../flag_enum.hpp" -#include "../string/reinterpret.hpp" +#include "../patch/stream.hpp" #include #include #define FLAG_ENUM ::yycc::flag_enum -#define REINTERPRET ::yycc::string::reinterpret using namespace std::literals::string_view_literals; +using namespace yycc::patch::stream; namespace yycc::carton::termcolor { @@ -213,7 +213,7 @@ namespace yycc::carton::termcolor { } void cprint(const std::u8string_view& words, Color foreground, Color background, Attribute styles, std::ostream& dst) { - dst << REINTERPRET::as_ordinary_view(colored(words, foreground, background, styles)); + dst << colored(words, foreground, background, styles); } void ecprint(const std::u8string_view& words, Color foreground, Color background, Attribute styles) { diff --git a/src/yycc/patch/stream.hpp b/src/yycc/patch/stream.hpp index 33fbe38..5faf09e 100644 --- a/src/yycc/patch/stream.hpp +++ b/src/yycc/patch/stream.hpp @@ -6,11 +6,10 @@ * @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::patch:stream; to import this namespace. + * For using this feature, please directly use using namespace yycc::patch::stream; to import this namespace. */ namespace yycc::patch::stream { - // TODO: fix all REINTERPRET::as_ordinary_view polyfill for std::ostream. // TODO: replace all old way (C-style output) with this new way if possible. std::ostream& operator<<(std::ostream& os, const std::u8string_view& u8str); diff --git a/src/yycc/rust/panic.cpp b/src/yycc/rust/panic.cpp index 3a93a80..dbe158d 100644 --- a/src/yycc/rust/panic.cpp +++ b/src/yycc/rust/panic.cpp @@ -1,12 +1,13 @@ #include "panic.hpp" #include "../carton/termcolor.hpp" -#include "../string/reinterpret.hpp" +#include "../patch/stream.hpp" #include #include #include #define TERMCOLOR ::yycc::carton::termcolor -#define REINTERPRET ::yycc::string::reinterpret + +using namespace yycc::patch::stream; namespace yycc::rust::panic { @@ -23,13 +24,13 @@ namespace yycc::rust::panic { // Print error message if we support it. // Setup color - dst << REINTERPRET::as_ordinary_view(TERMCOLOR::foreground(TERMCOLOR::Color::Red)); + dst << TERMCOLOR::foreground(TERMCOLOR::Color::Red); // File name and line number message dst << "program paniked at " << std::quoted(file) << ":Ln" << line << std::endl; // User custom message dst << "note: " << msg << std::endl; // Restore color - dst << REINTERPRET::as_ordinary_view(TERMCOLOR::reset());; + dst << TERMCOLOR::reset(); // Make sure all messages are flushed into screen. dst.flush();