Skip to content

Commit 2e60078

Browse files
committed
Optimize String ==, ends_with and begins_with by using memcmp.
1 parent 5b52b4b commit 2e60078

File tree

1 file changed

+8
-61
lines changed

1 file changed

+8
-61
lines changed

core/string/ustring.cpp

Lines changed: 8 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -550,18 +550,7 @@ bool String::operator==(const char32_t *p_str) const {
550550
return true;
551551
}
552552

553-
int l = length();
554-
555-
const char32_t *dst = get_data();
556-
557-
/* Compare char by char */
558-
for (int i = 0; i < l; i++) {
559-
if (p_str[i] != dst[i]) {
560-
return false;
561-
}
562-
}
563-
564-
return true;
553+
return memcmp(ptr(), p_str, len * sizeof(char32_t)) == 0;
565554
}
566555

567556
bool String::operator==(const String &p_str) const {
@@ -572,23 +561,11 @@ bool String::operator==(const String &p_str) const {
572561
return true;
573562
}
574563

575-
int l = length();
576-
577-
const char32_t *src = get_data();
578-
const char32_t *dst = p_str.get_data();
579-
580-
/* Compare char by char */
581-
for (int i = 0; i < l; i++) {
582-
if (src[i] != dst[i]) {
583-
return false;
584-
}
585-
}
586-
587-
return true;
564+
return memcmp(ptr(), p_str.ptr(), length() * sizeof(char32_t)) == 0;
588565
}
589566

590567
bool String::operator==(const StrRange<char32_t> &p_str_range) const {
591-
int len = p_str_range.len;
568+
const int len = p_str_range.len;
592569

593570
if (length() != len) {
594571
return false;
@@ -597,17 +574,7 @@ bool String::operator==(const StrRange<char32_t> &p_str_range) const {
597574
return true;
598575
}
599576

600-
const char32_t *c_str = p_str_range.c_str;
601-
const char32_t *dst = &operator[](0);
602-
603-
/* Compare char by char */
604-
for (int i = 0; i < len; i++) {
605-
if (c_str[i] != dst[i]) {
606-
return false;
607-
}
608-
}
609-
610-
return true;
577+
return memcmp(ptr(), p_str_range.c_str, len * sizeof(char32_t)) == 0;
611578
}
612579

613580
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 {
35733540
}
35743541

35753542
bool String::ends_with(const String &p_string) const {
3576-
int l = p_string.length();
3543+
const int l = p_string.length();
35773544
if (l > length()) {
35783545
return false;
35793546
}
3580-
35813547
if (l == 0) {
35823548
return true;
35833549
}
35843550

3585-
const char32_t *p = &p_string[0];
3586-
const char32_t *s = &operator[](length() - l);
3587-
3588-
for (int i = 0; i < l; i++) {
3589-
if (p[i] != s[i]) {
3590-
return false;
3591-
}
3592-
}
3593-
3594-
return true;
3551+
return memcmp(ptr() + (length() - l), p_string.ptr(), l * sizeof(char32_t)) == 0;
35953552
}
35963553

35973554
bool String::ends_with(const char *p_string) const {
@@ -3620,25 +3577,15 @@ bool String::ends_with(const char *p_string) const {
36203577
}
36213578

36223579
bool String::begins_with(const String &p_string) const {
3623-
int l = p_string.length();
3580+
const int l = p_string.length();
36243581
if (l > length()) {
36253582
return false;
36263583
}
3627-
36283584
if (l == 0) {
36293585
return true;
36303586
}
36313587

3632-
const char32_t *p = &p_string[0];
3633-
const char32_t *s = &operator[](0);
3634-
3635-
for (int i = 0; i < l; i++) {
3636-
if (p[i] != s[i]) {
3637-
return false;
3638-
}
3639-
}
3640-
3641-
return true;
3588+
return memcmp(ptr(), p_string.ptr(), l * sizeof(char32_t)) == 0;
36423589
}
36433590

36443591
bool String::begins_with(const char *p_string) const {

0 commit comments

Comments
 (0)