diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2022-11-06 15:54:56 +0100 |
---|---|---|
committer | Rémi Verschelde <rverschelde@gmail.com> | 2022-11-06 15:54:56 +0100 |
commit | 1836b4b79874e76cfc913a988423ea2b3dfad64c (patch) | |
tree | 40b897cba0282bbafb8139bde47f21ff17e8cc10 /core/string/ustring.cpp | |
parent | 5dfa56474a0c62e87d3c95c86cdee186a56d5f3b (diff) | |
parent | 8a47a12207348ad332c4ed5987c0ba432ff5de5a (diff) |
Merge pull request #64321 from KoBeWi/s_p_l_i_t
Add support for empty delimiter in `String.split()`
Diffstat (limited to 'core/string/ustring.cpp')
-rw-r--r-- | core/string/ustring.cpp | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index c86c8316fe..c6cc4cc4ac 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -1180,9 +1180,14 @@ Vector<String> String::split(const String &p_splitter, bool p_allow_empty, int p int len = length(); while (true) { - int end = find(p_splitter, from); - if (end < 0) { - end = len; + int end; + if (p_splitter.is_empty()) { + end = from + 1; + } else { + end = find(p_splitter, from); + if (end < 0) { + end = len; + } } if (p_allow_empty || (end > from)) { if (p_maxsplit <= 0) { @@ -1223,7 +1228,15 @@ Vector<String> String::rsplit(const String &p_splitter, bool p_allow_empty, int break; } - int left_edge = rfind(p_splitter, remaining_len - p_splitter.length()); + int left_edge; + if (p_splitter.is_empty()) { + left_edge = remaining_len - 1; + if (left_edge == 0) { + left_edge--; // Skip to the < 0 condition. + } + } else { + left_edge = rfind(p_splitter, remaining_len - p_splitter.length()); + } if (left_edge < 0) { // no more splitters, we're done |