Skip to content

Commit 02e6c0a

Browse files
miv391akien-mga
authored andcommitted
Fix String::begins_with when both strings are empty
(cherry picked from commit 3026b56)
1 parent 4e3cd10 commit 02e6c0a

File tree

2 files changed

+42
-27
lines changed

2 files changed

+42
-27
lines changed

core/string/ustring.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3329,10 +3329,14 @@ bool String::begins_with(const String &p_string) const {
33293329

33303330
bool String::begins_with(const char *p_string) const {
33313331
int l = length();
3332-
if (l == 0 || !p_string) {
3332+
if (!p_string) {
33333333
return false;
33343334
}
33353335

3336+
if (l == 0) {
3337+
return *p_string == 0;
3338+
}
3339+
33363340
const char32_t *str = &operator[](0);
33373341
int i = 0;
33383342

tests/core/string/test_string.h

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -579,48 +579,59 @@ struct test_27_data {
579579

580580
TEST_CASE("[String] Begins with") {
581581
test_27_data tc[] = {
582+
// Test cases for true:
582583
{ "res://foobar", "res://", true },
584+
{ "abc", "abc", true },
585+
{ "abc", "", true },
586+
{ "", "", true },
587+
// Test cases for false:
583588
{ "res", "res://", false },
584-
{ "abc", "abc", true }
589+
{ "abcdef", "foo", false },
590+
{ "abc", "ax", false },
591+
{ "", "abc", false }
585592
};
586593
size_t count = sizeof(tc) / sizeof(tc[0]);
587594
bool state = true;
588-
for (size_t i = 0; state && i < count; ++i) {
595+
for (size_t i = 0; i < count; ++i) {
589596
String s = tc[i].data;
590597
state = s.begins_with(tc[i].part) == tc[i].expected;
591-
if (state) {
592-
String sb = tc[i].part;
593-
state = s.begins_with(sb) == tc[i].expected;
594-
}
595-
CHECK(state);
596-
if (!state) {
597-
break;
598-
}
599-
};
600-
CHECK(state);
598+
CHECK_MESSAGE(state, "first check failed at: ", i);
599+
600+
String sb = tc[i].part;
601+
state = s.begins_with(sb) == tc[i].expected;
602+
CHECK_MESSAGE(state, "second check failed at: ", i);
603+
}
604+
605+
// Test "const char *" version also with nullptr.
606+
String s("foo");
607+
state = s.begins_with(nullptr) == false;
608+
CHECK_MESSAGE(state, "nullptr check failed");
609+
610+
String empty("");
611+
state = empty.begins_with(nullptr) == false;
612+
CHECK_MESSAGE(state, "nullptr check with empty string failed");
601613
}
602614

603615
TEST_CASE("[String] Ends with") {
604616
test_27_data tc[] = {
617+
// test cases for true:
605618
{ "res://foobar", "foobar", true },
619+
{ "abc", "abc", true },
620+
{ "abc", "", true },
621+
{ "", "", true },
622+
// test cases for false:
606623
{ "res", "res://", false },
607-
{ "abc", "abc", true }
624+
{ "", "abc", false },
625+
{ "abcdef", "foo", false },
626+
{ "abc", "xc", false }
608627
};
609628
size_t count = sizeof(tc) / sizeof(tc[0]);
610-
bool state = true;
611-
for (size_t i = 0; state && i < count; ++i) {
629+
for (size_t i = 0; i < count; ++i) {
612630
String s = tc[i].data;
613-
state = s.ends_with(tc[i].part) == tc[i].expected;
614-
if (state) {
615-
String sb = tc[i].part;
616-
state = s.ends_with(sb) == tc[i].expected;
617-
}
618-
CHECK(state);
619-
if (!state) {
620-
break;
621-
}
622-
};
623-
CHECK(state);
631+
String sb = tc[i].part;
632+
bool state = s.ends_with(sb) == tc[i].expected;
633+
CHECK_MESSAGE(state, "check failed at: ", i);
634+
}
624635
}
625636

626637
TEST_CASE("[String] format") {

0 commit comments

Comments
 (0)