From 659d1b5d0c6f1993c60e41d7cd80ff1cde784b67 Mon Sep 17 00:00:00 2001 From: Julian Date: Sat, 8 Mar 2025 11:56:40 -0500 Subject: [PATCH] Fix crash caused by trailing spaces --- core/string/ustring.cpp | 7 ++++++- core/string/ustring.h | 2 +- modules/gdscript/editor/gdscript_highlighter.cpp | 11 +++++++---- scene/gui/code_edit.cpp | 4 ++-- tests/core/string/test_string.h | 8 ++++++++ 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index f36103c34b4..80bf1daf0cd 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -1175,7 +1175,7 @@ String String::get_slicec(char32_t p_splitter, int p_slice) const { } } -Vector String::split_spaces() const { +Vector String::split_spaces(int p_maxsplit) const { Vector ret; int from = 0; int i = 0; @@ -1199,6 +1199,11 @@ Vector String::split_spaces() const { } 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)); inside = false; } diff --git a/core/string/ustring.h b/core/string/ustring.h index 46c8fb85e25..c728e8f5135 100644 --- a/core/string/ustring.h +++ b/core/string/ustring.h @@ -486,7 +486,7 @@ public: Vector split(const char *p_splitter = "", bool p_allow_empty = true, int p_maxsplit = 0) const; Vector rsplit(const String &p_splitter = "", bool p_allow_empty = true, int p_maxsplit = 0) const; Vector rsplit(const char *p_splitter = "", bool p_allow_empty = true, int p_maxsplit = 0) const; - Vector split_spaces() const; + Vector split_spaces(int p_maxsplit = 0) const; Vector split_floats(const String &p_splitter, bool p_allow_empty = true) const; Vector split_floats_mk(const Vector &p_splitters, bool p_allow_empty = true) const; Vector split_ints(const String &p_splitter, bool p_allow_empty = true) const; diff --git a/modules/gdscript/editor/gdscript_highlighter.cpp b/modules/gdscript/editor/gdscript_highlighter.cpp index 4309639fd41..13902b981df 100644 --- a/modules/gdscript/editor/gdscript_highlighter.cpp +++ b/modules/gdscript/editor/gdscript_highlighter.cpp @@ -151,10 +151,13 @@ Dictionary GDScriptSyntaxHighlighter::_get_line_syntax_highlighting_impl(int p_l } } // "#region" and "#endregion" only highlighted if they're the first region on the line. - if (color_regions[c].type == ColorRegion::TYPE_CODE_REGION && - str.strip_edges().split_spaces()[0] != "#region" && - str.strip_edges().split_spaces()[0] != "#endregion") { - match = false; + if (color_regions[c].type == ColorRegion::TYPE_CODE_REGION) { + Vector str_stripped_split = str.strip_edges().split_spaces(1); + if (!str_stripped_split.is_empty() && + str_stripped_split[0] != "#region" && + str_stripped_split[0] != "#endregion") { + match = false; + } } if (!match) { continue; diff --git a/scene/gui/code_edit.cpp b/scene/gui/code_edit.cpp index a78408b9ab9..c1d726f4e31 100644 --- a/scene/gui/code_edit.cpp +++ b/scene/gui/code_edit.cpp @@ -1872,7 +1872,7 @@ bool CodeEdit::is_line_code_region_start(int p_line) const { if (is_in_string(p_line) != -1) { return false; } - Vector split = get_line(p_line).strip_edges().split_spaces(); + Vector split = get_line(p_line).strip_edges().split_spaces(1); 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) { return false; } - Vector split = get_line(p_line).strip_edges().split_spaces(); + Vector split = get_line(p_line).strip_edges().split_spaces(1); return split.size() > 0 && split[0] == code_region_end_string; } diff --git a/tests/core/string/test_string.h b/tests/core/string/test_string.h index 35f693a3cee..16fde32307c 100644 --- a/tests/core/string/test_string.h +++ b/tests/core/string/test_string.h @@ -713,6 +713,14 @@ TEST_CASE("[String] Splitting") { CHECK(l[i] == slices[i]); } } + { + const String s = "Mars Jupiter Saturn Uranus"; + const char *slices[2] = { "Mars", "Jupiter Saturn Uranus" }; + Vector 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";