Skip to content

Commit

Permalink
StringLikeVariantOrder: Compare in-place
Browse files Browse the repository at this point in the history
  • Loading branch information
rune-scape committed Oct 22, 2024
1 parent b3bcb2d commit 151a338
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 28 deletions.
35 changes: 30 additions & 5 deletions core/string/string_name.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,24 +165,49 @@ class StringName {
static StringName search(const String &p_name);

struct AlphCompare {
_FORCE_INLINE_ bool operator()(const StringName &l, const StringName &r) const {
template <typename LT, typename RT>
_FORCE_INLINE_ bool operator()(const LT &l, const RT &r) const {
return compare(l, r);
}
_FORCE_INLINE_ static bool compare(const StringName &l, const StringName &r) {
const char *l_cname = l._data ? l._data->cname : "";
const char *r_cname = r._data ? r._data->cname : "";

if (l_cname) {
if (r_cname) {
return is_str_less(l_cname, r_cname);
return str_compare(l_cname, r_cname) < 0;
} else {
return is_str_less(l_cname, r._data->name.ptr());
return str_compare(l_cname, r._data->name.ptr()) < 0;
}
} else {
if (r_cname) {
return is_str_less(l._data->name.ptr(), r_cname);
return str_compare(l._data->name.ptr(), r_cname) < 0;
} else {
return is_str_less(l._data->name.ptr(), r._data->name.ptr());
return str_compare(l._data->name.ptr(), r._data->name.ptr()) < 0;
}
}
}
_FORCE_INLINE_ static bool compare(const String &l, const StringName &r) {
const char *r_cname = r._data ? r._data->cname : "";

if (r_cname) {
return str_compare(l.ptr(), r_cname) < 0;
} else {
return str_compare(l.ptr(), r._data->name.ptr()) < 0;
}
}
_FORCE_INLINE_ static bool compare(const StringName &l, const String &r) {
const char *l_cname = l._data ? l._data->cname : "";

if (l_cname) {
return str_compare(l_cname, r.ptr()) < 0;
} else {
return str_compare(l._data->name.ptr(), r.ptr()) < 0;
}
}
_FORCE_INLINE_ static bool compare(const String &l, const String &r) {
return str_compare(l.ptr(), r.ptr()) < 0;
}
};

StringName &operator=(const StringName &p_name);
Expand Down
12 changes: 6 additions & 6 deletions core/string/ustring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ bool Char16String::operator<(const Char16String &p_right) const {
return p_right.length() != 0;
}

return is_str_less(get_data(), p_right.get_data());
return str_compare(get_data(), p_right.get_data()) < 0;
}

Char16String &Char16String::operator+=(char16_t p_char) {
Expand Down Expand Up @@ -159,7 +159,7 @@ bool CharString::operator<(const CharString &p_right) const {
return p_right.length() != 0;
}

return is_str_less(get_data(), p_right.get_data());
return str_compare(get_data(), p_right.get_data()) < 0;
}

bool CharString::operator==(const CharString &p_right) const {
Expand Down Expand Up @@ -804,7 +804,7 @@ bool String::operator<(const char *p_str) const {
if (is_empty()) {
return true;
}
return is_str_less(get_data(), p_str);
return str_compare(get_data(), p_str) < 0;
}

bool String::operator<(const wchar_t *p_str) const {
Expand All @@ -817,10 +817,10 @@ bool String::operator<(const wchar_t *p_str) const {

#ifdef WINDOWS_ENABLED
// wchar_t is 16-bit
return is_str_less(get_data(), String::utf16((const char16_t *)p_str).get_data());
return str_compare(get_data(), String::utf16((const char16_t *)p_str).get_data()) < 0;
#else
// wchar_t is 32-bit
return is_str_less(get_data(), (const char32_t *)p_str);
return str_compare(get_data(), (const char32_t *)p_str) < 0;
#endif
}

Expand All @@ -832,7 +832,7 @@ bool String::operator<(const char32_t *p_str) const {
return true;
}

return is_str_less(get_data(), p_str);
return str_compare(get_data(), p_str) < 0;
}

bool String::operator<(const String &p_str) const {
Expand Down
14 changes: 3 additions & 11 deletions core/string/ustring.h
Original file line number Diff line number Diff line change
Expand Up @@ -531,21 +531,13 @@ struct FileNoCaseComparator {
};

template <typename L, typename R>
_FORCE_INLINE_ bool is_str_less(const L *l_ptr, const R *r_ptr) {
_FORCE_INLINE_ int64_t str_compare(const L *l_ptr, const R *r_ptr) {
while (true) {
const char32_t l = *l_ptr;
const char32_t r = *r_ptr;

if (l == 0 && r == 0) {
return false;
} else if (l == 0) {
return true;
} else if (r == 0) {
return false;
} else if (l < r) {
return true;
} else if (l > r) {
return false;
if (l != r || l == 0 || r == 0) {
return l - r;
}

l_ptr++;
Expand Down
18 changes: 18 additions & 0 deletions core/variant/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3560,6 +3560,24 @@ bool StringLikeVariantComparator::compare(const Variant &p_lhs, const Variant &p
return false;
}

bool StringLikeVariantOrder::compare(const Variant &p_lhs, const Variant &p_rhs) {
if (p_lhs.get_type() == Variant::STRING) {
if (p_rhs.get_type() == Variant::STRING) {
return StringName::AlphCompare::compare(*VariantInternal::get_string(&p_lhs), *VariantInternal::get_string(&p_rhs));
} else if (p_rhs.get_type() == Variant::STRING_NAME) {
return StringName::AlphCompare::compare(*VariantInternal::get_string(&p_lhs), *VariantInternal::get_string_name(&p_rhs));
}
} else if (p_lhs.get_type() == Variant::STRING_NAME) {
if (p_rhs.get_type() == Variant::STRING) {
return StringName::AlphCompare::compare(*VariantInternal::get_string_name(&p_lhs), *VariantInternal::get_string(&p_rhs));
} else if (p_rhs.get_type() == Variant::STRING_NAME) {
return StringName::AlphCompare::compare(*VariantInternal::get_string_name(&p_lhs), *VariantInternal::get_string_name(&p_rhs));
}
}

return p_lhs < p_rhs;
}

bool Variant::is_ref_counted() const {
return type == OBJECT && _get_obj().id.is_ref_counted();
}
Expand Down
7 changes: 1 addition & 6 deletions core/variant/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -855,12 +855,7 @@ struct StringLikeVariantComparator {
};

struct StringLikeVariantOrder {
static _ALWAYS_INLINE_ bool compare(const Variant &p_lhs, const Variant &p_rhs) {
if (p_lhs.is_string() && p_rhs.is_string()) {
return p_lhs.operator String() < p_rhs.operator String();
}
return p_lhs < p_rhs;
}
static bool compare(const Variant &p_lhs, const Variant &p_rhs);

_ALWAYS_INLINE_ bool operator()(const Variant &p_lhs, const Variant &p_rhs) const {
return compare(p_lhs, p_rhs);
Expand Down

0 comments on commit 151a338

Please sign in to comment.