You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-24 15:26:15 +00:00
Added rsplit() for String class
Docs updated
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user