Skip to content

Commit 97607e5

Browse files
authoredMar 24, 2024··
Merge pull request #11 from b1tflyyyy/added_index_op
Added index op
2 parents 9243c20 + c8252a0 commit 97607e5

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed
 

‎tests/utf8_string_test.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,17 @@ TEST(utf8_string, comparison_operators)
188188
ASSERT_EQ(result_4, false);
189189
ASSERT_EQ(result_5, false);
190190
ASSERT_EQ(result_6, false);
191+
}
192+
193+
// ---------------------------------------------------------------------------------------------------------------------
194+
TEST(utf8_string, index_operator)
195+
{
196+
utf8::ustring::uchar expected{ "п" };
197+
utf8::ustring ustr{ "привет" };
198+
199+
ASSERT_EQ(expected, ustr[0]);
200+
ASSERT_NE(expected, ustr[1]);
201+
202+
ustr.replace_char("s", 0);
203+
ASSERT_EQ(utf8::ustring::uchar{ "s" }, ustr[0]);
191204
}

‎utf8-string-lib/utf8_string.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,17 @@ namespace utf8
159159
return (String != other);
160160
}
161161

162+
// -----------------------------------------------------------------------------------------------------------------
163+
const ustring::uchar ustring::operator[](std::size_t index) const
164+
{
165+
const SSymbol_Info& symbol_info{ Symbols_Info[index] };
166+
167+
ustring::uchar tmp{ };
168+
std::memcpy(tmp.data(), String.c_str() + symbol_info.Symbol_Offset, symbol_info.Symbol_Size);
169+
170+
return tmp;
171+
}
172+
162173
// -----------------------------------------------------------------------------------------------------------------
163174
void ustring::replace_char(std::basic_string_view<char> new_symbol, std::size_t idx)
164175
{
@@ -173,6 +184,7 @@ namespace utf8
173184
std::basic_string<char> new_string{ };
174185
new_string.resize(old_string_size + new_symbol_size - old_symbol_info.Symbol_Size); // calculate & resize new string size
175186

187+
// TODO: refactor
176188
for (std::size_t i{}, counter{}; i < old_string_size;)
177189
{
178190
if (i == replace_position)

‎utf8-string-lib/utf8_string.hpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <cstring>
2828
#include <cstdint>
2929
#include <cstddef>
30+
#include <array>
3031
#include <stdexcept>
3132

3233
#define ONE_BYTE 0b0000'0000 // first bit 0
@@ -38,6 +39,9 @@ namespace utf8
3839
{
3940
class ustring
4041
{
42+
public:
43+
typedef std::array<char, 5> uchar;
44+
4145
public:
4246
friend std::ostream& operator<<(std::ostream& os, const ustring& ustr);
4347
friend std::istream& operator>>(std::istream& is, ustring& ustr);
@@ -68,9 +72,7 @@ namespace utf8
6872
bool operator!=(const std::basic_string<char>& other) const;
6973
bool operator!=(const std::basic_string_view<char>& other) const;
7074

71-
// TODO: ...............................................
72-
char& operator[](std::size_t index) = delete;
73-
const char& operator[](std::size_t index) const = delete;
75+
const ustring::uchar operator[](std::size_t index) const;
7476

7577
void replace_char(std::basic_string_view<char> new_symbol, std::size_t idx);
7678

0 commit comments

Comments
 (0)