MCPcopy Create free account
hub / github.com/Redot-Engine/redot-engine / _replace_common

Function _replace_common

core/string/ustring.cpp:3865–3920  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3863}
3864
3865static String _replace_common(const String &p_this, const String &p_key, const String &p_with, bool p_case_insensitive) {
3866 if (p_key.is_empty() || p_this.is_empty()) {
3867 return p_this;
3868 }
3869
3870 const size_t key_length = p_key.length();
3871
3872 int search_from = 0;
3873 int result = 0;
3874
3875 LocalVector<int> found;
3876
3877 while ((result = (p_case_insensitive ? p_this.findn(p_key, search_from) : p_this.find(p_key, search_from))) >= 0) {
3878 found.push_back(result);
3879 ERR_FAIL_COND_V_MSG((result + key_length) > INT32_MAX, p_this, "Key length too long");
3880 search_from = result + key_length;
3881 }
3882
3883 if (found.is_empty()) {
3884 return p_this;
3885 }
3886
3887 String new_string;
3888
3889 const int with_length = p_with.length();
3890 const int old_length = p_this.length();
3891
3892 new_string.resize_uninitialized(old_length + int(found.size()) * (with_length - key_length) + 1);
3893
3894 char32_t *new_ptrw = new_string.ptrw();
3895 const char32_t *old_ptr = p_this.ptr();
3896 const char32_t *with_ptr = p_with.ptr();
3897
3898 int last_pos = 0;
3899
3900 for (const int &pos : found) {
3901 if (last_pos != pos) {
3902 memcpy(new_ptrw, old_ptr + last_pos, (pos - last_pos) * sizeof(char32_t));
3903 new_ptrw += (pos - last_pos);
3904 }
3905 if (with_length) {
3906 memcpy(new_ptrw, with_ptr, with_length * sizeof(char32_t));
3907 new_ptrw += with_length;
3908 }
3909 last_pos = pos + key_length;
3910 }
3911
3912 if (last_pos != old_length) {
3913 memcpy(new_ptrw, old_ptr + last_pos, (old_length - last_pos) * sizeof(char32_t));
3914 new_ptrw += old_length - last_pos;
3915 }
3916
3917 *new_ptrw = 0;
3918
3919 return new_string;
3920}
3921
3922static String _replace_common(const String &p_this, char const *p_key, char const *p_with, bool p_case_insensitive) {

Callers 2

replaceMethod · 0.85
replacenMethod · 0.85

Calls 10

strlenFunction · 0.85
findnMethod · 0.80
sizeMethod · 0.65
is_emptyMethod · 0.45
lengthMethod · 0.45
findMethod · 0.45
push_backMethod · 0.45
resize_uninitializedMethod · 0.45
ptrwMethod · 0.45
ptrMethod · 0.45

Tested by

no test coverage detected