some refactor

This commit is contained in:
blueloveTH 2023-11-09 15:46:49 +08:00
parent 641a1711cf
commit 27f8d6be28
4 changed files with 120 additions and 102 deletions

View File

@ -55,69 +55,26 @@ struct Bytes{
int operator[](int i) const noexcept { return (int)_data[i]; } int operator[](int i) const noexcept { return (int)_data[i]; }
const unsigned char* data() const noexcept { return _data; } const unsigned char* data() const noexcept { return _data; }
bool operator==(const Bytes& rhs) const{ bool operator==(const Bytes& rhs) const;
if(_size != rhs._size) return false; bool operator!=(const Bytes& rhs) const;
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); }
Str str() const noexcept { return Str((char*)_data, _size); } Str str() const noexcept { return Str((char*)_data, _size); }
std::string_view sv() const noexcept { return std::string_view((char*)_data, _size); } std::string_view sv() const noexcept { return std::string_view((char*)_data, _size); }
Bytes() : _data(nullptr), _size(0) {} Bytes() : _data(nullptr), _size(0) {}
Bytes(unsigned char* p, int size): _data(p), _size(size) {} Bytes(unsigned char* p, int size): _data(p), _size(size) {}
Bytes(const std::vector<unsigned char>& 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()) {} Bytes(const Str& str): Bytes(str.sv()) {}
operator bool() const noexcept { return _data != nullptr; } operator bool() const noexcept { return _data != nullptr; }
// copy constructor Bytes(const std::vector<unsigned char>& v);
Bytes(const Bytes& rhs){ Bytes(std::string_view sv);
_data = new unsigned char[rhs._size]; Bytes(const Bytes& rhs);
_size = rhs._size; Bytes(Bytes&& rhs) noexcept;
for(int i=0; i<_size; i++) _data[i] = rhs._data[i]; Bytes& operator=(Bytes&& rhs) noexcept;
}
// 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<unsigned char*, int> detach() noexcept {
unsigned char* p = _data;
int size = _size;
_data = nullptr;
_size = 0;
return {p, size};
}
~Bytes(){
delete[] _data;
}
// delete copy assignment
Bytes& operator=(const Bytes& rhs) = delete; Bytes& operator=(const Bytes& rhs) = delete;
std::pair<unsigned char*, int> detach() noexcept;
~Bytes(){ delete[] _data;}
}; };
using Super = std::pair<PyObject*, Type>; using Super = std::pair<PyObject*, Type>;

View File

@ -131,56 +131,14 @@ struct SStream{
SStream(){} SStream(){}
SStream(int guess_size){ buffer.reserve(guess_size); } SStream(int guess_size){ buffer.reserve(guess_size); }
Str str(){ Str str();
// after this call, the buffer is no longer valid
auto detached = buffer.detach();
return Str(detached.first, detached.second);
}
SStream& operator<<(const Str& s){ SStream& operator<<(const Str& s);
buffer.extend(s.begin(), s.end()); SStream& operator<<(const char* s);
return *this; SStream& operator<<(i64 val);
} SStream& operator<<(const std::string& s);
SStream& operator<<(std::string_view s);
SStream& operator<<(const char* s){ SStream& operator<<(char c);
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;
}
template<typename T> template<typename T>
SStream& operator<<(T val){ SStream& operator<<(T val){

View File

@ -6,4 +6,56 @@ namespace pkpy{
_attr->~NameDict(); _attr->~NameDict();
pool128_dealloc(_attr); 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<unsigned char>& 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<unsigned char*, int> Bytes::detach() noexcept {
unsigned char* p = _data;
int size = _size;
_data = nullptr;
_size = 0;
return {p, size};
}
} // namespace pkpy } // namespace pkpy

View File

@ -420,4 +420,55 @@ int utf8len(unsigned char c, bool suppress){
const std::string& str = _r_interned()[index]; const std::string& str = _r_interned()[index];
return std::string_view(str); 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 } // namespace pkpy