1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-07 12:30:27 +00:00

String[size()] should return a default constructed CharType

As per the C++ standard 21.3.4.1 for std::string:
Returns: If pos < size(), returns data()[pos]. Otherwise, if pos ==
size(), the const version returns charT(). Otherwise, the behavior is
undefined.

Since the behavior is undefined Godot now does the same thing for const
and non-const versions of operator[].

This fixes #21242 and fixes #22221.
This commit is contained in:
Hein-Pieter van Braam
2019-01-04 16:01:54 +00:00
parent 1504c96112
commit ac99ed3cda
3 changed files with 49 additions and 2 deletions

View File

@@ -941,6 +941,33 @@ bool test_30() {
state = state && success;
OS::get_singleton()->print("Capitalize %ls: %ls, %s\n", input.c_str(), output.c_str(), success ? "OK" : "FAIL");
return state;
}
bool test_31() {
bool state = true;
bool success;
String a = "";
success = a[0] == 0;
OS::get_singleton()->print("Is 0 String[0]:, %s\n", success ? "OK" : "FAIL");
if (!success) state = false;
String b = "Godot";
success = b[b.size()] == 0;
OS::get_singleton()->print("Is 0 String[size()]:, %s\n", success ? "OK" : "FAIL");
if (!success) state = false;
const String c = "";
success = c[0] == 0;
OS::get_singleton()->print("Is 0 const String[0]:, %s\n", success ? "OK" : "FAIL");
if (!success) state = false;
const String d = "Godot";
success = d[d.size()] == 0;
OS::get_singleton()->print("Is 0 const String[size()]:, %s\n", success ? "OK" : "FAIL");
if (!success) state = false;
return state;
};
@@ -978,6 +1005,7 @@ TestFunc test_funcs[] = {
test_28,
test_29,
test_30,
test_31,
0
};