From 27f8d6be28d38e642260ba85fe22d28764d4c269 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Thu, 9 Nov 2023 15:46:49 +0800 Subject: [PATCH] some refactor --- include/pocketpy/obj.h | 63 +++++++----------------------------------- include/pocketpy/str.h | 56 +++++-------------------------------- src/obj.cpp | 52 ++++++++++++++++++++++++++++++++++ src/str.cpp | 51 ++++++++++++++++++++++++++++++++++ 4 files changed, 120 insertions(+), 102 deletions(-) diff --git a/include/pocketpy/obj.h b/include/pocketpy/obj.h index c97bcd1b..442d26d6 100644 --- a/include/pocketpy/obj.h +++ b/include/pocketpy/obj.h @@ -55,69 +55,26 @@ struct Bytes{ int operator[](int i) const noexcept { return (int)_data[i]; } const unsigned char* data() const noexcept { return _data; } - bool operator==(const Bytes& rhs) const{ - if(_size != rhs._size) return false; - for(int i=0; i<_size; i++) if(_data[i] != rhs._data[i]) return false; - return true; - } - bool operator!=(const Bytes& rhs) const{ return !(*this == rhs); } + bool operator==(const Bytes& rhs) const; + bool operator!=(const Bytes& rhs) const; Str str() const noexcept { return Str((char*)_data, _size); } std::string_view sv() const noexcept { return std::string_view((char*)_data, _size); } Bytes() : _data(nullptr), _size(0) {} Bytes(unsigned char* p, int size): _data(p), _size(size) {} - Bytes(const std::vector& v){ - _data = new unsigned char[v.size()]; - _size = v.size(); - for(int i=0; i<_size; i++) _data[i] = v[i]; - } - Bytes(std::string_view sv){ - _data = new unsigned char[sv.size()]; - _size = sv.size(); - for(int i=0; i<_size; i++) _data[i] = sv[i]; - } Bytes(const Str& str): Bytes(str.sv()) {} operator bool() const noexcept { return _data != nullptr; } - // copy constructor - Bytes(const Bytes& rhs){ - _data = new unsigned char[rhs._size]; - _size = rhs._size; - for(int i=0; i<_size; i++) _data[i] = rhs._data[i]; - } - - // move constructor - Bytes(Bytes&& rhs) noexcept { - _data = rhs._data; - _size = rhs._size; - rhs._data = nullptr; - rhs._size = 0; - } - - Bytes& operator=(Bytes&& rhs) noexcept { - delete[] _data; - _data = rhs._data; - _size = rhs._size; - rhs._data = nullptr; - rhs._size = 0; - return *this; - } - - std::pair detach() noexcept { - unsigned char* p = _data; - int size = _size; - _data = nullptr; - _size = 0; - return {p, size}; - } - - ~Bytes(){ - delete[] _data; - } - - // delete copy assignment + Bytes(const std::vector& v); + Bytes(std::string_view sv); + Bytes(const Bytes& rhs); + Bytes(Bytes&& rhs) noexcept; + Bytes& operator=(Bytes&& rhs) noexcept; Bytes& operator=(const Bytes& rhs) = delete; + std::pair detach() noexcept; + + ~Bytes(){ delete[] _data;} }; using Super = std::pair; diff --git a/include/pocketpy/str.h b/include/pocketpy/str.h index ed1e0021..cad7efc5 100644 --- a/include/pocketpy/str.h +++ b/include/pocketpy/str.h @@ -131,56 +131,14 @@ struct SStream{ SStream(){} SStream(int guess_size){ buffer.reserve(guess_size); } - Str str(){ - // after this call, the buffer is no longer valid - auto detached = buffer.detach(); - return Str(detached.first, detached.second); - } + Str str(); - SStream& operator<<(const Str& s){ - buffer.extend(s.begin(), s.end()); - return *this; - } - - SStream& operator<<(const char* s){ - buffer.extend(s, s + strlen(s)); - return *this; - } - - SStream& operator<<(i64 val){ - // str(-2**64).__len__() == 21 - buffer.reserve(buffer.size() + 24); - if(val == 0){ - buffer.push_back('0'); - return *this; - } - if(val < 0){ - buffer.push_back('-'); - val = -val; - } - char* begin = buffer.end(); - while(val){ - buffer.push_back('0' + val % 10); - val /= 10; - } - std::reverse(begin, buffer.end()); - return *this; - } - - SStream& operator<<(const std::string& s){ - buffer.extend(s.data(), s.data() + s.size()); - return *this; - } - - SStream& operator<<(std::string_view s){ - buffer.extend(s.data(), s.data() + s.size()); - return *this; - } - - SStream& operator<<(char c){ - buffer.push_back(c); - return *this; - } + SStream& operator<<(const Str& s); + SStream& operator<<(const char* s); + SStream& operator<<(i64 val); + SStream& operator<<(const std::string& s); + SStream& operator<<(std::string_view s); + SStream& operator<<(char c); template SStream& operator<<(T val){ diff --git a/src/obj.cpp b/src/obj.cpp index 6f048d2b..9b8722e9 100644 --- a/src/obj.cpp +++ b/src/obj.cpp @@ -6,4 +6,56 @@ namespace pkpy{ _attr->~NameDict(); pool128_dealloc(_attr); } + + bool Bytes::operator==(const Bytes& rhs) const{ + if(_size != rhs._size) return false; + for(int i=0; i<_size; i++) if(_data[i] != rhs._data[i]) return false; + return true; + } + bool Bytes::operator!=(const Bytes& rhs) const{ return !(*this == rhs); } + + Bytes::Bytes(const std::vector& v){ + _data = new unsigned char[v.size()]; + _size = v.size(); + for(int i=0; i<_size; i++) _data[i] = v[i]; + } + Bytes::Bytes(std::string_view sv){ + _data = new unsigned char[sv.size()]; + _size = sv.size(); + for(int i=0; i<_size; i++) _data[i] = sv[i]; + } + + // copy constructor + Bytes::Bytes(const Bytes& rhs){ + _data = new unsigned char[rhs._size]; + _size = rhs._size; + for(int i=0; i<_size; i++) _data[i] = rhs._data[i]; + } + + // move constructor + Bytes::Bytes(Bytes&& rhs) noexcept { + _data = rhs._data; + _size = rhs._size; + rhs._data = nullptr; + rhs._size = 0; + } + + // move assignment + Bytes& Bytes::operator=(Bytes&& rhs) noexcept { + delete[] _data; + _data = rhs._data; + _size = rhs._size; + rhs._data = nullptr; + rhs._size = 0; + return *this; + } + + std::pair Bytes::detach() noexcept { + unsigned char* p = _data; + int size = _size; + _data = nullptr; + _size = 0; + return {p, size}; + } + } // namespace pkpy \ No newline at end of file diff --git a/src/str.cpp b/src/str.cpp index 5e00be5b..7eb884f8 100644 --- a/src/str.cpp +++ b/src/str.cpp @@ -420,4 +420,55 @@ int utf8len(unsigned char c, bool suppress){ const std::string& str = _r_interned()[index]; return std::string_view(str); } + + Str SStream::str(){ + // after this call, the buffer is no longer valid + auto detached = buffer.detach(); + return Str(detached.first, detached.second); + } + + SStream& SStream::operator<<(const Str& s){ + buffer.extend(s.begin(), s.end()); + return *this; + } + + SStream& SStream::operator<<(const char* s){ + buffer.extend(s, s + strlen(s)); + return *this; + } + + SStream& SStream::operator<<(const std::string& s){ + buffer.extend(s.data(), s.data() + s.size()); + return *this; + } + + SStream& SStream::operator<<(std::string_view s){ + buffer.extend(s.data(), s.data() + s.size()); + return *this; + } + + SStream& SStream::operator<<(char c){ + buffer.push_back(c); + return *this; + } + + SStream& SStream::operator<<(i64 val){ + // str(-2**64).__len__() == 21 + buffer.reserve(buffer.size() + 24); + if(val == 0){ + buffer.push_back('0'); + return *this; + } + if(val < 0){ + buffer.push_back('-'); + val = -val; + } + char* begin = buffer.end(); + while(val){ + buffer.push_back('0' + val % 10); + val /= 10; + } + std::reverse(begin, buffer.end()); + return *this; + } } // namespace pkpy \ No newline at end of file