StringName: operator== compares in-place

This commit is contained in:
rune-scape 2024-05-30 13:24:48 -07:00
parent 40b378e9e2
commit 6abd17265b
2 changed files with 92 additions and 19 deletions

View File

@ -39,6 +39,30 @@ StaticCString StaticCString::create(const char *p_ptr) {
return scs; return scs;
} }
bool StringName::_Data::operator==(const String &p_name) const {
if (cname) {
return p_name == cname;
} else {
return name == p_name;
}
}
bool StringName::_Data::operator!=(const String &p_name) const {
return !operator==(p_name);
}
bool StringName::_Data::operator==(const char *p_name) const {
if (cname) {
return strcmp(cname, p_name) == 0;
} else {
return name == p_name;
}
}
bool StringName::_Data::operator!=(const char *p_name) const {
return !operator==(p_name);
}
StringName _scs_create(const char *p_chr, bool p_static) { StringName _scs_create(const char *p_chr, bool p_static) {
return (p_chr[0] ? StringName(StaticCString::create(p_chr), p_static) : StringName()); return (p_chr[0] ? StringName(StaticCString::create(p_chr), p_static) : StringName());
} }
@ -139,19 +163,19 @@ void StringName::unref() {
} }
bool StringName::operator==(const String &p_name) const { bool StringName::operator==(const String &p_name) const {
if (!_data) { if (_data) {
return (p_name.length() == 0); return _data->operator==(p_name);
} }
return (_data->get_name() == p_name); return p_name.is_empty();
} }
bool StringName::operator==(const char *p_name) const { bool StringName::operator==(const char *p_name) const {
if (!_data) { if (_data) {
return (p_name[0] == 0); return _data->operator==(p_name);
} }
return (_data->get_name() == p_name); return p_name[0] == 0;
} }
bool StringName::operator!=(const String &p_name) const { bool StringName::operator!=(const String &p_name) const {
@ -168,9 +192,47 @@ bool StringName::operator!=(const StringName &p_name) const {
return _data != p_name._data; return _data != p_name._data;
} }
void StringName::operator=(const StringName &p_name) { char32_t StringName::operator[](int p_index) const {
if (_data) {
if (_data->cname) {
CRASH_BAD_INDEX(p_index, static_cast<long>(strlen(_data->cname)));
return _data->cname[p_index];
} else {
return _data->name[p_index];
}
}
CRASH_BAD_INDEX(p_index, 0);
return 0;
}
int StringName::length() const {
if (_data) {
if (_data->cname) {
return strlen(_data->cname);
} else {
return _data->name.length();
}
}
return 0;
}
bool StringName::is_empty() const {
if (_data) {
if (_data->cname) {
return _data->cname[0] == 0;
} else {
return _data->name.is_empty();
}
}
return true;
}
StringName &StringName::operator=(const StringName &p_name) {
if (this == &p_name) { if (this == &p_name) {
return; return *this;
} }
unref(); unref();
@ -178,6 +240,8 @@ void StringName::operator=(const StringName &p_name) {
if (p_name._data && p_name._data->refcount.ref()) { if (p_name._data && p_name._data->refcount.ref()) {
_data = p_name._data; _data = p_name._data;
} }
return *this;
} }
StringName::StringName(const StringName &p_name) { StringName::StringName(const StringName &p_name) {
@ -217,7 +281,7 @@ StringName::StringName(const char *p_name, bool p_static) {
while (_data) { while (_data) {
// compare hash first // compare hash first
if (_data->hash == hash && _data->get_name() == p_name) { if (_data->hash == hash && _data->operator==(p_name)) {
break; break;
} }
_data = _data->next; _data = _data->next;
@ -276,7 +340,7 @@ StringName::StringName(const StaticCString &p_static_string, bool p_static) {
while (_data) { while (_data) {
// compare hash first // compare hash first
if (_data->hash == hash && _data->get_name() == p_static_string.ptr) { if (_data->hash == hash && _data->operator==(p_static_string.ptr)) {
break; break;
} }
_data = _data->next; _data = _data->next;
@ -334,7 +398,7 @@ StringName::StringName(const String &p_name, bool p_static) {
_data = _table[idx]; _data = _table[idx];
while (_data) { while (_data) {
if (_data->hash == hash && _data->get_name() == p_name) { if (_data->hash == hash && _data->operator==(p_name)) {
break; break;
} }
_data = _data->next; _data = _data->next;
@ -393,7 +457,7 @@ StringName StringName::search(const char *p_name) {
while (_data) { while (_data) {
// compare hash first // compare hash first
if (_data->hash == hash && _data->get_name() == p_name) { if (_data->hash == hash && _data->operator==(p_name)) {
break; break;
} }
_data = _data->next; _data = _data->next;
@ -430,7 +494,7 @@ StringName StringName::search(const char32_t *p_name) {
while (_data) { while (_data) {
// compare hash first // compare hash first
if (_data->hash == hash && _data->get_name() == p_name) { if (_data->hash == hash && _data->operator==(p_name)) {
break; break;
} }
_data = _data->next; _data = _data->next;
@ -456,7 +520,7 @@ StringName StringName::search(const String &p_name) {
while (_data) { while (_data) {
// compare hash first // compare hash first
if (_data->hash == hash && p_name == _data->get_name()) { if (_data->hash == hash && _data->operator==(p_name)) {
break; break;
} }
_data = _data->next; _data = _data->next;
@ -475,15 +539,15 @@ StringName StringName::search(const String &p_name) {
} }
bool operator==(const String &p_name, const StringName &p_string_name) { bool operator==(const String &p_name, const StringName &p_string_name) {
return p_name == p_string_name.operator String(); return p_string_name.operator==(p_name);
} }
bool operator!=(const String &p_name, const StringName &p_string_name) { bool operator!=(const String &p_name, const StringName &p_string_name) {
return p_name != p_string_name.operator String(); return p_string_name.operator!=(p_name);
} }
bool operator==(const char *p_name, const StringName &p_string_name) { bool operator==(const char *p_name, const StringName &p_string_name) {
return p_name == p_string_name.operator String(); return p_string_name.operator==(p_name);
} }
bool operator!=(const char *p_name, const StringName &p_string_name) { bool operator!=(const char *p_name, const StringName &p_string_name) {
return p_name != p_string_name.operator String(); return p_string_name.operator!=(p_name);
} }

View File

@ -60,6 +60,11 @@ class StringName {
uint32_t debug_references = 0; uint32_t debug_references = 0;
#endif #endif
String get_name() const { return cname ? String(cname) : name; } String get_name() const { return cname ? String(cname) : name; }
bool operator==(const String &p_name) const;
bool operator!=(const String &p_name) const;
bool operator==(const char *p_name) const;
bool operator!=(const char *p_name) const;
int idx = 0; int idx = 0;
uint32_t hash = 0; uint32_t hash = 0;
_Data *prev = nullptr; _Data *prev = nullptr;
@ -99,6 +104,10 @@ public:
bool operator!=(const String &p_name) const; bool operator!=(const String &p_name) const;
bool operator!=(const char *p_name) const; bool operator!=(const char *p_name) const;
char32_t operator[](int p_index) const;
int length() const;
bool is_empty() const;
_FORCE_INLINE_ bool is_node_unique_name() const { _FORCE_INLINE_ bool is_node_unique_name() const {
if (!_data) { if (!_data) {
return false; return false;
@ -175,7 +184,7 @@ public:
} }
}; };
void operator=(const StringName &p_name); StringName &operator=(const StringName &p_name);
StringName(const char *p_name, bool p_static = false); StringName(const char *p_name, bool p_static = false);
StringName(const StringName &p_name); StringName(const StringName &p_name);
StringName(const String &p_name, bool p_static = false); StringName(const String &p_name, bool p_static = false);