Skip to content

Commit 754e3b3

Browse files
committed
Merge pull request #103825 from JulianHeuser/region_highlight_crash_fix
Fix crash related to #region/#endregion caused by trailing spaces
2 parents c19244c + 659d1b5 commit 754e3b3

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

core/string/ustring.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1175,7 +1175,7 @@ String String::get_slicec(char32_t p_splitter, int p_slice) const {
11751175
}
11761176
}
11771177

1178-
Vector<String> String::split_spaces() const {
1178+
Vector<String> String::split_spaces(int p_maxsplit) const {
11791179
Vector<String> ret;
11801180
int from = 0;
11811181
int i = 0;
@@ -1199,6 +1199,11 @@ Vector<String> String::split_spaces() const {
11991199
}
12001200

12011201
if (empty && inside) {
1202+
if (p_maxsplit > 0 && p_maxsplit == ret.size()) {
1203+
// Put rest of the string and leave cycle.
1204+
ret.push_back(substr(from));
1205+
break;
1206+
}
12021207
ret.push_back(substr(from, i - from));
12031208
inside = false;
12041209
}

core/string/ustring.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ class String {
471471
Vector<String> split(const char *p_splitter = "", bool p_allow_empty = true, int p_maxsplit = 0) const;
472472
Vector<String> rsplit(const String &p_splitter = "", bool p_allow_empty = true, int p_maxsplit = 0) const;
473473
Vector<String> rsplit(const char *p_splitter = "", bool p_allow_empty = true, int p_maxsplit = 0) const;
474-
Vector<String> split_spaces() const;
474+
Vector<String> split_spaces(int p_maxsplit = 0) const;
475475
Vector<double> split_floats(const String &p_splitter, bool p_allow_empty = true) const;
476476
Vector<float> split_floats_mk(const Vector<String> &p_splitters, bool p_allow_empty = true) const;
477477
Vector<int> split_ints(const String &p_splitter, bool p_allow_empty = true) const;

modules/gdscript/editor/gdscript_highlighter.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,13 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l
151151
}
152152
}
153153
// "#region" and "#endregion" only highlighted if they're the first region on the line.
154-
if (color_regions[c].type == ColorRegion::TYPE_CODE_REGION &&
155-
str.strip_edges().split_spaces()[0] != "#region" &&
156-
str.strip_edges().split_spaces()[0] != "#endregion") {
157-
match = false;
154+
if (color_regions[c].type == ColorRegion::TYPE_CODE_REGION) {
155+
Vector<String> str_stripped_split = str.strip_edges().split_spaces(1);
156+
if (!str_stripped_split.is_empty() &&
157+
str_stripped_split[0] != "#region" &&
158+
str_stripped_split[0] != "#endregion") {
159+
match = false;
160+
}
158161
}
159162
if (!match) {
160163
continue;

scene/gui/code_edit.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,7 +1872,7 @@ bool CodeEdit::is_line_code_region_start(int p_line) const {
18721872
if (is_in_string(p_line) != -1) {
18731873
return false;
18741874
}
1875-
Vector<String> split = get_line(p_line).strip_edges().split_spaces();
1875+
Vector<String> split = get_line(p_line).strip_edges().split_spaces(1);
18761876
return split.size() > 0 && split[0] == code_region_start_string;
18771877
}
18781878

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

tests/core/string/test_string.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,14 @@ TEST_CASE("[String] Splitting") {
730730
CHECK(l[i] == slices[i]);
731731
}
732732
}
733+
{
734+
const String s = "Mars Jupiter Saturn Uranus";
735+
const char *slices[2] = { "Mars", "Jupiter Saturn Uranus" };
736+
Vector<String> l = s.split_spaces(1);
737+
for (int i = 0; i < l.size(); i++) {
738+
CHECK(l[i] == slices[i]);
739+
}
740+
}
733741

734742
{
735743
const String s = "1.2;2.3 4.5";

0 commit comments

Comments
 (0)