From 17caf99b343f6bf8e0075ed99324671116460214 Mon Sep 17 00:00:00 2001 From: BLUELOVETH Date: Thu, 27 Jul 2023 11:42:05 +0800 Subject: [PATCH] ... --- include/pocketpy/obj.h | 63 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 51 insertions(+), 12 deletions(-) diff --git a/include/pocketpy/obj.h b/include/pocketpy/obj.h index ac66d73b..5f809245 100644 --- a/include/pocketpy/obj.h +++ b/include/pocketpy/obj.h @@ -47,27 +47,66 @@ struct StarWrapper{ }; struct Bytes{ - std::vector _data; - bool _ok; + const char* _data; + int _size; - int size() const noexcept { return _data.size(); } + int size() const noexcept { return _size; } int operator[](int i) const noexcept { return (int)(uint8_t)_data[i]; } - const char* data() const noexcept { return _data.data(); } + const char* data() const noexcept { return _data; } bool operator==(const Bytes& rhs) const noexcept { - return _data == rhs._data; + return _size == rhs._size && memcmp(_data, rhs._data, _size) == 0; } + bool operator!=(const Bytes& rhs) const noexcept { - return _data != rhs._data; + return _size != rhs._size || memcmp(_data, rhs._data, _size) != 0; } - Str str() const noexcept { return Str(_data.data(), _data.size()); } + Str str() const noexcept { return Str(_data, _size); } + std::string_view sv() const noexcept { return std::string_view(_data, _size); } - Bytes() : _data(), _ok(false) {} - Bytes(std::vector&& data) : _data(std::move(data)), _ok(true) {} - Bytes(const Str& data) : _data(data.begin(), data.end()), _ok(true) {} - Bytes(std::string_view sv): _data(sv.begin(), sv.end()), _ok(true) {} - operator bool() const noexcept { return _ok; } + Bytes() : _data(nullptr), _size(0) {} + Bytes(std::vector&& v){ + char a[sizeof(std::vector)]; + new (a) std::vector(std::move(v)); + std::vector* p = reinterpret_cast*>(a); + _data = p->data(); + _size = p->size(); + } + Bytes(std::string_view sv){ + _data = new char[sv.size()]; + memcpy((void*)_data, sv.data(), sv.size()); + _size = sv.size(); + } + Bytes(const Str& str): Bytes(str.sv()) {} + operator bool() const noexcept { return _data != nullptr;} + + // copy constructor + Bytes(const Bytes& rhs): Bytes(rhs.sv()) {} + + // move constructor + Bytes(Bytes&& rhs){ + _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; + } + + // delete copy assignment + Bytes& operator=(const Bytes& rhs) = delete; + + ~Bytes(){ + delete[] _data; + } }; using Super = std::pair;