@@ -162,10 +162,8 @@ namespace utf8
162
162
// -----------------------------------------------------------------------------------------------------------------
163
163
const ustring::uchar ustring::operator [](std::size_t index) const
164
164
{
165
- const SSymbol_Info& symbol_info{ Symbols_Info[index ] };
166
-
167
165
ustring::uchar tmp{ };
168
- std::memcpy (tmp.data (), String.c_str () + symbol_info .Symbol_Offset , symbol_info .Symbol_Size );
166
+ std::memcpy (tmp.data (), String.c_str () + Symbols_Info[ index ] .Symbol_Offset , Symbols_Info[ index ] .Symbol_Size );
169
167
170
168
return tmp;
171
169
}
@@ -184,20 +182,7 @@ namespace utf8
184
182
std::basic_string<char > new_string{ };
185
183
new_string.resize (old_string_size + new_symbol_size - old_symbol_info.Symbol_Size ); // calculate & resize new string size
186
184
187
- // TODO: refactor
188
- for (std::size_t i{}, counter{}; i < old_string_size;)
189
- {
190
- if (i == replace_position)
191
- {
192
- std::memcpy (new_string.data () + i, new_symbol.data (), new_symbol_size);
193
- counter += new_symbol_size;
194
- i += old_symbol_info.Symbol_Size ;
195
-
196
- continue ;
197
- }
198
-
199
- new_string[counter++] = String[i++];
200
- }
185
+ Copy_With_Char_Replacing (old_string_size, replace_position, old_symbol_info.Symbol_Size , new_symbol_size, new_string, new_symbol);
201
186
202
187
auto new_offset{ static_cast <std::ptrdiff_t >(new_symbol_size) - static_cast <std::ptrdiff_t >(old_symbol_info.Symbol_Size ) };
203
188
Recalculate_Symbols_Offset (idx + 1 , new_offset);
@@ -271,4 +256,25 @@ namespace utf8
271
256
}
272
257
}
273
258
}
259
+
260
+ // -----------------------------------------------------------------------------------------------------------------
261
+ void ustring::Copy_With_Char_Replacing (std::size_t old_string_size, std::size_t replace_position, std::size_t old_symbol_size, std::size_t new_symbol_size, std::basic_string<char >& dest, std::basic_string_view<char > new_symbol)
262
+ {
263
+ // src_counter -> String
264
+ // dest counter -> new/dest string
265
+ for (std::size_t src_counter{}, dest_counter{}; src_counter < old_string_size;)
266
+ {
267
+ if (src_counter == replace_position)
268
+ {
269
+ std::memcpy (dest.data () + replace_position, new_symbol.data (), new_symbol_size); // write new symbol in the replacing position
270
+
271
+ dest_counter += new_symbol_size; // skip new symbol in replace_position
272
+ src_counter += old_symbol_size; // skip old symbol in the same position
273
+
274
+ continue ;
275
+ }
276
+
277
+ dest[dest_counter++] = String[src_counter++];
278
+ }
279
+ }
274
280
}
0 commit comments