diff options
author | Dmitry Koteroff <vortex@verona.im> | 2017-12-15 22:23:58 +0300 |
---|---|---|
committer | Dmitry Koteroff <vortex@verona.im> | 2017-12-15 22:23:58 +0300 |
commit | 6fe415ca7f439d56321d327dfdd18105a09a70d1 (patch) | |
tree | 73c92b2a464cc8b8cc03f3749fa27fd3deeca6bd /core/ustring.cpp | |
parent | 7767d89c4501041229d723d5445aa103ffe71937 (diff) | |
download | redot-engine-6fe415ca7f439d56321d327dfdd18105a09a70d1.tar.gz |
Added rsplit() for String class
Docs updated
Diffstat (limited to 'core/ustring.cpp')
-rw-r--r-- | core/ustring.cpp | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/core/ustring.cpp b/core/ustring.cpp index 1bf7d000c3..621fdf4011 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -770,6 +770,46 @@ Vector<String> String::split(const String &p_splitter, bool p_allow_empty, int p return ret; } +Vector<String> String::rsplit(const String &p_splitter, bool p_allow_empty, int p_maxsplit) const { + + Vector<String> ret; + const int len = length(); + int from = len; + + while (true) { + + int end = rfind(p_splitter, from); + if (end < 0) + end = 0; + + if (p_allow_empty || (end < from)) { + const String str = substr(end > 0 ? end + p_splitter.length() : end, end > 0 ? from - end : from + 2); + + if (p_maxsplit <= 0) { + ret.push_back(str); + } else if (p_maxsplit > 0) { + + // Put rest of the string and leave cycle. + if (p_maxsplit == ret.size()) { + ret.push_back(substr(0, from + 2)); + break; + } + + // Otherwise, push items until positive limit is reached. + ret.push_back(str); + } + } + + if (end == 0) + break; + + from = end - p_splitter.length(); + } + + ret.invert(); + return ret; +} + Vector<float> String::split_floats(const String &p_splitter, bool p_allow_empty) const { Vector<float> ret; |