This commit is contained in:
blueloveTH 2023-06-30 00:05:11 +08:00
parent d8ea87e408
commit e11702aebc
2 changed files with 10 additions and 7 deletions

View File

@ -23,6 +23,8 @@ struct Str{
char* data;
char _inlined[16];
const char* _cached_c_str = nullptr;
bool is_inlined() const { return data == _inlined; }
Str(): size(0), is_ascii(true), data(_inlined) {}
@ -98,6 +100,7 @@ struct Str{
~Str(){
if(!is_inlined()) pool64.dealloc(data);
if(_cached_c_str != nullptr) free((void*)_cached_c_str);
}
Str operator+(const Str& other) const {
@ -201,10 +204,11 @@ struct Str{
return p;
}
const char* c_str_temp() const {
static THREAD_LOCAL std::string temp;
temp.assign(data, size);
return temp.c_str();
const char* c_str(){
if(_cached_c_str == nullptr){
_cached_c_str = c_str_dup();
}
return _cached_c_str;
}
std::string_view sv() const {
@ -429,7 +433,6 @@ struct FastStrStream{
struct CString{
const char* ptr;
CString(const char* ptr): ptr(ptr) {}
CString(const Str& str): ptr(str.c_str_temp()) {}
operator const char*() const { return ptr; }
};

View File

@ -824,11 +824,11 @@ template<> inline bool _py_cast<bool>(VM* vm, PyObject* obj){
template<> inline CString py_cast<CString>(VM* vm, PyObject* obj){
vm->check_non_tagged_type(obj, vm->tp_str);
return PK_OBJ_GET(Str, obj).c_str_temp();
return PK_OBJ_GET(Str, obj).c_str();
}
template<> inline CString _py_cast<CString>(VM* vm, PyObject* obj){
return PK_OBJ_GET(Str, obj).c_str_temp();
return PK_OBJ_GET(Str, obj).c_str();
}
inline PyObject* py_var(VM* vm, const char val[]){