diff options
author | Rémi Verschelde <rverschelde@gmail.com> | 2017-12-15 19:56:39 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-15 19:56:39 +0100 |
commit | 7767d89c4501041229d723d5445aa103ffe71937 (patch) | |
tree | adaef9dab705577179bd505e9fd8ffcc232f5c21 /core/ustring.cpp | |
parent | 0de3cde6fc9f7d02295d12a823261340efa7f841 (diff) | |
parent | 5302fd125b36d453615483f6ced4e40e973c499a (diff) | |
download | redot-engine-7767d89c4501041229d723d5445aa103ffe71937.tar.gz |
Merge pull request #14580 from Krakean/string_split_new_arg
Added third argument for String.split() function
Diffstat (limited to 'core/ustring.cpp')
-rw-r--r-- | core/ustring.cpp | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/core/ustring.cpp b/core/ustring.cpp index 3a0708851e..1bf7d000c3 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -734,7 +734,7 @@ Vector<String> String::split_spaces() const { return ret; } -Vector<String> String::split(const String &p_splitter, bool p_allow_empty) const { +Vector<String> String::split(const String &p_splitter, bool p_allow_empty, int p_maxsplit) const { Vector<String> ret; int from = 0; @@ -745,8 +745,21 @@ Vector<String> String::split(const String &p_splitter, bool p_allow_empty) const int end = find(p_splitter, from); if (end < 0) end = len; - if (p_allow_empty || (end > from)) - ret.push_back(substr(from, end - from)); + if (p_allow_empty || (end > from)) { + if (p_maxsplit <= 0) + ret.push_back(substr(from, end - from)); + else if (p_maxsplit > 0) { + + // Put rest of the string and leave cycle. + if (p_maxsplit == ret.size()) { + ret.push_back(substr(from, len)); + break; + } + + // Otherwise, push items until positive limit is reached. + ret.push_back(substr(from, end - from)); + } + } if (end == len) break; |