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

Function _replace_chars_common

core/string/ustring.cpp:4113–4164  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4111
4112template <class T>
4113static String _replace_chars_common(const String &p_this, const T *p_keys, int p_keys_len, char32_t p_with) {
4114 ERR_FAIL_COND_V_MSG(p_with == 0, p_this, "`with` must not be the NUL character.");
4115
4116 // Delegate if p_keys is a single element.
4117 if (p_keys_len == 1) {
4118 return p_this.replace_char(*p_keys, p_with);
4119 } else if (p_keys_len == 0) {
4120 return p_this;
4121 }
4122
4123 int len = p_this.length();
4124 if (len == 0) {
4125 return p_this;
4126 }
4127
4128 int index = 0;
4129 const char32_t *old_ptr = p_this.ptr();
4130 for (; index < len; ++index) {
4131 if (_contains_char(old_ptr[index], p_keys, p_keys_len)) {
4132 break;
4133 }
4134 }
4135
4136 // If no occurrence of `keys` was found, return this.
4137 if (index == len) {
4138 return p_this;
4139 }
4140
4141 // If we found at least one occurrence of `keys`, create new string.
4142 String new_string;
4143 new_string.resize_uninitialized(len + 1);
4144 char32_t *new_ptr = new_string.ptrw();
4145
4146 // Copy part of input before `key`.
4147 memcpy(new_ptr, old_ptr, index * sizeof(char32_t));
4148
4149 new_ptr[index] = p_with;
4150
4151 // Copy or replace rest of input.
4152 for (++index; index < len; ++index) {
4153 const char32_t old_char = old_ptr[index];
4154 if (_contains_char(old_char, p_keys, p_keys_len)) {
4155 new_ptr[index] = p_with;
4156 } else {
4157 new_ptr[index] = old_char;
4158 }
4159 }
4160
4161 new_ptr[index] = 0;
4162
4163 return new_string;
4164}
4165
4166String String::replace_chars(const String &p_keys, char32_t p_with) const {
4167 return _replace_chars_common(*this, p_keys.ptr(), p_keys.length(), p_with);

Callers 1

replace_charsMethod · 0.85

Calls 6

_contains_charFunction · 0.85
replace_charMethod · 0.80
lengthMethod · 0.45
ptrMethod · 0.45
resize_uninitializedMethod · 0.45
ptrwMethod · 0.45

Tested by

no test coverage detected