Skip to content

Commit 0c7d78f

Browse files
committed
StringLikeVariantOrder: Compare in-place
1 parent fc827bb commit 0c7d78f

File tree

5 files changed

+60
-28
lines changed

5 files changed

+60
-28
lines changed

core/string/string_name.h

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,24 +166,49 @@ class StringName {
166166
static StringName search(const String &p_name);
167167

168168
struct AlphCompare {
169-
_FORCE_INLINE_ bool operator()(const StringName &l, const StringName &r) const {
169+
template <typename LT, typename RT>
170+
_FORCE_INLINE_ bool operator()(const LT &l, const RT &r) const {
171+
return compare(l, r);
172+
}
173+
_FORCE_INLINE_ static bool compare(const StringName &l, const StringName &r) {
170174
const char *l_cname = l._data ? l._data->cname : "";
171175
const char *r_cname = r._data ? r._data->cname : "";
172176

173177
if (l_cname) {
174178
if (r_cname) {
175-
return is_str_less(l_cname, r_cname);
179+
return str_compare(l_cname, r_cname) < 0;
176180
} else {
177-
return is_str_less(l_cname, r._data->name.ptr());
181+
return str_compare(l_cname, r._data->name.ptr()) < 0;
178182
}
179183
} else {
180184
if (r_cname) {
181-
return is_str_less(l._data->name.ptr(), r_cname);
185+
return str_compare(l._data->name.ptr(), r_cname) < 0;
182186
} else {
183-
return is_str_less(l._data->name.ptr(), r._data->name.ptr());
187+
return str_compare(l._data->name.ptr(), r._data->name.ptr()) < 0;
184188
}
185189
}
186190
}
191+
_FORCE_INLINE_ static bool compare(const String &l, const StringName &r) {
192+
const char *r_cname = r._data ? r._data->cname : "";
193+
194+
if (r_cname) {
195+
return str_compare(l.get_data(), r_cname) < 0;
196+
} else {
197+
return str_compare(l.get_data(), r._data->name.ptr()) < 0;
198+
}
199+
}
200+
_FORCE_INLINE_ static bool compare(const StringName &l, const String &r) {
201+
const char *l_cname = l._data ? l._data->cname : "";
202+
203+
if (l_cname) {
204+
return str_compare(l_cname, r.get_data()) < 0;
205+
} else {
206+
return str_compare(l._data->name.ptr(), r.get_data()) < 0;
207+
}
208+
}
209+
_FORCE_INLINE_ static bool compare(const String &l, const String &r) {
210+
return str_compare(l.get_data(), r.get_data()) < 0;
211+
}
187212
};
188213

189214
StringName &operator=(const StringName &p_name);

core/string/ustring.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ bool Char16String::operator<(const Char16String &p_right) const {
9797
return p_right.length() != 0;
9898
}
9999

100-
return is_str_less(get_data(), p_right.get_data());
100+
return str_compare(get_data(), p_right.get_data()) < 0;
101101
}
102102

103103
Char16String &Char16String::operator+=(char16_t p_char) {
@@ -155,7 +155,7 @@ bool CharString::operator<(const CharString &p_right) const {
155155
return p_right.length() != 0;
156156
}
157157

158-
return is_str_less(get_data(), p_right.get_data());
158+
return str_compare(get_data(), p_right.get_data()) < 0;
159159
}
160160

161161
bool CharString::operator==(const CharString &p_right) const {
@@ -640,7 +640,7 @@ bool String::operator<(const char *p_str) const {
640640
if (is_empty()) {
641641
return true;
642642
}
643-
return is_str_less(get_data(), p_str);
643+
return str_compare(get_data(), p_str) < 0;
644644
}
645645

646646
bool String::operator<(const wchar_t *p_str) const {
@@ -653,10 +653,10 @@ bool String::operator<(const wchar_t *p_str) const {
653653

654654
#ifdef WINDOWS_ENABLED
655655
// wchar_t is 16-bit
656-
return is_str_less(get_data(), String::utf16((const char16_t *)p_str).get_data());
656+
return str_compare(get_data(), String::utf16((const char16_t *)p_str).get_data()) < 0;
657657
#else
658658
// wchar_t is 32-bit
659-
return is_str_less(get_data(), (const char32_t *)p_str);
659+
return str_compare(get_data(), (const char32_t *)p_str) < 0;
660660
#endif
661661
}
662662

@@ -668,7 +668,7 @@ bool String::operator<(const char32_t *p_str) const {
668668
return true;
669669
}
670670

671-
return is_str_less(get_data(), p_str);
671+
return str_compare(get_data(), p_str) < 0;
672672
}
673673

674674
bool String::operator<(const String &p_str) const {

core/string/ustring.h

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -695,21 +695,13 @@ struct FileNoCaseComparator {
695695
};
696696

697697
template <typename L, typename R>
698-
_FORCE_INLINE_ bool is_str_less(const L *l_ptr, const R *r_ptr) {
698+
_FORCE_INLINE_ int64_t str_compare(const L *l_ptr, const R *r_ptr) {
699699
while (true) {
700700
const char32_t l = *l_ptr;
701701
const char32_t r = *r_ptr;
702702

703-
if (l == 0 && r == 0) {
704-
return false;
705-
} else if (l == 0) {
706-
return true;
707-
} else if (r == 0) {
708-
return false;
709-
} else if (l < r) {
710-
return true;
711-
} else if (l > r) {
712-
return false;
703+
if (l == 0 || l != r) {
704+
return static_cast<int64_t>(l) - static_cast<int64_t>(r);
713705
}
714706

715707
l_ptr++;

core/variant/variant.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3406,6 +3406,26 @@ bool StringLikeVariantComparator::compare(const Variant &p_lhs, const Variant &p
34063406
return false;
34073407
}
34083408

3409+
bool StringLikeVariantOrder::compare(const Variant &p_lhs, const Variant &p_rhs) {
3410+
if (p_lhs.get_type() == Variant::STRING) {
3411+
const String &lhs = *VariantInternal::get_string(&p_lhs);
3412+
if (p_rhs.get_type() == Variant::STRING) {
3413+
return StringName::AlphCompare::compare(lhs, *VariantInternal::get_string(&p_rhs));
3414+
} else if (p_rhs.get_type() == Variant::STRING_NAME) {
3415+
return StringName::AlphCompare::compare(lhs, *VariantInternal::get_string_name(&p_rhs));
3416+
}
3417+
} else if (p_lhs.get_type() == Variant::STRING_NAME) {
3418+
const StringName &lhs = *VariantInternal::get_string_name(&p_lhs);
3419+
if (p_rhs.get_type() == Variant::STRING) {
3420+
return StringName::AlphCompare::compare(lhs, *VariantInternal::get_string(&p_rhs));
3421+
} else if (p_rhs.get_type() == Variant::STRING_NAME) {
3422+
return StringName::AlphCompare::compare(lhs, *VariantInternal::get_string_name(&p_rhs));
3423+
}
3424+
}
3425+
3426+
return p_lhs < p_rhs;
3427+
}
3428+
34093429
bool Variant::is_ref_counted() const {
34103430
return type == OBJECT && _get_obj().id.is_ref_counted();
34113431
}

core/variant/variant.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -909,12 +909,7 @@ struct StringLikeVariantComparator {
909909
};
910910

911911
struct StringLikeVariantOrder {
912-
static _ALWAYS_INLINE_ bool compare(const Variant &p_lhs, const Variant &p_rhs) {
913-
if (p_lhs.is_string() && p_rhs.is_string()) {
914-
return p_lhs.operator String() < p_rhs.operator String();
915-
}
916-
return p_lhs < p_rhs;
917-
}
912+
static bool compare(const Variant &p_lhs, const Variant &p_rhs);
918913

919914
_ALWAYS_INLINE_ bool operator()(const Variant &p_lhs, const Variant &p_rhs) const {
920915
return compare(p_lhs, p_rhs);

0 commit comments

Comments
 (0)