You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-04 12:00:25 +00:00
Optimize String.count and String.countn by avoiding repeated reallocations.
This commit is contained in:
@@ -3735,14 +3735,12 @@ int String::_count(const String &p_string, int p_from, int p_to, bool p_case_ins
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int c = 0;
|
int c = 0;
|
||||||
int idx = -1;
|
int idx = 0;
|
||||||
do {
|
while ((idx = p_case_insensitive ? str.findn(p_string, idx) : str.find(p_string, idx)) != -1) {
|
||||||
idx = p_case_insensitive ? str.findn(p_string) : str.find(p_string);
|
// Skip the occurrence itself.
|
||||||
if (idx != -1) {
|
idx += slen;
|
||||||
str = str.substr(idx + slen, str.length() - slen);
|
|
||||||
++c;
|
++c;
|
||||||
}
|
}
|
||||||
} while (idx != -1);
|
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3774,14 +3772,12 @@ int String::_count(const char *p_string, int p_from, int p_to, bool p_case_insen
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
int c = 0;
|
int c = 0;
|
||||||
int idx = -1;
|
int idx = 0;
|
||||||
do {
|
while ((idx = p_case_insensitive ? str.findn(p_string, idx) : str.find(p_string, idx)) != -1) {
|
||||||
idx = p_case_insensitive ? str.findn(p_string) : str.find(p_string);
|
// Skip the occurrence itself.
|
||||||
if (idx != -1) {
|
idx += substring_length;
|
||||||
str = str.substr(idx + substring_length, str.length() - substring_length);
|
|
||||||
++c;
|
++c;
|
||||||
}
|
}
|
||||||
} while (idx != -1);
|
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user