1
0

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).
This commit is contained in:
2025-09-28 21:37:05 +08:00
parent bd5032cee7
commit 8d7eff2a15
4 changed files with 18 additions and 21 deletions

View File

@ -1,14 +1,15 @@
#include "tabulate.hpp" #include "tabulate.hpp"
#include "wcwidth.hpp" #include "wcwidth.hpp"
#include "../num/safe_op.hpp" #include "../num/safe_op.hpp"
#include "../string/reinterpret.hpp" #include "../patch/stream.hpp"
#include <stdexcept> #include <stdexcept>
#include <ranges> #include <ranges>
#define WCWIDTH ::yycc::carton::wcwidth #define WCWIDTH ::yycc::carton::wcwidth
#define REINTERPRET ::yycc::string::reinterpret
#define SAFEOP ::yycc::num::safe_op #define SAFEOP ::yycc::num::safe_op
using namespace yycc::patch::stream;
namespace yycc::carton::tabulate { namespace yycc::carton::tabulate {
#pragma region Tabulate Width #pragma region Tabulate Width
@ -94,38 +95,34 @@ namespace yycc::carton::tabulate {
std::u8string_view spaces_view(spaces); std::u8string_view spaces_view(spaces);
// Print table // Print table
// Define a convenient macro
#define CVT(data) REINTERPRET::as_ordinary_view(data)
// Show header // Show header
if (this->header_display) { if (this->header_display) {
dst << CVT(this->prefix_string); dst << this->prefix_string;
for (const auto [index, item] : std::views::enumerate(header)) { for (const auto [index, item] : std::views::enumerate(header)) {
auto diff = SAFEOP::saturating_sub(widths.get_column_width(index), item.get_text_width()); 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; dst << std::endl;
} }
// Show bar // Show bar
if (this->bar_display) { if (this->bar_display) {
dst << CVT(this->prefix_string); dst << this->prefix_string;
auto bar_width = this->bar.get_text_width(); auto bar_width = this->bar.get_text_width();
for (auto index : std::views::iota(ZERO, n)) { for (auto index : std::views::iota(ZERO, n)) {
auto diff = SAFEOP::saturating_sub(widths.get_column_width(index), bar_width); 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; dst << std::endl;
} }
// Show data // Show data
for (const auto& row : this->rows) { for (const auto& row : this->rows) {
dst << CVT(this->prefix_string); dst << this->prefix_string;
for (const auto [index, item] : std::views::enumerate(row)) { for (const auto [index, item] : std::views::enumerate(row)) {
auto diff = SAFEOP::saturating_sub(widths.get_column_width(index), item.get_text_width()); 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; dst << std::endl;
} }
// Undef macro
#undef CVT
} }
size_t Tabulate::get_column_count() const { size_t Tabulate::get_column_count() const {

View File

@ -1,13 +1,13 @@
#include "termcolor.hpp" #include "termcolor.hpp"
#include "../flag_enum.hpp" #include "../flag_enum.hpp"
#include "../string/reinterpret.hpp" #include "../patch/stream.hpp"
#include <stdexcept> #include <stdexcept>
#include <bit> #include <bit>
#define FLAG_ENUM ::yycc::flag_enum #define FLAG_ENUM ::yycc::flag_enum
#define REINTERPRET ::yycc::string::reinterpret
using namespace std::literals::string_view_literals; using namespace std::literals::string_view_literals;
using namespace yycc::patch::stream;
namespace yycc::carton::termcolor { 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) { 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) { void ecprint(const std::u8string_view& words, Color foreground, Color background, Attribute styles) {

View File

@ -6,11 +6,10 @@
* @brief This namespace add UTF8 support for \c std::ostream. * @brief This namespace add UTF8 support for \c std::ostream.
* @details * @details
* The operator overloads written in this namespace will give \c std::ostream ability to write UTF8 string and its char. * 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::patch:stream;</TT> to import this namespace. * For using this feature, please directly use <TT>using namespace yycc::patch::stream;</TT> to import this namespace.
*/ */
namespace yycc::patch::stream { 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. // 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); std::ostream& operator<<(std::ostream& os, const std::u8string_view& u8str);

View File

@ -1,12 +1,13 @@
#include "panic.hpp" #include "panic.hpp"
#include "../carton/termcolor.hpp" #include "../carton/termcolor.hpp"
#include "../string/reinterpret.hpp" #include "../patch/stream.hpp"
#include <cstdlib> #include <cstdlib>
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
#define TERMCOLOR ::yycc::carton::termcolor #define TERMCOLOR ::yycc::carton::termcolor
#define REINTERPRET ::yycc::string::reinterpret
using namespace yycc::patch::stream;
namespace yycc::rust::panic { namespace yycc::rust::panic {
@ -23,13 +24,13 @@ namespace yycc::rust::panic {
// Print error message if we support it. // Print error message if we support it.
// Setup color // Setup color
dst << REINTERPRET::as_ordinary_view(TERMCOLOR::foreground(TERMCOLOR::Color::Red)); dst << TERMCOLOR::foreground(TERMCOLOR::Color::Red);
// File name and line number message // File name and line number message
dst << "program paniked at " << std::quoted(file) << ":Ln" << line << std::endl; dst << "program paniked at " << std::quoted(file) << ":Ln" << line << std::endl;
// User custom message // User custom message
dst << "note: " << msg << std::endl; dst << "note: " << msg << std::endl;
// Restore color // Restore color
dst << REINTERPRET::as_ordinary_view(TERMCOLOR::reset());; dst << TERMCOLOR::reset();
// Make sure all messages are flushed into screen. // Make sure all messages are flushed into screen.
dst.flush(); dst.flush();