| 4111 | |
| 4112 | template <class T> |
| 4113 | static 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 | |
| 4166 | String 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); |
no test coverage detected