mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-22 12:30:19 +00:00
...
This commit is contained in:
parent
ba248ae0f3
commit
8e21cd4baf
46
src/str.h
46
src/str.h
@ -21,15 +21,26 @@ struct Str{
|
|||||||
int size;
|
int size;
|
||||||
bool is_ascii;
|
bool is_ascii;
|
||||||
char* data;
|
char* data;
|
||||||
|
char _inlined[16];
|
||||||
|
|
||||||
Str(): size(0), is_ascii(true), data(nullptr) {}
|
bool is_inlined() const { return data == _inlined; }
|
||||||
|
|
||||||
|
Str(): size(0), is_ascii(true), data(_inlined) {}
|
||||||
|
|
||||||
|
void _alloc(){
|
||||||
|
if(size <= 16){
|
||||||
|
this->data = _inlined;
|
||||||
|
}else{
|
||||||
|
this->data = (char*)pool64.alloc(size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Str(int size, bool is_ascii): size(size), is_ascii(is_ascii) {
|
Str(int size, bool is_ascii): size(size), is_ascii(is_ascii) {
|
||||||
data = (char*)pool64.alloc(size);
|
_alloc();
|
||||||
}
|
}
|
||||||
|
|
||||||
#define STR_INIT() \
|
#define STR_INIT() \
|
||||||
data = (char*)pool64.alloc(size); \
|
_alloc(); \
|
||||||
for(int i=0; i<size; i++){ \
|
for(int i=0; i<size; i++){ \
|
||||||
data[i] = s[i]; \
|
data[i] = s[i]; \
|
||||||
if(!isascii(s[i])) is_ascii = false; \
|
if(!isascii(s[i])) is_ascii = false; \
|
||||||
@ -54,13 +65,19 @@ struct Str{
|
|||||||
#undef STR_INIT
|
#undef STR_INIT
|
||||||
|
|
||||||
Str(const Str& other): size(other.size), is_ascii(other.is_ascii) {
|
Str(const Str& other): size(other.size), is_ascii(other.is_ascii) {
|
||||||
data = (char*)pool64.alloc(size);
|
_alloc();
|
||||||
memcpy(data, other.data, size);
|
memcpy(data, other.data, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
Str(Str&& other): size(other.size), is_ascii(other.is_ascii), data(other.data) {
|
Str(Str&& other): size(other.size), is_ascii(other.is_ascii) {
|
||||||
other.data = nullptr;
|
if(other.is_inlined()){
|
||||||
other.size = 0;
|
data = _inlined;
|
||||||
|
for(int i=0; i<size; i++) _inlined[i] = other._inlined[i];
|
||||||
|
}else{
|
||||||
|
data = other.data;
|
||||||
|
other.data = other._inlined;
|
||||||
|
other.size = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* begin() const { return data; }
|
const char* begin() const { return data; }
|
||||||
@ -71,25 +88,16 @@ struct Str{
|
|||||||
size_t hash() const{ return std::hash<std::string_view>()(sv()); }
|
size_t hash() const{ return std::hash<std::string_view>()(sv()); }
|
||||||
|
|
||||||
Str& operator=(const Str& other){
|
Str& operator=(const Str& other){
|
||||||
if(data!=nullptr) pool64.dealloc(data);
|
if(!is_inlined()) pool64.dealloc(data);
|
||||||
size = other.size;
|
size = other.size;
|
||||||
is_ascii = other.is_ascii;
|
is_ascii = other.is_ascii;
|
||||||
data = (char*)pool64.alloc(size);
|
_alloc();
|
||||||
memcpy(data, other.data, size);
|
memcpy(data, other.data, size);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Str& operator=(Str&& other) noexcept{
|
|
||||||
if(data!=nullptr) pool64.dealloc(data);
|
|
||||||
size = other.size;
|
|
||||||
is_ascii = other.is_ascii;
|
|
||||||
data = other.data;
|
|
||||||
other.data = nullptr;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
~Str(){
|
~Str(){
|
||||||
if(data!=nullptr) pool64.dealloc(data);
|
if(!is_inlined()) pool64.dealloc(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
Str operator+(const Str& other) const {
|
Str operator+(const Str& other) const {
|
||||||
|
@ -11,13 +11,13 @@ using List = pod_vector<PyObject*>;
|
|||||||
|
|
||||||
class Tuple {
|
class Tuple {
|
||||||
PyObject** _args;
|
PyObject** _args;
|
||||||
PyObject* _inlined[2];
|
PyObject* _inlined[3];
|
||||||
int _size;
|
int _size;
|
||||||
|
|
||||||
bool is_inlined() const { return _args == _inlined; }
|
bool is_inlined() const { return _args == _inlined; }
|
||||||
|
|
||||||
void _alloc(int n){
|
void _alloc(int n){
|
||||||
if(n <= 2){
|
if(n <= 3){
|
||||||
this->_args = _inlined;
|
this->_args = _inlined;
|
||||||
}else{
|
}else{
|
||||||
this->_args = (PyObject**)pool64.alloc(n * sizeof(void*));
|
this->_args = (PyObject**)pool64.alloc(n * sizeof(void*));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user