|
@@ -54,14 +54,7 @@ public:
|
|
|
}
|
|
}
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-
|
|
|
|
|
-std::map<std::string, pkpy::shared_ptr<_StrMemory>, std::less<>> _strIntern;
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-class _StrLiteral : public std::string_view {
|
|
|
|
|
-public:
|
|
|
|
|
- constexpr _StrLiteral(const char* str, size_t len) : std::string_view(str, len) {}
|
|
|
|
|
-};
|
|
|
|
|
|
|
+typedef std::string_view _StrLiteral;
|
|
|
|
|
|
|
|
inline constexpr _StrLiteral operator "" _c(const char* str, size_t len){
|
|
inline constexpr _StrLiteral operator "" _c(const char* str, size_t len){
|
|
|
return _StrLiteral(str, len);
|
|
return _StrLiteral(str, len);
|
|
@@ -70,50 +63,28 @@ inline constexpr _StrLiteral operator "" _c(const char* str, size_t len){
|
|
|
class _Str {
|
|
class _Str {
|
|
|
private:
|
|
private:
|
|
|
pkpy::shared_ptr<_StrMemory> _s;
|
|
pkpy::shared_ptr<_StrMemory> _s;
|
|
|
- bool interned = false;
|
|
|
|
|
public:
|
|
public:
|
|
|
_Str(_StrLiteral s){
|
|
_Str(_StrLiteral s){
|
|
|
- construct(s);
|
|
|
|
|
- intern();
|
|
|
|
|
|
|
+ _s = pkpy::make_shared<_StrMemory>(std::string(s));
|
|
|
}
|
|
}
|
|
|
_Str(const char* s){
|
|
_Str(const char* s){
|
|
|
- construct(s);
|
|
|
|
|
|
|
+ _s = pkpy::make_shared<_StrMemory>(std::string(s));
|
|
|
}
|
|
}
|
|
|
_Str(const char* s, size_t len){
|
|
_Str(const char* s, size_t len){
|
|
|
- construct(std::string_view(s, len));
|
|
|
|
|
|
|
+ _s = pkpy::make_shared<_StrMemory>(std::string(s, len));
|
|
|
}
|
|
}
|
|
|
_Str(){
|
|
_Str(){
|
|
|
- construct("");
|
|
|
|
|
|
|
+ _s = pkpy::make_shared<_StrMemory>(std::string());
|
|
|
}
|
|
}
|
|
|
_Str(const std::string& s){
|
|
_Str(const std::string& s){
|
|
|
- construct(s);
|
|
|
|
|
|
|
+ _s = pkpy::make_shared<_StrMemory>(s);
|
|
|
}
|
|
}
|
|
|
- _Str(const _Str& s) : _s(s._s), interned(s.interned) {}
|
|
|
|
|
|
|
+ _Str(const _Str& s) : _s(s._s) {}
|
|
|
|
|
|
|
|
- // for move constructor, we do not check if the string is interned!!
|
|
|
|
|
_Str(std::string&& s){
|
|
_Str(std::string&& s){
|
|
|
this->_s = pkpy::make_shared<_StrMemory>(std::move(s));
|
|
this->_s = pkpy::make_shared<_StrMemory>(std::move(s));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- void construct(std::string_view sv){
|
|
|
|
|
- auto it = _strIntern.find(sv);
|
|
|
|
|
- if(it != _strIntern.end()){
|
|
|
|
|
- this->_s = it->second;
|
|
|
|
|
- interned = true;
|
|
|
|
|
- }else{
|
|
|
|
|
- this->_s = pkpy::make_shared<_StrMemory>(std::string(sv));
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // force the string to be interned
|
|
|
|
|
- void intern(){
|
|
|
|
|
- if(interned) return;
|
|
|
|
|
- auto it = _strIntern.find(*this->_s);
|
|
|
|
|
- if(it == _strIntern.end()) _strIntern[*this->_s] = this->_s;
|
|
|
|
|
- else this->_s = it->second;
|
|
|
|
|
- interned = true;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
inline int u8_length() const {
|
|
inline int u8_length() const {
|
|
|
return this->_s->u8_length();
|
|
return this->_s->u8_length();
|
|
|
}
|
|
}
|
|
@@ -139,12 +110,10 @@ public:
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const _Str& other) const {
|
|
bool operator==(const _Str& other) const {
|
|
|
- if(interned && other.interned) return _s == other._s;
|
|
|
|
|
return *_s == *other._s;
|
|
return *_s == *other._s;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const _Str& other) const {
|
|
bool operator!=(const _Str& other) const {
|
|
|
- if(interned && other.interned) return _s != other._s;
|
|
|
|
|
return *_s != *other._s;
|
|
return *_s != *other._s;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -260,6 +229,11 @@ const _Str& __json__ = _Str("__json__"_c);
|
|
|
const _Str& __name__ = _Str("__name__"_c);
|
|
const _Str& __name__ = _Str("__name__"_c);
|
|
|
const _Str& __len__ = _Str("__len__"_c);
|
|
const _Str& __len__ = _Str("__len__"_c);
|
|
|
|
|
|
|
|
|
|
+const _Str& m_append = _Str("append"_c);
|
|
|
|
|
+const _Str& m_eval = _Str("eval"_c);
|
|
|
|
|
+const _Str& __enter__ = _Str("__enter__"_c);
|
|
|
|
|
+const _Str& __exit__ = _Str("__exit__"_c);
|
|
|
|
|
+
|
|
|
const _Str CMP_SPECIAL_METHODS[] = {
|
|
const _Str CMP_SPECIAL_METHODS[] = {
|
|
|
"__lt__"_c, "__le__"_c, "__eq__"_c, "__ne__"_c, "__gt__"_c, "__ge__"_c
|
|
"__lt__"_c, "__le__"_c, "__eq__"_c, "__ne__"_c, "__gt__"_c, "__ge__"_c
|
|
|
}; // __ne__ should not be used
|
|
}; // __ne__ should not be used
|