From 2ab2614ab41fee05111243b16eaf6ec925881493 Mon Sep 17 00:00:00 2001 From: Andrey Date: Wed, 13 Apr 2022 17:59:24 +0300 Subject: [PATCH] PICout optimization --- CMakeLists.txt | 2 +- libs/main/core/picout.cpp | 71 ++++++++++++++++++----------- libs/main/core/picout.h | 11 ++++- libs/main/core/piinit.cpp | 7 ++- libs/main/core/pistring.cpp | 4 +- main.cpp | 91 +++++++++++++++++++++++++++++++++++-- 6 files changed, 149 insertions(+), 37 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 47d5f275..c72a778b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -451,7 +451,7 @@ if (NOT CROSSTOOLS) #target_link_libraries(pip_plugin pip) add_executable(pip_test "main.cpp") - target_link_libraries(pip_test pip pip_cloud pip_lua) + target_link_libraries(pip_test pip) endif() else() diff --git a/libs/main/core/picout.cpp b/libs/main/core/picout.cpp index 560c44e1..40a19ae6 100644 --- a/libs/main/core/picout.cpp +++ b/libs/main/core/picout.cpp @@ -155,11 +155,7 @@ WORD PICout::__Private__::dattr = 0; DWORD PICout::__Private__::smode = 0; #endif -PICout::PICout(PIFlags controls): fo_(true), cc_(false), fc_(false), act_(true), cnb_(10), co_(controls) { - init(); -} - -PICout::PICout(PICoutControl control): fo_(true), cc_(false), fc_(false), act_(true), cnb_(10), co_(control) { +PICout::PICout(int controls): fo_(true), cc_(false), fc_(false), act_(true), cnb_(10), co_(controls) { init(); } @@ -323,7 +319,7 @@ PICout PICout::operator <<(const PIFlags & v) } -#define PICOUTTOTARGET(v) { \ +#define PICOUTTOTARGET(v) { \ if (buffer_) {\ (*buffer_) << (v);\ } else {\ @@ -331,15 +327,14 @@ PICout PICout::operator <<(const PIFlags & v) if (PICout::isOutputDeviceActive(PICout::Buffer)) PICout::__string__() << (v);\ }\ } -#define PICOUTTOTARGETS(v) { \ - if (buffer_) {\ - (*buffer_) << (v);\ - } else {\ - if (PICout::isOutputDeviceActive(PICout::StdOut)) std::cout << (v).dataConsole();\ - if (PICout::isOutputDeviceActive(PICout::Buffer)) PICout::__string__() << (v);\ - }\ + +#define PINUMERICCOUT(v) { \ + if (!act_) return *this; \ + space(); \ + if (cnb_ == 10) PICOUTTOTARGET(v) \ + else writePIString(PIString::fromNumber(v, cnb_)); \ + return *this; \ } -#define PINUMERICCOUT if (cnb_ == 10) PICOUTTOTARGET(v) else PICOUTTOTARGETS(PIString::fromNumber(v, cnb_)) PICout PICout::operator <<(const char * v) {if (!act_) return *this; if (v[0] == '\0') return *this; space(); quote(); PICOUTTOTARGET(v) quote(); return *this;} @@ -348,29 +343,29 @@ PICout PICout::operator <<(const bool v) {if (!act_) return *this; space(); if ( PICout PICout::operator <<(const char v) {if (!act_) return *this; space(); PICOUTTOTARGET(v) return *this;} -PICout PICout::operator <<(const uchar v) {if (!act_) return *this; space(); if (cnb_ == 10) PICOUTTOTARGET(ushort(v)) else PICOUTTOTARGETS(PIString::fromNumber(v, cnb_)) return *this;} +PICout PICout::operator <<(const uchar v) {PINUMERICCOUT(ushort(v))} -PICout PICout::operator <<(const short int v) {if (!act_) return *this; space(); PINUMERICCOUT return *this;} +PICout PICout::operator <<(const short int v) {PINUMERICCOUT(v)} -PICout PICout::operator <<(const ushort v) {if (!act_) return *this; space(); PINUMERICCOUT return *this;} +PICout PICout::operator <<(const ushort v) {PINUMERICCOUT(v)} -PICout PICout::operator <<(const int v) {if (!act_) return *this; space(); PINUMERICCOUT return *this;} +PICout PICout::operator <<(const int v) {PINUMERICCOUT(v)} -PICout PICout::operator <<(const uint v) {if (!act_) return *this; space(); PINUMERICCOUT return *this;} +PICout PICout::operator <<(const uint v) {PINUMERICCOUT(v)} -PICout PICout::operator <<(const long v) {if (!act_) return *this; space(); PINUMERICCOUT return *this;} +PICout PICout::operator <<(const long v) {PINUMERICCOUT(v)} -PICout PICout::operator <<(const ulong v) {if (!act_) return *this; space(); PINUMERICCOUT return *this;} +PICout PICout::operator <<(const ulong v) {PINUMERICCOUT(v)} -PICout PICout::operator <<(const llong v) {if (!act_) return *this; space(); PINUMERICCOUT return *this;} +PICout PICout::operator <<(const llong v) {PINUMERICCOUT(v)} -PICout PICout::operator <<(const ullong v) {if (!act_) return *this; space(); PINUMERICCOUT return *this;} +PICout PICout::operator <<(const ullong v) {PINUMERICCOUT(v)} PICout PICout::operator <<(const float v) {if (!act_) return *this; space(); PICOUTTOTARGET(v) return *this;} PICout PICout::operator <<(const double v) {if (!act_) return *this; space(); PICOUTTOTARGET(v) return *this;} -PICout PICout::operator <<(const void * v) {if (!act_) return *this; space(); PICOUTTOTARGET("0x") PICOUTTOTARGETS(PIString::fromNumber(ullong(v), 16)) return *this;} +PICout PICout::operator <<(const void * v) {if (!act_) return *this; space(); PICOUTTOTARGET("0x") writePIString(PIString::fromNumber(ullong(v), 16)); return *this;} PICout PICout::operator <<(const PIObject * v) { if (!act_) return *this; @@ -379,9 +374,9 @@ PICout PICout::operator <<(const PIObject * v) { else { PICOUTTOTARGET(v->className()) PICOUTTOTARGET("*(0x") - PICOUTTOTARGETS(PIString::fromNumber(ullong(v), 16)) + writePIString(PIString::fromNumber(ullong(v), 16)); PICOUTTOTARGET(", \"") - PICOUTTOTARGET(v->name()) + writePIString(v->name()); PICOUTTOTARGET("\")") } return *this; @@ -526,6 +521,30 @@ PICout & PICout::newLine() { } +PICout & PICout::write(const char * str, int len) { + if (buffer_) { + buffer_->append(PIString(str, len)); + } else { + if (PICout::isOutputDeviceActive(PICout::StdOut)) std::cout.write(str, len); + if (PICout::isOutputDeviceActive(PICout::Buffer)) PICout::__string__().append(PIString(str, len)); + } + return *this; +} + + +PICout & PICout::writePIString(const PIString & s) { + if (buffer_) { + buffer_->append(s); + } else { + if (PICout::isOutputDeviceActive(PICout::StdOut)) { + for (const PIChar & c: s) std::wcout.put(c.toWChar()); + } + if (PICout::isOutputDeviceActive(PICout::Buffer)) PICout::__string__().append(s); + } + return *this; +} + + void PICout::init() { #ifdef WINDOWS if (__Private__::hOut == 0) { diff --git a/libs/main/core/picout.h b/libs/main/core/picout.h index 3eb0e3df..a366a297 100644 --- a/libs/main/core/picout.h +++ b/libs/main/core/picout.h @@ -130,8 +130,7 @@ public: //! \~english Default constructor with default features (AddSpaces and AddNewLine) //! \~russian Конструктор по умолчанию (AddSpaces и AddNewLine) - PICout(PIFlags controls = PICoutManipulators::DefaultControls); - PICout(PICoutManipulators::PICoutControl control = PICoutManipulators::DefaultControls); + PICout(int controls = PICoutManipulators::DefaultControls); //! \~english Construct with default features (AddSpaces and AddNewLine), but if \"active\" is false does nothing //! \~russian Конструктор по умолчанию (AddSpaces и AddNewLine), но если не \"active\" то будет неактивным @@ -284,6 +283,14 @@ public: //! \~russian Условно добавляет новую строку PICout & newLine(); + //! \~english Write raw data + //! \~russian Пишет сырые символы + PICout & write(const char * str, int len); + + //! \~english Write raw \a PIString + //! \~russian Пишет сырой \a PIString + PICout & writePIString(const PIString & s); + //! \~english Set output device to \a PICout::Buffer and if "clear" clear it //! \~russian Устанавливает устройство вывода на \a PICout::Buffer и если "clear" то очищает его static bool setBufferActive(bool on, bool clear = false); diff --git a/libs/main/core/piinit.cpp b/libs/main/core/piinit.cpp index 78417830..37f6d1b3 100644 --- a/libs/main/core/piinit.cpp +++ b/libs/main/core/piinit.cpp @@ -31,6 +31,7 @@ #ifdef ESP_PLATFORM # include "esp_system.h" #endif +#include #ifdef WINDOWS # include extern FILETIME __pi_ftjan1970; @@ -208,8 +209,12 @@ PIInit::PIInit() { #endif char cbuff[1024]; memset(cbuff, 0, 1024); - if (gethostname(cbuff, 1023) == 0) + if (gethostname(cbuff, 1023) == 0) { sinfo->hostname = cbuff; + } +// std::ios_base::sync_with_stdio(false); +// std::locale utf8( std::locale(), new std::codecvt_utf8 ); +// std::wcout.imbue(utf8); #ifdef WINDOWS SYSTEM_INFO sysinfo; GetSystemInfo(&sysinfo); diff --git a/libs/main/core/pistring.cpp b/libs/main/core/pistring.cpp index 8850deb5..f0449bdb 100644 --- a/libs/main/core/pistring.cpp +++ b/libs/main/core/pistring.cpp @@ -1729,9 +1729,7 @@ PIString versionNormalize(const PIString & v) { PICout operator <<(PICout s, const PIString & v) { s.space(); s.quote(); - s.setControl(0, true); - s << v.data(); - s.restoreControl(); + s.writePIString(v); s.quote(); return s; } diff --git a/main.cpp b/main.cpp index c5103f0a..d6f0ed6a 100644 --- a/main.cpp +++ b/main.cpp @@ -1,9 +1,92 @@ #include "pip.h" -#include + + +class ROString { +public: + inline ROString() : len(0), str(nullptr) {} + inline ROString(const char * string) { + str = string; + len = strlen(string); + } + inline ROString(const char * data, size_t size) { + str = data; + len = size; + } +// inline ROString(const char string[]) { +// str = string; +// len = sizeof(string); +// } + + inline const char & operator[](size_t pos) const {return str[pos];} + inline const char * data() const {return str;} + + inline bool isEmpty() const {return len == 0;} + inline bool isNotEmpty() const {return len > 0;} + inline size_t length() const {return len;} + inline size_t size() const {return len;} + inline ssize_t size_s() const {return len;} + + //! \~\brief + //! \~english Assign operator. + //! \~russian Оператор присваивания. + inline ROString & operator =(const ROString & s) { + if (this == &s) return *this; + len = s.len; + str = s.str; + return *this; + } + + //! \~\brief + //! \~english Assign move operator. + //! \~russian Оператор перемещающего присваивания. + inline ROString & operator =(ROString && s) { + swap(s); + return *this; + } + + inline void swap(ROString& v) { + piSwap(str, v.str); + piSwap(len, v.len); + } + +private: + size_t len; + const char * str; +}; + +PICout operator <<(PICout s, const ROString & v) { + s.space(); + s.quote(); + s.write(v.data(), v.size()); + s.quote(); + return s; +} int main(int argc, char * argv[]) { - PIVector v{5, 7, 4, 2, 8, 6, 1, 9, 0, 3}; - v.sort([](const int & a, const int & b){return a > b;}); - piCout << v; // 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 + const char * dd = "12345"; + char text[]{ "hello" }; + ROString s("test"); + piCout << s; + ROString s2; + piCout << s2; + s2 = s; + piCout << s2; + s2 = text; + s = s2; + piCout << s << s2; +// s2 = s; + text[1] = '0'; + piCout << text; + piCout << s << s2 << ROString(text, 3); +// piCout << s << s2; +// PIString ss(s.data(), s.length()); +// piCout << ss; + PICout(PICoutManipulators::DefaultControls | PICoutManipulators::AddQuotes) << PIString(dd); + + piCout << PIString::fromUTF8("test"); + piCout << PIString::fromUTF8("бюд\n"); + piCout.writePIString(PIString::fromUTF8("test\n")); + piCout.writePIString(PIString::fromUTF8("бюд\n")); + piCout << "бюд\n"; return 0; }