Fix crash caused by trailing spaces

This commit is contained in:
Julian 2025-03-08 11:56:40 -05:00
parent b5bdb88062
commit 659d1b5d0c
5 changed files with 24 additions and 8 deletions

View File

@ -1175,7 +1175,7 @@ String String::get_slicec(char32_t p_splitter, int p_slice) const {
} }
} }
Vector<String> String::split_spaces() const { Vector<String> String::split_spaces(int p_maxsplit) const {
Vector<String> ret; Vector<String> ret;
int from = 0; int from = 0;
int i = 0; int i = 0;
@ -1199,6 +1199,11 @@ Vector<String> String::split_spaces() const {
} }
if (empty && inside) { if (empty && inside) {
if (p_maxsplit > 0 && p_maxsplit == ret.size()) {
// Put rest of the string and leave cycle.
ret.push_back(substr(from));
break;
}
ret.push_back(substr(from, i - from)); ret.push_back(substr(from, i - from));
inside = false; inside = false;
} }

View File

@ -486,7 +486,7 @@ public:
Vector<String> split(const char *p_splitter = "", bool p_allow_empty = true, int p_maxsplit = 0) const; Vector<String> split(const char *p_splitter = "", bool p_allow_empty = true, int p_maxsplit = 0) const;
Vector<String> rsplit(const String &p_splitter = "", bool p_allow_empty = true, int p_maxsplit = 0) const; Vector<String> rsplit(const String &p_splitter = "", bool p_allow_empty = true, int p_maxsplit = 0) const;
Vector<String> rsplit(const char *p_splitter = "", bool p_allow_empty = true, int p_maxsplit = 0) const; Vector<String> rsplit(const char *p_splitter = "", bool p_allow_empty = true, int p_maxsplit = 0) const;
Vector<String> split_spaces() const; Vector<String> split_spaces(int p_maxsplit = 0) const;
Vector<double> split_floats(const String &p_splitter, bool p_allow_empty = true) const; Vector<double> split_floats(const String &p_splitter, bool p_allow_empty = true) const;
Vector<float> split_floats_mk(const Vector<String> &p_splitters, bool p_allow_empty = true) const; Vector<float> split_floats_mk(const Vector<String> &p_splitters, bool p_allow_empty = true) const;
Vector<int> split_ints(const String &p_splitter, bool p_allow_empty = true) const; Vector<int> split_ints(const String &p_splitter, bool p_allow_empty = true) const;

View File

@ -151,11 +151,14 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
} }
} }
// "#region" and "#endregion" only highlighted if they're the first region on the line. // "#region" and "#endregion" only highlighted if they're the first region on the line.
if (color_regions[c].type == ColorRegion::TYPE_CODE_REGION && if (color_regions[c].type == ColorRegion::TYPE_CODE_REGION) {
str.strip_edges().split_spaces()[0] != "#region" && Vector<String> str_stripped_split = str.strip_edges().split_spaces(1);
str.strip_edges().split_spaces()[0] != "#endregion") { if (!str_stripped_split.is_empty() &&
str_stripped_split[0] != "#region" &&
str_stripped_split[0] != "#endregion") {
match = false; match = false;
} }
}
if (!match) { if (!match) {
continue; continue;
} }

View File

@ -1872,7 +1872,7 @@ bool CodeEdit::is_line_code_region_start(int p_line) const {
if (is_in_string(p_line) != -1) { if (is_in_string(p_line) != -1) {
return false; return false;
} }
Vector<String> split = get_line(p_line).strip_edges().split_spaces(); Vector<String> split = get_line(p_line).strip_edges().split_spaces(1);
return split.size() > 0 && split[0] == code_region_start_string; return split.size() > 0 && split[0] == code_region_start_string;
} }
@ -1884,7 +1884,7 @@ bool CodeEdit::is_line_code_region_end(int p_line) const {
if (is_in_string(p_line) != -1) { if (is_in_string(p_line) != -1) {
return false; return false;
} }
Vector<String> split = get_line(p_line).strip_edges().split_spaces(); Vector<String> split = get_line(p_line).strip_edges().split_spaces(1);
return split.size() > 0 && split[0] == code_region_end_string; return split.size() > 0 && split[0] == code_region_end_string;
} }

View File

@ -713,6 +713,14 @@ TEST_CASE("[String] Splitting") {
CHECK(l[i] == slices[i]); CHECK(l[i] == slices[i]);
} }
} }
{
const String s = "Mars Jupiter Saturn Uranus";
const char *slices[2] = { "Mars", "Jupiter Saturn Uranus" };
Vector<String> l = s.split_spaces(1);
for (int i = 0; i < l.size(); i++) {
CHECK(l[i] == slices[i]);
}
}
{ {
const String s = "1.2;2.3 4.5"; const String s = "1.2;2.3 4.5";