diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index a773a7b664f..ce723fb8ca2 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 35442aa8e8f..692e6bda51a 100644 --- a/core/string/ustring.h +++ b/core/string/ustring.h @@ -471,7 +471,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 016092d5620..278e584a5a7 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 ff3b97a292b..37552f7597d 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 ee7471afdb5..0558824e64c 100644 --- a/tests/core/string/test_string.h +++ b/tests/core/string/test_string.h @@ -730,6 +730,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";