Skip to content

Commit

Permalink
StringName: operator== compares in-place
Browse files Browse the repository at this point in the history
  • Loading branch information
rune-scape committed Aug 28, 2024
1 parent 40b378e commit 0d22c9d
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 19 deletions.
102 changes: 84 additions & 18 deletions core/string/string_name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,32 @@ StaticCString StaticCString::create(const char *p_ptr) {
return scs;
}

StringName::_Data *StringName::_table[STRING_TABLE_LEN];

Check failure on line 42 in core/string/string_name.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor w/ Mono (target=editor)

redefinition of 'StringName::_Data* StringName::_table [65536]'

Check failure on line 42 in core/string/string_name.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with doubles and GCC sanitizers (target=editor, tests=yes, dev_build=yes, scu_build=yes, precision=double, use_asan=yes, use_ubsan=yes, linker=gold)

redefinition of 'StringName::_Data* StringName::_table [65536]'

Check failure on line 42 in core/string/string_name.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with clang sanitizers (target=editor, tests=yes, dev_build=yes, use_asan=yes, use_ubsan=yes, use_llvm=yes, linker=lld)

redefinition of '_table'

Check failure on line 42 in core/string/string_name.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Editor (target=editor, tests=yes)

'StringName::_Data *StringName::_table[65536]': redefinition

Check failure on line 42 in core/string/string_name.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with ThreadSanitizer (target=editor, tests=yes, dev_build=yes, use_tsan=yes, use_llvm=yes, linker=lld)

redefinition of '_table'

Check failure on line 42 in core/string/string_name.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Template w/ Mono (target=template_release)

redefinition of 'StringName::_Data* StringName::_table [65536]'

Check failure on line 42 in core/string/string_name.cpp

View workflow job for this annotation

GitHub Actions / 🏁 Windows / Template (target=template_release)

'StringName::_Data *StringName::_table[65536]': redefinition

Check failure on line 42 in core/string/string_name.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Minimal template (target=template_release, everything disabled)

redefinition of 'StringName::_Data* StringName::_table [65536]'

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 !this->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 !this->operator==(p_name);
}

StringName _scs_create(const char *p_chr, bool p_static) {
return (p_chr[0] ? StringName(StaticCString::create(p_chr), p_static) : StringName());
}
Expand Down Expand Up @@ -139,19 +165,19 @@ void StringName::unref() {
}

bool StringName::operator==(const String &p_name) const {
if (!_data) {
return (p_name.length() == 0);
if (_data) {
return _data->operator==(p_name);
}

return (_data->get_name() == p_name);
return p_name.is_empty();
}

bool StringName::operator==(const char *p_name) const {
if (!_data) {
return (p_name[0] == 0);
if (_data) {
return _data->operator==(p_name);
}

return (_data->get_name() == p_name);
return p_name[0] == 0;
}

bool StringName::operator!=(const String &p_name) const {
Expand All @@ -168,16 +194,56 @@ bool StringName::operator!=(const StringName &p_name) const {
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) {
return;
return *this;
}

unref();

if (p_name._data && p_name._data->refcount.ref()) {
_data = p_name._data;
}

return *this;
}

StringName::StringName(const StringName &p_name) {
Expand Down Expand Up @@ -217,7 +283,7 @@ StringName::StringName(const char *p_name, bool p_static) {

while (_data) {
// compare hash first
if (_data->hash == hash && _data->get_name() == p_name) {
if (_data->hash == hash && _data->operator==(p_name)) {
break;
}
_data = _data->next;
Expand Down Expand Up @@ -276,7 +342,7 @@ StringName::StringName(const StaticCString &p_static_string, bool p_static) {

while (_data) {
// 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;
}
_data = _data->next;
Expand Down Expand Up @@ -334,7 +400,7 @@ StringName::StringName(const String &p_name, bool p_static) {
_data = _table[idx];

while (_data) {
if (_data->hash == hash && _data->get_name() == p_name) {
if (_data->hash == hash && _data->operator==(p_name)) {
break;
}
_data = _data->next;
Expand Down Expand Up @@ -393,7 +459,7 @@ StringName StringName::search(const char *p_name) {

while (_data) {
// compare hash first
if (_data->hash == hash && _data->get_name() == p_name) {
if (_data->hash == hash && _data->operator==(p_name)) {
break;
}
_data = _data->next;
Expand Down Expand Up @@ -430,7 +496,7 @@ StringName StringName::search(const char32_t *p_name) {

while (_data) {
// compare hash first
if (_data->hash == hash && _data->get_name() == p_name) {
if (_data->hash == hash && _data->operator==(p_name)) {
break;
}
_data = _data->next;
Expand All @@ -456,7 +522,7 @@ StringName StringName::search(const String &p_name) {

while (_data) {
// compare hash first
if (_data->hash == hash && p_name == _data->get_name()) {
if (_data->hash == hash && _data->operator==(p_name)) {
break;
}
_data = _data->next;
Expand All @@ -475,15 +541,15 @@ StringName StringName::search(const String &p_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) {
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) {
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) {
return p_name != p_string_name.operator String();
return p_string_name.operator!=(p_name);
}
11 changes: 10 additions & 1 deletion core/string/string_name.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ class StringName {
uint32_t debug_references = 0;
#endif
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;
uint32_t hash = 0;
_Data *prev = nullptr;
Expand Down Expand Up @@ -99,6 +104,10 @@ class StringName {
bool operator!=(const String &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 {
if (!_data) {
return false;
Expand Down Expand Up @@ -175,7 +184,7 @@ class StringName {
}
};

void operator=(const StringName &p_name);
StringName &operator=(const StringName &p_name);
StringName(const char *p_name, bool p_static = false);
StringName(const StringName &p_name);
StringName(const String &p_name, bool p_static = false);
Expand Down

0 comments on commit 0d22c9d

Please sign in to comment.