Skip to content

Commit 171addd

Browse files
authored
Merge pull request #12 from b1tflyyyy/refactored_replace_char
refactored replace char method
2 parents 97607e5 + 8a4aad9 commit 171addd

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

utf8-string-lib/utf8_string.cpp

+23-17
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,8 @@ namespace utf8
162162
// -----------------------------------------------------------------------------------------------------------------
163163
const ustring::uchar ustring::operator[](std::size_t index) const
164164
{
165-
const SSymbol_Info& symbol_info{ Symbols_Info[index] };
166-
167165
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);
169167

170168
return tmp;
171169
}
@@ -184,20 +182,7 @@ namespace utf8
184182
std::basic_string<char> new_string{ };
185183
new_string.resize(old_string_size + new_symbol_size - old_symbol_info.Symbol_Size); // calculate & resize new string size
186184

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);
201186

202187
auto new_offset{ static_cast<std::ptrdiff_t>(new_symbol_size) - static_cast<std::ptrdiff_t>(old_symbol_info.Symbol_Size) };
203188
Recalculate_Symbols_Offset(idx + 1, new_offset);
@@ -271,4 +256,25 @@ namespace utf8
271256
}
272257
}
273258
}
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+
}
274280
}

utf8-string-lib/utf8_string.hpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ namespace utf8
5454
explicit ustring(const std::basic_string<char>& default_string);
5555
explicit ustring(const std::basic_string_view<char>& default_string);
5656

57-
constexpr ~ustring() = default;
57+
~ustring() = default;
5858

5959
explicit ustring(const ustring& other);
6060
explicit ustring(ustring&& other) noexcept;
@@ -80,9 +80,10 @@ namespace utf8
8080
[[nodiscard]] const std::basic_string<char>& get_c_str() const;
8181

8282
private:
83-
std::uint8_t Get_Size_Of_Symbol(std::uint8_t symbol);
84-
void Recalculate_Symbols_Offset(std::size_t start_pos, std::ptrdiff_t new_offset);
85-
void Copy_With_Metadata_To_Internal_String(const char* default_string);
83+
[[clang::always_inline, gnu::always_inline]] __inline__ std::uint8_t Get_Size_Of_Symbol(std::uint8_t symbol);
84+
[[clang::always_inline, gnu::always_inline]] __inline__ void Recalculate_Symbols_Offset(std::size_t start_pos, std::ptrdiff_t new_offset);
85+
[[clang::always_inline, gnu::always_inline]] __inline__ void Copy_With_Metadata_To_Internal_String(const char* default_string);
86+
[[clang::always_inline, gnu::always_inline]] __inline__ void 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);
8687

8788
private:
8889
struct SSymbol_Info

0 commit comments

Comments
 (0)