Merge pull request #72547 from MewPurPur/string-split-fix
Fix String.split() with empty string and delimeter
This commit is contained in:
commit
ab4a7b2b77
|
@ -1157,6 +1157,14 @@ Vector<String> String::split_spaces() const {
|
||||||
|
|
||||||
Vector<String> String::split(const String &p_splitter, bool p_allow_empty, int p_maxsplit) const {
|
Vector<String> String::split(const String &p_splitter, bool p_allow_empty, int p_maxsplit) const {
|
||||||
Vector<String> ret;
|
Vector<String> ret;
|
||||||
|
|
||||||
|
if (is_empty()) {
|
||||||
|
if (p_allow_empty) {
|
||||||
|
ret.push_back("");
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
int from = 0;
|
int from = 0;
|
||||||
int len = length();
|
int len = length();
|
||||||
|
|
||||||
|
|
|
@ -512,6 +512,14 @@ TEST_CASE("[String] Splitting") {
|
||||||
CHECK(l[i] == slices_3[i]);
|
CHECK(l[i] == slices_3[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s = "";
|
||||||
|
l = s.split();
|
||||||
|
CHECK(l.size() == 1);
|
||||||
|
CHECK(l[0] == "");
|
||||||
|
|
||||||
|
l = s.split("", false);
|
||||||
|
CHECK(l.size() == 0);
|
||||||
|
|
||||||
s = "Mars Jupiter Saturn Uranus";
|
s = "Mars Jupiter Saturn Uranus";
|
||||||
const char *slices_s[4] = { "Mars", "Jupiter", "Saturn", "Uranus" };
|
const char *slices_s[4] = { "Mars", "Jupiter", "Saturn", "Uranus" };
|
||||||
l = s.split_spaces();
|
l = s.split_spaces();
|
||||||
|
|
Loading…
Reference in New Issue