From 708dbcd201c3f4c9764b9e11196976aa88119faa Mon Sep 17 00:00:00 2001 From: xx Date: Tue, 16 Apr 2024 16:30:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7print=E6=89=93=E5=8D=B0=20?= =?UTF-8?q?=E5=8D=87=E7=BA=A7buffer=EF=BC=8C=E6=94=AF=E6=8C=81=E6=A8=A1?= =?UTF-8?q?=E6=9D=BFappend?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/ybase/buffer.h | 7 ++++--- include/yutil/print.h | 1 + src/ybase/src/buffer.cpp | 10 +--------- src/yutil/src/print.cpp | 12 ++++++++---- 4 files changed, 14 insertions(+), 16 deletions(-) diff --git a/include/ybase/buffer.h b/include/ybase/buffer.h index 6a70ab5..c4c877a 100644 --- a/include/ybase/buffer.h +++ b/include/ybase/buffer.h @@ -44,9 +44,10 @@ public: void append(const char* data, size_t length); void append(const ylib::buffer& data); void append(std::initializer_list char_list); - - void append_c(char value); - void append_uc(unsigned char value); + template + void append(const T& value) { + append((const char*)&value, sizeof(T)); + } void clear(); void resize(size_t length); diff --git a/include/yutil/print.h b/include/yutil/print.h index 1707035..0b83035 100644 --- a/include/yutil/print.h +++ b/include/yutil/print.h @@ -4,6 +4,7 @@ namespace ylib { void println(const std::string& text,ylib::ConsoleTextColor color = ylib::ConsoleTextColor::DEFAULT); + void print(const std::string& text, ylib::ConsoleTextColor color = ylib::ConsoleTextColor::DEFAULT); } diff --git a/src/ybase/src/buffer.cpp b/src/ybase/src/buffer.cpp index 5ca336e..8587850 100644 --- a/src/ybase/src/buffer.cpp +++ b/src/ybase/src/buffer.cpp @@ -74,15 +74,7 @@ void ylib::buffer::append(std::initializer_list char_list) this->append((char*)new_buf, idx); free(new_buf); } -void ylib::buffer::append_c(char value) -{ - append((const char*)&value,1); -} -void ylib::buffer::append_uc(unsigned char value) -{ - append((const char*)&value,1); -} void ylib::buffer::clear() { @@ -232,7 +224,7 @@ unsigned char &ylib::buffer::operator[](size_t index) const if (index + 1 > m_data.m_use_length) { std::string msg = "ylib::buffer::operator[] index="+std::to_string(index) + " but data length is "+std::to_string(m_data.m_use_length); - throw std::exception(msg.c_str()); + throw ylib::exception(msg.c_str()); } return m_data.data()[index]; } diff --git a/src/yutil/src/print.cpp b/src/yutil/src/print.cpp index e624ced..ea33e1c 100644 --- a/src/yutil/src/print.cpp +++ b/src/yutil/src/print.cpp @@ -10,15 +10,19 @@ const std::string LINUX_RED = "\033[31m"; const std::string LINUX_GREEN = "\033[32m"; const std::string LINUX_YELLOW = "\033[33m"; const std::string LINUX_BLUE = "\033[34m"; -//#endif +//#endif void ylib::println(const std::string& text, ylib::ConsoleTextColor color) +{ + print(text + "\r\n", color); +} +void ylib::print(const std::string& text, ylib::ConsoleTextColor color) { #ifdef _WIN32 - if(color == ylib::YELLOW) + if (color == ylib::YELLOW) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY); else SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color); - printf("%s\r\n", text.c_str()); + printf("%s", text.c_str()); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), ylib::ConsoleTextColor::DEFAULT); #else switch (color) @@ -40,7 +44,7 @@ void ylib::println(const std::string& text, ylib::ConsoleTextColor color) break; default: break; - } +} std::cout << LINUX_RESET << std::endl; #endif