1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-19 14:31:59 +00:00

Optimize String ==, ends_with and begins_with by using memcmp.

This commit is contained in:
Lukas Tenbrink
2025-01-13 15:23:08 +01:00
parent 5b52b4b5c4
commit 2e600789dd

View File

@@ -550,18 +550,7 @@ bool String::operator==(const char32_t *p_str) const {
return true; return true;
} }
int l = length(); return memcmp(ptr(), p_str, len * sizeof(char32_t)) == 0;
const char32_t *dst = get_data();
/* Compare char by char */
for (int i = 0; i < l; i++) {
if (p_str[i] != dst[i]) {
return false;
}
}
return true;
} }
bool String::operator==(const String &p_str) const { bool String::operator==(const String &p_str) const {
@@ -572,23 +561,11 @@ bool String::operator==(const String &p_str) const {
return true; return true;
} }
int l = length(); return memcmp(ptr(), p_str.ptr(), length() * sizeof(char32_t)) == 0;
const char32_t *src = get_data();
const char32_t *dst = p_str.get_data();
/* Compare char by char */
for (int i = 0; i < l; i++) {
if (src[i] != dst[i]) {
return false;
}
}
return true;
} }
bool String::operator==(const StrRange<char32_t> &p_str_range) const { bool String::operator==(const StrRange<char32_t> &p_str_range) const {
int len = p_str_range.len; const int len = p_str_range.len;
if (length() != len) { if (length() != len) {
return false; return false;
@@ -597,17 +574,7 @@ bool String::operator==(const StrRange<char32_t> &p_str_range) const {
return true; return true;
} }
const char32_t *c_str = p_str_range.c_str; return memcmp(ptr(), p_str_range.c_str, len * sizeof(char32_t)) == 0;
const char32_t *dst = &operator[](0);
/* Compare char by char */
for (int i = 0; i < len; i++) {
if (c_str[i] != dst[i]) {
return false;
}
}
return true;
} }
bool operator==(const char *p_chr, const String &p_str) { bool operator==(const char *p_chr, const String &p_str) {
@@ -3573,25 +3540,15 @@ int String::rfindn(const char *p_str, int p_from) const {
} }
bool String::ends_with(const String &p_string) const { bool String::ends_with(const String &p_string) const {
int l = p_string.length(); const int l = p_string.length();
if (l > length()) { if (l > length()) {
return false; return false;
} }
if (l == 0) { if (l == 0) {
return true; return true;
} }
const char32_t *p = &p_string[0]; return memcmp(ptr() + (length() - l), p_string.ptr(), l * sizeof(char32_t)) == 0;
const char32_t *s = &operator[](length() - l);
for (int i = 0; i < l; i++) {
if (p[i] != s[i]) {
return false;
}
}
return true;
} }
bool String::ends_with(const char *p_string) const { bool String::ends_with(const char *p_string) const {
@@ -3620,25 +3577,15 @@ bool String::ends_with(const char *p_string) const {
} }
bool String::begins_with(const String &p_string) const { bool String::begins_with(const String &p_string) const {
int l = p_string.length(); const int l = p_string.length();
if (l > length()) { if (l > length()) {
return false; return false;
} }
if (l == 0) { if (l == 0) {
return true; return true;
} }
const char32_t *p = &p_string[0]; return memcmp(ptr(), p_string.ptr(), l * sizeof(char32_t)) == 0;
const char32_t *s = &operator[](0);
for (int i = 0; i < l; i++) {
if (p[i] != s[i]) {
return false;
}
}
return true;
} }
bool String::begins_with(const char *p_string) const { bool String::begins_with(const char *p_string) const {